Example usage for com.google.gson JsonElement isJsonNull

List of usage examples for com.google.gson JsonElement isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:org.opendaylight.vtn.javaapi.openstack.validation.PortResourceValidator.java

License:Open Source License

/**
 * Validates the parameters of POST request body
 * /*from  w w  w .  j a va2 s.  c  o m*/
 * @param requestBody
 *            - JSON request body corresponding to POST operation
 * @return
 */
public boolean validatePost(final JsonObject requestBody) {
    LOG.trace("Start PortResourceValidator#validatePost()");
    boolean isValid = true;
    // validation of mandatory parameters
    if (requestBody == null || !requestBody.has(VtnServiceOpenStackConsts.PORT)
            || !requestBody.has(VtnServiceOpenStackConsts.DATAPATH_ID)
            || !requestBody.has(VtnServiceOpenStackConsts.VID)) {
        isValid = false;
        setInvalidParameter(UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
    } else {
        // validation of id
        if (requestBody.has(VtnServiceOpenStackConsts.ID)) {
            final JsonElement id = requestBody.get(VtnServiceOpenStackConsts.ID);
            if (id.isJsonNull() || id.getAsString().isEmpty()
                    || !validator.isValidMaxLengthAlphaNum(id.getAsString(), VtnServiceJsonConsts.LEN_24)) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.ID + VtnServiceConsts.COLON
                        + (id.isJsonNull() ? id : id.getAsString()));
            }
        }

        // validation of datapath_id
        if (isValid) {
            final JsonElement datapathId = requestBody.get(VtnServiceOpenStackConsts.DATAPATH_ID);
            setInvalidParameter(VtnServiceOpenStackConsts.DATAPATH_ID);
            if (datapathId.isJsonNull() || !isValidDataPathId(datapathId.getAsString())) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.DATAPATH_ID + VtnServiceConsts.COLON
                        + (datapathId.isJsonNull() ? datapathId : datapathId.getAsString()));
            }
        }

        // validation of port
        if (isValid) {
            final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);
            if (!port.isJsonNull() && !port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL)) {
                if (port.getAsString().isEmpty()) {
                    isValid = false;
                } else {
                    try {
                        isValid = validator.isValidBigIntegerRangeString(new BigInteger(port.getAsString()),
                                VtnServiceJsonConsts.BIG_VAL0, VtnServiceJsonConsts.BIG_VAL_4294967040);
                    } catch (Exception e) {
                        isValid = false;
                    }
                }

                // set message as per above checks
                if (!isValid) {
                    setInvalidParameter(VtnServiceOpenStackConsts.PORT + VtnServiceConsts.COLON
                            + (port.isJsonNull() ? port : port.getAsString()));
                }
            }
        }

        // validation of vid
        if (isValid) {
            final JsonElement vid = requestBody.get(VtnServiceOpenStackConsts.VID);
            setInvalidParameter(VtnServiceOpenStackConsts.VID);
            if (vid.isJsonNull() || vid.getAsString().isEmpty()) {
                isValid = false;
            } else {
                try {
                    isValid = validator.isValidRange(vid.getAsString(), VtnServiceJsonConsts.VAL_1,
                            VtnServiceJsonConsts.VAL_4095);
                    if (Integer.parseInt(vid.getAsString()) == VtnServiceJsonConsts.VAL_65535) {
                        isValid = true;
                    }
                } catch (Exception e) {
                    isValid = false;
                }
            }

            // set message as per above checks
            if (!isValid) {
                setInvalidParameter(VtnServiceOpenStackConsts.VID + VtnServiceConsts.COLON
                        + (vid.isJsonNull() ? vid : vid.getAsString()));
            }
        }

        // validation of filters.
        if (isValid) {
            setInvalidParameter(VtnServiceOpenStackConsts.FILTERS);
            if (requestBody.has(VtnServiceOpenStackConsts.FILTERS)) {
                if (requestBody.get(VtnServiceOpenStackConsts.FILTERS).isJsonArray()) {
                    if (requestBody.getAsJsonArray(VtnServiceOpenStackConsts.FILTERS).size() > 0) {
                        JsonArray filters = requestBody.getAsJsonArray(VtnServiceOpenStackConsts.FILTERS);
                        Iterator<JsonElement> iterator = filters.iterator();
                        while (iterator.hasNext()) {
                            JsonElement filterID = iterator.next();
                            if (filterID.isJsonPrimitive()) {
                                isValid = isValidFilterId(filterID.getAsString());
                                // Set message as per above checks
                                if (!isValid) {
                                    setInvalidParameter(VtnServiceOpenStackConsts.FILTER_RES_ID
                                            + VtnServiceConsts.COLON + filterID.getAsString());
                                    LOG.debug("Invalid flow filter id: %s", filterID.getAsString());
                                    break;
                                }
                            } else {
                                setInvalidParameter(
                                        UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
                                isValid = false;
                                break;
                            }
                        }
                    }
                } else {
                    setInvalidParameter(UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
                    isValid = false;
                }
            }
            // filters is not specified, The isValid is true.
        }
    }

    if (isValid) {
        final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);
        if (port.isJsonNull() || port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL)) {
            if (requestBody.has(VtnServiceOpenStackConsts.FILTERS)
                    && requestBody.getAsJsonArray(VtnServiceOpenStackConsts.FILTERS).size() > 0) {
                isValid = false;
                setInvalidParameter("filters,but no port");
            }
        }
    }

    // validation of port and datapath_id combination
    if (isValid) {
        final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);
        final JsonElement datapathid = requestBody.get(VtnServiceOpenStackConsts.DATAPATH_ID);
        if (datapathid.getAsString().isEmpty() && (!port.isJsonNull()
                && !port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL))) {
            isValid = false;
            setInvalidParameter("port specified, but datapath_id not " + "specified");
        }
    }
    LOG.trace("Complete PortResourceValidator#validatePost()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.openstack.validation.RouteResourceValidator.java

License:Open Source License

/**
 * Validates the parameters of POST request body
 * /*from w w w .  j a  v  a  2 s  .c om*/
 * @param requestBody
 *            - JSON request body corresponding to POST operation
 * @return - validation status as true or false
 */
private boolean validatePost(JsonObject requestBody) {
    LOG.trace("Start RouteResourceValidator#validatePost()");
    boolean isValid = true;

    // validation of mandatory parameters
    if (requestBody == null || !requestBody.has(VtnServiceOpenStackConsts.DESTNATION)
            || !requestBody.has(VtnServiceOpenStackConsts.NEXTHOP)) {
        isValid = false;
        setInvalidParameter(UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
    } else {
        final JsonElement destination = requestBody.get(VtnServiceOpenStackConsts.DESTNATION);

        // validation of destination
        if (destination.isJsonNull() || destination.getAsString().isEmpty()
                || !isValidDestination(destination.getAsString())) {
            isValid = false;
            setInvalidParameter(VtnServiceOpenStackConsts.DESTNATION + VtnServiceConsts.COLON
                    + (destination.isJsonNull() ? destination : destination.getAsString()));
        }

        final JsonElement nexthop = requestBody.get(VtnServiceOpenStackConsts.NEXTHOP);

        // validation of nexthop
        if (isValid) {
            if (nexthop.isJsonNull() || nexthop.getAsString().isEmpty()
                    || IpAddressUtil.textToNumericFormatV4(nexthop.getAsString()) == null) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.NEXTHOP + VtnServiceConsts.COLON
                        + (nexthop.isJsonNull() ? nexthop : nexthop.getAsString()));
            }
        }

        /*
         * Check special case for IP address in POST operation
         */
        if (isValid) {
            final String nexthopIp = requestBody.get(VtnServiceOpenStackConsts.NEXTHOP).getAsString();
            if (VtnServiceOpenStackConsts.DEFAULT_IP.equals(nexthopIp)) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.NEXTHOP + VtnServiceConsts.COLON + nexthopIp);
            }
        }
    }
    LOG.trace("Complete RouteResourceValidator#validatePost()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.openstack.validation.RouterInterfaceResourceValidator.java

License:Open Source License

/**
 * Validates the parameters of PUT request body
 * //from  w  w  w .  jav a  2 s .  c o  m
 * @param requestBody
 *            - JSON request body corresponding to PUT operation
 * @return - validation status as true or false
 */
private boolean validatePut(JsonObject requestBody) {
    boolean isValid = true;
    // validation of ip_address
    if (requestBody.has(VtnServiceOpenStackConsts.IP_ADDRESS)) {
        final JsonElement ipAddress = requestBody.get(VtnServiceOpenStackConsts.IP_ADDRESS);

        if (ipAddress.isJsonNull() || ipAddress.getAsString().isEmpty()
                || !isValidIP(ipAddress.getAsString())) {
            isValid = false;
            setInvalidParameter(VtnServiceOpenStackConsts.IP_ADDRESS + VtnServiceConsts.COLON
                    + (ipAddress.isJsonNull() ? ipAddress : ipAddress.getAsString()));
        }
    }

    // validation of mac_address
    if (isValid && requestBody.has(VtnServiceOpenStackConsts.MAC_ADDRESS)) {
        final JsonElement macAddress = requestBody.get(VtnServiceOpenStackConsts.MAC_ADDRESS);

        if (macAddress.isJsonNull() || macAddress.getAsString().isEmpty()
                || !isValidMac(macAddress.getAsString())) {
            isValid = false;
            setInvalidParameter(VtnServiceOpenStackConsts.MAC_ADDRESS + VtnServiceConsts.COLON
                    + (macAddress.isJsonNull() ? macAddress : macAddress.getAsString()));
        }
    }
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.openstack.validation.RouterInterfaceResourceValidator.java

License:Open Source License

/**
 * Validates the parameters of POST request body
 * /*from   www.ja v  a  2  s  . co m*/
 * @param requestBody
 *            - JSON request body corresponding to POST operation
 * @return - validation status as true or false
 */
private boolean validatePost(JsonObject requestBody) {

    boolean isValid = true;

    // validation of mandatory parameter
    if (requestBody == null || !requestBody.has(VtnServiceOpenStackConsts.NET_ID)) {
        isValid = false;
        setInvalidParameter(UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
    } else {
        final JsonElement netId = requestBody.get(VtnServiceOpenStackConsts.NET_ID);

        // validation of net_id
        if (netId.isJsonNull() || netId.getAsString().isEmpty()
                || !validator.isValidMaxLengthAlphaNum(netId.getAsString(), VtnServiceJsonConsts.LEN_31)) {
            isValid = false;
            setInvalidParameter(VtnServiceOpenStackConsts.NET_ID + VtnServiceConsts.COLON
                    + (netId.isJsonNull() ? netId : netId.getAsString()));
        }

        if (isValid) {
            isValid = validatePut(requestBody);
        }
        /*
         * Check special case for IP address in POST operation
         */
        if (isValid && requestBody.has(VtnServiceOpenStackConsts.IP_ADDRESS)) {
            final String ipAddress = requestBody.get(VtnServiceOpenStackConsts.IP_ADDRESS).getAsString();
            if (VtnServiceOpenStackConsts.DEFAULT_IP.equals(ipAddress.split(VtnServiceConsts.SLASH)[0])) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.IP_ADDRESS + VtnServiceConsts.COLON + ipAddress);
            }
        }

        /*
         * Check special case for MAC address in POST operation
         */
        if (isValid && requestBody.has(VtnServiceOpenStackConsts.MAC_ADDRESS)) {
            final String macAddress = requestBody.get(VtnServiceOpenStackConsts.MAC_ADDRESS).getAsString();
            if (VtnServiceOpenStackConsts.DEFAULT_MAC.equals(macAddress)) {
                isValid = false;
                setInvalidParameter(
                        VtnServiceOpenStackConsts.MAC_ADDRESS + VtnServiceConsts.COLON + macAddress);
            }
        }
    }
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.resources.openstack.FiltersResource.java

License:Open Source License

/**
 * When port information is invalid, create error massage.
 * /*from  w ww  . j  av  a2 s .c o  m*/
 * @param info
 *            - Port information reference
 * @return - result as true or false
 */
private String createPortErrorMsg(final JsonObject info) {
    StringBuffer msg = new StringBuffer();

    /* Format1: apply_ports:{tenant:os_vtn_1,network:os_vbr_#2,port:1} */
    /* Format2: apply_ports:{tenant:os_vtn_1,router:os_vrt_#,interface:2} */

    /* Append "apply_ports:{tenant:" */
    // msg.append(VtnServiceOpenStackConsts.APPLY_PORTS);
    //msg.append(VtnServiceConsts.COLON);
    msg.append(VtnServiceConsts.OPEN_CURLY_BRACES);
    msg.append(VtnServiceOpenStackConsts.TENANT);
    msg.append(VtnServiceConsts.COLON);

    if (info.has(VtnServiceOpenStackConsts.TENANT)) {
        JsonElement tenantId = info.get(VtnServiceOpenStackConsts.TENANT);
        if (!(tenantId.isJsonNull() || tenantId.getAsString().isEmpty())) {
            /* Append "os_vtn_1" */
            msg.append(tenantId.getAsString());
        }
    }

    /* Append "," */
    msg.append(VtnServiceConsts.COMMA);

    if (info.has(VtnServiceOpenStackConsts.ROUTER)) {
        /* Append "router:" */
        msg.append(VtnServiceOpenStackConsts.ROUTER);
        msg.append(VtnServiceConsts.COLON);

        JsonElement routerId = info.get(VtnServiceOpenStackConsts.ROUTER);
        if (!(routerId.isJsonNull() || routerId.getAsString().isEmpty())) {
            /* Append "os_vrt_#" */
            msg.append(routerId.getAsString());
        }

        /* Append ",interface:" */
        msg.append(VtnServiceConsts.COMMA);
        msg.append(VtnServiceOpenStackConsts.INTERFACE);
        msg.append(VtnServiceConsts.COLON);

        if (info.has(VtnServiceOpenStackConsts.INTERFACE)) {
            JsonElement ifId = info.get(VtnServiceOpenStackConsts.INTERFACE);
            if (!(ifId.isJsonNull() || ifId.getAsString().isEmpty())) {
                /* Append "2" */
                msg.append(ifId.getAsString());
            }
        }
    } else {
        /* Append "network:" */
        msg.append(VtnServiceOpenStackConsts.NETWORK);
        msg.append(VtnServiceConsts.COLON);

        if (info.has(VtnServiceOpenStackConsts.NETWORK)) {
            JsonElement netId = info.get(VtnServiceOpenStackConsts.NETWORK);
            if (!(netId.isJsonNull() || netId.getAsString().isEmpty())) {
                /* Append "os_vbr_#2" */
                msg.append(netId.getAsString());
            }
        }

        /* Append ",port:" */
        msg.append(VtnServiceConsts.COMMA);
        msg.append(VtnServiceOpenStackConsts.PORT);
        msg.append(VtnServiceConsts.COLON);

        if (info.has(VtnServiceOpenStackConsts.PORT)) {
            JsonElement portId = info.get(VtnServiceOpenStackConsts.PORT);
            if (!(portId.isJsonNull() || portId.getAsString().isEmpty())) {
                /* Append "1" */
                msg.append(portId.getAsString());
            }
        }
    }

    /* Append "}" */
    msg.append(VtnServiceConsts.CLOSE_CURLY_BRACES);

    return msg.toString();
}

From source file:org.opendaylight.vtn.javaapi.resources.openstack.PortsResource.java

License:Open Source License

/**
 * Handler method for POST operation of Port
 * /*from   w  w  w. j a  v a  2 s.  c om*/
 * @see org.opendaylight.vtn.javaapi.resources.AbstractResource#post(com
 *      .google.gson.JsonObject)
 */
@Override
public int post(JsonObject requestBody) {
    LOG.trace("Start NetworksResource#post()");

    int errorCode = UncResultCode.UNC_SERVER_ERROR.getValue();

    boolean isCommitRequired = false;
    String generatedVbrIfName = null;

    int counter = -1;
    Connection connection = null;

    try {
        connection = VtnServiceInitManager.getDbConnectionPoolMap().getConnection();

        /*
         * Check for instances that they exists or not, if not then return
         * 404 error
         */
        if (checkForNotFoundResources(connection)) {
            final ResourceIdManager resourceIdManager = new ResourceIdManager();
            /*
             * generate "id" is it is not present in request body
             */
            if (!requestBody.has(VtnServiceOpenStackConsts.ID)) {
                LOG.info("Resource id auto-generation is required.");

                final FreeCounterBean freeCounterBean = new FreeCounterBean();
                freeCounterBean.setResourceId(VtnServiceOpenStackConsts.PORT_RES_ID);
                freeCounterBean.setVtnName(getTenantId());

                counter = resourceIdManager.getResourceCounter(connection, freeCounterBean);
                if (counter != -1) {
                    LOG.debug("Resource id auto-generation is " + "successfull : " + counter);
                    // if id is generated successfully
                    generatedVbrIfName = VtnServiceOpenStackConsts.IF_PREFIX + counter;
                    requestBody.addProperty(VtnServiceOpenStackConsts.ID, generatedVbrIfName);
                }
            } else {
                generatedVbrIfName = VtnServiceOpenStackConsts.IF_PREFIX
                        + requestBody.get(VtnServiceOpenStackConsts.ID).getAsString();
                requestBody.addProperty(VtnServiceOpenStackConsts.ID, generatedVbrIfName);
                counter = 0;
            }

            LOG.debug("Counter : " + counter);
            LOG.debug("if_name : " + generatedVbrIfName);

            if (counter >= 0) {
                /*
                 * resource insertion in database, if is is successful then
                 * continue to execute operations at UNC. Otherwise return
                 * HTTP 409
                 */
                final VBridgeInterfaceBean vInterfaceBean = new VBridgeInterfaceBean();

                vInterfaceBean.setVbrIfId(counter);
                vInterfaceBean.setVtnName(getTenantId());
                vInterfaceBean.setVbrName(getNetId());
                vInterfaceBean.setVbrIfName(generatedVbrIfName);
                /*
                 * initialize with default port-map setting. It should be
                 * updated when vlan-map operation is performed
                 */
                vInterfaceBean.setMapType(VtnServiceJsonConsts.PORTMAP);
                vInterfaceBean.setLogicalPortId(VtnServiceJsonConsts.NONE);

                final VBridgeInterfaceDao vInterfaceDao = new VBridgeInterfaceDao();
                int status = vInterfaceDao.insert(connection, vInterfaceBean);

                if (status == 1) {
                    LOG.info("Resource insertion successful at " + "database operation.");

                    final RestResource restResource = new RestResource();

                    final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);

                    if (port.isJsonNull()
                            || port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL)) {
                        /*
                         * if port is specified as NULL then vlan-map
                         * operations are required to be performed
                         */
                        errorCode = performVlanMapOperations(requestBody, restResource);

                        if (errorCode == UncCommonEnum.UncResultCode.UNC_SUCCESS.getValue()) {
                            LOG.info("vlan-map Creation at UNC is " + "successful.");

                            vInterfaceBean.setMapType(VtnServiceJsonConsts.VLANMAP);
                            vInterfaceBean.setLogicalPortId(logicalPortId);

                            /**
                             * if operation is performed for vlan-map then
                             * update map_type and logical_port_id in
                             * database
                             */
                            if (vInterfaceDao.updateVlanMapInfo(connection, vInterfaceBean) == 1) {
                                isCommitRequired = true;
                                if (counter != 0) {
                                    final JsonObject response = new JsonObject();
                                    response.addProperty(VtnServiceOpenStackConsts.ID, String.valueOf(counter));
                                    setInfo(response);
                                }
                                LOG.info("map_type and logical_port_id " + "successfully updated in "
                                        + "database.");
                            } else {
                                errorCode = UncResultCode.UNC_SERVER_ERROR.getValue();
                                LOG.error("map_type and logical_port_id " + "update failed in database.");
                            }
                        } else {
                            LOG.error("vlan-map Creation at UNC is " + "failed.");
                        }
                    } else {
                        /*
                         * if port is not specified as NULL then port-map
                         * operations are required to be performed
                         */
                        if (setControllerId(connection, requestBody) == Boolean.TRUE) {
                            errorCode = performPortMapOperations(requestBody, restResource);
                            /*
                             * Filter operations.
                             */
                            if (errorCode == UncCommonEnum.UncResultCode.UNC_SUCCESS.getValue()) {
                                LOG.info("port-map Creation at UNC is " + "successful.");
                                /*
                                 * if filters is specified as no empty, then
                                 * filter operations are required to
                                 * performed.
                                 */
                                if (requestBody.has(VtnServiceOpenStackConsts.FILTERS) && requestBody
                                        .get(VtnServiceOpenStackConsts.FILTERS).getAsJsonArray().size() > 0) {
                                    errorCode = performFilterOperations(requestBody, restResource, connection);
                                    if (errorCode == UncCommonEnum.UncResultCode.UNC_SUCCESS.getValue()) {
                                        LOG.info("Filter Creation at " + "UNC is successful.");
                                    } else {
                                        errorCode = UncResultCode.UNC_SERVER_ERROR.getValue();
                                        LOG.error("Filter Creation at " + "UNC is failed.");
                                    }
                                }
                            } else {
                                errorCode = UncResultCode.UNC_SERVER_ERROR.getValue();
                                LOG.error("port-map Creation at UNC is " + "failed.");
                            }

                            if (errorCode == UncCommonEnum.UncResultCode.UNC_SUCCESS.getValue()) {
                                isCommitRequired = true;
                                if (counter != 0) {
                                    final JsonObject response = new JsonObject();
                                    response.addProperty(VtnServiceOpenStackConsts.ID, String.valueOf(counter));
                                    setInfo(response);
                                }
                            }
                        } else {
                            LOG.error("Error ocurred while setting " + "controller_id");
                        }
                    }
                    checkForSpecificErrors(restResource.getInfo());
                } else {
                    LOG.error("Resource insertion failed at " + "database operation.");
                }
            } else {
                LOG.error("Error occurred while generation of id.");
            }
        } else {
            LOG.error("Resource not found error.");
        }

        /*
         * If all processing are OK, the commit all the database transaction
         * made for current connection. Otherwise do the roll-back
         */
        if (isCommitRequired) {
            // connection.commit();
            setOpenStackConnection(connection);
            LOG.info("Resource deletion successful in database.");
        } else {
            connection.rollback();
            LOG.info("Resource deletion is roll-backed.");
        }

        /*
         * set response, if it is not set during processing for create
         * tenant
         */
        if (errorCode != UncResultCode.UNC_SUCCESS.getValue()) {
            if (getInfo() == null) {
                createErrorInfo(UncResultCode.UNC_INTERNAL_SERVER_ERROR.getValue());
            }
        }
    } catch (final SQLException exception) {
        LOG.error(exception, "Internal server error : " + exception);
        errorCode = UncResultCode.UNC_SERVER_ERROR.getValue();
        if (exception.getSQLState().equalsIgnoreCase(VtnServiceOpenStackConsts.CONFLICT_SQL_STATE)) {
            LOG.error("Conflict found during creation of resource");
            if (counter != 0) {
                generatedVbrIfName = String.valueOf(counter);
            }
            createErrorInfo(UncResultCode.UNC_CONFLICT_FOUND.getValue(),
                    getCutomErrorMessage(UncResultCode.UNC_CONFLICT_FOUND.getMessage(),
                            VtnServiceOpenStackConsts.IF_ID, generatedVbrIfName));
        } else {
            createErrorInfo(UncResultCode.UNC_INTERNAL_SERVER_ERROR.getValue());
        }
    } finally {
        if (connection != null && !isCommitRequired) {
            try {
                connection.rollback();
                LOG.info("roll-back successful.");
            } catch (final SQLException e) {
                LOG.error(e, "Rollback error : " + e);
            }
            LOG.info("Free connection...");
            VtnServiceInitManager.getDbConnectionPoolMap().freeConnection(connection);
        }
    }
    LOG.trace("Complete NetworksResource#post()");
    return errorCode;
}

From source file:org.opendaylight.vtn.javaapi.validation.VtnServiceValidator.java

License:Open Source License

/**
 * Validates the parameters of POST request body
 * //from  w ww . j  a v  a 2 s.  c  o  m
 * @param validator
 *            - instance for CommonValidator
 * @param requestBody
 *            - JSON object contains "id" and "description" parameters
 * @return - validation status as true or false
 */
public boolean validatePost(final CommonValidator validator, final JsonObject requestBody) {
    LOG.trace("Start VtnServiceValidator#validatePost()");
    boolean isValid = true;
    // validation of id
    if (requestBody != null && requestBody.has(VtnServiceOpenStackConsts.ID)) {
        final JsonElement id = requestBody.get(VtnServiceOpenStackConsts.ID);
        if (id.isJsonNull() || id.getAsString().isEmpty()
                || !validator.isValidMaxLengthAlphaNum(id.getAsString(), VtnServiceJsonConsts.LEN_31)) {
            isValid = false;
            setInvalidParameter(VtnServiceOpenStackConsts.ID + VtnServiceConsts.COLON
                    + (id.isJsonNull() ? id : id.getAsString()));
        }
    }

    if (isValid) {
        isValid = validatePut(validator, requestBody);
    }

    LOG.trace("Complete VtnServiceValidator#validatePost()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.validation.VtnServiceValidator.java

License:Open Source License

/**
 * Validates the parameters of PUT request body
 * //from ww  w . ja v a  2  s. c  o  m
 * @param validator
 *            - instance for CommonValidator
 * @param requestBody
 *            - JSON object "description" parameter
 * @return - validation status as true or false
 */
public boolean validatePut(final CommonValidator validator, final JsonObject requestBody) {
    LOG.trace("Start VtnServiceValidator#validatePut()");
    boolean isValid = true;
    // validation of description
    if (requestBody != null && requestBody.has(VtnServiceOpenStackConsts.DESCRIPTION)) {
        final JsonElement description = requestBody.get(VtnServiceOpenStackConsts.DESCRIPTION);
        if (!description.isJsonNull()) {
            if (hasInvaidDescChars(description.getAsString())
                    || !validator.isValidMaxLength(description.getAsString(), VtnServiceJsonConsts.LEN_127)) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.DESCRIPTION + VtnServiceConsts.COLON
                        + description.getAsString());
            }
        }
    }
    LOG.trace("Complete VtnServiceValidator#validatePut()");
    return isValid;
}

From source file:org.opendolphin.core.comm.JsonCodec.java

License:Apache License

private boolean booleanOrFalse(JsonElement element) {
    if (element.isJsonNull()) {
        return false;
    }//from   w  ww . j  av a2s  . com
    return element.getAsBoolean();
}

From source file:org.opendolphin.core.comm.JsonCodec.java

License:Apache License

private String stringOrNull(JsonElement element) {
    if (element.isJsonNull()) {
        return null;
    }//  w  ww  .  j a va 2s .  co  m
    return element.getAsString();
}