2 * Copyright (c) 2012-2013 NEC Corporation
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
9 package org.opendaylight.vtn.javaapi.resources.logical;
11 import java.util.ArrayList;
12 import java.util.List;
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;
41 * The Class VTepGroupResource.
43 /* This class handles delete and get methods */
44 @UNCVtnService(path = "/vtns/{vtn_name}/vtepgroups/{vtepgroup_name}")
45 public class VTepGroupResource extends AbstractResource {
48 private String vtnName;
49 @UNCField("vtepgroup_name")
50 private String vtepgroupName;
51 private static final Logger LOG = Logger.getLogger(VTepGroupResource.class
53 public VTepGroupResource() {
55 LOG.trace("Start VTepGroupResource#VTepGroupResource()");
56 setValidator(new VTepGroupResourceValidator(this));
57 LOG.trace("Complete VTepGroupResource#VTepGroupResource()");
62 public String getVtnName() {
66 * @return the vtepgroupName
68 public String getVtepgroupName() {
72 * Implementation of Put method of VTepGroup API
75 * the request Json object
78 * @throws VtnServiceException
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();
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());
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)
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());
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) {
138 .rollBackIpcRequest(requestProcessor);
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);
153 LOG.debug("Creation of member data started..");
154 if (null != vTepGroup
156 .has(VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)
158 .get(VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)) {
159 final JsonArray vTepGroupMemberArray = vTepGroup.get(
160 VtnServiceJsonConsts.VTEPGROUPMEMBERNAME)
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());
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) {
180 .rollBackIpcRequest(requestProcessor);
186 LOG.debug("Creation of member data finished successfully..");
187 } catch (final VtnServiceException e) {
188 getExceptionHandler()
189 .raise(Thread.currentThread().getStackTrace()[1]
191 + VtnServiceConsts.HYPHEN
192 + Thread.currentThread().getStackTrace()[1]
194 UncJavaAPIErrorCode.IPC_SERVER_ERROR
196 UncJavaAPIErrorCode.IPC_SERVER_ERROR
197 .getErrorMessage(), e);
200 if (status == ClientSession.RESP_FATAL) {
201 if (null != requestProcessor.getErrorJson()) {
202 setInfo(requestProcessor.getErrorJson());
204 createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
207 status = UncResultCode.UNC_SERVER_ERROR.getValue();
209 getConnPool().destroySession(session);
212 LOG.debug("Complete Ipc framework call");
213 LOG.trace("Completed VTepGroupResource#delete()");
219 * Implementation of Delete method of VTep Group Resource API
222 * @throws VtnServiceException
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;
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]
250 + VtnServiceConsts.HYPHEN
251 + Thread.currentThread().getStackTrace()[1]
253 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
254 UncJavaAPIErrorCode.IPC_SERVER_ERROR
255 .getErrorMessage(), e);
258 if (status == ClientSession.RESP_FATAL) {
259 if (null != requestProcessor
260 && null != requestProcessor.getErrorJson()) {
261 setInfo(requestProcessor.getErrorJson());
263 createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
266 status = UncResultCode.UNC_SERVER_ERROR.getValue();
268 getConnPool().destroySession(session);
270 LOG.debug("Complete Ipc framework call");
271 LOG.trace("Completed VTepGroupResource#delete()");
275 * Implementation of get method of Vtep group Interface API
278 * the request Json object
281 * @throws VtnServiceException
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;
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]
310 + VtnServiceConsts.HYPHEN
311 + Thread.currentThread().getStackTrace()[1]
313 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
314 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage());
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());
325 requestProcessor.createIpcRequestPacket(
326 IpcRequestPacketEnum.KT_VTEP_GRP_MEMBER_GET,
327 requestBody, getUriParameters(requestBody));
328 LOG.debug("Request packet created successfully for 2nd call");
333 .setIpcUint32Value(UncOperationEnum.UNC_OP_READ_SIBLING_BEGIN
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]
341 + VtnServiceConsts.HYPHEN
342 + Thread.currentThread().getStackTrace()[1]
344 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
345 UncJavaAPIErrorCode.IPC_SERVER_ERROR
348 LOG.debug("Request packet created successfully");
349 vtepGroup = responseGenerator
350 .getVtepGroupMembers(
351 requestProcessor.getIpcResponsePacket(),
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
366 VtnServiceJsonConsts.MEMBERVTEPS).get(
368 requestProcessor.createIpcRequestPacket(
369 IpcRequestPacketEnum.KT_VTEP_GRP_MEMBER_GET,
370 memberJson, getUriParameters(requestBody));
375 .setIpcUint32Value(UncOperationEnum.UNC_OP_READ_SIBLING
377 status = requestProcessor.processIpcRequest();
378 vtepGroup = responseGenerator.getVtepGroupMembers(
379 requestProcessor.getIpcResponsePacket(),
381 if (!vtepGroup.has(VtnServiceJsonConsts.MEMBERVTEPS)
383 .getAsJsonArray(VtnServiceJsonConsts.MEMBERVTEPS) != null
384 && vtepGroup.getAsJsonArray(
385 VtnServiceJsonConsts.MEMBERVTEPS)
392 root.add(VtnServiceJsonConsts.VTEPGROUP, vtepGroup);
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]
404 + VtnServiceConsts.HYPHEN
405 + Thread.currentThread().getStackTrace()[1]
407 UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
408 UncJavaAPIErrorCode.IPC_SERVER_ERROR
409 .getErrorMessage(), e);
412 if (status == ClientSession.RESP_FATAL) {
413 if (null != requestProcessor
414 && null != requestProcessor.getErrorJson()) {
415 setInfo(requestProcessor.getErrorJson());
417 createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR
420 status = UncResultCode.UNC_SERVER_ERROR.getValue();
422 getConnPool().destroySession(session);
424 LOG.trace("Completed VTepGroupResource#get()");
429 * Add URI parameters to list
431 * @return parameter list
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());
448 uriParameters.add(vtepgroupName);
450 LOG.trace("Complete VTepGroupResource#getUriParameters()");
451 return uriParameters;