Java tutorial
/* * 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.VBypassResourceValidator; /** * The Class VBypassResource implements get, delete and put. */ @UNCVtnService(path = "/vtns/{vtn_name}/vbypasses/{vbypass_name}") public class VBypassResource extends AbstractResource { /** The vtn name. */ @UNCField("vtn_name") private String vtnName; /** * @return the vtn name */ public final String getVtnName() { return vtnName; } /** The vbypass name. */ @UNCField("vbypass_name") private String vbypassName; /** * @return the VBypass name */ public final String getVbypassName() { return vbypassName; } private static final Logger LOG = Logger.getLogger(VBypassResource.class.getName()); /** * Instantiates a new VBypass resource. */ public VBypassResource() { super(); LOG.trace("Start VBypassResource#VBypassResource()"); setValidator(new VBypassResourceValidator(this)); LOG.trace("Completed VBypassResource#VBypassResource()"); } /** * Implementation of put method of VBypass * * @param requestBody * the request Json object * * @return Error code * @throws VtnServiceException */ @Override public final int put(final JsonObject requestBody) throws VtnServiceException { LOG.trace("Starts VBypassResource#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 vBypass = requestBody.getAsJsonObject(VtnServiceJsonConsts.VBYPASS); if (vBypass.has(VtnServiceJsonConsts.DOMAINID)) { vBypass.remove(VtnServiceJsonConsts.DOMAINID); } requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VUNKNOWN_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 VBypassResource#put()"); return status; } /** * Implementation of delete method of VBypass * * @return Error code * @throws VtnServiceException */ @Override public final int delete() throws VtnServiceException { LOG.trace("Starts VBypassResource#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_VUNKNOWN_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 VBypassResource#delete()"); return status; } /** * Implementation of get method of VBypass * * @param requestBody * the request Json object * * @return Error code * @throws VtnServiceException */ @Override public final int get(final JsonObject requestBody) throws VtnServiceException { LOG.trace("Starts VBypassResource#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_VUNKNOWN_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.getVBypassResponse(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 VBypassResource#get()"); return status; } /** * Add URI parameters to list * * @return parameter list */ private List<String> getUriParameters() { LOG.trace("Start VBypassResource#getUriParameters()"); final List<String> uriParameters = new ArrayList<String>(); uriParameters.add(vtnName); uriParameters.add(vbypassName); LOG.trace("Completed VBypassResource#getUriParameters()"); return uriParameters; } }