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