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