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

Java tutorial

Introduction

Here is the source code for org.opendaylight.vtn.javaapi.resources.logical.VLinkResource.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.ipc.IpcDataUnit;
import org.opendaylight.vtn.core.ipc.IpcStruct;
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.UncStructEnum;
import org.opendaylight.vtn.javaapi.ipc.enums.UncUPLLEnums;
import org.opendaylight.vtn.javaapi.resources.AbstractResource;
import org.opendaylight.vtn.javaapi.validation.logical.VLinkResourceValidator;

/**
 * The Class VLinkResource.
 * 
 * @author NHST
 * @version 1.0
 */
/* This class handles delete, put and get methods */
@UNCVtnService(path = "/vtns/{vtn_name}/vlinks/{vlk_name}")
public class VLinkResource extends AbstractResource {
    /** The VTN name. */
    @UNCField("vtn_name")
    private String vtnName;

    /**
     * Gets the vtn name.
     * 
     * @return the vtn name
     */
    public final String getVtnName() {
        return vtnName;
    }

    /**
     * Gets the vlk name.
     * 
     * @return the vlk name
     */
    public final String getVlkName() {
        return vlkName;
    }

    /** The VLK name. */
    @UNCField("vlk_name")
    private String vlkName;
    private static final Logger LOG = Logger.getLogger(VLinkResource.class.getName());

    /**
     * Instantiates a new v link resource.
     */
    public VLinkResource() {
        LOG.trace("Start VLinkResource#VLinkResource()");
        setValidator(new VLinkResourceValidator(this));
        LOG.trace("Completed VLinkResource#VLinkResource()");
    }

    /**
     * Implementation of Put method of VLink Resource API
     * 
     * @param requestBody
     *            the request Json object
     * 
     * @return Error code
     * @throws VtnServiceException
     */
    @Override
    public final int put(final JsonObject requestBody) throws VtnServiceException {
        LOG.trace("Start VLinkResource#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 vLink = requestBody.getAsJsonObject(VtnServiceJsonConsts.VLINK);
            if (vLink.has(VtnServiceJsonConsts.VNODE1NAME)) {
                vLink.remove(VtnServiceJsonConsts.VNODE1NAME);
            }
            if (vLink.has(VtnServiceJsonConsts.IF1NAME)) {
                vLink.remove(VtnServiceJsonConsts.IF1NAME);
            }
            if (vLink.has(VtnServiceJsonConsts.VNODE2NAME)) {
                vLink.remove(VtnServiceJsonConsts.VNODE2NAME);
            }
            if (vLink.has(VtnServiceJsonConsts.IF2NAME)) {
                vLink.remove(VtnServiceJsonConsts.IF2NAME);
            }
            requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VLINK_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("Completed VLinkResource#put()");
        return status;
    }

    /**
     * Implementation of Delete method of VLink Resource API
     * 
     * @return Error code
     * @throws VtnServiceException
     */
    @Override
    public final int delete() throws VtnServiceException {
        LOG.trace("Start VLinkResource#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_VLINK_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("Completed VLinkResource#delete()");
        return status;
    }

    /**
     * Implementation of get method of VLink API
     * 
     * @param requestBody
     *            the request Json object
     * 
     * @return Error code
     * @throws VtnServiceException
     */
    @Override
    public final int get(final JsonObject requestBody) throws VtnServiceException {
        LOG.trace("Start VLinkResource#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_VLINK_GET, requestBody,
                    getUriParameters());
            final IpcStruct valStruct = new IpcStruct(UncStructEnum.ValVlink.getValue());
            requestProcessor.getRequestPacket().setValStruct(valStruct);
            if (requestBody.get(VtnServiceJsonConsts.TARGETDB).getAsString()
                    .equalsIgnoreCase(VtnServiceJsonConsts.STATE)) {
                LOG.debug("Add ValVlinkSt in Request Packet");
                final IpcDataUnit[] extraDataUnits = new IpcDataUnit[1];
                final IpcStruct valStructSt = new IpcStruct(UncStructEnum.ValVlinkSt.getValue());
                extraDataUnits[0] = valStructSt;
                requestProcessor.getRequestPacket().setExtraDataUnits(extraDataUnits);
            }
            LOG.debug("Request packet created successfully");
            status = requestProcessor.processIpcRequest();
            LOG.debug("Request packet processed with status" + status);
            final IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
            setInfo(responseGenerator.getVLinkResponse(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("Completed VLinkResource#get()");
        return status;
    }

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

}