2c7fb64fb059883252e31381a12f7bb9a95892ab
[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.JsonObject;
15 import org.opendaylight.vtn.core.ipc.ClientSession;
16 import org.opendaylight.vtn.core.util.Logger;
17 import org.opendaylight.vtn.javaapi.annotation.UNCField;
18 import org.opendaylight.vtn.javaapi.annotation.UNCVtnService;
19 import org.opendaylight.vtn.javaapi.constants.VtnServiceConsts;
20 import org.opendaylight.vtn.javaapi.constants.VtnServiceJsonConsts;
21 import org.opendaylight.vtn.javaapi.exception.VtnServiceException;
22 import org.opendaylight.vtn.javaapi.ipc.IpcRequestProcessor;
23 import org.opendaylight.vtn.javaapi.ipc.conversion.IpcDataUnitWrapper;
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.UncOperationEnum;
30 import org.opendaylight.vtn.javaapi.ipc.enums.UncOption1Enum;
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.VBridgeFlowFilterEntryResourceValidator;
34
35 /**
36  * The Class VBridgeInterfaceFlowFilterEntryResource implements delete, put and
37  * get methods.
38  * 
39  */
40 @UNCVtnService(path = "/vtns/{vtn_name}/vbridges/{vbr_name}/interfaces/{if_name}/flowfilters/{ff_type}/flowfilterentries/{seqnum}")
41 public class VBridgeInterfaceFlowFilterEntryResource extends AbstractResource {
42
43         /** The VTN name. */
44         @UNCField("vtn_name")
45         private String vtnName;
46         /** The VBR name. */
47         @UNCField("vbr_name")
48         private String vbrName;
49         /** The if name. */
50         @UNCField("if_name")
51         private String ifName;
52         /** The FF type. */
53         @UNCField("ff_type")
54         private String ffType;
55         /** The Sequence Number. */
56         @UNCField("seqnum")
57         private String seqnum;
58
59         /** The Constant LOG. */
60         private static final Logger LOG = Logger
61                         .getLogger(VBridgeInterfaceFlowFilterEntryResource.class
62                                         .getName());
63
64         /**
65          * Instantiates a new v bridge interface flow filter entry resource.
66          */
67         public VBridgeInterfaceFlowFilterEntryResource() {
68                 super();
69                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#VBridgeInterfaceFlowFilterEntryResource()");
70                 setValidator(new VBridgeFlowFilterEntryResourceValidator(this));
71                 LOG.trace("Completed VBridgeInterfaceFlowFilterEntryResource#VBridgeInterfaceFlowFilterEntryResource()");
72         }
73
74         /**
75          * @return the vtnName
76          */
77         public String getVtnName() {
78                 return vtnName;
79         }
80
81         /**
82          * @return the vbrName
83          */
84         public String getVbrName() {
85                 return vbrName;
86         }
87
88         /**
89          * @return the ifName
90          */
91         public String getIfName() {
92                 return ifName;
93         }
94
95         /**
96          * @return the ffType
97          */
98         public String getFfType() {
99                 return ffType;
100         }
101
102         /**
103          * @return the seqnum
104          */
105         public String getSeqnum() {
106                 return seqnum;
107         }
108
109         /**
110          * Implementation of Put method of VBridgeInterfaceFlowFilterEntry API
111          * 
112          * @param requestBody
113          *            the request Json object
114          * 
115          * @return Error code
116          * @throws VtnServiceException
117          */
118         @Override
119         public int put(final JsonObject requestBody) throws VtnServiceException {
120                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#put()");
121                 ClientSession session = null;
122                 IpcRequestProcessor requestProcessor = null;
123                 int status = ClientSession.RESP_FATAL;
124                 try {
125                         LOG.debug("Start Ipc framework call");
126                         session = getConnPool().getSession(
127                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
128                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
129                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
130                                         getExceptionHandler());
131                         LOG.debug("Session created successfully");
132                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
133                                         getConfigID(), getExceptionHandler());
134                         requestProcessor.createIpcRequestPacket(
135                                         IpcRequestPacketEnum.KT_VBRIF_FLOWFILTER_ENTRY_UPDATE,
136                                         requestBody, getUriParameters());
137                         LOG.debug("Request packet created successfully");
138                         status = requestProcessor.processIpcRequest();
139                         LOG.debug("Request packet processed with status" + status);
140                         LOG.debug("Complete Ipc framework call");
141                 } catch (final VtnServiceException e) {
142                         getExceptionHandler()
143                                         .raise(Thread.currentThread().getStackTrace()[1]
144                                                         .getClassName()
145                                                         + VtnServiceConsts.HYPHEN
146                                                         + Thread.currentThread().getStackTrace()[1]
147                                                                         .getMethodName(),
148                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
149                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
150                                                                         .getErrorMessage(), e);
151                         throw e;
152                 } finally {
153                         if (status == ClientSession.RESP_FATAL) {
154                                 if (null != requestProcessor.getErrorJson()) {
155                                         setInfo(requestProcessor.getErrorJson());
156                                 } else {
157                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
158                                                         .getValue());
159                                 }
160                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
161                         }
162                         getConnPool().destroySession(session);
163                 }
164                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#put()");
165                 return status;
166         }
167
168         /**
169          * Implementation of Delete method of VBridgeInterfaceFlowFilterEntry API
170          * 
171          * 
172          * @return Error code
173          * @throws VtnServiceException
174          */
175         @Override
176         public int delete() throws VtnServiceException {
177                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#delete()");
178                 ClientSession session = null;
179                 IpcRequestProcessor requestProcessor = null;
180                 int status = ClientSession.RESP_FATAL;
181                 try {
182                         LOG.debug("Start Ipc framework call");
183                         session = getConnPool().getSession(
184                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
185                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
186                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
187                                         getExceptionHandler());
188                         LOG.debug("Session created successfully");
189                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
190                                         getConfigID(), getExceptionHandler());
191                         requestProcessor.createIpcRequestPacket(
192                                         IpcRequestPacketEnum.KT_VBRIF_FLOWFILTER_ENTRY_DELETE,
193                                         getNullJsonObject(), getUriParameters());
194                         LOG.debug("Request packet created successfully");
195                         status = requestProcessor.processIpcRequest();
196                         LOG.debug("Request packet processed with status" + status);
197                         LOG.debug("Complete Ipc framework call");
198                 } catch (final VtnServiceException e) {
199                         getExceptionHandler()
200                                         .raise(Thread.currentThread().getStackTrace()[1]
201                                                         .getClassName()
202                                                         + VtnServiceConsts.HYPHEN
203                                                         + Thread.currentThread().getStackTrace()[1]
204                                                                         .getMethodName(),
205                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
206                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
207                                                                         .getErrorMessage(), e);
208                         throw e;
209                 } finally {
210                         if (status == ClientSession.RESP_FATAL) {
211                                 if (null != requestProcessor.getErrorJson()) {
212                                         setInfo(requestProcessor.getErrorJson());
213                                 } else {
214                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
215                                                         .getValue());
216                                 }
217                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
218                         }
219                         getConnPool().destroySession(session);
220                 }
221                 LOG.trace("Completed VBridgeInterfaceFlowFilterEntryResource#delete()");
222                 return status;
223         }
224
225         /**
226          * Implementation of Get method of VBridgeInterfaceFlowFilterEntry API
227          * 
228          * @param requestBody
229          *            the request Json object
230          * 
231          * @return Error code
232          * @throws VtnServiceException
233          */
234         @Override
235         public int get(final JsonObject requestBody) throws VtnServiceException {
236                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#get()");
237                 ClientSession session = null;
238                 IpcRequestProcessor requestProcessor = null;
239                 int status = ClientSession.RESP_FATAL;
240                 try {
241                         LOG.debug("Start Ipc framework call");
242                         session = getConnPool().getSession(
243                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
244                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
245                                         UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),
246                                         getExceptionHandler());
247                         LOG.debug("Session created successfully");
248                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
249                                         getConfigID(), getExceptionHandler());
250                         requestProcessor.createIpcRequestPacket(
251                                         IpcRequestPacketEnum.KT_VBRIF_FLOWFILTER_ENTRY_GET,
252                                         requestBody, getUriParameters());
253                         requestProcessor.getRequestPacket().setOperation(
254                                         IpcDataUnitWrapper
255                                                         .setIpcUint32Value(UncOperationEnum.UNC_OP_READ
256                                                                         .ordinal()));
257                         if (!requestBody.has(VtnServiceJsonConsts.TARGETDB)
258                                         || (requestBody.has(VtnServiceJsonConsts.TARGETDB) && requestBody
259                                                         .get(VtnServiceJsonConsts.TARGETDB).getAsString()
260                                                         .equalsIgnoreCase(VtnServiceJsonConsts.STATE))) {
261                                 requestProcessor
262                                                 .getRequestPacket()
263                                                 .setOption1(
264                                                                 IpcDataUnitWrapper
265                                                                                 .setIpcUint32Value((UncOption1Enum.UNC_OPT1_DETAIL
266                                                                                                 .ordinal())));
267                         }
268                         LOG.debug("Request packet created successfully");
269                         status = requestProcessor.processIpcRequest();
270                         LOG.debug("Request packet processed with status" + status);
271                         final IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
272                         setInfo(responseGenerator
273                                         .getVBridgeInterfaceFlowFilterEntryResponse(
274                                                         requestProcessor.getIpcResponsePacket(),
275                                                         requestBody, VtnServiceJsonConsts.SHOW));
276                         LOG.debug("Response object created successfully");
277                         LOG.debug("Complete Ipc framework call");
278                 } catch (final VtnServiceException e) {
279                         getExceptionHandler()
280                                         .raise(Thread.currentThread().getStackTrace()[1]
281                                                         .getClassName()
282                                                         + VtnServiceConsts.HYPHEN
283                                                         + Thread.currentThread().getStackTrace()[1]
284                                                                         .getMethodName(),
285                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
286                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
287                                                                         .getErrorMessage(), e);
288                         throw e;
289                 } finally {
290                         if (status == ClientSession.RESP_FATAL) {
291                                 if (null != requestProcessor
292                                                 && null != requestProcessor.getErrorJson()) {
293                                         setInfo(requestProcessor.getErrorJson());
294                                 } else {
295                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
296                                                         .getValue());
297                                 }
298                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
299                         }
300                         getConnPool().destroySession(session);
301                 }
302                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#get()");
303                 return status;
304         }
305
306         /**
307          * Add URI parameters to list
308          * 
309          * @return parameter list
310          */
311         private List<String> getUriParameters() {
312                 LOG.trace("Start VBridgeInterfaceFlowFilterEntryResource#getUriParameters()");
313                 final List<String> uriParameters = new ArrayList<String>();
314                 uriParameters.add(vtnName);
315                 uriParameters.add(vbrName);
316                 uriParameters.add(ifName);
317                 uriParameters.add(ffType);
318                 uriParameters.add(seqnum);
319                 LOG.trace("Completed VBridgeInterfaceFlowFilterEntryResource#getUriParameters()");
320                 return uriParameters;
321         }
322 }