afe80ba78db18ed9ad37514c3cf15482399f9f96
[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.PortMapResourceValidator;
32
33 /**
34  * The Class VTepInterfacePortMapResource implements delete, put and get methods.
35  *
36  */
37
38 @UNCVtnService(path = "/vtns/{vtn_name}/vteps/{vtep_name}/interfaces/{if_name}/portmap")
39 public class VTepInterfacePortMapResource extends AbstractResource {
40         /** The vtn name. */
41         @UNCField("vtn_name")
42         private String vtnName;
43         /** The vbr name. */
44         @UNCField("vtep_name")
45         private String vtepName;
46         /** The if name. */
47         @UNCField("if_name")
48         private String ifName;
49
50         /** The Constant LOG. */
51         private static final Logger LOG = Logger.getLogger(VTepInterfacePortMapResource.class.getSimpleName());
52         /**
53          * Gets the vtn name.
54          *
55          * @return the vtn name
56          */
57         public String getVtnName() {
58                 return vtnName;
59         }
60         /**
61          * Gets the vbr name.
62          *
63          * @return the vbr name
64          */
65         public String getVtepName() {
66                 return vtepName;
67         }
68         /**
69          * Gets the if name.
70          *
71          * @return the if name
72          */
73         public String getIfName() {
74                 return ifName;
75         }
76         /**
77          * Instantiates a new port map resource.
78          */
79         public VTepInterfacePortMapResource() {
80                 super();
81                 LOG.trace("Start VTepInterfacePortMapResource#VTepInterfacePortMapResource()");
82                 setValidator(new PortMapResourceValidator(this));
83                 LOG.trace("Complete VTepInterfacePortMapResource#VTepInterfacePortMapResource()");
84         }
85
86         /**
87          * Implementation of Delete method of VTep Interface PortMap API
88          * 
89          * @return Error code
90          * @throws VtnServiceException
91          */
92         @Override
93         public int delete() throws VtnServiceException {
94                 LOG.trace("Start VTepInterfacePortMapResource#delete()");
95                 ClientSession session = null;
96                 IpcRequestProcessor requestProcessor = null;
97                 int status = ClientSession.RESP_FATAL;
98                 try {
99                         session = getConnPool().getSession(UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,UncUPLLEnums.UPLL_IPC_SERVICE_NAME,UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),getExceptionHandler());
100                         LOG.debug("Session created successfully");
101                         requestProcessor = new IpcRequestProcessor(session, getSessionID(), getConfigID(),getExceptionHandler());
102                         requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VTEP_IF_UPDATE,getNullJsonObject(),getUriParameters());
103                         LOG.debug("Request packet created successfully");
104                         status= requestProcessor.processIpcRequest();
105                 } catch (final VtnServiceException e) {
106                         getExceptionHandler().raise(
107                                         Thread.currentThread().getStackTrace()[1].getClassName()
108                                         + VtnServiceConsts.HYPHEN
109                                         + Thread.currentThread().getStackTrace()[1].getMethodName(),
110                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
111                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage(), e);
112                         throw e;
113                 } finally {
114                         if (status == ClientSession.RESP_FATAL) {
115                                 if(null != requestProcessor.getErrorJson()){
116                                         setInfo(requestProcessor.getErrorJson());
117                                 } else {
118                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
119                                 }
120                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
121                         }
122                         getConnPool().destroySession(session);
123                 }
124                 LOG.trace("Complete VTepInterfacePortMapResource#delete()");
125                 return status;
126         }
127         /**
128          * Implementation of get method of VTep Interface PortMap API
129          * 
130          * @param requestBody the request Json object
131          * 
132          * @return Error code
133          * @throws VtnServiceException
134          */
135         @Override
136         public int get(final JsonObject requestBody) throws VtnServiceException {
137                 LOG.trace("Start VTepInterfacePortMapResource#get()");
138                 ClientSession session = null;
139                 IpcRequestProcessor requestProcessor = null;
140                 int status = ClientSession.RESP_FATAL;
141                 try {
142                         session = getConnPool().getSession(UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,UncUPLLEnums.UPLL_IPC_SERVICE_NAME,UncUPLLEnums.ServiceID.UPLL_READ_SVC_ID.ordinal(),getExceptionHandler());
143                         LOG.debug("Session created successfully");
144                         requestProcessor = new IpcRequestProcessor(session, getSessionID(), getConfigID(),getExceptionHandler());
145                         requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VTEP_IF_GET, requestBody,getUriParameters());
146                         LOG.debug("Request Packet created successfully");
147                         status= requestProcessor.processIpcRequest();
148                         IpcLogicalResponseFactory responseGenerator = new IpcLogicalResponseFactory();
149                         setInfo(responseGenerator.getPortMapResponse(requestProcessor.getIpcResponsePacket(),requestBody,VtnServiceJsonConsts.SHOW,VtnServiceJsonConsts.VTEP_INTERFACE_PORTMAP));
150                         LOG.debug("response object created successfully");
151                         LOG.debug("Ipc framework call complete");
152                 } catch (final VtnServiceException e) {
153                         getExceptionHandler().raise(
154                                         Thread.currentThread().getStackTrace()[1].getClassName()
155                                         + VtnServiceConsts.HYPHEN
156                                         + Thread.currentThread().getStackTrace()[1].getMethodName(),
157                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
158                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage(), e);
159                         throw e;
160                 } finally {
161                         if (status == ClientSession.RESP_FATAL) {
162                                 if(null != requestProcessor.getErrorJson()){
163                                         setInfo(requestProcessor.getErrorJson());
164                                 } else {
165                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
166                                 }
167                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
168                         }
169                         getConnPool().destroySession(session);
170                 }
171                 LOG.trace("Complete VTepInterfacePortMapResource#get()");
172                 return status;
173         }
174
175         /**
176          * Implementation of Put method of PortMap API
177          * 
178          * @param requestBody the request Json object
179          * 
180          * @return Error code
181          * @throws VtnServiceException
182          */
183         @Override
184         public int put(final JsonObject requestBody) throws VtnServiceException {
185                 LOG.trace("Start VTepInterfacePortMapResource#put()");
186                 ClientSession session = null;
187                 IpcRequestProcessor requestProcessor = null;
188                 int status = ClientSession.RESP_FATAL;
189                 try {
190                         session = getConnPool().getSession(UncUPLLEnums.UPLL_IPC_CHANNEL_NAME,UncUPLLEnums.UPLL_IPC_SERVICE_NAME,UncUPLLEnums.ServiceID.UPLL_EDIT_SVC_ID.ordinal(),getExceptionHandler());
191                         LOG.debug("Session created successfully");
192                         requestProcessor = new IpcRequestProcessor(session, getSessionID(), getConfigID(),getExceptionHandler());
193                         requestProcessor.createIpcRequestPacket(IpcRequestPacketEnum.KT_VTEP_IF_UPDATE, requestBody,getUriParameters());
194                         LOG.debug("Request Packet created successfully");
195                         status = requestProcessor.processIpcRequest();
196                 }catch (final VtnServiceException e) {
197                         getExceptionHandler().raise(
198                                         Thread.currentThread().getStackTrace()[1].getClassName()
199                                         + VtnServiceConsts.HYPHEN
200                                         + Thread.currentThread().getStackTrace()[1].getMethodName(),
201                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorCode(),
202                                         UncJavaAPIErrorCode.IPC_SERVER_ERROR.getErrorMessage(), e);
203                         throw e;
204                 } finally {
205                         if (status == ClientSession.RESP_FATAL) {
206                                 if(null != requestProcessor.getErrorJson()){
207                                         setInfo(requestProcessor.getErrorJson());
208                                 } else {
209                                         createErrorInfo(UncCommonEnum.UncResultCode.UNC_SERVER_ERROR.getValue());
210                                 }
211                                 status = UncResultCode.UNC_SERVER_ERROR.getValue();
212                         }
213                         getConnPool().destroySession(session);
214                 }
215                 LOG.trace("Complete VTepInterfacePortMapResource#put()");
216                 return status;
217         }
218         /**
219          * Add URI parameters to list
220          * @return
221          */
222         private List<String> getUriParameters() {
223                 final List<String> uriParameters = new ArrayList<String>();
224                 uriParameters.add(vtnName);
225                 uriParameters.add(vtepName);
226                 uriParameters.add(ifName);
227                 return uriParameters;
228         }
229 }