Groovy scripts header correction
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericPutVnf.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.apache.commons.lang3.StringUtils.*\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  * TODO: Support Putting vnf type = vpe and vce\r
33  *\r
34  * This class supports the GenericPutVnf Sub Flow.\r
35  * This Generic sub flow can be used by any flow for accomplishing\r
36  * the goal of Creating/Updating(PUT) a Vnf Object (in AAI).  The flow\r
37  * supports the Creating/Updating of 3 types of Vnfs (generic-vnf, vce, and vpe).\r
38  * The "type" must be provided by the calling flow and this type should\r
39  * be mapped to the variable GENPV_type. The type should either be\r
40  * "generic-vnf", "vce", or "vpe".  In addition, the Vnf Id and\r
41  * payload should be provided.\r
42  *\r
43  * Upon successful completion of this sub flow the\r
44  * GENPV_SuccessIndicator.  An MSOWorkflowException will\r
45  * be thrown if an error occurs at any time during this\r
46  * sub flow. Please map input variables to the corresponding\r
47  * variable names below.\r
48  *\r
49  *\r
50  * Incoming Required Variables:\r
51  * @param - GENPV_vnfId\r
52  * @param - GENPV_vnfPayload\r
53  * @param - GENPV_type\r
54  *\r
55  *\r
56  * Outgoing Variables:\r
57  * @param - GENPV_SuccessIndicator\r
58  * @param - WorkflowException\r
59  */\r
60 class GenericPutVnf  extends AbstractServiceTaskProcessor{\r
61 \r
62         String Prefix = "GENPV_"\r
63         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
64 \r
65         /**\r
66          * This method validates the incoming variables and\r
67          * generates a Vnf Id if one is not provided.\r
68          *\r
69          * @param - execution\r
70          */\r
71         public void preProcessRequest(Execution execution) {\r
72                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
73                 execution.setVariable("prefix",Prefix)\r
74                 utils.log("DEBUG", " *** STARTED GenericPutVnf PreProcessRequest Process*** ", isDebugEnabled)\r
75 \r
76                 execution.setVariable("GENPV_SuccessIndicator", false)\r
77                 execution.setVariable("GENPV_FoundIndicator", false)\r
78 \r
79                 try{\r
80                         // Get Variables\r
81                         String payload = execution.getVariable("GENPV_vnfPayload")\r
82                         utils.log("DEBUG", "Incoming Vnf Payload is: " + payload, isDebugEnabled)\r
83                         String type = execution.getVariable("GENPV_type")\r
84                         utils.log("DEBUG", "Incoming Type of Vnf is: " + type, isDebugEnabled)\r
85 \r
86                         if(isBlank(payload) || isBlank(type)){\r
87                                 utils.log("ERROR", "Incoming Vnf Payload and/or Type is null. These Variables are required!", isDebugEnabled)\r
88                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Payload and/or Type is null. These Variables are required!")\r
89                         }else{\r
90                                 String vnfId = execution.getVariable("GENPV_vnfId")\r
91                                 if(isBlank(vnfId)){\r
92                                         vnfId = UUID.randomUUID().toString()\r
93                                         utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)\r
94                                         execution.setVariable("GENPV_vnfId", vnfId)\r
95                                 }else{\r
96                                         utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)\r
97                                 }\r
98                         }\r
99                 }catch(BpmnError b){\r
100                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
101                         throw b\r
102                 }catch(Exception e){\r
103                         utils.log("ERROR", " Error encountered within GenericPutVnf PreProcessRequest method!" + e, isDebugEnabled)\r
104                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericPutVnf PreProcessRequest")\r
105 \r
106                 }\r
107                 utils.log("DEBUG", "*** COMPLETED GenericPutVnf PreProcessRequest Process ***", isDebugEnabled)\r
108         }\r
109 \r
110         /**\r
111          * This method executes a Put call to AAI to create\r
112          * or update a Vnf Object using the provided payload\r
113          *\r
114          * @param - execution\r
115          */\r
116         public void putVnf(Execution execution){\r
117                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
118                 execution.setVariable("prefix",Prefix)\r
119                 utils.log("DEBUG", " *** STARTED GenericPutVnf PutVnf Process*** ", isDebugEnabled)\r
120                 try {\r
121                         String vnfId = execution.getVariable("GENPV_vnfId")\r
122                         String payload = execution.getVariable("GENPV_vnfPayload")\r
123                         String type = execution.getVariable("GENPV_type")\r
124 \r
125                         AaiUtil aaiUtil = new AaiUtil(this)\r
126                         def aai_uri = ""\r
127                         if(type.equals("generic-vnf")){\r
128                                 aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)\r
129                         }else if(type.equals("vce")){\r
130                                 aai_uri = aaiUtil.getNetworkVceUri(execution)\r
131                         }else if(type.equals("vpe")){\r
132                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericPutVnf does not yet support getting type of vnf = vpe")\r
133                         }else{\r
134                                 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)\r
135                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENPV_type")\r
136                         }\r
137                         utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)\r
138 \r
139                         String path = "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")\r
140                         utils.log("DEBUG", "PUT Vnf Endpoint is: " + path, isDebugEnabled)\r
141 \r
142                         String putVnfAAIPath = execution.getVariable("URN_aai_endpoint") + path\r
143                         execution.setVariable("GENPV_putVnfAAIPath", putVnfAAIPath)\r
144                         utils.logAudit("PUT Vnf Url is: " + putVnfAAIPath)\r
145 \r
146                         APIResponse apiResponse = aaiUtil.executeAAIPutCall(execution, putVnfAAIPath, payload)\r
147                         int responseCode = apiResponse.getStatusCode()\r
148                         execution.setVariable("GENPV_putVnfResponseCode", responseCode)\r
149                         utils.logAudit("AAI Response Code: " + responseCode)\r
150                         String aaiResponse = apiResponse.getResponseBodyAsString()\r
151                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
152                         execution.setVariable("GENPV_putVnfResponse", aaiResponse)\r
153                         utils.logAudit("AAI Response: " + aaiResponse)\r
154 \r
155                         if(responseCode == 200 || responseCode == 201){\r
156                                 utils.log("DEBUG", "PUT Vnf Received a Good Response Code.", isDebugEnabled)\r
157                         }else{\r
158                                 utils.log("DEBUG", "PUT Vnf Received a Bad Response Code. Response Code is: " + responseCode, isDebugEnabled)\r
159                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
160                                 throw new BpmnError("MSOWorkflowException")\r
161                         }\r
162                 }catch(BpmnError b){\r
163                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
164                         throw b\r
165                 }catch(Exception e){\r
166                         utils.log("ERROR", " Error encountered within GenericPutVnf PutVnf method!" + e, isDebugEnabled)\r
167                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During PutVnf")\r
168                 }\r
169                 utils.log("DEBUG", " *** COMPLETED GenericPutVnf PutVnf Process*** ", isDebugEnabled)\r
170         }\r
171 \r
172 }\r