2d605823cb719a573c9612abc225fdbae0f69d35
[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.JsonArray;
15 import com.google.gson.JsonElement;
16 import com.google.gson.JsonNull;
17 import com.google.gson.JsonObject;
18 import org.opendaylight.vtn.core.ipc.ClientSession;
19 import org.opendaylight.vtn.core.util.Logger;
20 import org.opendaylight.vtn.javaapi.annotation.UNCField;
21 import org.opendaylight.vtn.javaapi.annotation.UNCVtnService;
22 import org.opendaylight.vtn.javaapi.constants.VtnServiceConsts;
23 import org.opendaylight.vtn.javaapi.constants.VtnServiceJsonConsts;
24 import org.opendaylight.vtn.javaapi.exception.VtnServiceException;
25 import org.opendaylight.vtn.javaapi.init.VtnServiceConfiguration;
26 import org.opendaylight.vtn.javaapi.init.VtnServiceInitManager;
27 import org.opendaylight.vtn.javaapi.ipc.IpcRequestProcessor;
28 import org.opendaylight.vtn.javaapi.ipc.IpcRollback;
29 import org.opendaylight.vtn.javaapi.ipc.conversion.IpcDataUnitWrapper;
30 import org.opendaylight.vtn.javaapi.ipc.conversion.IpcLogicalResponseFactory;
31 import org.opendaylight.vtn.javaapi.ipc.enums.IpcRequestPacketEnum;
32 import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum;
33 import org.opendaylight.vtn.javaapi.ipc.enums.UncCommonEnum.UncResultCode;
34 import org.opendaylight.vtn.javaapi.ipc.enums.UncJavaAPIErrorCode;
35 import org.opendaylight.vtn.javaapi.ipc.enums.UncOperationEnum;
36 import org.opendaylight.vtn.javaapi.ipc.enums.UncUPLLEnums;
37 import org.opendaylight.vtn.javaapi.resources.AbstractResource;
38 import org.opendaylight.vtn.javaapi.validation.logical.VTepGroupResourceValidator;
39
40 /**
41  * The Class VTepGroupResource.
42  */
43 /* This class handles delete and get methods */
44 @UNCVtnService(path = "/vtns/{vtn_name}/vtepgroups/{vtepgroup_name}")
45 public class VTepGroupResource extends AbstractResource {
46         /** The vtn name. */
47         @UNCField("vtn_name")
48         private String vtnName;
49         @UNCField("vtepgroup_name")
50         private String vtepgroupName;
51         private static final Logger LOG = Logger.getLogger(VTepGroupResource.class
52                         .getSimpleName());
53         public VTepGroupResource() {
54                 super();
55                 LOG.trace("Start VTepGroupResource#VTepGroupResource()");
56                 setValidator(new VTepGroupResourceValidator(this));
57                 LOG.trace("Complete VTepGroupResource#VTepGroupResource()");
58         }
59         /**
60          * @return the vtnName
61          */
62         public String getVtnName() {
63                 return vtnName;
64         }
65         /**
66          * @return the vtepgroupName
67          */
68         public String getVtepgroupName() {
69                 return vtepgroupName;
70         }
71         /**
72          * Implementation of Put method of VTepGroup API
73          * 
74          * @param requestBody
75          *            the request Json object
76          * 
77          * @return Error code
78          * @throws VtnServiceException
79          */
80         @Override
81         public int put(final JsonObject requestBody) throws VtnServiceException {
82                 LOG.trace("Starts VTepGroupResource#get()");
83                 ClientSession session = null;
84                 IpcRequestProcessor requestProcessor = null;
85                 JsonObject vTepGroup = null;
86                 int status = ClientSession.RESP_FATAL;
87                 final JsonObject getRequestBody = new JsonObject();
88                 // pending query for the same will update once query will be resolved
89                 getRequestBody.addProperty(VtnServiceJsonConsts.TARGETDB, VtnServiceJsonConsts.STATE);
90                 status = get(getRequestBody);
91                 LOG.debug("Request packet processed with status" + status);
92                 if (status == VtnServiceConsts.RESPONSE_SUCCESS) {
93                         JsonObject root = getInfo();
94                         try {
95                                 LOG.debug("Start Ipc framework call");
96                                 // Deleting all the member data.
97                                 LOG.debug("Deletion of member data started..");
98                                 final IpcRollback ipcRollback = new IpcRollback();
99                                 session = getConnPool().getSession(
100                                                 UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
101                                                 UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
102                                                 UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
103                                                 getExceptionHandler());
104                                 LOG.debug("Session created successfully");
105                                 // getting all the member data along with vTepGroup
106                                 requestProcessor = new IpcRequestProcessor(session,
107                                                 getSessionID(), getConfigID(), getExceptionHandler());
108
109                                 if (null != root && root.has(VtnServiceJsonConsts.VTEPGROUP)
110                                                 && null != root.get(VtnServiceJsonConsts.VTEPGROUP)) {
111                                         if (root.getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP)
112                                                         .has(VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)
113                                                         && null != root.getAsJsonObject(
114                                                                         VtnServiceJsonConsts.VTEPGROUP).get(
115                                                                         VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)) {
116                                                 final JsonArray vTepGroupMemberArray = root
117                                                                 .getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP)
118                                                                 .get(VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)
119                                                                 .getAsJsonArray();
120                                                 if (vTepGroupMemberArray.size() > 0) {
121                                                         for (final JsonElement jsonElement : vTepGroupMemberArray) {
122                                                                 status = ClientSession.RESP_FATAL;
123                                                                 requestProcessor.setServiceInfo(
124                                                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
125                                                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal());
126                                                                 requestProcessor
127                                                                                 .createIpcRequestPacket(
128                                                                                                 IpcRequestPacketEnum.KT_VTEP_GRP_MEMBER_DELETE,
129                                                                                                 jsonElement.getAsJsonObject(),
130                                                                                                 getUriParameters(requestBody));
131                                                                 LOG.debug("Request packet deleted successfully");
132                                                                 ipcRollback.pushIpcPacket(requestProcessor
133                                                                                 .getRequestPacket());
134                                                                 status = requestProcessor.processIpcRequest();
135                                                                 LOG.debug("Request packet processed with status" + status);
136                                                                 if (status == ClientSession.RESP_FATAL) {
137                                                                         ipcRollback
138                                                                                         .rollBackIpcRequest(requestProcessor);
139                                                                         break;
140                                                                 }
141                                                         }
142                                                 }
143                                         }
144                                 }
145                                 LOG.debug("Deletion of member data finished successfully..");
146                                 // creating Data members along with VtepGroup
147                                 //status = ClientSession.RESP_FATAL;
148                                 if (null != requestBody
149                                                 && requestBody.has(VtnServiceJsonConsts.VTEPGROUP)) {
150                                         vTepGroup = requestBody
151                                                         .getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP);
152                                 }
153                                 LOG.debug("Creation of member data started..");
154                                 if (null != vTepGroup
155                                                 && vTepGroup
156                                                                 .has(VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)
157                                                 && null != vTepGroup
158                                                                 .get(VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)) {
159                                         final JsonArray vTepGroupMemberArray = vTepGroup.get(
160                                                         VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)
161                                                         .getAsJsonArray();
162                                         if (vTepGroupMemberArray.size() > 0) {                                          
163                                                 for (final JsonElement jsonElement : vTepGroupMemberArray) {
164                                                         status = ClientSession.RESP_FATAL;
165                                                         requestProcessor.setServiceInfo(
166                                                                 UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
167                                                                 UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal());
168                                                         requestProcessor
169                                                                         .createIpcRequestPacket(
170                                                                                         IpcRequestPacketEnum.KT_VTEP_GRP_MEMBER_CREATE,
171                                                                                         jsonElement.getAsJsonObject(),
172                                                                                         getUriParameters(requestBody));
173                                                         ipcRollback.pushIpcPacket(requestProcessor
174                                                                         .getRequestPacket());
175                                                         LOG.debug("Request packet created successfully");
176                                                         status = requestProcessor.processIpcRequest();
177                                                         LOG.debug("Request packet processed with status" + status);
178                                                         if (status == ClientSession.RESP_FATAL) {
179                                                                 ipcRollback
180                                                                                 .rollBackIpcRequest(requestProcessor);
181                                                                 break;
182                                                         }
183                                                 }
184                                         }
185                                 }
186                                 LOG.debug("Creation of member data finished successfully..");
187                         } catch (final VtnServiceException e) {
188                                 getExceptionHandler()
189                                                 .raise(Thread.currentThread().getStackTrace()[1]
190                                                                 .getClassName()
191                                                                 + VtnServiceConsts.HYPHEN
192                                                                 + Thread.currentThread().getStackTrace()[1]
193                                                                                 .getMethodName(),
194                                                                 UncJavaAPIErrorCode.IPC_SERVER_ERROR
195                                                                                 .getErrorCode(),
196                                                                 UncJavaAPIErrorCode.IPC_SERVER_ERROR
197                                                                                 .getErrorMessage(), e);
198                                 throw e;
199                         } finally {
200                                 if (status == ClientSession.RESP_FATAL) {
201                                         if (null != requestProcessor.getErrorJson()) {
202                                                 setInfo(requestProcessor.getErrorJson());
203                                         } else {
204                                                 createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
205                                                                 .getValue());
206                                         }
207                                         status = UncResultCode.UNC_SERVER_ERROR.getValue();
208                                 }
209                                 getConnPool().destroySession(session);
210                         }
211
212                         LOG.debug("Complete Ipc framework call");
213                         LOG.trace("Completed VTepGroupResource#delete()");
214                 }
215                 setInfo(null);
216                 return status;
217         }
218         /**
219          * Implementation of Delete method of VTep Group Resource API
220          * 
221          * @return Error code
222          * @throws VtnServiceException
223          */
224         @Override
225         public int delete() throws VtnServiceException {
226                 LOG.trace("starts VTepGroupResource#get()");
227                 ClientSession session = null;
228                 IpcRequestProcessor requestProcessor = null;
229                 int status = ClientSession.RESP_FATAL;
230                 try {
231                         LOG.debug("Start Ipc framework call");
232                         session = getConnPool().getSession(
233                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
234                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
235                                         UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),
236                                         getExceptionHandler());
237                         LOG.debug("Session created successfully");
238                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
239                                         getConfigID(), getExceptionHandler());
240                         requestProcessor.createIpcRequestPacket(
241                                         IpcRequestPacketEnum.KT_VTEP_GRP_DELETE,
242                                         getNullJsonObject(), getUriParameters(getNullJsonObject()));
243                         LOG.debug("Request packet created successfully");
244                         status = requestProcessor.processIpcRequest();
245                         LOG.debug("Request packet processed with status" + status);
246                 } catch (final VtnServiceException e) {
247                         getExceptionHandler()
248                                         .raise(Thread.currentThread().getStackTrace()[1]
249                                                         .getClassName()
250                                                         + VtnServiceConsts.HYPHEN
251                                                         + Thread.currentThread().getStackTrace()[1]
252                                                                         .getMethodName(),
253                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
254                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
255                                                                         .getErrorMessage(), e);
256                         throw e;
257                 } finally {
258                         if (status == ClientSession.RESP_FATAL) {
259                                 if (null != requestProcessor
260                                                 && null != requestProcessor.getErrorJson()) {
261                                         setInfo(requestProcessor.getErrorJson());
262                                 } else {
263                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
264                                                         .getValue());
265                                 }
266                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
267                         }
268                         getConnPool().destroySession(session);
269                 }
270                 LOG.debug("Complete Ipc framework call");
271                 LOG.trace("Completed VTepGroupResource#delete()");
272                 return status;
273         }
274         /**
275          * Implementation of get method of Vtep group Interface API
276          * 
277          * @param requestBody
278          *            the request Json object
279          * 
280          * @return Error code
281          * @throws VtnServiceException
282          */
283         @Override
284         public int get(final JsonObject requestBody) throws VtnServiceException {
285                 LOG.trace("starts VTepGroupResource#get()");
286                 ClientSession session = null;
287                 final JsonObject root = new JsonObject();
288                 IpcRequestProcessor requestProcessor = null;
289                 int status = ClientSession.RESP_FATAL;
290                 try {
291                         LOG.debug("Start Ipc framework call");
292                         session = getConnPool().getSession(
293                                         UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,
294                                         UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
295                                         UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),
296                                         getExceptionHandler());
297                         LOG.debug("Session created successfully");
298                         requestProcessor = new IpcRequestProcessor(session, getSessionID(),
299                                         getConfigID(), getExceptionHandler());
300                         requestProcessor.createIpcRequestPacket(
301                                         IpcRequestPacketEnum.KT_VTEP_GRP_GET, requestBody,
302                                         getUriParameters(requestBody));
303                         LOG.debug("Request packet created successfully");
304                         status = requestProcessor.processIpcRequest();
305                         LOG.debug("Request packet processed for 1st call with status" + status);
306                         if (status == ClientSession.RESP_FATAL) {
307                                 throw new VtnServiceException(
308                                                 Thread.currentThread().getStackTrace()[1]
309                                                                 .getClassName()
310                                                                 + VtnServiceConsts.HYPHEN
311                                                                 + Thread.currentThread().getStackTrace()[1]
312                                                                                 .getMethodName(),
313                                                 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
314                                                 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage());
315                         }
316                         IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
317                         JsonObject vtepGroup = responseGenerator.getVTepGroupResponse(
318                                         requestProcessor.getIpcResponsePacket(), requestBody,
319                                         VtnServiceJsonConsts.SHOW);
320                         if(!(vtepGroup.get(VtnServiceJsonConsts.VTEPGROUP) instanceof JsonNull)){
321                                 requestProcessor.setServiceInfo(
322                                                 UncUPLLEnums.UPLL_IPC_SERVICE_NAME,
323                                                 UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal());
324
325                                 requestProcessor.createIpcRequestPacket(
326                                                 IpcRequestPacketEnum.KT_VTEP_GRP_MEMBER_GET,
327                                                 requestBody, getUriParameters(requestBody));
328                                 LOG.debug("Request packet created successfully for 2nd call");
329                                 requestProcessor
330                                                 .getRequestPacket()
331                                                 .setOperation(
332                                                                 IpcDataUnitWrapper
333                                                                                 .setIpcUint32Value(UncOperationEnum.UNC_OP_READ_SIBLING_BEGIN
334                                                                                                 .ordinal()));
335                                 status = requestProcessor.processIpcRequest();
336                                 LOG.debug("Request packet processed with status" + status);
337                                 if (status == ClientSession.RESP_FATAL) {
338                                         throw new VtnServiceException(
339                                                         Thread.currentThread().getStackTrace()[1]
340                                                                         .getClassName()
341                                                                         + VtnServiceConsts.HYPHEN
342                                                                         + Thread.currentThread().getStackTrace()[1]
343                                                                                         .getMethodName(),
344                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
345                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
346                                                                         .getErrorMessage());
347                                 }
348                                 LOG.debug("Request packet created successfully");
349                                 vtepGroup = responseGenerator
350                                                 .getVtepGroupMembers(
351                                                                 requestProcessor.getIpcResponsePacket(),
352                                                                 vtepGroup
353                                                                                 .getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP));
354                                 JsonArray memberJsonArray = vtepGroup
355                                                 .getAsJsonArray(VtnServiceJsonConsts.MEMBERVTEPS);
356                                 VtnServiceConfiguration configuration = VtnServiceInitManager
357                                                 .getConfigurationMap();
358                                 final int max_rep_count = Integer.parseInt(configuration
359                                                 .getConfigValue(VtnServiceConsts.MAX_REP_DEFAULT));
360                                 int memberIndex = memberJsonArray.size();
361                                 if (memberJsonArray.size() >= max_rep_count) {
362                                         while (memberIndex >= max_rep_count) {
363                                                 memberIndex = memberJsonArray.size();
364                                                 JsonObject memberJson = (JsonObject) vtepGroup
365                                                                 .getAsJsonArray(
366                                                                                 VtnServiceJsonConsts.MEMBERVTEPS).get(
367                                                                                 memberIndex - 1);
368                                                 requestProcessor.createIpcRequestPacket(
369                                                                 IpcRequestPacketEnum.KT_VTEP_GRP_MEMBER_GET,
370                                                                 memberJson, getUriParameters(requestBody));
371                                                 requestProcessor
372                                                                 .getRequestPacket()
373                                                                 .setOperation(
374                                                                                 IpcDataUnitWrapper
375                                                                                                 .setIpcUint32Value(UncOperationEnum.UNC_OP_READ_SIBLING
376                                                                                                                 .ordinal()));
377                                                 status = requestProcessor.processIpcRequest();
378                                                 vtepGroup = responseGenerator.getVtepGroupMembers(
379                                                                 requestProcessor.getIpcResponsePacket(),
380                                                                 vtepGroup);
381                                                 if (!vtepGroup.has(VtnServiceJsonConsts.MEMBERVTEPS)
382                                                                 || vtepGroup
383                                                                                 .getAsJsonArray(VtnServiceJsonConsts.MEMBERVTEPS) != null
384                                                                 && vtepGroup.getAsJsonArray(
385                                                                                 VtnServiceJsonConsts.MEMBERVTEPS)
386                                                                                 .size() == 0) {
387                                                         break;
388                                                 }
389                                                 memberIndex++;
390                                         }
391                                 }
392                                 root.add(VtnServiceJsonConsts.VTEPGROUP, vtepGroup);
393                                 setInfo(root);
394                         } else {
395                                 setInfo(vtepGroup);
396                         }
397                         
398                         LOG.debug("Response object created successfully");
399                         LOG.debug("Complete Ipc framework call");
400                 } catch (final VtnServiceException e) {
401                         getExceptionHandler()
402                                         .raise(Thread.currentThread().getStackTrace()[1]
403                                                         .getClassName()
404                                                         + VtnServiceConsts.HYPHEN
405                                                         + Thread.currentThread().getStackTrace()[1]
406                                                                         .getMethodName(),
407                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
408                                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR
409                                                                         .getErrorMessage(), e);
410                         throw e;
411                 } finally {
412                         if (status == ClientSession.RESP_FATAL) {
413                                 if (null != requestProcessor
414                                                 && null != requestProcessor.getErrorJson()) {
415                                         setInfo(requestProcessor.getErrorJson());
416                                 } else {
417                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
418                                                         .getValue());
419                                 }
420                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
421                         }
422                         getConnPool().destroySession(session);
423                 }
424                 LOG.trace("Completed VTepGroupResource#get()");
425                 return status;
426         }
427
428         /**
429          * Add URI parameters to list
430          * 
431          * @return parameter list
432          */
433         private List<String> getUriParameters(final JsonObject requestBody) {
434                 LOG.trace("Start VTepGroupResource#getUriParameters()");
435                 LOG.info("request body : " + requestBody);
436                 final List<String> uriParameters = new ArrayList<String>();
437                 uriParameters.add(vtnName);
438                 if (null != requestBody
439                                 && requestBody.has(VtnServiceJsonConsts.VTEPGROUP)
440                                 && null != requestBody
441                                                 .getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP)
442                                 && requestBody.getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP)
443                                                 .has(VtnServiceJsonConsts.VTEPGROUPNAME)) {
444                         uriParameters.add(requestBody
445                                         .getAsJsonObject(VtnServiceJsonConsts.VTEPGROUP)
446                                         .get(VtnServiceJsonConsts.VTEPGROUPNAME).getAsString());
447                 } else {
448                         uriParameters.add(vtepgroupName);
449                 }
450                 LOG.trace("Complete VTepGroupResource#getUriParameters()");
451                 return uriParameters;
452         }
453 }