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