Example usage for com.google.gson JsonObject getAsJsonPrimitive

List of usage examples for com.google.gson JsonObject getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

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

License:Open Source License

/**
 * Checks if is valid tagetdb db./*from w ww  . jav  a  2  s  . c  o m*/
 * 
 * @param targetdb
 *            the value of targetdb in the request Json object
 * @return true, if is valid request db
 */
public final boolean isValidRequestDBState(final JsonObject requestBody) {
    LOG.trace("Start CommonValidator#isValidRequestDBState");
    boolean isValid = true;
    if (requestBody.has(VtnServiceJsonConsts.TARGETDB)
            && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString() != null
            && !requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString().isEmpty()) {
        final String targetdb = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString();
        isValid = targetdb.equalsIgnoreCase(VtnServiceJsonConsts.STATE);
    } else {
        requestBody.remove(VtnServiceJsonConsts.TARGETDB);
        requestBody.addProperty(VtnServiceJsonConsts.TARGETDB, VtnServiceJsonConsts.STATE);
    }
    LOG.trace("Complete CommonValidator#isValidRequestDBState");
    return isValid;
}

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

License:Open Source License

/**
 * Validates action_type parameter for VTN Flow Filter POST/PUT APIs
 * /*from   w w w .j  a  v  a 2s.c  o  m*/
 * @param requestBody
 * @return
 */
public boolean isValidVtnFlowFilterEntry(JsonObject requestBody) {
    LOG.trace("Start CommonValidator#isValidVtnFlowFilterEntry()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.FLOWFILTERENTRY);
    if (requestBody.has(VtnServiceJsonConsts.FLOWFILTERENTRY)
            && requestBody.get(VtnServiceJsonConsts.FLOWFILTERENTRY).isJsonObject()) {
        isValid = true;
        final JsonObject ffEntry = requestBody.getAsJsonObject(VtnServiceJsonConsts.FLOWFILTERENTRY);
        // validation for key: action_type
        if (isValid) {
            setInvalidParameter(VtnServiceJsonConsts.ACTIONTYPE);
            // only pass supported in VTN mode
            if (ffEntry.has(VtnServiceJsonConsts.ACTIONTYPE)
                    && ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.ACTIONTYPE).getAsString() != null) {
                final String actionType = ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.ACTIONTYPE)
                        .getAsString();
                isValid = actionType.equalsIgnoreCase(VtnServiceJsonConsts.PASS)
                        || actionType.equalsIgnoreCase(VtnServiceJsonConsts.EMPTY);
            }
        }
    }
    LOG.trace("Complete CommonValidator#isValidVtnFlowFilterEntry()");
    return isValid;
}

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

License:Open Source License

/**
 * Validate post request Json for Acquire Configuration Mode API.
 * //from   w  w w  . j  av  a  2 s .  c  om
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePost(final JsonObject requestBody) {
    LOG.trace("Start ConfigModeResourceValidator#validatePost()");
    boolean isValid = true;
    setInvalidParameter(VtnServiceJsonConsts.OP);
    if (requestBody.has(VtnServiceJsonConsts.OP)
            && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString() != null) {
        isValid = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString()
                .equalsIgnoreCase(VtnServiceJsonConsts.FORCE);
    }

    if (isValid && requestBody.has(VtnServiceJsonConsts.TIMEOUT)) {
        String timeout = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TIMEOUT).getAsString();
        if (timeout != null && !timeout.isEmpty()) {
            long min = Integer.MIN_VALUE;
            long max = Integer.MAX_VALUE;
            try {
                isValid = validator.isValidRange(timeout, min, max);
            } catch (Exception e) {
                isValid = false;
            }
        } else {
            isValid = false;
        }

        if (!isValid) {
            setInvalidParameter(VtnServiceJsonConsts.TIMEOUT);
        }
    }
    LOG.trace("Complete ConfigModeResourceValidator#validatePost()");
    return isValid;
}

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

License:Open Source License

/**
 * Validate put request json for Commit Configuration and Save Configuration
 * API.//from w  w  w .j av  a2s  . c  o m
 * 
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePut(final JsonObject requestBody) {
    LOG.trace("Start ConfigResourceValidator#validatePut()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.CONFIGURATION);
    if (requestBody.has(VtnServiceJsonConsts.CONFIGURATION)
            && requestBody.get(VtnServiceJsonConsts.CONFIGURATION).isJsonObject()) {
        final JsonObject configuration = requestBody.getAsJsonObject(VtnServiceJsonConsts.CONFIGURATION);
        setInvalidParameter(VtnServiceJsonConsts.OPERATION);
        if (configuration.has(VtnServiceJsonConsts.OPERATION)
                && configuration.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION).getAsString() != null) {
            final String operation = configuration.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION)
                    .getAsString();
            isValid = operation.equalsIgnoreCase(VtnServiceJsonConsts.COMMIT)
                    || operation.equalsIgnoreCase(VtnServiceJsonConsts.SAVE);
        }
    }
    LOG.trace("Complete ConfigResourceValidator#validatePut()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.validation.logical.ArpEntryResourceValidator.java

License:Open Source License

/**
 * Validate get request Json object for ArpEntry API.
 * /*from www .  j av a2  s .  com*/
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 * @throws VtnServiceException
 *             the vtn service exception
 */
private boolean validateGet(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("Start ArpEntryResourceValidator#validateGet()");
    boolean isValid = true;
    // validation for key: op(optional)
    setInvalidParameter(VtnServiceJsonConsts.OP);
    isValid = validator.isValidOperationForCount(requestBody);
    // validation for key: type(optional)
    if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.TYPE);
        if (requestBody.has(VtnServiceJsonConsts.TYPE)
                && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TYPE).getAsString() != null) {
            final String type = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TYPE).getAsString();
            isValid = type.equalsIgnoreCase(VtnServiceJsonConsts.STATIC)
                    || type.equalsIgnoreCase(VtnServiceJsonConsts.DYNAMIC);

        }
    }
    LOG.trace("Complete ArpEntryResourceValidator#validateGet()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.validation.logical.DhcpRelayResourceValidator.java

License:Open Source License

/**
 * Validate put request Json object for DhcpRelay API.
 * //from   ww  w  .java2  s  .c  o m
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 * @throws VtnServiceException
 *             the vtn service exception
 */
private boolean validatePut(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("Start DhcpRelayResourceValidator#validatePut()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.DHCPRELAY);
    if (requestBody.has(VtnServiceJsonConsts.DHCPRELAY)
            && requestBody.get(VtnServiceJsonConsts.DHCPRELAY).isJsonObject()) {
        final JsonObject dhcpRelay = requestBody.getAsJsonObject(VtnServiceJsonConsts.DHCPRELAY);
        // validation for key: relay_status(mandatory)
        setInvalidParameter(VtnServiceJsonConsts.DHCPRELAYSTATUS);
        if (dhcpRelay.has(VtnServiceJsonConsts.DHCPRELAYSTATUS)
                && dhcpRelay.getAsJsonPrimitive(VtnServiceJsonConsts.DHCPRELAYSTATUS).getAsString() != null) {
            final String relayStatus = dhcpRelay.getAsJsonPrimitive(VtnServiceJsonConsts.DHCPRELAYSTATUS)
                    .getAsString();
            isValid = relayStatus.equalsIgnoreCase(VtnServiceJsonConsts.ENABLE)
                    || relayStatus.equalsIgnoreCase(VtnServiceJsonConsts.DISABLE);
        }
    }
    LOG.trace("Complete DhcpRelayResourceValidator#validatePut()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.validation.logical.DhcpRelayServerResourceValidator.java

License:Open Source License

/**
 * Validate post request Json object for DhcpRelayServer API.
 * //from   www  . ja va2  s .  c om
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePost(final JsonObject requestBody) {
    LOG.trace("Start DhcpRelayServerResourceValidator#validatePost()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.SERVER);
    if (requestBody.has(VtnServiceJsonConsts.SERVER)
            && requestBody.get(VtnServiceJsonConsts.SERVER).isJsonObject()) {
        final JsonObject dcpRelayServer = requestBody.getAsJsonObject(VtnServiceJsonConsts.SERVER);
        // validation for key: ipaddr(mandatory)
        setInvalidParameter(VtnServiceJsonConsts.IPADDR);
        if (dcpRelayServer.has(VtnServiceJsonConsts.IPADDR)
                && dcpRelayServer.getAsJsonPrimitive(VtnServiceJsonConsts.IPADDR).getAsString() != null) {
            isValid = validator
                    .isValidIpV4(dcpRelayServer.getAsJsonPrimitive(VtnServiceJsonConsts.IPADDR).getAsString());
        }
    }
    LOG.trace("Complete DhcpRelayServerResourceValidator#validatePost()");
    return isValid;

}

From source file:org.opendaylight.vtn.javaapi.validation.logical.DhcpRelayServerResourceValidator.java

License:Open Source License

/**
 * Validate get request json for DhcpRelayServer API.
 * /*from  ww w  .  j a  va 2s .  c o m*/
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 * @throws JsonParseException
 *             the json parse exception
 */
private boolean validateGet(final JsonObject requestBody, final boolean opFlag) throws JsonParseException {
    LOG.trace("Start DhcpRelayServerResourceValidator#validateGet()");
    boolean isValid = true;
    // validation for key: targetdb(optional)
    setInvalidParameter(VtnServiceJsonConsts.TARGETDB);
    if (isValid) {
        isValid = validator.isValidRequestDB(requestBody);
    }
    if (!opFlag) {
        if (requestBody.has(VtnServiceJsonConsts.OP)) {
            requestBody.remove(VtnServiceJsonConsts.OP);
        } else {
            LOG.debug("No need to remove");
        }
        if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
            requestBody.remove(VtnServiceJsonConsts.INDEX);
        } else {
            LOG.debug("No need to remove");
        }
        if (requestBody.has(VtnServiceJsonConsts.MAX)) {
            requestBody.remove(VtnServiceJsonConsts.MAX);
        } else {
            LOG.debug("No need to remove");
        }
    } else {
        // validation for key: op(optional)
        if (isValid) {
            setInvalidParameter(VtnServiceJsonConsts.OP);
            isValid = validator.isValidOperationForCount(requestBody);
        }
        // validation for key: index(optional)
        if (isValid) {
            setInvalidParameter(VtnServiceJsonConsts.INDEX);
            if (requestBody.has(VtnServiceJsonConsts.INDEX)
                    && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString() != null) {
                isValid = validator
                        .isValidIpV4(requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString());
            }
        }
        // validation for key: max(optional)
        if (isValid) {
            setInvalidParameter(VtnServiceJsonConsts.MAX);
            isValid = validator.isValidMaxRepetition(requestBody);
        }
    }
    LOG.trace("Complete DhcpRelayServerResourceValidator#validateGet()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.validation.logical.FlowFilterEntriesResourceValidator.java

License:Open Source License

/**
 * Validate post request Json object for FlowFilterEntriesResource.
 * //from   w  w w.j ava 2s.c  o m
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePost(final JsonObject requestBody) {
    LOG.trace("Start FlowFilterEntriesResourceValidator#validatePost()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.FLOWFILTERENTRY);
    if (requestBody.has(VtnServiceJsonConsts.FLOWFILTERENTRY)
            && requestBody.get(VtnServiceJsonConsts.FLOWFILTERENTRY).isJsonObject()) {
        final JsonObject ffEntry = requestBody.getAsJsonObject(VtnServiceJsonConsts.FLOWFILTERENTRY);
        // validation for mandatory key: seqnum
        setInvalidParameter(VtnServiceJsonConsts.SEQNUM);
        if (ffEntry.has(VtnServiceJsonConsts.SEQNUM)
                && ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.SEQNUM).getAsString() != null
                && !ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.SEQNUM).getAsString().isEmpty()) {
            isValid = validator.isValidRange(
                    ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.SEQNUM).getAsString(),
                    VtnServiceJsonConsts.VAL_1, VtnServiceJsonConsts.VAL_65535);
        } else {
            isValid = false;
        }
        if (isValid) {
            isValid = validator.isValidFlowFilterEntry(requestBody)
                    && validator.isValidVtnFlowFilterEntry(requestBody);
            setInvalidParameter(validator.getInvalidParameter());
        }
    }
    LOG.trace("Complete FlowFilterEntriesResourceValidator#validatePost()");
    return isValid;
}

From source file:org.opendaylight.vtn.javaapi.validation.logical.FlowFilterEntryResourceValidator.java

License:Open Source License

/**
 * Validate get request Json object for FlowFilterEntry API
 * /*from w  ww .  j  av  a 2s.c  o  m*/
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validateGet(final JsonObject requestBody) {
    LOG.trace("Start FlowFilterEntryResourceValidator#validateGet()");
    boolean isValid = false;

    // validation for key: targetdb
    setInvalidParameter(VtnServiceJsonConsts.TARGETDB);
    isValid = validator.isValidRequestDB(requestBody);

    // validation for key: op
    if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.OP);
        // isValid = validator.isValidOperation(requestBody);
        if (requestBody.has(VtnServiceJsonConsts.OP)
                && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString() != null
                && !requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString().isEmpty()) {
            final String operation = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString();
            isValid = operation.equalsIgnoreCase(VtnServiceJsonConsts.DETAIL);
        } else {
            requestBody.remove(VtnServiceJsonConsts.OP);
            requestBody.addProperty(VtnServiceJsonConsts.OP, VtnServiceJsonConsts.NORMAL);
        }
    }

    if (isValid) {
        // validation for key: controller_id(optional)
        setInvalidParameter(VtnServiceJsonConsts.CONTROLLERID);
        if (requestBody.has(VtnServiceJsonConsts.CONTROLLERID)
                && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.CONTROLLERID).getAsString() != null) {
            isValid = validator.isValidMaxLengthAlphaNum(
                    requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.CONTROLLERID).getAsString(),
                    VtnServiceJsonConsts.LEN_31);
        }
    }

    if (isValid) {
        // validation for key: domain_id(optional)
        setInvalidParameter(VtnServiceJsonConsts.DOMAINID);
        if (requestBody.has(VtnServiceJsonConsts.DOMAINID)
                && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.DOMAINID).getAsString() != null) {
            isValid = validator.isValidDomainId(
                    requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.DOMAINID).getAsString(),
                    VtnServiceJsonConsts.LEN_31);
        }
    }

    /*
     * controller_id and domain_id are mandatory for detail case
     */
    if (requestBody.get(VtnServiceJsonConsts.OP).getAsString().equalsIgnoreCase(VtnServiceJsonConsts.DETAIL)
            && requestBody.get(VtnServiceJsonConsts.TARGETDB).getAsString()
                    .equalsIgnoreCase(VtnServiceJsonConsts.STATE)) {
        setInvalidParameter(
                VtnServiceJsonConsts.CONTROLLERID + VtnServiceConsts.HYPHEN + VtnServiceJsonConsts.DOMAINID);
        if (requestBody.has(VtnServiceJsonConsts.CONTROLLERID)
                && requestBody.has(VtnServiceJsonConsts.DOMAINID)) {
            LOG.info("mandatory parameters are provided for detail case");
        } else {
            LOG.error("mandatory parameters are not provided for detail case");
            isValid = false;
        }
    }

    // index parameter should not be used in show APIs
    if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
        requestBody.remove(VtnServiceJsonConsts.INDEX);
    }

    LOG.trace("Complete FlowFilterEntryResourceValidator#validateGet()");
    return isValid;
}