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