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.resources.logical.VLinksResource.java

License:Open Source License

private final JsonObject getResponseJsonArrayLink(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();/*from w  w  w .  j  av  a  2s . co m*/
    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.
        long noSuchInstanceFlag = 0;
        noSuchInstanceFlag = memberIndex;
        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()));
            }
            getModifiedRequestPacket(requestBody, requestProcessor);
            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) {
                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());
            }
            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.physical.AlarmResource.java

License:Open Source License

/**
 * Implementation of put method of AlarmResource API
 * /*from  ww  w.  ja  v a2s. c  o m*/
 * @param requestBody
 *            the request Json object
 * 
 * @return Error code
 * @throws VtnServiceException
 */
@Override
public final int put(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("Start Alarm#put()");
    ClientSession session = null;
    int status = ClientSession.RESP_FATAL;
    try {
        LOG.debug("Start Ipc framework call");
        session = getConnPool().getSession(UncSYSMGEnums.SYSMG_IPC_CHANNEL_NAME,
                UncSYSMGEnums.NOMG_IPC_SERVICE_NAME, UncSYSMGEnums.NodeMgrServiceID.kAlarmClear.ordinal(),
                getExceptionHandler());
        LOG.info("Session created successfully");
        session.addOutput(IpcDataUnitWrapper
                .setIpcUint64Value(requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.ALARMNO).getAsString()));
        LOG.info("Request packet created successfully");
        status = session.invoke();
        LOG.info("Request packet processed with status:" + status);
        if (status != UncSYSMGEnums.NodeIpcErrorT.NOMG_E_OK.ordinal()) {
            LOG.info("Error occurred while performing operation");
            createNoMgErrorInfo(UncIpcErrorCode.getNodeCodes(status));
            status = UncResultCode.UNC_SERVER_ERROR.getValue();
        } else {
            LOG.info("Opeartion successfully performed");
            status = UncResultCode.UNC_SUCCESS.getValue();
        }
        LOG.debug("Complete Ipc framework call");
    } catch (final IpcException e) {
        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 Alarm#put()");
    return status;
}

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

License:Open Source License

/**
 * Implementation of post method of session API
 * //from   www.ja  v a2 s .  c  om
 * @param requestBody
 *            the request JsonObject
 * 
 * @return Error code
 * @throws VtnServiceException
 *             the vtn service exception
 */
@Override
public final int post(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("Starts SessionsResource#post()");
    ClientSession session = null;
    ClientSession sessionEnable = null;
    int status = ClientSession.RESP_FATAL;
    try {
        JsonObject sessionJson = null;
        // unauthorized user check
        if (requestBody != null && requestBody.has(VtnServiceJsonConsts.SESSION)) {
            sessionJson = requestBody.getAsJsonObject(VtnServiceJsonConsts.SESSION);
            if (sessionJson.has(VtnServiceJsonConsts.USERNAME)) {
                String username = sessionJson.get(VtnServiceJsonConsts.USERNAME).getAsString();
                if (!(username.equalsIgnoreCase(VtnServiceJsonConsts.ADMIN)
                        || username.equalsIgnoreCase(VtnServiceJsonConsts.OPER))) {
                    createErrorInfo(UncCommonEnum.UncResultCode.UNC_UNAUTHORIZED.getValue());
                    status = UncResultCode.UNC_CLIENT_ERROR.getValue();
                    return status;
                }
            }
        }
        LOG.debug("Start Ipc framework call");
        session = getConnPool().getSession(UncSessionEnums.UNCD_IPC_CHANNEL, UncSessionEnums.UNCD_IPC_SERVICE,
                UncSessionEnums.ServiceID.kUsessSessAdd.ordinal(), getExceptionHandler());
        LOG.info("Session created successfully");
        // set IPC timeout
        session.setTimeout(null);
        // create request packet for IPC call based on API key and
        // JsonObject
        final IpcStruct usessIpcReqSessAdd = new IpcStruct(UncStructEnum.UsessIpcReqSessAdd.getValue());
        if (requestBody != null && requestBody.has(VtnServiceJsonConsts.SESSION)) {
            // add user name to usess_ipc_req_sess_add structure
            if (sessionJson.has(VtnServiceJsonConsts.USERNAME)
                    && sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.USERNAME).getAsString()
                            .equalsIgnoreCase(VtnServiceJsonConsts.ADMIN)) {
                usessIpcReqSessAdd.set(VtnServiceIpcConsts.SESS_UNAME,
                        IpcDataUnitWrapper.setIpcUint8ArrayValue(VtnServiceIpcConsts.USESS_USER_WEB_ADMIN));
                LOG.debug("Login from admin user"
                        + sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.USERNAME).getAsString());
            } else {
                usessIpcReqSessAdd.set(VtnServiceIpcConsts.SESS_UNAME,
                        IpcDataUnitWrapper.setIpcUint8ArrayValue(VtnServiceIpcConsts.USESS_USER_WEB_OPER));
                LOG.debug("Login from oper user"
                        + sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.USERNAME).getAsString());
            }
            LOG.debug("sess_uname: " + IpcDataUnitWrapper.getIpcStructUint8ArrayValue(usessIpcReqSessAdd,
                    VtnServiceIpcConsts.SESS_UNAME));
            // add password to usess_ipc_req_sess_add structure
            usessIpcReqSessAdd.set(VtnServiceIpcConsts.SESS_PASSWD, IpcDataUnitWrapper.setIpcUint8ArrayValue(
                    sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.PASSWORD).getAsString()));

            // add login name to usess_ipc_req_sess_add structure
            if (sessionJson.has(VtnServiceJsonConsts.LOGINNAME)
                    && sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.LOGINNAME).getAsString() != null) {
                usessIpcReqSessAdd.set(VtnServiceIpcConsts.LOGIN_NAME, IpcDataUnitWrapper.setIpcUint8ArrayValue(
                        sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.LOGINNAME).getAsString()));
            } else {
                usessIpcReqSessAdd.set(VtnServiceIpcConsts.LOGIN_NAME,
                        IpcDataUnitWrapper.setIpcUint8ArrayValue(VtnServiceConsts.EMPTY_STRING));
            }
            // add type to usess_ipc_req_sess_add structure
            if (sessionJson.has(VtnServiceJsonConsts.TYPE)
                    && sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.TYPE).getAsString() != null
                    && sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.TYPE).getAsString()
                            .equalsIgnoreCase(VtnServiceJsonConsts.WEBAPI)) {
                usessIpcReqSessAdd.set(VtnServiceIpcConsts.SESS_TYPE, IpcDataUnitWrapper
                        .setIpcInt32Value(UncSessionEnums.UsessTypeE.USESS_TYPE_WEB_API.ordinal()));
            } else {
                usessIpcReqSessAdd.set(VtnServiceIpcConsts.SESS_TYPE, IpcDataUnitWrapper
                        .setIpcInt32Value(UncSessionEnums.UsessTypeE.USESS_TYPE_WEB_UI.ordinal()));
            }
            LOG.debug("sess_type: " + IpcDataUnitWrapper.getIpcStructUint32Value(usessIpcReqSessAdd,
                    VtnServiceIpcConsts.SESS_TYPE));

            // add ip address to usess_ipc_req_sess_add structure
            usessIpcReqSessAdd.set(VtnServiceJsonConsts.IPADDR, IpcDataUnitWrapper.setIpcInet4AddressValue(
                    sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.IPADDR).getAsString()));

            // add info to usess_ipc_req_sess_add structure
            if (sessionJson.has(VtnServiceJsonConsts.INFO)
                    && sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.INFO).getAsString() != null) {
                usessIpcReqSessAdd.set(VtnServiceJsonConsts.INFO, IpcDataUnitWrapper.setIpcUint8ArrayValue(
                        sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.INFO).getAsString()));
            } else {
                usessIpcReqSessAdd.set(VtnServiceJsonConsts.INFO,
                        IpcDataUnitWrapper.setIpcUint8ArrayValue(VtnServiceConsts.EMPTY_STRING));
            }
            session.addOutput(usessIpcReqSessAdd);
            LOG.info("Request packet created successfully");
            status = session.invoke();
            LOG.info("Request packet processed with status:" + status);
            if (status != UncSessionEnums.UsessIpcErrE.USESS_E_OK.ordinal()) {
                LOG.info("Error occurred while performing operation");
                createSessionErrorInfo(UncIpcErrorCode.getSessionCodes(status));
                status = UncResultCode.UNC_SERVER_ERROR.getValue();
            } else {
                LOG.info("Opeartion successfully performed");
                final JsonObject response = new JsonObject();
                final IpcStruct responseStruct = (IpcStruct) session.getResponse(0);
                final JsonObject sessionInfo = new JsonObject();
                final String sessionId = IpcDataUnitWrapper
                        .getIpcStructUint32Value(responseStruct, VtnServiceIpcConsts.ID).toString();
                sessionInfo.addProperty(VtnServiceJsonConsts.SESSIONID, sessionId);
                response.add(VtnServiceJsonConsts.SESSION, sessionInfo);
                setInfo(response);

                if (sessionJson.has(VtnServiceJsonConsts.USERNAME)
                        && sessionJson.getAsJsonPrimitive(VtnServiceJsonConsts.USERNAME).getAsString()
                                .equalsIgnoreCase(VtnServiceJsonConsts.ADMIN)) {
                    sessionEnable = getConnPool().getSession(UncSessionEnums.UNCD_IPC_CHANNEL,
                            UncSessionEnums.UNCD_IPC_SERVICE, UncSessionEnums.ServiceID.kUsessEnable.ordinal(),
                            getExceptionHandler());
                    LOG.info("Session created successfully");
                    //set IPC timeout
                    sessionEnable.setTimeout(null);
                    // create request packet for IPC call based on API key
                    // and JsonObject
                    final IpcStruct usessIpcReqSessEnable = new IpcStruct(
                            UncStructEnum.UsessIpcReqSessEnable.getValue());
                    // create request IPC Structure for current session from
                    // which user logged in
                    final IpcStruct usessIpcReqSessIdCurrent = new IpcStruct(
                            UncStructEnum.UsessIpcSessId.getValue());
                    usessIpcReqSessIdCurrent.set(VtnServiceIpcConsts.ID,
                            IpcDataUnitWrapper.setIpcUint32Value(sessionId));
                    usessIpcReqSessEnable.set(VtnServiceIpcConsts.CURRENT, usessIpcReqSessIdCurrent);
                    usessIpcReqSessEnable.set(VtnServiceIpcConsts.ENABLE_PASSWORD,
                            IpcDataUnitWrapper.setIpcUint8ArrayValue(sessionJson
                                    .getAsJsonPrimitive(VtnServiceJsonConsts.PASSWORD).getAsString()));
                    sessionEnable.addOutput(usessIpcReqSessEnable);
                    LOG.info("Request packet created successfully");
                    status = sessionEnable.invoke();
                    LOG.info("Request packet processed with status:" + status);
                    if (status != UncSessionEnums.UsessIpcErrE.USESS_E_OK.ordinal()) {
                        LOG.info("Error occurred while performing operation");
                        createSessionErrorInfo(UncIpcErrorCode.getSessionCodes(status));
                        status = UncResultCode.UNC_SERVER_ERROR.getValue();
                    } else {
                        status = UncResultCode.UNC_SUCCESS.getValue();
                    }
                } else {
                    status = UncResultCode.UNC_SUCCESS.getValue();
                }
            }
        }
        LOG.debug("Complete Ipc framework call");
    } catch (final VtnServiceException e) {
        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);
        if (sessionEnable != null) {
            getConnPool().destroySession(sessionEnable);
        }
    }
    LOG.trace("Completed SessionsResource#post()");
    return status;
}

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

License:Open Source License

/**
 * Implementation of Get method of session API
 * //from   ww  w. j a v a 2  s.  c om
 * @param requestBody
 *            the request JsonObject
 * 
 * @return Error code
 * @throws VtnServiceException
 *             the vtn service exception
 */
@Override
public final int get(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("starts SessionsResource#get()");
    ClientSession session = null;
    int status = ClientSession.RESP_FATAL;
    try {
        LOG.debug("Start Ipc framework call");
        if (requestBody != null && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString()
                .equalsIgnoreCase(VtnServiceJsonConsts.COUNT)) {
            session = getConnPool().getSession(UncSessionEnums.UNCD_IPC_CHANNEL,
                    UncSessionEnums.UNCD_IPC_SERVICE, UncSessionEnums.ServiceID.kUsessSessCount.ordinal(),
                    getExceptionHandler());
        } else {
            session = getConnPool().getSession(UncSessionEnums.UNCD_IPC_CHANNEL,
                    UncSessionEnums.UNCD_IPC_SERVICE, UncSessionEnums.ServiceID.kUsessSessList.ordinal(),
                    getExceptionHandler());
        }
        LOG.info("Session created successfully");
        // create request packet for IPC call based on API key and
        // JsonObject
        final IpcStruct usessIpcReqSessId = new IpcStruct(UncStructEnum.UsessIpcSessId.getValue());
        usessIpcReqSessId.set(VtnServiceJsonConsts.ID,
                IpcDataUnitWrapper.setIpcUint32Value(String.valueOf(getSessionID())));
        session.addOutput(usessIpcReqSessId);
        LOG.info("Request packet created successfully");
        status = session.invoke();
        LOG.info("Request packet processed with status:" + status);
        if (status != UncSessionEnums.UsessIpcErrE.USESS_E_OK.ordinal()) {
            LOG.info("Error occurred while performing operation");
            createSessionErrorInfo(UncIpcErrorCode.getSessionCodes(status));
            status = UncResultCode.UNC_SERVER_ERROR.getValue();
        } else {
            LOG.info("Opeartion successfully performed");
            final JsonObject response = new JsonObject();

            String opType = VtnServiceJsonConsts.NORMAL;
            if (requestBody.has(VtnServiceJsonConsts.OP)) {
                opType = requestBody.get(VtnServiceJsonConsts.OP).getAsString();
            }
            if (opType.equalsIgnoreCase(VtnServiceJsonConsts.COUNT)) {
                /*
                 * Create Json for Count
                 */
                JsonObject sessJson = null;
                sessJson = new JsonObject();
                sessJson.addProperty(VtnServiceJsonConsts.COUNT,
                        IpcDataUnitWrapper.getIpcDataUnitValue(session.getResponse(0)));
                response.add(VtnServiceJsonConsts.SESSIONS, sessJson);
            } else {
                createGetResponse(session, response, opType);
            }
            setInfo(response);
            status = UncResultCode.UNC_SUCCESS.getValue();
        }
        LOG.debug("Complete Ipc framework call");
    } catch (final VtnServiceException e) {
        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 SessionsResource#delete()");
    return status;
}

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

License:Open Source License

/**
 * Implementation of put method of password API
 * /*  w ww .  j  ava 2 s  . c o m*/
 * @param requestBody
 *            the request JsonObject
 * 
 * @return Error code
 * @throws VtnServiceException
 *             the vtn service exception
 */
@Override
public final int put(final JsonObject requestBody) throws VtnServiceException {
    LOG.trace("Starts UserResource#put()");
    ClientSession session = null;
    int status = ClientSession.RESP_FATAL;
    try {
        LOG.debug("Start Ipc framework call");
        session = getConnPool().getSession(UncSessionEnums.UNCD_IPC_CHANNEL, UncSessionEnums.UNCD_IPC_SERVICE,
                UncSessionEnums.ServiceID.kUserUserPasswd.ordinal(), getExceptionHandler());
        LOG.info("Session created successfully");
        // create request packet for IPC call based on API key and
        // JsonObject
        final IpcStruct usessIpcReqUserPasswd = new IpcStruct(UncStructEnum.usessIpcReqUserPasswd.getValue());
        // create request IPC Structure for session from which user
        // logged in
        final IpcStruct usessIpcReqSessId = new IpcStruct(UncStructEnum.UsessIpcSessId.getValue());
        usessIpcReqSessId.set(VtnServiceIpcConsts.ID,
                IpcDataUnitWrapper.setIpcUint32Value(String.valueOf(getSessionID())));
        usessIpcReqUserPasswd.set(VtnServiceJsonConsts.CURRENT, usessIpcReqSessId);

        // set user name
        LOG.info("set user name for : " + getUserName());
        usessIpcReqUserPasswd.set(VtnServiceIpcConsts.SESS_UNAME,
                IpcDataUnitWrapper.setIpcUint8ArrayValue(getUNCUserName(getUserName())));

        // set password
        LOG.info("set password");
        usessIpcReqUserPasswd.set(VtnServiceIpcConsts.SESS_PASSWD, IpcDataUnitWrapper.setIpcUint8ArrayValue(
                requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.PASSWORD).getAsString()));

        session.addOutput(usessIpcReqUserPasswd);
        LOG.info("Request packet created successfully");
        status = session.invoke();
        LOG.info("Request packet processed with status:" + status);
        if (status != UncSessionEnums.UsessIpcErrE.USESS_E_OK.ordinal()) {
            LOG.info("Error occurred while performing operation");
            createSessionErrorInfo(UncIpcErrorCode.getSessionCodes(status));
            status = UncResultCode.UNC_SERVER_ERROR.getValue();
        } else {
            LOG.info("Opeartion successfully performed");
            status = 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 invoke 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 UserResource#put()");
    return status;
}

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

License:Open Source License

/**
 * Validate put request json for Abort Candidate Configuration API
 * //w  ww  .j a  v  a  2  s.  c om
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePut(final JsonObject requestBody) {
    LOG.trace("Start AbortCandidateConfigResourceValidator#validatePut()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.CANDIDATE);
    if (requestBody.has(VtnServiceJsonConsts.CANDIDATE)
            && requestBody.get(VtnServiceJsonConsts.CANDIDATE).isJsonObject()) {
        final JsonObject candidate = requestBody.getAsJsonObject(VtnServiceJsonConsts.CANDIDATE);
        setInvalidParameter(VtnServiceJsonConsts.OPERATION);
        if (candidate.has(VtnServiceJsonConsts.OPERATION)
                && candidate.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION).getAsString() != null) {
            final String operation = candidate.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION).getAsString();
            isValid = operation.equalsIgnoreCase(VtnServiceJsonConsts.ABORT);
        }
    }
    LOG.trace("Complete AbortCandidateConfigResourceValidator#validatePut()");
    return isValid;
}

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

License:Open Source License

/**
 * Validate request json for put method of Audit controller API
 * //from  w  w  w.  j a v  a2  s .  co  m
 * @param requestBody
 *            the request Json object
 * 
 * @return true, if successful
 */
private boolean validatePut(final JsonObject requestBody) {
    LOG.trace("Start AuditControllerResourceValidator#validatePut()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.AUDIT);
    if (requestBody.has(VtnServiceJsonConsts.AUDIT)
            && requestBody.get(VtnServiceJsonConsts.AUDIT).isJsonObject()) {
        isValid = true;
        final JsonObject audit = requestBody.getAsJsonObject(VtnServiceJsonConsts.AUDIT);
        setInvalidParameter(VtnServiceJsonConsts.FORCE);
        if (audit.has(VtnServiceJsonConsts.FORCE)
                && audit.getAsJsonPrimitive(VtnServiceJsonConsts.FORCE).getAsString() != null) {
            String force = audit.getAsJsonPrimitive(VtnServiceJsonConsts.FORCE).getAsString();
            isValid = force.equalsIgnoreCase(VtnServiceJsonConsts.TRUE)
                    || force.equalsIgnoreCase(VtnServiceJsonConsts.FALSE);
        }
    }
    LOG.trace("Complete AuditControllerResourceValidator#validatePut()");
    return isValid;
}

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

License:Open Source License

/**
 * Validate put request json for Enable/Disable Auto-save Status API.
 * /*from ww w  .  j a va 2 s . com*/
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePut(final JsonObject requestBody) {
    LOG.trace("Start AutoSaveResourceValidator#validatePut()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.AUTOSAVE);
    if (requestBody.has(VtnServiceJsonConsts.AUTOSAVE)
            && requestBody.get(VtnServiceJsonConsts.AUTOSAVE).isJsonObject()) {
        final JsonObject autoSave = requestBody.getAsJsonObject(VtnServiceJsonConsts.AUTOSAVE);
        setInvalidParameter(VtnServiceJsonConsts.AUTOSAVESTATUS);
        if (autoSave.has(VtnServiceJsonConsts.AUTOSAVESTATUS)
                && autoSave.getAsJsonPrimitive(VtnServiceJsonConsts.AUTOSAVESTATUS).getAsString() != null) {
            // checking whether auto_save_status is enable or disable
            final String status = autoSave.getAsJsonPrimitive(VtnServiceJsonConsts.AUTOSAVESTATUS)
                    .getAsString();
            isValid = status.equalsIgnoreCase(VtnServiceJsonConsts.ENABLE)
                    || status.equalsIgnoreCase(VtnServiceJsonConsts.DISABLE);
        }
    }
    LOG.trace("Complete AutoSaveResourceValidator#validatePut()");
    return isValid;
}

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

License:Open Source License

/**
 * Validate put request json for Clear Startup Configuration API.
 * //from   w w w. j  a  v a 2  s. com
 * @param requestBody
 *            the request Json object
 * @return true, if successful
 */
private boolean validatePut(final JsonObject requestBody) {
    LOG.trace("Start ClearStartupConfigurationResourceValidator#validatePut()");
    boolean isValid = false;
    setInvalidParameter(VtnServiceJsonConsts.STARTUP);
    if (requestBody.has(VtnServiceJsonConsts.STARTUP)
            && requestBody.get(VtnServiceJsonConsts.STARTUP).isJsonObject()) {
        final JsonObject startup = requestBody.getAsJsonObject(VtnServiceJsonConsts.STARTUP);
        setInvalidParameter(VtnServiceJsonConsts.OPERATION);
        if (startup.has(VtnServiceJsonConsts.OPERATION)
                && startup.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION).getAsString() != null
                && !startup.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION).getAsString().isEmpty()) {
            isValid = startup.getAsJsonPrimitive(VtnServiceJsonConsts.OPERATION).getAsString()
                    .equalsIgnoreCase(VtnServiceJsonConsts.CLEAR);
        }
    }
    LOG.trace("Complete ClearStartupConfigurationResourceValidator#validatePut()");
    return isValid;
}

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

License:Open Source License

/**
 * Checks if is valid get request.//from w w  w  . ja  va  2s  . c om
 * 
 * @param requestBody
 *            the request JSON object
 * @return true, if is valid get
 */
public final boolean isValidGet(final JsonObject requestBody, final boolean opFlag) {
    LOG.trace("Start CommonValidator#isValidGet");
    boolean isValid = true;

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

    /*
     * Remove unwanted parameters from request body for Show APIs
     */
    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
        if (isValid) {
            invalidParameter = VtnServiceJsonConsts.OP;
            isValid = isValidOperation(requestBody);
        }
        // validation for key: index
        if (isValid) {
            invalidParameter = VtnServiceJsonConsts.INDEX;
            if (requestBody.has(VtnServiceJsonConsts.INDEX)
                    && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString() != null
                    && !requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString().isEmpty()) {
                isValid = isValidMaxLengthAlphaNum(
                        requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString(),
                        VtnServiceJsonConsts.LEN_31);
            }
        }
        // validation for key: max_repitition
        if (isValid) {
            invalidParameter = VtnServiceJsonConsts.MAX;
            isValid = isValidMaxRepetition(requestBody);
        }
    }
    LOG.trace("Complete CommonValidator#isValidGet");
    return isValid;
}