2 * Copyright (c) 2012-2013 NEC Corporation
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this
7 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.vtn.javaapi.resources.logical;
12 import java.util.ArrayList;
13 import java.util.List;
15 import com.google.gson.JsonObject;
16 import org.opendaylight.vtn.core.ipc.ClientSession;
17 import org.opendaylight.vtn.core.util.Logger;
18 import org.opendaylight.vtn.javaapi.annotation.UNCField;
19 import org.opendaylight.vtn.javaapi.annotation.UNCVtnService;
20 import org.opendaylight.vtn.javaapi.constants.VtnServiceConsts;
21 import org.opendaylight.vtn.javaapi.constants.VtnServiceJsonConsts;
22 import org.opendaylight.vtn.javaapi.exception.VtnServiceException;
23 import org.opendaylight.vtn.javaapi.ipc.IpcRequestProcessor;
24 import org.opendaylight.vtn.javaapi.ipc.conversion.IpcLogicalResponseFactory;
25 import org.opendaylight.vtn.javaapi.ipc.enums.IpcRequestPacketEnum;
26 import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum;
27 import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum.UncResultCode;
28 import org.opendaylight.vtn.javaapi.ipc.enums.UncJavaAPIErrorCode;
29 import org.opendaylight.vtn.javaapi.ipc.enums.UncUPLLEnums;
30 import org.opendaylight.vtn.javaapi.resources.AbstractResource;
31 import org.opendaylight.vtn.javaapi.validation.logical.StaticIpRouteResourceValidator;
34 * The Class StaticIpRoutesResource implements post, get method.
36 @UNCVtnService(path = "/vtns/{vtn_name}/vrouters/{vrt_name}/static_iproutes")
37 public class StaticIpRoutesResource extends AbstractResource {
41 private String vtnName;
45 private String vrtName;
49 * @return the vtn name
51 public String getVtnName() {
56 * @return the vrt name
58 public String getVrtName() {
62 private static final Logger LOG = Logger
63 .getLogger(StaticIpRoutesResource.class.getName());
66 * Instantiates a new static ip routes resource.
68 public StaticIpRoutesResource() {
70 LOG.trace("Start StaticIpRoutesResource#StaticIpRoutesResource()");
71 setValidator(new StaticIpRouteResourceValidator(this));
72 LOG.trace("Complete StaticIpRoutesResource#StaticIpRoutesResource()");
76 * Implementation of post method of Static Ip Routes
79 * the request Json object
82 * @throws VtnServiceException
85 public int post(final JsonObject requestBody) throws VtnServiceException {
86 LOG.trace("Start StaticIpRoutesResource#post()");
87 ClientSession session = null;
88 IpcRequestProcessor requestProcessor = null;
89 int status = ClientSession.RESP_FATAL;
91 LOG.debug("Start Ipc framework call");
92 session = getConnPool().getSession(
93 UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
94 UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
95 UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
96 getExceptionHandler());
97 LOG.debug("Session created successfully");
98 requestProcessor = new IpcRequestProcessor(session, getSessionID(),
99 getConfigID(), getExceptionHandler());
100 if (requestBody != null
101 && requestBody.has(VtnServiceJsonConsts.STATIC_IPROUTE)
102 && !((JsonObject) requestBody
103 .get(VtnServiceJsonConsts.STATIC_IPROUTE))
104 .has(VtnServiceJsonConsts.NMG_NAME)) {
105 ((JsonObject) requestBody
106 .get(VtnServiceJsonConsts.STATIC_IPROUTE)).addProperty(
107 VtnServiceJsonConsts.NMG_NAME, "");
109 requestProcessor.createIpcRequestPacket(
110 IpcRequestPacketEnum.KT_VRT_IPROUTE_CREATE, requestBody,
111 getUriParameters(requestBody));
112 LOG.debug("Request Packet created successfully");
113 status = requestProcessor.processIpcRequest();
114 LOG.debug("Request packet processed with status" + status);
116 String ipAddr = null;
117 String nextHopAddr = null;
118 String netMask = null;
119 String nmgName = null;
120 String staticIpRouteId = null;
121 if (requestBody != null
122 && requestBody.has(VtnServiceJsonConsts.STATIC_IPROUTE)
123 && ((JsonObject) requestBody
124 .get(VtnServiceJsonConsts.STATIC_IPROUTE))
125 .has(VtnServiceJsonConsts.IPADDR)
126 && ((JsonObject) requestBody
127 .get(VtnServiceJsonConsts.STATIC_IPROUTE))
128 .has(VtnServiceJsonConsts.NETMASK)
129 && ((JsonObject) requestBody
130 .get(VtnServiceJsonConsts.STATIC_IPROUTE))
131 .has(VtnServiceJsonConsts.NMG_NAME)
132 && ((JsonObject) requestBody
133 .get(VtnServiceJsonConsts.STATIC_IPROUTE))
134 .has(VtnServiceJsonConsts.NEXTHOPADDR)) {
135 ipAddr = ((JsonObject) requestBody
136 .get(VtnServiceJsonConsts.STATIC_IPROUTE)).get(
137 VtnServiceJsonConsts.IPADDR).getAsString();
138 nextHopAddr = ((JsonObject) requestBody
139 .get(VtnServiceJsonConsts.STATIC_IPROUTE)).get(
140 VtnServiceJsonConsts.NEXTHOPADDR).getAsString();
141 netMask = ((JsonObject) requestBody
142 .get(VtnServiceJsonConsts.STATIC_IPROUTE)).get(
143 VtnServiceJsonConsts.NETMASK).getAsString();
144 nmgName = ((JsonObject) requestBody
145 .get(VtnServiceJsonConsts.STATIC_IPROUTE)).get(
146 VtnServiceJsonConsts.NMG_NAME).getAsString();
147 staticIpRouteId = ipAddr + VtnServiceJsonConsts.HYPHEN
148 + nextHopAddr + VtnServiceJsonConsts.HYPHEN + netMask
149 + VtnServiceJsonConsts.HYPHEN + nmgName;
151 JsonObject root = new JsonObject();
152 JsonObject staticIpRoute = new JsonObject();
153 staticIpRoute.addProperty(VtnServiceJsonConsts.STATICIPROUTEID,
155 root.add(VtnServiceJsonConsts.STATIC_IPROUTE, staticIpRoute);
157 LOG.debug("Response object created successfully");
158 LOG.debug("Complete Ipc framework call");
159 } catch (final VtnServiceException e) {
160 getExceptionHandler()
161 .raise(Thread.currentThread().getStackTrace()[1]
163 + VtnServiceConsts.HYPHEN
164 + Thread.currentThread().getStackTrace()[1]
166 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
167 UncJavaAPIErrorCode.IPC_SERVER_ERROR
168 .getErrorMessage(), e);
171 if (status == ClientSession.RESP_FATAL) {
172 if (null != requestProcessor.getErrorJson()) {
173 setInfo(requestProcessor.getErrorJson());
175 createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
178 status = UncResultCode.UNC_SERVER_ERROR.getValue();
180 getConnPool().destroySession(session);
182 LOG.trace("Complete StaticIpRoutesResource#post()");
187 * Implementation of get method of Static Ip Routes
190 * the request Json object
193 * @throws VtnServiceException
196 public int get(final JsonObject requestBody) throws VtnServiceException {
198 LOG.trace("Start StaticIpRoutesResource#get()");
199 ClientSession session = null;
200 IpcRequestProcessor requestProcessor = null;
201 int status = ClientSession.RESP_FATAL;
203 LOG.debug("Start Ipc framework call");
204 session = getConnPool().getSession(
205 UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
206 UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
207 UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),
208 getExceptionHandler());
209 LOG.debug("Session created successfully");
210 requestProcessor = new IpcRequestProcessor(session, getSessionID(),
211 getConfigID(), getExceptionHandler());
212 requestProcessor.createIpcRequestPacket(
213 IpcRequestPacketEnum.KT_VRT_IPROUTE_GET, requestBody,
214 getUriParameters(requestBody));
215 LOG.debug("Request Packet created successfully");
216 status = requestProcessor.processIpcRequest();
217 LOG.debug("Request packet processed with status" + status);
218 IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
219 setInfo(responseGenerator.getStaticIpRouteResponse(
220 requestProcessor.getIpcResponsePacket(), requestBody,
221 VtnServiceJsonConsts.LIST));
222 LOG.debug("Response object created successfully");
223 LOG.debug("Complete Ipc framework call");
224 } catch (final VtnServiceException e) {
225 getExceptionHandler()
226 .raise(Thread.currentThread().getStackTrace()[1]
228 + VtnServiceConsts.HYPHEN
229 + Thread.currentThread().getStackTrace()[1]
231 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
232 UncJavaAPIErrorCode.IPC_SERVER_ERROR
233 .getErrorMessage(), e);
236 if (status == ClientSession.RESP_FATAL) {
237 if (null != requestProcessor.getErrorJson()) {
238 setInfo(requestProcessor.getErrorJson());
240 createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
243 status = UncResultCode.UNC_SERVER_ERROR.getValue();
245 getConnPool().destroySession(session);
247 LOG.trace("Start StaticIpRoutesResource#get()");
252 * Add URI parameters to list
256 private List<String> getUriParameters(final JsonObject requestBody) {
257 LOG.trace("Start StaticIpRoutesResource#getUriParameters()");
258 final List<String> uriParameters = new ArrayList<String>();
259 uriParameters.add(vtnName);
260 uriParameters.add(vrtName);
261 if (requestBody != null && requestBody.has(VtnServiceJsonConsts.INDEX)) {
262 uriParameters.add(requestBody.get(VtnServiceJsonConsts.INDEX)
265 LOG.trace("Completed StaticIpRoutesResource#getUriParameters()");
266 return uriParameters;