f76996342e3b07635b93ff4ebc100a4ec3d61cdf
[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
10 package org.opendaylight.vtn.javaapi.resources.logical;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
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.VBridgeFlowFilterEntriesResourceValidator;
32
33 /**
34  * The Class VRouterInterfaceFlowFilterEntriesResource implements post and get
35  * methods.
36  * 
37  */
38 /* This class handles post and get methods */
39 @UNCVtnService(path = "/vtns/{vtn_name}/vrouters/{vrt_name}/interfaces/{if_name}/flowfilters/{ff_type}/flowfilterentries")
40 public class VRouterInterfaceFlowFilterEntriesResource extends AbstractResource {
41
42         /** The VTN name. */
43         @UNCField("vtn_name")
44         private String vtnName;
45         /** The VRT name. */
46         @UNCField("vrt_name")
47         private String vrtName;
48         /** The if name. */
49         @UNCField("if_name")
50         private String ifName;
51         /** The FF type. */
52         @UNCField("ff_type")
53         private String ffType;
54
55         /** The Constant LOG. */
56         private static final Logger LOG = Logger
57                         .getLogger(VRouterInterfaceFlowFilterEntriesResource.class
58                                         .getName());
59
60         /**
61          * Instantiates a new v router interface flow filter entries resource.
62          */
63         public VRouterInterfaceFlowFilterEntriesResource() {
64                 super();
65                 LOG.trace("Start VRouterInterfaceFlowFilterEntriesResource#VRouterInterfaceFlowFilterEntriesResource()");
66                 setValidator(new VBridgeFlowFilterEntriesResourceValidator(this));
67                 LOG.trace("Completed VRouterInterfaceFlowFilterEntriesResource#VRouterInterfaceFlowFilterEntriesResource()");
68         }
69
70         /**
71          * @return the vtnName
72          */
73         public String getVtnName() {
74                 return vtnName;
75         }
76
77         /**
78          * @return the vrtName
79          */
80         public String getVrtName() {
81                 return vrtName;
82         }
83
84         /**
85          * @return the ifName
86          */
87         public String getIfName() {
88                 return ifName;
89         }
90
91         /**
92          * @return the ffType
93          */
94         public String getFfType() {
95                 return ffType;
96         }
97
98         /**
99          * Implementation of Post method of VRouterInterfaceFlowFilterEntry API
100          * 
101          * @param requestBody
102          *            the request Json object
103          * 
104          * @return Error code
105          * @throws VtnServiceException
106          */
107         @Override
108         public int post(final JsonObject requestBody) throws VtnServiceException {
109                 LOG.trace("Start VRouterInterfaceFlowFilterEntriesResource#post()");
110                 ClientSession session = null;
111                 IpcRequestProcessor requestProcessor = null;
112                 int status = ClientSession.RESP_FATAL;
113                 try {
114                         LOG.debug("Start Ipc framework call");
115                         session = getConnPool().getSession(
116                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
117                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
118                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
119                                         getExceptionHandler());
120                         LOG.debug("Session created successfully");
121                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
122                                         getConfigID(), getExceptionHandler());
123                         requestProcessor.createIpcRequestPacket(
124                                         IpcRequestPacketEnum.KT_VRTIF_FLOWFILTER_ENTRY_CREATE,
125                                         requestBody, getUriParameters(requestBody));
126                         LOG.debug("Request packet created successfully");
127                         status = requestProcessor.processIpcRequest();
128                         LOG.debug("Request packet processed with status" + status);
129                         LOG.debug("Complete Ipc framework call");
130                 } catch (final VtnServiceException e) {
131                         getExceptionHandler()
132                                         .raise(Thread.currentThread().getStackTrace()[1]
133                                                         .getClassName()
134                                                         + VtnServiceConsts.HYPHEN
135                                                         + Thread.currentThread().getStackTrace()[1]
136                                                                         .getMethodName(),
137                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
138                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
139                                                                         .getErrorMessage(), e);
140                         throw e;
141                 } finally {
142                         if (status == ClientSession.RESP_FATAL) {
143                                 if (null != requestProcessor.getErrorJson()) {
144                                         setInfo(requestProcessor.getErrorJson());
145                                 } else {
146                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
147                                                         .getValue());
148                                 }
149                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
150                         }
151                         getConnPool().destroySession(session);
152                 }
153                 LOG.trace("Completed VRouterInterfaceFlowFilterEntriesResource#post()");
154                 return status;
155         }
156
157         /**
158          * Implementation of Get method of VRouterInterfaceFlowFilterEntry API
159          * 
160          * @param requestBody
161          *            the request Json object
162          * 
163          * @return Error code
164          * @throws VtnServiceException
165          */
166         @Override
167         public int get(final JsonObject requestBody) throws VtnServiceException {
168                 LOG.trace("Start VRouterInterfaceFlowFilterEntriesResource#get()");
169                 ClientSession session = null;
170                 IpcRequestProcessor requestProcessor = null;
171                 int status = ClientSession.RESP_FATAL;
172                 try {
173                         LOG.debug("Start Ipc framework call");
174                         session = getConnPool().getSession(
175                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
176                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
177                                         UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),
178                                         getExceptionHandler());
179                         LOG.debug("Session created successfully");
180                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
181                                         getConfigID(), getExceptionHandler());
182                         requestProcessor.createIpcRequestPacket(
183                                         IpcRequestPacketEnum.KT_VRTIF_FLOWFILTER_ENTRY_GET,
184                                         requestBody, getUriParameters(requestBody));
185                         LOG.debug("Request packet created successfully");
186                         status = requestProcessor.processIpcRequest();
187                         LOG.debug("Request packet processed with status" + status);
188                         final IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
189                         setInfo(responseGenerator
190                                         .getVRouterInterfaceFlowFilterEntryResponse(
191                                                         requestProcessor.getIpcResponsePacket(),
192                                                         requestBody, VtnServiceJsonConsts.LIST));
193                         LOG.debug("Response object created successfully");
194                         LOG.debug("Complete Ipc framework call");
195                 } catch (final VtnServiceException e) {
196                         getExceptionHandler()
197                                         .raise(Thread.currentThread().getStackTrace()[1]
198                                                         .getClassName()
199                                                         + VtnServiceConsts.HYPHEN
200                                                         + Thread.currentThread().getStackTrace()[1]
201                                                                         .getMethodName(),
202                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
203                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
204                                                                         .getErrorMessage(), e);
205                         throw e;
206                 } finally {
207                         if (status == ClientSession.RESP_FATAL) {
208                                 if (null != requestProcessor
209                                                 && null != requestProcessor.getErrorJson()) {
210                                         setInfo(requestProcessor.getErrorJson());
211                                 } else {
212                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
213                                                         .getValue());
214                                 }
215                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
216                         }
217                         getConnPool().destroySession(session);
218                 }
219                 LOG.trace("Completed VRouterInterfaceFlowFilterEntriesResource#get()");
220                 return status;
221         }
222
223         /**
224          * Add URI parameters to list
225          * 
226          * @return
227          */
228         private List<String> getUriParameters(final JsonObject requestBody) {
229                 LOG.trace("Start VRouterInterfaceFlowFilterEntriesResource#getUriParameters()");
230                 final List<String> uriParameters = new ArrayList<String>();
231                 uriParameters.add(vtnName);
232                 uriParameters.add(vrtName);
233                 uriParameters.add(ifName);
234                 uriParameters.add(ffType);
235                 if (requestBody != null && requestBody.has(VtnServiceJsonConsts.INDEX)) {
236                         uriParameters.add(requestBody.get(VtnServiceJsonConsts.INDEX)
237                                         .getAsString());
238                 }LOG.trace("Completed VRouterInterfaceFlowFilterEntriesResource#getUriParameters()");
239                 return uriParameters;
240         }
241 }