b0238c4e657337bd5d21b9cf9017e5d017d3bf8a
[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.StaticIpRouteResourceValidator;
32
33 /**
34  * The Class StaticIpRoutesResource implements post, get method.
35  */
36 @UNCVtnService(path = "/vtns/{vtn_name}/vrouters/{vrt_name}/static_iproutes")
37 public class StaticIpRoutesResource extends AbstractResource {
38
39         /** The vtn name. */
40         @UNCField("vtn_name")
41         private String vtnName;
42
43         /** The vrt name. */
44         @UNCField("vrt_name")
45         private String vrtName;
46
47         /**
48          * 
49          * @return the vtn name
50          */
51         public String getVtnName() {
52                 return vtnName;
53         }
54
55         /**
56          * @return the vrt name
57          */
58         public String getVrtName() {
59                 return vrtName;
60         }
61
62         private static final Logger LOG = Logger
63                         .getLogger(StaticIpRoutesResource.class.getName());
64
65         /**
66          * Instantiates a new static ip routes resource.
67          */
68         public StaticIpRoutesResource() {
69                 super();
70                 LOG.trace("Start StaticIpRoutesResource#StaticIpRoutesResource()");
71                 setValidator(new StaticIpRouteResourceValidator(this));
72                 LOG.trace("Complete StaticIpRoutesResource#StaticIpRoutesResource()");
73         }
74
75         /**
76          * Implementation of post method of Static Ip Routes
77          * 
78          * @param requestBody
79          *            the request Json object
80          * 
81          * @return Error code
82          * @throws VtnServiceException
83          */
84         @Override
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;
90                 try {
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, "");
108                         }
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);
115                         // StaticIp Route
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;
150                         }
151                         JsonObject root = new JsonObject();
152                         JsonObject staticIpRoute = new JsonObject();
153                         staticIpRoute.addProperty(VtnServiceJsonConsts.STATICIPROUTEID,
154                                         staticIpRouteId);
155                         root.add(VtnServiceJsonConsts.STATIC_IPROUTE, staticIpRoute);
156                         setInfo(root);
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]
162                                                         .getClassName()
163                                                         + VtnServiceConsts.HYPHEN
164                                                         + Thread.currentThread().getStackTrace()[1]
165                                                                         .getMethodName(),
166                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
167                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
168                                                                         .getErrorMessage(), e);
169                         throw e;
170                 } finally {
171                         if (status == ClientSession.RESP_FATAL) {
172                                 if (null != requestProcessor.getErrorJson()) {
173                                         setInfo(requestProcessor.getErrorJson());
174                                 } else {
175                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
176                                                         .getValue());
177                                 }
178                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
179                         }
180                         getConnPool().destroySession(session);
181                 }
182                 LOG.trace("Complete StaticIpRoutesResource#post()");
183                 return status;
184         }
185
186         /**
187          * Implementation of get method of Static Ip Routes
188          * 
189          * @param requestBody
190          *            the request Json object
191          * 
192          * @return Error code
193          * @throws VtnServiceException
194          */
195         @Override
196         public int get(final JsonObject requestBody) throws VtnServiceException {
197
198                 LOG.trace("Start StaticIpRoutesResource#get()");
199                 ClientSession session = null;
200                 IpcRequestProcessor requestProcessor = null;
201                 int status = ClientSession.RESP_FATAL;
202                 try {
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]
227                                                         .getClassName()
228                                                         + VtnServiceConsts.HYPHEN
229                                                         + Thread.currentThread().getStackTrace()[1]
230                                                                         .getMethodName(),
231                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
232                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
233                                                                         .getErrorMessage(), e);
234                         throw e;
235                 } finally {
236                         if (status == ClientSession.RESP_FATAL) {
237                                 if (null != requestProcessor.getErrorJson()) {
238                                         setInfo(requestProcessor.getErrorJson());
239                                 } else {
240                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
241                                                         .getValue());
242                                 }
243                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
244                         }
245                         getConnPool().destroySession(session);
246                 }
247                 LOG.trace("Start StaticIpRoutesResource#get()");
248                 return status;
249         }
250
251         /**
252          * Add URI parameters to list
253          * 
254          * @return
255          */
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)
263                                         .getAsString());
264                 }
265                 LOG.trace("Completed StaticIpRoutesResource#getUriParameters()");
266                 return uriParameters;
267         }
268
269 }