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.ipc.IpcRequestProcessor.java

License:Open Source License

/**
 * Update data type according to "targetdb" parameter's value
 * // ww w  .j av a  2s . co  m
 * @param requestBody
 */
private void setDataType(final JsonObject requestBody) {

    LOG.trace("Start IpcRequestProcessor#setDataType()");
    // check if "targetdb" parameter is received
    if (requestBody.has(VtnServiceJsonConsts.TARGETDB)) {
        // get value of "targetdb" parameter
        final String dataType = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString()
                .trim();
        // set the data type to request packet
        if (dataType.equalsIgnoreCase(VtnServiceJsonConsts.STARTUP)) {
            requestPacket.setDataType(new IpcUint32(UncDataType.UNC_DT_STARTUP.ordinal()));
        } else if (dataType.equalsIgnoreCase(VtnServiceJsonConsts.RUNNING)) {
            requestPacket.setDataType(new IpcUint32(UncDataType.UNC_DT_RUNNING.ordinal()));
        } else if (dataType.equalsIgnoreCase(VtnServiceJsonConsts.CANDIDATE)) {
            requestPacket.setDataType(new IpcUint32(UncDataType.UNC_DT_CANDIDATE.ordinal()));
        } else {
            LOG.warning("No need to update data type from state to another one");
        }
    } else {
        LOG.debug("No need to do anything");
    }
    LOG.trace("Complete IpcRequestProcessor#setDataType()");
}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcRequestProcessor.java

License:Open Source License

/**
 * Update max repetition type according to "max_repetition" parameter's
 * value/*www . j a va2 s. c  o m*/
 * 
 * @param requestBody
 * @throws VtnServiceException
 */
private void setMaxRepCount(final JsonObject requestBody) throws VtnServiceException {

    LOG.trace("Start IpcRequestProcessor#setMaxRepCount()");
    // check id "max_repetition" parameter is received
    if (requestBody.has(VtnServiceJsonConsts.MAX) && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.MAX)
            .getAsLong() < VtnServiceConsts.MAX_REP_COUNT) {
        // set the value of "max_repetition" parameter to IPC request
        // parameter
        requestPacket.setMaxRepCount(
                new IpcUint32(requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.MAX).getAsString()));
    } else {
        requestPacket.setMaxRepCount(new IpcUint32(VtnServiceConsts.MAX_REP_COUNT));
    }
    LOG.trace("Complete IpcRequestProcessor#setMaxRepCount()");
}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcStructFactory.java

License:Open Source License

/**
 * Gets the key vtn flow filter controller struct.
 * //  w ww.  j a v a2 s .  c o m
 * @param requestBody
 *            the request body
 * @param uriParameters
 *            the uri parameters
 * @return the key vtn flow filter entry struct
 */
public final IpcStruct getKeyVtnFlowfilterControllerStruct(final JsonObject requestBody,
        final List<String> uriParameters) {
    // Lower level structure
    /*
     * ipc_struct KeyVtnFlowfilterController { KeyVtn vtn_key; UINT8
     * controller_name[NN+1]; };
     */
    LOG.trace("Start getKeyVtnFlowfilterControllerStruct");
    final IpcStruct keyVtnFlowfilterControllerStruct = new IpcStruct(
            UncStructEnum.KeyVtnFlowfilterController.getValue());
    IpcStruct keyVtnStruct = null;
    if (uriParameters != null && uriParameters.size() >= UncIndexEnum.ONE.ordinal()) {
        keyVtnStruct = getKeyVtnStruct(requestBody, uriParameters.subList(0, 1));
    }
    keyVtnFlowfilterControllerStruct.set(VtnServiceIpcConsts.VTNKEY, keyVtnStruct);
    if (requestBody != null && requestBody.has(VtnServiceJsonConsts.DOMAINID)) {
        keyVtnFlowfilterControllerStruct.set(VtnServiceJsonConsts.DOMAINID,
                IpcDataUnitWrapper.setIpcUint8ArrayValue(
                        requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.DOMAINID).getAsString()));
    } else {
        LOG.warning("request body and uri parameters are not correct for getKeyVtnFlowfilterControllerStruct");
    }
    if (requestBody != null && requestBody.has(VtnServiceJsonConsts.CONTROLLERID)) {
        keyVtnFlowfilterControllerStruct.set(VtnServiceIpcConsts.CONTROLLERNAME,
                IpcDataUnitWrapper.setIpcUint8ArrayValue(
                        requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.CONTROLLERID).getAsString()));
    } else {
        LOG.warning("request body and uri parameters are not correct for getKeyVtnFlowfilterControllerStruct");
    }
    LOG.info("Key Structure: " + keyVtnFlowfilterControllerStruct.toString());
    LOG.trace("Complete getKeyVtnFlowfilterControllerStruct");
    return keyVtnFlowfilterControllerStruct;
}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcStructFactory.java

License:Open Source License

/**
 * Gets val_policingmap struct./*from   ww w  .ja v a 2s.  com*/
 * 
 * @param requestBody
 *            the request body
 * @param uriParameters
 *            the uri parameters
 * @return the valflowfilterEntry struct
 */
public final IpcStruct getValPolicingmapStruct(final JsonObject requestBody, final List<String> uriParameters) {
    LOG.trace("Start getValPolicingmapStruct");
    final IpcStruct valVbrPolicingmapStruct = IpcDataUnitWrapper
            .setIpcStructValue(UncStructEnum.ValPolicingmap.getValue());
    if (requestBody.has(VtnServiceJsonConsts.POLICINGMAP)
            && requestBody.get(VtnServiceJsonConsts.POLICINGMAP).isJsonObject()) {
        final JsonObject policingmap = requestBody.getAsJsonObject(VtnServiceJsonConsts.POLICINGMAP);
        if (policingmap.has(VtnServiceJsonConsts.PROFILE_NAME)) {
            String profileName = policingmap.getAsJsonPrimitive(VtnServiceJsonConsts.PROFILE_NAME)
                    .getAsString();
            valVbrPolicingmapStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValPolicingMapIndex.UPLL_IDX_POLICERNAME_PM.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_VALID.ordinal()));
            valVbrPolicingmapStruct.set(VtnServiceIpcConsts.POLICERNAME,
                    IpcDataUnitWrapper.setIpcUint8ArrayValue(profileName, valVbrPolicingmapStruct,
                            UncStructIndexEnum.ValPolicingMapIndex.UPLL_IDX_POLICERNAME_PM.ordinal()));
        } else {
            valVbrPolicingmapStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValPolicingMapIndex.UPLL_IDX_POLICERNAME_PM.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_INVALID.ordinal()));
        }
    } else {
        LOG.warning("request body is not correct for getValPolicingmapStruct");
    }
    LOG.trace("Complete getValPolicingmapStruct");
    return valVbrPolicingmapStruct;
}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcStructFactory.java

License:Open Source License

/**
 * Gets the key Vtn PolicingMap Controller struct.
 * /*  w ww .  java 2  s  .  c om*/
 * @param requestBody
 *            the request body
 * @param uriParameters
 *            the uri parameters
 * @return the Key Vtn PolicingMap Controller Structure
 */
public final IpcStruct getKeyVtnPolicingMapControllerStruct(final JsonObject requestBody,
        final List<String> uriParameters) {
    LOG.trace("Start getKeyVtnPolicingMapControllerStruct");
    final IpcStruct keyVtnPolcingMapControllerStruct = new IpcStruct(
            UncStructEnum.KeyVtnPolicingMapController.getValue());
    IpcStruct keyVtnStruct = null;
    if (uriParameters != null && uriParameters.size() >= UncIndexEnum.ONE.ordinal()) {
        keyVtnStruct = getKeyVtnStruct(requestBody, uriParameters.subList(0, 1));
    }
    keyVtnPolcingMapControllerStruct.set(VtnServiceIpcConsts.VTNKEY, keyVtnStruct);
    if (requestBody != null && requestBody.has(VtnServiceJsonConsts.DOMAINID)
            && requestBody.has(VtnServiceJsonConsts.CONTROLLERID)) {
        keyVtnPolcingMapControllerStruct.set(VtnServiceIpcConsts.DOMAINID,
                IpcDataUnitWrapper.setIpcUint8ArrayValue(
                        requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.DOMAINID).getAsString()));
        keyVtnPolcingMapControllerStruct.set(VtnServiceIpcConsts.CONTROLLERNAME,
                IpcDataUnitWrapper.setIpcUint8ArrayValue(
                        requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.CONTROLLERID).getAsString()));
    } else {
        LOG.warning("request body and uri parameters are not correct for getKeyVtnPolcingMapControllerStruct");
    }
    LOG.trace("Complete getKeyVtnPolicingMapControllerStruct");
    return keyVtnPolcingMapControllerStruct;
}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcStructFactory.java

License:Open Source License

/**
 * Gets the Value VTN Path Map Entry Struct.
 * /*from   w  w  w.j  a  va2  s. c om*/
 * @param requestBody
 *            the request body
 * @param uriParameters
 *            the uri parameters
 * @return the Value VTN Path Map Entry Structure
 */
public final IpcStruct getValVtnPathMapEntryStruct(final JsonObject requestBody,
        final List<String> uriParameters) {
    LOG.trace("Start getValVtnPathMapEntryStruct");
    final IpcStruct valVtnPathMapEntryStruct = IpcDataUnitWrapper
            .setIpcStructValue(UncStructEnum.ValVtnPathMapEntry.getValue());
    if (requestBody.has(VtnServiceJsonConsts.PATHMAPENTRY)
            && requestBody.get(VtnServiceJsonConsts.PATHMAPENTRY).isJsonObject()) {
        final JsonObject pathMapEntryJsonObject = requestBody
                .getAsJsonObject(VtnServiceJsonConsts.PATHMAPENTRY);
        if (pathMapEntryJsonObject.has(VtnServiceJsonConsts.FLNAME)) {
            String flName = pathMapEntryJsonObject.getAsJsonPrimitive(VtnServiceJsonConsts.FLNAME)
                    .getAsString();
            valVtnPathMapEntryStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValVtnPathMapEntryIndex.UPLL_IDX_FLOWLIST_NAME_VPME.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_VALID.ordinal()));
            valVtnPathMapEntryStruct.set(VtnServiceIpcConsts.FLOWLIST_NAME,
                    IpcDataUnitWrapper.setIpcUint8ArrayValue(flName, valVtnPathMapEntryStruct,
                            UncStructIndexEnum.ValVtnPathMapEntryIndex.UPLL_IDX_FLOWLIST_NAME_VPME.ordinal()));
        } else {
            valVtnPathMapEntryStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValVtnPathMapEntryIndex.UPLL_IDX_FLOWLIST_NAME_VPME.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_INVALID.ordinal()));
        }
    } else {
        LOG.warning("request body is not correct for getValVtnPathMapEntryStruct");
    }
    LOG.trace("Complete getValVtnPathMapEntryStruct");
    return valVtnPathMapEntryStruct;

}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcStructFactory.java

License:Open Source License

/**
 * Gets the Value VTN Path Policy Entry Struct.
 * /*from   ww  w. j  a v a 2  s  .co  m*/
 * @param requestBody
 *            the request body
 * @param uriParameters
 *            the uri parameters
 * @return the Value VTN Path Policy Entry Structure
 */
public final IpcStruct getValVtnPathmapPpolicyEntryStruct(final JsonObject requestBody,
        final List<String> uriParameters) {
    LOG.trace("Start getValVtnPathmapPpolicyEntryStruct");
    final IpcStruct valVtnpPolicyEntryStruct = IpcDataUnitWrapper
            .setIpcStructValue(UncStructEnum.ValVtnPathmapPpolicyEntry.getValue());
    if (requestBody != null && requestBody.has(VtnServiceJsonConsts.PATHPOLICYENTRY)
            && requestBody.get(VtnServiceJsonConsts.PATHPOLICYENTRY).isJsonObject()) {
        final JsonObject pPolicyEntryJsonObject = requestBody
                .getAsJsonObject(VtnServiceJsonConsts.PATHPOLICYENTRY);
        if (pPolicyEntryJsonObject.has(VtnServiceJsonConsts.POLICYID)) {
            String policyId = pPolicyEntryJsonObject.getAsJsonPrimitive(VtnServiceJsonConsts.POLICYID)
                    .getAsString();
            valVtnpPolicyEntryStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValVtnPathmapPpolicyEntryIndex.UPLL_IDX_POLICY_ID_VPMPPE.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_VALID.ordinal()));
            valVtnpPolicyEntryStruct.set(VtnServiceIpcConsts.POLICYID, IpcDataUnitWrapper.setIpcUint8Value(
                    policyId, valVtnpPolicyEntryStruct,
                    UncStructIndexEnum.ValVtnPathmapPpolicyEntryIndex.UPLL_IDX_POLICY_ID_VPMPPE.ordinal()));
        } else {
            valVtnpPolicyEntryStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValVtnPathmapPpolicyEntryIndex.UPLL_IDX_POLICY_ID_VPMPPE.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_INVALID.ordinal()));
        }
        if (pPolicyEntryJsonObject.has(VtnServiceJsonConsts.AGEOUTTIME)) {
            String ageoutTime = pPolicyEntryJsonObject.getAsJsonPrimitive(VtnServiceJsonConsts.AGEOUTTIME)
                    .getAsString();
            valVtnpPolicyEntryStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValVtnPathmapPpolicyEntryIndex.UPLL_IDX_AGING_TIME_OUT_VPMPPE.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_VALID.ordinal()));
            valVtnpPolicyEntryStruct.set(VtnServiceIpcConsts.AGINGOUTTIME,
                    IpcDataUnitWrapper.setIpcUint16Value(ageoutTime, valVtnpPolicyEntryStruct,
                            UncStructIndexEnum.ValVtnPathmapPpolicyEntryIndex.UPLL_IDX_AGING_TIME_OUT_VPMPPE
                                    .ordinal()));
        } else {
            valVtnpPolicyEntryStruct.set(VtnServiceIpcConsts.VALID,
                    UncStructIndexEnum.ValVtnPathmapPpolicyEntryIndex.UPLL_IDX_AGING_TIME_OUT_VPMPPE.ordinal(),
                    IpcDataUnitWrapper.setIpcUint8Value(UncStructIndexEnum.Valid.UNC_VF_INVALID.ordinal()));
        }
    } else {
        LOG.error("request body is not correct for getValVtnPathmapPpolicyEntryStruct");
    }
    LOG.trace("Complete getValVtnPathmapPpolicyEntryStruct");
    return valVtnpPolicyEntryStruct;
}

From source file:org.opendaylight.vtn.javaapi.resources.AbstractResource.java

License:Open Source License

/**
 * Invoke the UPPL services, if the previous call contains more than 10K
 * (configurable)//from   w w w .  j  a  v a 2 s .c  o  m
 * 
 * @param requestBody
 * @param requestProcessor
 * @param responseGenerator
 * @param responseArray
 * @param JsonArrayName
 * @param IndexName
 * @param requestPackeEnumName
 * @param uriParameters
 * @param methodName
 * @return
 * @throws VtnServiceException
 */
public JsonObject getResponseJsonArrayPhysical(final JsonObject requestBody,
        final IpcRequestProcessor requestProcessor, final Object responseGenerator,
        final JsonArray responseArray, final String JsonArrayName, final String IndexName,
        final IpcRequestPacketEnum requestPackeEnumName, final List<String> uriParameters,
        final String methodName) throws VtnServiceException {
    // session reset
    requestProcessor.setServiceInfo(UncUPPLEnums.UPPL_IPC_SVC_NAME,
            UncUPPLEnums.ServiceID.UPPL_SVC_READREQ.ordinal());
    int status = ClientSession.RESP_FATAL;
    long memberIndex = 0;
    long max_repetition = 0;
    //get max_repetition.
    max_repetition = Long.parseLong(requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.MAX).getAsString());
    memberIndex = responseArray.size();
    if (memberIndex != 0 && memberIndex < max_repetition) {
        JsonObject memberLastIndex = (JsonObject) responseArray.get(responseArray.size() - 1);
        if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
            uriParameters.remove(uriParameters.size() - 1);
            uriParameters.add(uriParameters.size(), memberLastIndex.get(IndexName).getAsString());
        } else {
            uriParameters.add(memberLastIndex.get(IndexName).getAsString());
        }
        // (noSuchInstanceFlag = 0 ) means that all data has been acquired
        // (noSuchInstanceFlag != 0) means that there may be data.
        int noSuchInstanceFlag = 0;
        noSuchInstanceFlag = responseArray.size();
        while (noSuchInstanceFlag != 0 && memberIndex < max_repetition) {

            JsonArray memberArray = null;
            memberLastIndex = (JsonObject) responseArray.get(responseArray.size() - 1);
            uriParameters.remove(uriParameters.size() - 1);
            uriParameters.add(uriParameters.size(), memberLastIndex.get(IndexName).getAsString());

            requestProcessor.createIpcRequestPacket(requestPackeEnumName, requestBody, uriParameters);

            // update the operation to read_sibling.
            if (requestProcessor.getRequestPacket().getOperation()
                    .intValue() != UncOperationEnum.UNC_OP_READ_SIBLING.ordinal()) {
                requestProcessor.getRequestPacket()
                        .setOperation(new IpcUint32(UncOperationEnum.UNC_OP_READ_SIBLING.ordinal()));
            }

            status = requestProcessor.processIpcRequest();
            if (status == ClientSession.RESP_FATAL) {
                throw new VtnServiceException(
                        Thread.currentThread().getStackTrace()[1].getClassName() + VtnServiceConsts.HYPHEN
                                + Thread.currentThread().getStackTrace()[1].getMethodName(),
                        UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
                        UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage());
            }
            try {
                Method method;

                final Class<IpcPhysicalResponseFactory> sourceClass = IpcPhysicalResponseFactory.class;
                // get the method name to get the IpcLogicalResponseFactory
                // object for given key
                method = sourceClass.getMethod(methodName,
                        new Class<?>[] { IpcDataUnit[].class, JsonObject.class, String.class });
                // get IpcLogicalResponseFactory object

                memberArray = ((JsonObject) method.invoke(responseGenerator,
                        requestProcessor.getIpcResponsePacket(), requestBody, VtnServiceJsonConsts.LIST))
                                .getAsJsonArray(JsonArrayName);
                noSuchInstanceFlag = memberArray.size();
            } catch (final Exception e) {
                exceptionHandler.raise(
                        Thread.currentThread().getStackTrace()[1].getClassName() + VtnServiceConsts.HYPHEN
                                + Thread.currentThread().getStackTrace()[1].getMethodName(),
                        UncJavaAPIErrorCode.INTERNAL_ERROR.getErrorCode(),
                        UncJavaAPIErrorCode.INTERNAL_ERROR.getErrorMessage(), e);
            }
            if (null != memberArray && !memberArray.isJsonNull() && memberArray.size() > 0) {
                // all the required data have been acquired.
                if (memberIndex + memberArray.size() > max_repetition) {
                    for (long i = memberIndex; i < max_repetition; i++) {
                        responseArray.getAsJsonArray().add((memberArray.get((int) (i - memberIndex))));
                    }
                    memberIndex = max_repetition;
                } else {
                    responseArray.getAsJsonArray().addAll(memberArray);
                    memberIndex += memberArray.size();
                }
            } else {
                break;
            }
        }
    }
    final JsonObject root = new JsonObject();
    root.add(JsonArrayName, responseArray);
    return root;
}

From source file:org.opendaylight.vtn.javaapi.resources.AbstractResource.java

License:Open Source License

/**
 * Invoke the UPLL services, if the previous call contains more than 10K
 * (configurable)// w w w.  ja v  a  2  s  .co  m
 * 
 * @param requestBody
 * @param requestProcessor
 * @param responseGenerator
 * @param responseArray
 * @param JsonArrayName
 * @param IndexName
 * @param requestPackeEnumName
 * @param uriParameters
 * @param methodName
 * @return
 * @throws VtnServiceException
 */
public JsonObject getResponseJsonArrayLogical(final JsonObject requestBody,
        final IpcRequestProcessor requestProcessor, final Object responseGenerator,
        final JsonArray responseArray, final String JsonArrayName, final String IndexName,
        final IpcRequestPacketEnum requestPackeEnumName, final List<String> uriParameters,
        final String methodName) throws VtnServiceException {
    // session reset
    requestProcessor.setServiceInfo(UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
            UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal());
    int status = ClientSession.RESP_FATAL;
    long memberIndex = 0;
    long max_repetition = 0;
    //get max_repetition.
    max_repetition = Long.parseLong(requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.MAX).getAsString());
    memberIndex = responseArray.size();

    if (memberIndex != 0 && memberIndex < max_repetition) {
        JsonObject memberLastIndex = (JsonObject) responseArray.get(responseArray.size() - 1);
        if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
            uriParameters.remove(uriParameters.size() - 1);
        }

        String indexParams[] = IndexName.split(VtnServiceConsts.COMMA);
        for (String param : indexParams) {
            uriParameters.add(memberLastIndex.get(param).getAsString());
        }

        // (noSuchInstanceFlag = 0 ) means that all data has been acquired.
        // (noSuchInstanceFlag != 0) means that there may be data.
        int noSuchInstanceFlag = 0;
        noSuchInstanceFlag = responseArray.size();
        while (noSuchInstanceFlag != 0 && memberIndex < max_repetition) {

            JsonArray memberArray = null;
            memberLastIndex = (JsonObject) responseArray.get(responseArray.size() - 1);
            uriParameters.remove(uriParameters.size() - 1);
            uriParameters.add(uriParameters.size(), memberLastIndex.get(IndexName).getAsString());

            requestProcessor.createIpcRequestPacket(requestPackeEnumName, requestBody, uriParameters);

            // update the operation to read_sibling.
            if (requestProcessor.getRequestPacket().getOperation()
                    .intValue() != UncOperationEnum.UNC_OP_READ_SIBLING.ordinal()) {
                requestProcessor.getRequestPacket()
                        .setOperation(new IpcUint32(UncOperationEnum.UNC_OP_READ_SIBLING.ordinal()));
            }

            status = requestProcessor.processIpcRequest();
            if (status == ClientSession.RESP_FATAL) {
                throw new VtnServiceException(
                        Thread.currentThread().getStackTrace()[1].getClassName() + VtnServiceConsts.HYPHEN
                                + Thread.currentThread().getStackTrace()[1].getMethodName(),
                        UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
                        UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage());
            }
            try {
                Method method;

                final Class<IpcLogicalResponseFactory> sourceClass = IpcLogicalResponseFactory.class;
                // get the method name to get the IpcLogicalResponseFactory
                // object for given key
                method = sourceClass.getMethod(methodName,
                        new Class<?>[] { IpcDataUnit[].class, JsonObject.class, String.class });
                // get IpcLogicalResponseFactory object

                memberArray = ((JsonObject) method.invoke(responseGenerator,
                        requestProcessor.getIpcResponsePacket(), requestBody, VtnServiceJsonConsts.LIST))
                                .getAsJsonArray(JsonArrayName);
                noSuchInstanceFlag = memberArray.size();
            } catch (final Exception e) {
                exceptionHandler.raise(
                        Thread.currentThread().getStackTrace()[1].getClassName() + VtnServiceConsts.HYPHEN
                                + Thread.currentThread().getStackTrace()[1].getMethodName(),
                        UncJavaAPIErrorCode.INTERNAL_ERROR.getErrorCode(),
                        UncJavaAPIErrorCode.INTERNAL_ERROR.getErrorMessage(), e);
            }
            if (null != memberArray && !memberArray.isJsonNull() && memberArray.size() > 0) {
                // all the required data have been acquired.
                if (memberIndex + memberArray.size() > max_repetition) {
                    for (long i = memberIndex; i < max_repetition; i++) {
                        responseArray.getAsJsonArray().add((memberArray.get((int) (i - memberIndex))));
                    }
                    memberIndex = max_repetition;
                } else {
                    responseArray.getAsJsonArray().addAll(memberArray);
                    memberIndex += memberArray.size();
                }
            } else {
                break;
            }
        }
    }
    final JsonObject root = new JsonObject();
    root.add(JsonArrayName, responseArray);
    return root;
}

From source file:org.opendaylight.vtn.javaapi.resources.AcquireConfigModeResource.java

License:Open Source License

/**
 * Implementation of Post method of configuration mode API.
 * //from   w ww  . j a va  2 s. c  o m
 * @param requestBody
 *            the request Json Object
 * 
 * @return Error code
 * @throws VtnServiceException
 *             the vtn service exception
 */
@Override
public final int post(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("Starts AcquireConfigModeResource#post()");
    ClientSession session = null;
    int status = ClientSession.RESP_FATAL;
    boolean isForce = false;
    try {
        LOG.debug("Start Ipc framework call");
        session = getConnPool().getSession(UncTCEnums.UNC_CHANNEL_NAME, UncTCEnums.TC_SERVICE_NAME,
                UncTCEnums.ServiceID.TC_CONFIG_SERVICES.ordinal(), getExceptionHandler());
        LOG.info("Session created successfully");
        if (requestBody != null && requestBody.has(VtnServiceJsonConsts.OP)
                && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString()
                        .equalsIgnoreCase(VtnServiceJsonConsts.FORCE)) {
            session.addOutput(IpcDataUnitWrapper
                    .setIpcUint32Value(UncTCEnums.ServiceType.TC_OP_CONFIG_ACQUIRE_FORCE.ordinal()));
            isForce = true;
        } else if (requestBody != null && requestBody.has(VtnServiceJsonConsts.TIMEOUT)) {
            session.setTimeout(null);
            session.addOutput(IpcDataUnitWrapper
                    .setIpcUint32Value(UncTCEnums.ServiceType.TC_OP_CONFIG_ACQUIRE_TIMED.ordinal()));
        } else {
            session.addOutput(IpcDataUnitWrapper
                    .setIpcUint32Value(UncTCEnums.ServiceType.TC_OP_CONFIG_ACQUIRE.ordinal()));
        }
        session.addOutput(IpcDataUnitWrapper.setIpcUint32Value(getSessionID()));
        if (!isForce && requestBody != null && requestBody.has(VtnServiceJsonConsts.TIMEOUT)) {
            String str = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TIMEOUT).getAsString();
            int timeout = Integer.parseInt(str);
            session.addOutput(IpcDataUnitWrapper.setIpcInt32Value(timeout));
        }
        LOG.info("Request packet created successfully");
        status = session.invoke();
        LOG.info("Request packet processed with status:" + status);
        final String operationType = IpcDataUnitWrapper
                .getIpcDataUnitValue(session.getResponse(VtnServiceJsonConsts.VAL_0));
        final String sessionId = IpcDataUnitWrapper
                .getIpcDataUnitValue(session.getResponse(VtnServiceJsonConsts.VAL_1));
        final int operationStatus = Integer.parseInt(
                IpcDataUnitWrapper.getIpcDataUnitValue(session.getResponse(VtnServiceJsonConsts.VAL_2)));
        LOG.info("Response  received successfully");
        LOG.info("OperationType " + operationType);
        LOG.info("SessionId " + sessionId);
        LOG.info("OperationStatus " + operationStatus);
        if (operationStatus != UncTCEnums.OperationStatus.TC_OPER_SUCCESS.getCode()) {
            createTcErrorInfo(UncIpcErrorCode.getTcCodes(operationStatus));
            LOG.info("Request not processed successfully");
            status = UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue();
        } else {
            final String configId = IpcDataUnitWrapper
                    .getIpcDataUnitValue(session.getResponse(VtnServiceJsonConsts.VAL_3));
            LOG.info("ConfigId " + configId);
            final JsonObject response = new JsonObject();
            final JsonObject config = new JsonObject();
            config.addProperty(VtnServiceJsonConsts.CONFIGID, configId);
            response.add(VtnServiceJsonConsts.CONFIGMODE, config);
            setInfo(response);
            LOG.info("Request processed successfully");
            status = UncCommonEnum.UncResultCode.UNC_SUCCESS.getValue();
        }
        LOG.debug("Complete Ipc framework call");
    } catch (final VtnServiceException e) {
        LOG.info("Error occured while performing getSession operation");
        getExceptionHandler().raise(
                Thread.currentThread().getStackTrace()[1].getClassName() + VtnServiceConsts.HYPHEN
                        + Thread.currentThread().getStackTrace()[1].getMethodName(),
                UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
                UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage(), e);
        throw e;
    } catch (final IpcException e) {
        LOG.info("Error occured while performing addOutput operation");
        getExceptionHandler().raise(
                Thread.currentThread().getStackTrace()[1].getClassName() + VtnServiceConsts.HYPHEN
                        + Thread.currentThread().getStackTrace()[1].getMethodName(),
                UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
                UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage(), e);
    } finally {
        if (status == ClientSession.RESP_FATAL) {
            createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
            status = UncResultCode.UNC_SERVER_ERROR.getValue();
        }

        // destroy session by common handler
        getConnPool().destroySession(session);
    }
    LOG.trace("Completed AcquireConfigModeResource#post()");
    return status;
}