org.opendaylight.vtn.javaapi.resources.logical.VRouterResource.java Source code

Java tutorial

Introduction

Here is the source code for org.opendaylight.vtn.javaapi.resources.logical.VRouterResource.java

Source

/*
 * Copyright (c) 2012-2014 NEC Corporation
 * All rights reserved.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.vtn.javaapi.resources.logical;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonObject;
import org.opendaylight.vtn.core.ipc.ClientSession;
import org.opendaylight.vtn.core.util.Logger;
import org.opendaylight.vtn.javaapi.annotation.UNCField;
import org.opendaylight.vtn.javaapi.annotation.UNCVtnService;
import org.opendaylight.vtn.javaapi.constants.VtnServiceConsts;
import org.opendaylight.vtn.javaapi.constants.VtnServiceJsonConsts;
import org.opendaylight.vtn.javaapi.exception.VtnServiceException;
import org.opendaylight.vtn.javaapi.ipc.IpcRequestProcessor;
import org.opendaylight.vtn.javaapi.ipc.conversion.IpcLogicalResponseFactory;
import org.opendaylight.vtn.javaapi.ipc.enums.IpcRequestPacketEnum;
import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum;
import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum.UncResultCode;
import org.opendaylight.vtn.javaapi.ipc.enums.UncJavaAPIErrorCode;
import org.opendaylight.vtn.javaapi.ipc.enums.UncUPLLEnums;
import org.opendaylight.vtn.javaapi.resources.AbstractResource;
import org.opendaylight.vtn.javaapi.validation.logical.VRouterResourceValidator;

/**
 * The Class VRouterResource implements delete, get and put methods.
 */
@UNCVtnService(path = "/vtns/{vtn_name}/vrouters/{vrt_name}")
public class VRouterResource extends AbstractResource {
    /** The vtn name. */
    @UNCField("vtn_name")
    private String vtnName;
    /** The vrt name. */
    @UNCField("vrt_name")
    private String vrtName;

    /**
     * @return the vtnName
     */
    public final String getVtnName() {
        return vtnName;
    }

    /**
     * @return the vrtName
     */
    public final String getVrtName() {
        return vrtName;
    }

    private static final Logger LOG = Logger.getLogger(VRouterResource.class.getName());

    /**
     * Instantiates a new VRouter resource.
     */
    public VRouterResource() {
        super();
        LOG.trace("Start VRouterResource#VRouterResource()");
        setValidator(new VRouterResourceValidator(this));
        LOG.trace("Complete VRouterResource#VRouterResource()");
    }

    /**
     * Implementation of delete method of VRouter
     * 
     * @return Error code
     * @throws VtnServiceException
     */
    @Override
    public final int delete() throws VtnServiceException {
        LOG.trace("Start VRouterResource#delete()");
        ClientSession session = null;
        IpcRequestProcessor requestProcessor = null;
        int status = ClientSession.RESP_FATAL;
        try {
            LOG.debug("Start Ipc framework call");
            session = getConnPool().getSession(UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
                    UncUPLLEnums.UPLL_IPC_SERVICE_NAME, UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
                    getExceptionHandler());
            LOG.debug("Session created successfully");
            requestProcessor = new IpcRequestProcessor(session, getSessionID(), getConfigID(),
                    getExceptionHandler());
            requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VROUTER_DELETE, getNullJsonObject(),
                    getUriParameters());
            LOG.debug("Request Packet created successfully");
            status = requestProcessor.processIpcRequest();
            LOG.debug("Request packet processed with status" + status);
            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;
        } finally {
            if (status == ClientSession.RESP_FATAL) {
                if (null != requestProcessor.getErrorJson()) {
                    setInfo(requestProcessor.getErrorJson());
                } else {
                    createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
                }
                status = UncResultCode.UNC_SERVER_ERROR.getValue();
            }
            getConnPool().destroySession(session);
        }
        LOG.trace("Complete VRouterResource#delete()");
        return status;
    }

    /**
     * Implementation of get method of VRouter
     * 
     * @param requestBody
     *            the request Json object
     * 
     * @return Error code
     * @throws VtnServiceException
     */
    @Override
    public final int get(final JsonObject requestBody) throws VtnServiceException {
        LOG.trace("Start VRouterResource#get()");
        ClientSession session = null;
        IpcRequestProcessor requestProcessor = null;
        int status = ClientSession.RESP_FATAL;
        try {
            LOG.debug("Start Ipc framework call");
            session = getConnPool().getSession(UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
                    UncUPLLEnums.UPLL_IPC_SERVICE_NAME, UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),
                    getExceptionHandler());
            LOG.debug("Session created successfully");
            requestProcessor = new IpcRequestProcessor(session, getSessionID(), getConfigID(),
                    getExceptionHandler());
            requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VROUTER_GET, requestBody,
                    getUriParameters());
            LOG.debug("Request Packet created successfully");
            status = requestProcessor.processIpcRequest();
            LOG.debug("Request packet processed with status" + status);
            final IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
            setInfo(responseGenerator.getVRouterResponse(requestProcessor.getIpcResponsePacket(), requestBody,
                    VtnServiceJsonConsts.SHOW));
            LOG.debug("Response object created successfully");
            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;
        } finally {
            if (status == ClientSession.RESP_FATAL) {
                if (null != requestProcessor.getErrorJson()) {
                    setInfo(requestProcessor.getErrorJson());
                } else {
                    createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
                }
                status = UncResultCode.UNC_SERVER_ERROR.getValue();
            }
            getConnPool().destroySession(session);
        }
        LOG.trace("Complete VRoutersResource#get()");
        return status;
    }

    /**
     * Implementation of Put method of VRouter
     * 
     * @param requestBody
     *            the request Json object
     * 
     * @return Error code
     * @throws VtnServiceException
     */
    @Override
    public final int put(final JsonObject requestBody) throws VtnServiceException {
        LOG.trace("Start VRouterResource#put()");
        ClientSession session = null;
        IpcRequestProcessor requestProcessor = null;
        int status = ClientSession.RESP_FATAL;
        try {
            LOG.debug("Start Ipc framework call");
            session = getConnPool().getSession(UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
                    UncUPLLEnums.UPLL_IPC_SERVICE_NAME, UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
                    getExceptionHandler());
            LOG.debug("Session created successfully");
            requestProcessor = new IpcRequestProcessor(session, getSessionID(), getConfigID(),
                    getExceptionHandler());
            //removal of unused parameter in request body.
            JsonObject vRouter = requestBody.getAsJsonObject(VtnServiceJsonConsts.VROUTER);
            if (vRouter.has(VtnServiceJsonConsts.CONTROLLERID)) {
                vRouter.remove(VtnServiceJsonConsts.CONTROLLERID);
            }
            if (vRouter.has(VtnServiceJsonConsts.DOMAINID)) {
                vRouter.remove(VtnServiceJsonConsts.DOMAINID);
            }
            requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VROUTER_UPDATE, requestBody,
                    getUriParameters());
            LOG.debug("Request Packet created successfully");
            status = requestProcessor.processIpcRequest();
            LOG.debug("Request packet processed with status" + status);
            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;
        } finally {
            if (status == ClientSession.RESP_FATAL) {
                if (null != requestProcessor.getErrorJson()) {
                    setInfo(requestProcessor.getErrorJson());
                } else {
                    createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
                }
                status = UncResultCode.UNC_SERVER_ERROR.getValue();
            }
            getConnPool().destroySession(session);
        }
        LOG.trace("Complete VRouterResource#put()");
        return status;
    }

    /**
     * Add URI parameters to list
     * 
     * @return
     */
    private List<String> getUriParameters() {
        LOG.trace("Start VRouterResource#getUriParameters()");
        final List<String> uriParameters = new ArrayList<String>();
        uriParameters.add(vtnName);
        uriParameters.add(vrtName);
        LOG.trace("Completed VRouterResource#getUriParameters()");
        return uriParameters;
    }
}