3192971f9bea59cb77735c1916297baf0616dbbb
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericGetVnf.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import static org.openecomp.mso.bpmn.common.scripts.GenericUtils.*;\r
24 \r
25 import org.apache.commons.lang3.*\r
26 import org.camunda.bpm.engine.delegate.BpmnError\r
27 import org.camunda.bpm.engine.runtime.Execution\r
28 import org.openecomp.mso.rest.APIResponse\r
29 import org.springframework.web.util.UriUtils\r
30 \r
31 \r
32 /**\r
33  * TODO: Support getting vnf type = vpe\r
34  *\r
35  * This class supports the GenericGetVnf Sub Flow.\r
36  * This Generic sub flow can be used by any flow for accomplishing\r
37  * the goal of getting a Vnf Object (from AAI).  The flow currently\r
38  * supports the querying of 2 types of Vnfs, generic-vnf and vce. The\r
39  * type must be provided by the calling flow and the type should\r
40  * be mapped to the variable GENGV_type. The type should either be\r
41  * "generic-vnf" or "vce".  If the Vnf Id is not provided by the calling\r
42  * flow then this sub flow will execute the query to get the\r
43  * Vnf using the Vnf Name. Therefore, the calling flow must provide\r
44  * either the Vnf Id or Vnf Name.\r
45  *\r
46  * Upon successful completion of this sub flow the\r
47  * GENGV_SuccessIndicator will be true and the query response payload\r
48  * will be set to GENGV_vnf.  An MSOWorkflowException will\r
49  * be thrown upon unsuccessful completion or if an error occurs\r
50  * at any time during this sub flow. Please map variables\r
51  * to the corresponding variable names below.\r
52  *\r
53  * Note - if this sub flow receives a Not Found (404) response\r
54  * from AAI at any time this will be considered an acceptable\r
55  * successful response however the GENGV_FoundIndicator\r
56  * set to false.  This will allow the calling flow to distinguish\r
57  * between the two success scenarios, "Success where Vnf is found"\r
58  * and "Success where Vnf is NOT found".\r
59  *\r
60  *\r
61  * Variable Mapping Below\r
62  *\r
63  * In Mapping Variables:\r
64  *   @param - GENGV_vnfId  or  @param - GENGV_vnfName\r
65  *   @param - GENGV_type\r
66  *\r
67  * Out Mapping Variables:\r
68  *   @param - GENGV_vnf\r
69  *   @param - GENGV_SuccessIndicator\r
70  *   @param - GENGV_FoundIndicator\r
71  *   @param - WorkflowException\r
72  *\r
73  *\r
74  */\r
75 class GenericGetVnf extends AbstractServiceTaskProcessor{\r
76 \r
77         String Prefix = "GENGV_"\r
78         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
79 \r
80         /**\r
81          * This method validates the incoming variables and\r
82          * determines the subsequent event based on which\r
83          * variables the calling flow provided.\r
84          *\r
85          * @param - execution\r
86          */\r
87         public void preProcessRequest(Execution execution) {\r
88                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
89                 execution.setVariable("prefix",Prefix)\r
90                 utils.log("DEBUG", " *** STARTED GenericGetVnf PreProcessRequest Process*** ", isDebugEnabled)\r
91 \r
92                 execution.setVariable("GENGV_getVnfByName", false)\r
93                 execution.setVariable("GENGV_SuccessIndicator", false)\r
94                 execution.setVariable("GENGV_FoundIndicator", false)\r
95 \r
96                 try{\r
97                         // Get Variables\r
98                         String vnfId = execution.getVariable("GENGV_vnfId")\r
99                         utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)\r
100                         String vnfName = execution.getVariable("GENGV_vnfName")\r
101                         utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)\r
102 \r
103                         if(isBlank(vnfId) && isBlank(vnfName)){\r
104                                 utils.log("DEBUG", "Incoming Vnf Name and Vnf Id are null. At least one is required!", isDebugEnabled)\r
105                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Name and Vnf Id are null. At least one is required.")\r
106                         }else{\r
107                                 if(isBlank(vnfId)){\r
108                                         execution.setVariable("GENGV_getVnfByName", true)\r
109                                 }\r
110                         }\r
111 \r
112                 }catch(BpmnError b){\r
113                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
114                         throw b\r
115                 }catch(Exception e){\r
116                         utils.log("DEBUG", " Error encountered within GenericGetVnf PreProcessRequest method!" + e, isDebugEnabled)\r
117                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetVnf PreProcessRequest")\r
118 \r
119                 }\r
120                 utils.log("DEBUG", "*** COMPLETED GenericGetVnf PreProcessRequest Process ***", isDebugEnabled)\r
121         }\r
122 \r
123         /**\r
124          * This method executes a GET call to AAI to obtain the\r
125          * Vnf using the Vnf Name\r
126          *\r
127          * @param - execution\r
128          */\r
129         public void getVnfByName(Execution execution){\r
130                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
131                 execution.setVariable("prefix",Prefix)\r
132                 utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)\r
133                 try {\r
134                         String vnfName = execution.getVariable("GENGV_vnfName")\r
135                         utils.log("DEBUG", "Getting Vnf by Vnf Name: " + vnfName, isDebugEnabled)\r
136                         String type = execution.getVariable("GENGV_type")\r
137                         utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)\r
138 \r
139                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
140                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
141 \r
142                         //Determine Type of Vnf Querying For.\r
143                         def aai_uri = ""\r
144                         if(type.equals("generic-vnf")){\r
145                                 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
146                         }else if(type.equals("vce")){\r
147                                 aai_uri = aaiUriUtil.getNetworkVceUri(execution)\r
148                         }else{\r
149                                 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)\r
150                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")\r
151                         }\r
152 \r
153                         String getVnfPath = "${aai_endpoint}${aai_uri}?vnf-name=" + UriUtils.encode(vnfName, "UTF-8") + "&depth=1"\r
154 \r
155                         execution.setVariable("GENGV_getVnfPath", getVnfPath)\r
156                         utils.logAudit("Get Vnf Url is: " + getVnfPath)\r
157 \r
158                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)\r
159                         int responseCode = response.getStatusCode()\r
160                         execution.setVariable("GENGV_getVnfResponseCode", responseCode)\r
161                         utils.log("DEBUG", "  GET Vnf response code is: " + responseCode, isDebugEnabled)\r
162 \r
163                         String aaiResponse = response.getResponseBodyAsString()\r
164                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
165                         execution.setVariable("GENGV_getVnfResponse", aaiResponse)\r
166 \r
167                         //Process Response\r
168                         if(responseCode == 200){\r
169                                 utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)\r
170                                         if(utils.nodeExists(aaiResponse, type)){\r
171                                                 utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)\r
172                                                 execution.setVariable("GENGV_FoundIndicator", true)\r
173                                                 execution.setVariable("GENGV_vnf", aaiResponse)\r
174                                                 execution.setVariable("WorkflowResponse", aaiResponse)\r
175                                         }else{\r
176                                                 utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)\r
177                                         }\r
178 \r
179                         }else if(responseCode == 404){\r
180                                 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)\r
181                         }else{\r
182                                 utils.log("DEBUG", "GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)\r
183                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
184                                 throw new BpmnError("MSOWorkflowException")\r
185                         }\r
186                 }catch(BpmnError b){\r
187                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
188                         throw b\r
189                 }catch(Exception e){\r
190                         utils.log("ERROR", " Error encountered within GenericGetVnf GetVnfByName method!" + e, isDebugEnabled)\r
191                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfByName")\r
192                 }\r
193                 utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)\r
194         }\r
195 \r
196         /**\r
197          * This method executes a GET call to AAI to obtain the\r
198          * Vnf using the Vnf Id\r
199          *\r
200          * @param - execution\r
201          */\r
202         public void getVnfById(Execution execution){\r
203                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
204                 execution.setVariable("prefix",Prefix)\r
205                 utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)\r
206                 try {\r
207                         String vnfId = execution.getVariable("GENGV_vnfId")\r
208                         utils.log("DEBUG", "Getting Vnf by Vnf Id: " + vnfId, isDebugEnabled)\r
209                         String type = execution.getVariable("GENGV_type")\r
210                         utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)\r
211 \r
212                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
213                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
214 \r
215                         //Determine Type of Vnf Querying For.\r
216                         def aai_uri = ""\r
217                         if(type.equals("generic-vnf")){\r
218                                 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
219                         }else if(type.equals("vce")){\r
220                                 aai_uri = aaiUriUtil.getNetworkVceUri(execution)\r
221                         }else if(type.equals("vpe")){\r
222                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericGetVnf does not yet support getting type of vnf = vpe")\r
223                         }else{\r
224                                 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)\r
225                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")\r
226                         }\r
227                         utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)\r
228 \r
229                         String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"\r
230                         utils.log("DEBUG", "GET Vnf Endpoint is: " + getVnfPath, isDebugEnabled)\r
231 \r
232                         execution.setVariable("GENGV_getVnfPath", getVnfPath)\r
233                         utils.logAudit("Get Vnf Url is: " + getVnfPath)\r
234 \r
235                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)\r
236                         int responseCode = response.getStatusCode()\r
237                         execution.setVariable("GENGV_getVnfResponseCode", responseCode)\r
238                         utils.log("DEBUG", "  GET Vnf response code is: " + responseCode, isDebugEnabled)\r
239 \r
240                         String aaiResponse = response.getResponseBodyAsString()\r
241                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
242                         execution.setVariable("GENGV_getVnfResponse", aaiResponse)\r
243 \r
244                         //Process Response\r
245                         if(responseCode == 200){\r
246                                 utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)\r
247                                         if(utils.nodeExists(aaiResponse, type)){\r
248                                                 utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)\r
249                                                 execution.setVariable("GENGV_FoundIndicator", true)\r
250                                                 execution.setVariable("GENGV_vnf", aaiResponse)\r
251                                                 execution.setVariable("WorkflowResponse", aaiResponse)\r
252                                         }else{\r
253                                                 utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)\r
254                                         }\r
255 \r
256                         }else if(responseCode == 404){\r
257                                 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)\r
258                         }else{\r
259                                 utils.log("DEBUG", "GET Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)\r
260                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
261                                 throw new BpmnError("MSOWorkflowException")\r
262                         }\r
263                 }catch(BpmnError b){\r
264                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
265                         throw b\r
266                 }catch(Exception e){\r
267                         utils.log("ERROR", " Error encountered within GenericGetVnf GetVnfById method!" + e, isDebugEnabled)\r
268                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfById")\r
269                 }\r
270                 utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)\r
271         }\r
272 \r
273 }\r