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