57cd9e12d073346b632462acdcd2645f8e540e6b
[vtn.git] /
1 /*
2  * Copyright (c) 2012-2013 NEC Corporation
3  * All rights reserved.
4  *
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
8  */
9 package org.opendaylight.vtn.javaapi.resources.logical;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import com.google.gson.JsonNull;
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.IpcDataUnitWrapper;
25 import org.opendaylight.vtn.javaapi.ipc.conversion.IpcLogicalResponseFactory;
26 import org.opendaylight.vtn.javaapi.ipc.enums.IpcRequestPacketEnum;
27 import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum;
28 import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum.UncResultCode;
29 import org.opendaylight.vtn.javaapi.ipc.enums.UncJavaAPIErrorCode;
30 import org.opendaylight.vtn.javaapi.ipc.enums.UncOption2Enum;
31 import org.opendaylight.vtn.javaapi.ipc.enums.UncUPLLEnums;
32 import org.opendaylight.vtn.javaapi.resources.AbstractResource;
33 import org.opendaylight.vtn.javaapi.validation.logical.InterfaceResourceValidator;
34
35 /**
36  * The Class VBypassInterfaceResource.
37  */
38 /* This class handles put, delete and get methods */
39 @UNCVtnService(path = "/vtns/{vtn_name}/vbypasses/{vbypass_name}/interfaces/{if_name}")
40 public class VBypassInterfaceResource extends AbstractResource {
41         /** The vtn name. */
42         @UNCField("vtn_name")
43         private String vtnName;
44         /** The vbypass name. */
45         @UNCField("vbypass_name")
46         private String vbypassName;
47         /** The interface name. */
48         @UNCField("if_name")
49         private String ifName;
50         /**
51          * Gets the vtn name.
52          * 
53          * @return the vtn name
54          */
55         public String getVtnName() {
56                 return vtnName;
57         }
58         /**
59          * Gets the vbypass name.
60          * 
61          * @return the vbypass name
62          */
63         public String getVbypassName() {
64                 return vbypassName;
65         }
66         /**
67          * Gets the if name.
68          * 
69          * @return the if name
70          */
71         public String getIfName() {
72                 return ifName;
73         }
74         private static final Logger LOG = Logger
75                         .getLogger(VBypassInterfaceResource.class.getName());
76         /**
77          * Instantiates a new VBypass interface resource.
78          */
79         public VBypassInterfaceResource() {
80                 super();
81                 LOG.trace("Start VBypassInterfaceResource#VBypassInterfacesResource()");
82                 setValidator(new InterfaceResourceValidator(this));
83                 LOG.trace("Completed VBypassInterfaceResource#VBypassInterfacesResource()");
84         }
85         /**
86          * Implementation of Put method of VBypass Interface API
87          * 
88          * @param requestBody
89          *            the request Json object
90          * 
91          * @return Error code
92          * @throws VtnServiceException
93          */
94         @Override
95         public int put(final JsonObject requestBody) throws VtnServiceException {
96                 LOG.trace("Starts VBypassInterfaceResource#put()");
97                 ClientSession session = null;
98                 IpcRequestProcessor requestProcessor = null;
99                 int status = ClientSession.RESP_FATAL;
100                 try {
101                         LOG.debug("Start Ipc framework call");
102                         session = getConnPool().getSession(
103                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
104                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
105                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
106                                         getExceptionHandler());
107                         LOG.debug("Session created successfully");
108                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
109                                         getConfigID(), getExceptionHandler());
110                         requestProcessor.createIpcRequestPacket(
111                                         IpcRequestPacketEnum.KT_VUNK_IF_UPDATE, requestBody,
112                                         getUriParameters());
113                         status = requestProcessor.processIpcRequest();
114                         LOG.debug("Request packet processed with status" + status);
115                         LOG.debug("Complete Ipc framework call");
116                 } catch (final VtnServiceException e) {
117                         getExceptionHandler()
118                         .raise(Thread.currentThread().getStackTrace()[1]
119                                         .getClassName()
120                                         + VtnServiceConsts.HYPHEN
121                                         + Thread.currentThread().getStackTrace()[1]
122                                                         .getMethodName(),
123                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
124                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
125                                                         .getErrorMessage(), e);
126                         throw e;
127                 } finally {
128                         if (status == ClientSession.RESP_FATAL) {
129                                 if (null != requestProcessor.getErrorJson()) {
130                                         setInfo(requestProcessor.getErrorJson());
131                                 } else {
132                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
133                                                         .getValue());
134                                 }
135                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
136                         }
137                         getConnPool().destroySession(session);
138                 }
139                 LOG.trace("Completed VBypassInterfaceResource#put()");
140                 return status;
141         }
142         /**
143          * Implementation of Delete method of VBypass Interface API
144          * 
145          * @return Error code
146          * @throws VtnServiceException
147          */
148         @Override
149         public int delete() throws VtnServiceException {
150                 LOG.trace("starts VBypassInterfaceResource#delete()");
151                 ClientSession session = null;
152                 IpcRequestProcessor requestProcessor = null;
153                 int status = ClientSession.RESP_FATAL;
154                 try {
155                         LOG.debug("Start Ipc framework call");
156                         session = getConnPool().getSession(
157                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
158                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
159                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
160                                         getExceptionHandler());
161                         LOG.debug("Session created successfully");
162                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
163                                         getConfigID(), getExceptionHandler());
164                         requestProcessor.createIpcRequestPacket(
165                                         IpcRequestPacketEnum.KT_VUNK_IF_DELETE,
166                                         getNullJsonObject(), getUriParameters());
167                         status = requestProcessor.processIpcRequest();
168                         LOG.debug("Request packet processed with status" + status);
169                         LOG.debug("Complete Ipc framework call");
170                 } catch (final VtnServiceException e) {
171                         getExceptionHandler()
172                         .raise(Thread.currentThread().getStackTrace()[1]
173                                         .getClassName()
174                                         + VtnServiceConsts.HYPHEN
175                                         + Thread.currentThread().getStackTrace()[1]
176                                                         .getMethodName(),
177                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
178                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
179                                                         .getErrorMessage(), e);
180                         throw e;
181                 } finally {
182                         if (status == ClientSession.RESP_FATAL) {
183                                 if (null != requestProcessor.getErrorJson()) {
184                                         setInfo(requestProcessor.getErrorJson());
185                                 } else {
186                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
187                                                         .getValue());
188                                 }
189                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
190                         }
191                         getConnPool().destroySession(session);
192                 }
193                 LOG.trace("Completed VBypassInterfaceResource#delete()");
194                 return status;
195         }
196         /**
197          * Implementation of get method of VBypass Interface API
198          * 
199          * @param requestBody
200          *            the request Json object
201          * 
202          * @return Error code
203          * @throws VtnServiceException
204          */
205         @Override
206         public int get(final JsonObject requestBody) throws VtnServiceException {
207                 LOG.trace("Starts VBypassInterfaceResource#get()");
208                 ClientSession session = null;
209                 IpcRequestProcessor requestProcessor = null;
210                 int status = ClientSession.RESP_FATAL;
211                 try {
212                         LOG.debug("Start Ipc framework call");
213                         session = getConnPool().getSession(
214                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
215                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
216                                         UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),
217                                         getExceptionHandler());
218                         LOG.debug("Session created successfully");
219                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
220                                         getConfigID(), getExceptionHandler());
221                         requestProcessor.createIpcRequestPacket(
222                                         IpcRequestPacketEnum.KT_VUNK_IF_GET, requestBody,
223                                         getUriParameters());
224                         LOG.debug("Request packet created successfully for 1st request");
225                         status = requestProcessor.processIpcRequest();
226                         LOG.debug("Request packet processed for 1st call with status" + status);
227                         if (status == ClientSession.RESP_FATAL) {
228                                 throw new VtnServiceException(
229                                                 Thread.currentThread().getStackTrace()[1]
230                                                                 .getClassName()
231                                                                 + VtnServiceConsts.HYPHEN
232                                                                 + Thread.currentThread().getStackTrace()[1]
233                                                                                 .getMethodName(),
234                                                                                 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
235                                                                                 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage());
236                         }
237                         JsonObject neighbor = null;
238                         JsonObject vbypassInterfaceJson = null;
239                         IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
240                         String dataType = null;
241                         if (requestBody.has(VtnServiceJsonConsts.TARGETDB)) {
242                                 dataType = requestBody.get(VtnServiceJsonConsts.TARGETDB)
243                                                 .getAsString();
244                         }
245                         vbypassInterfaceJson = responseGenerator.getVBypassInterfaceResponse(
246                                         requestProcessor.getIpcResponsePacket(), requestBody,
247                                         VtnServiceJsonConsts.SHOW);
248                         if (VtnServiceJsonConsts.STATE.equalsIgnoreCase(dataType) && !(vbypassInterfaceJson.get(VtnServiceJsonConsts.INTERFACE) instanceof JsonNull)) {
249                                 requestProcessor.setServiceInfo(
250                                                 UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
251                                                 UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal());
252                                 requestProcessor.createIpcRequestPacket(
253                                                 IpcRequestPacketEnum.KT_VUNK_IF_GET, requestBody,
254                                                 getUriParameters());
255                                 requestProcessor
256                                 .getRequestPacket()
257                                 .setOption2(
258                                                 IpcDataUnitWrapper
259                                                 .setIpcUint32Value(UncOption2Enum.UNC_OPT2_NEIGHBOR
260                                                                 .ordinal()));
261                                 LOG.debug("Request packet created successfully for 2nd request");
262                                 status = requestProcessor.processIpcRequest();
263                                 LOG.debug("Request packet for 2nd request processed with status" + status);
264                                 neighbor = responseGenerator.getNeighborResponse(
265                                                 requestProcessor.getIpcResponsePacket(), requestBody,
266                                                 VtnServiceJsonConsts.SHOW);
267                                 vbypassInterfaceJson.get(VtnServiceJsonConsts.INTERFACE)
268                                 .getAsJsonObject()
269                                 .add(VtnServiceJsonConsts.NEIGHBOR, neighbor);
270                         }
271                         setInfo(vbypassInterfaceJson);
272                         LOG.debug("Response object created successfully");
273                         LOG.debug("Complete Ipc framework call");
274                 } catch (final VtnServiceException e) {
275                         getExceptionHandler()
276                         .raise(Thread.currentThread().getStackTrace()[1]
277                                         .getClassName()
278                                         + VtnServiceConsts.HYPHEN
279                                         + Thread.currentThread().getStackTrace()[1]
280                                                         .getMethodName(),
281                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
282                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
283                                                         .getErrorMessage(), e);
284                         throw e;
285                 } finally {
286                         if (status == ClientSession.RESP_FATAL) {
287                                 if (null != requestProcessor.getErrorJson()) {
288                                         setInfo(requestProcessor.getErrorJson());
289                                 } else {
290                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
291                                                         .getValue());
292                                 }
293                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
294                         }
295                         getConnPool().destroySession(session);
296                 }
297                 LOG.trace("Completed VBypassInterfaceResource#get()");
298                 return status;
299         }
300         /**
301          * Add URI parameters to list
302          * 
303          * @return parameter list
304          */
305         private List<String> getUriParameters() {
306                 LOG.trace("Start VBypassInterfaceResource#getUriParameters()");
307                 List<String> uriParameters = new ArrayList<String>();
308                 uriParameters.add(vtnName);
309                 uriParameters.add(vbypassName);
310                 uriParameters.add(ifName);
311                 LOG.trace("Completed VBypassInterfaceResource#getUriParameters()");
312                 return uriParameters;
313         }
314 }