Remove unnecessary use of Calendar.getInstance()
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / CreateVnfInfra.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 java.util.UUID;
24
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.runtime.Execution;
27 import static org.apache.commons.lang3.StringUtils.*;
28
29 import org.openecomp.mso.bpmn.core.WorkflowException
30 import org.openecomp.mso.bpmn.core.json.JsonUtils;
31
32
33 /**
34  * This class supports the CreateVnfInfra Flow
35  * with the creation of a generic vnf for
36  * infrastructure.
37  */
38 class CreateVnfInfra extends AbstractServiceTaskProcessor {
39
40         String Prefix="CREVI_"
41         ExceptionUtil exceptionUtil = new ExceptionUtil()
42         JsonUtils jsonUtil = new JsonUtils()
43         VidUtils vidUtils = new VidUtils(this)
44
45         /**
46          * This method gets and validates the incoming
47          * request.
48          *
49          * @param - execution
50          */
51         public void preProcessRequest(Execution execution) {
52                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
53                 execution.setVariable("prefix",Prefix)
54                 utils.log("DEBUG", " *** STARTED CreateVnfInfra PreProcessRequest Process*** ", isDebugEnabled)
55
56                 execution.setVariable("CREVI_sentSyncResponse", false)
57
58                 try{
59                         // Get Variables
60                         String createVnfRequest = execution.getVariable("bpmnRequest")
61                         execution.setVariable("CREVI_createVnfRequest", createVnfRequest)
62                         utils.logAudit("Incoming CreateVnfInfra Request is: \n" + createVnfRequest)
63
64                         if(createVnfRequest != null){
65
66                                 String requestId = execution.getVariable("att-mso-request-id")
67                                 execution.setVariable("CREVI_requestId", requestId)
68                                 utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
69
70                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
71                                 execution.setVariable("CREVI_serviceInstanceId", serviceInstanceId)
72                                 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
73
74                                 String vnfType = execution.getVariable("vnfType")
75                                 execution.setVariable("CREVI_vnfType", vnfType)
76                                 utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
77
78                                 String vnfName = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.instanceName")
79                                 execution.setVariable("CREVI_vnfName", vnfName)
80                                 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
81
82                                 String serviceId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.productFamilyId")
83                                 execution.setVariable("CREVI_serviceId", serviceId)
84                                 utils.log("DEBUG", "Incoming Service Id is: " + serviceId, isDebugEnabled)
85
86                                 String source = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.source")
87                                 execution.setVariable("CREVI_source", source)
88                                 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
89
90                                 String suppressRollback = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.suppressRollback")
91                                 execution.setVariable("CREVI_suppressRollback", suppressRollback)
92                                 utils.log("DEBUG", "Incoming Suppress Rollback is: " + suppressRollback, isDebugEnabled)
93
94                                 String modelInvariantId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo.modelInvariantId")
95                                 execution.setVariable("CREVI_modelInvariantId", modelInvariantId)
96                                 utils.log("DEBUG", "Incoming Invariant Id is: " + modelInvariantId, isDebugEnabled)
97
98                                 String modelVersion = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo.modelVersion")
99                                 execution.setVariable("CREVI_modelVersion", modelVersion)
100                                 utils.log("DEBUG", "Incoming Model Version is: " + modelVersion, isDebugEnabled)
101
102                                 //For Completion Handler & Fallout Handler
103                                 String requestInfo =
104                                 """<request-info xmlns="http://ecomp.att.com/mso/infra/vnf-request/v1">
105                                         <request-id>${requestId}</request-id>
106                                         <action>CREATE</action>
107                                         <source>${source}</source>
108                                    </request-info>"""
109
110                                 execution.setVariable("CREVI_requestInfo", requestInfo)
111
112                                 //TODO: Orch Status - TBD, will come from SDN-C Response in 1702
113                                 String orchStatus = "Created"
114                                 execution.setVariable("CREVI_orchStatus", orchStatus)
115
116                                 //TODO: Equipment Role - Should come from SDN-C Response in 1702
117                                 String equipmentRole = " "
118                                 execution.setVariable("CREVI_equipmentRole", equipmentRole)
119
120                                 String vnfId = execution.getVariable("testVnfId") // for junits
121                                 if(isBlank(vnfId)){
122                                         vnfId = UUID.randomUUID().toString()
123                                         utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
124                                 }
125                                 execution.setVariable("CREVI_vnfId", vnfId)
126
127                                 // Setting for Sub Flow Calls
128                                 execution.setVariable("CREVI_type", "generic-vnf")
129                                 execution.setVariable("GENGS_type", "service-instance")
130
131                         }else{
132                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
133                         }
134
135                 }catch(BpmnError b){
136                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
137                         throw b
138                 }catch(Exception e){
139                         utils.log("DEBUG", " Error Occured in CreateVnfInfra PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
140                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra PreProcessRequest")
141
142                 }
143                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PreProcessRequest Process ***", isDebugEnabled)
144         }
145
146         public void sendSyncResponse (Execution execution) {
147                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
148                 execution.setVariable("prefix",Prefix)
149
150                 utils.log("DEBUG", " *** STARTED CreateVnfInfra SendSyncResponse Process *** ", isDebugEnabled)
151
152                 try {
153                         String requestId = execution.getVariable("CREVI_requestId")
154                         String vnfId = execution.getVariable("CREVI_vnfId")
155
156                         String createVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
157
158                         utils.log("DEBUG", " CreateVnfInfra Sync Response is: \n"  + createVnfResponse, isDebugEnabled)
159
160                         sendWorkflowResponse(execution, 202, createVnfResponse)
161
162                         execution.setVariable("CREVI_sentSyncResponse", true)
163
164                 } catch (Exception ex) {
165                         utils.log("DEBUG", "Error Occured in CreateVnfInfra SendSyncResponse Process " + ex.getMessage(), isDebugEnabled)
166                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra SendSyncResponse Process")
167
168                 }
169                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra SendSyncResponse Process ***", isDebugEnabled)
170         }
171
172         public void prepareCreateGenericVnf (Execution execution) {
173                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
174                 execution.setVariable("prefix",Prefix)
175
176                 utils.log("DEBUG", " *** STARTED CreateVnfInfra PrepareCreateGenericVnf Process *** ", isDebugEnabled)
177                 try {
178                         //Get Vnf Info
179                         String vnfId = execution.getVariable("CREVI_vnfId")
180                         def vnfName = execution.getVariable("CREVI_vnfName")
181                         def vnfType = execution.getVariable("CREVI_vnfType")
182                         def serviceId = execution.getVariable("CREVI_serviceId")
183                         def orchStatus = execution.getVariable("CREVI_orchStatus")
184                         def modelInvariantId = execution.getVariable("CREVI_modelInvariantId")
185                         def modelVersion = execution.getVariable("CREVI_modelVersion")
186                         // TODO: 1702 Variable
187                         def equipmentRole = execution.getVariable("CREVI_equipmentRole")
188
189                         //Get Service Instance Info
190                         def serviceInstanceId = execution.getVariable("CREVI_serviceInstanceId")
191                         String siRelatedLink = execution.getVariable("GENGS_siResourceLink")
192
193                         int custStart = siRelatedLink.indexOf("customer/")
194                         int custEnd = siRelatedLink.indexOf("/service-subscriptions")
195                         String globalCustId = siRelatedLink.substring(custStart + 9, custEnd)
196                         int serviceStart = siRelatedLink.indexOf("service-subscription/")
197                         int serviceEnd = siRelatedLink.indexOf("/service-instances/")
198                         String serviceType = siRelatedLink.substring(serviceStart + 21, serviceEnd)
199
200                         //Get Namespace
201                         AaiUtil aaiUtil = new AaiUtil(this)
202                         def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
203                         String namespace = aaiUtil.getNamespaceFromUri(aai_uri)
204
205                         String payload =
206                                         """<generic-vnf xmlns="${namespace}">
207                                 <vnf-id>${vnfId}</vnf-id>
208                                 <vnf-name>${vnfName}</vnf-name>
209                                 <service-id>${serviceId}</service-id>
210                                 <vnf-type>${vnfType}</vnf-type>
211                                 <orchestration-status>${orchStatus}</orchestration-status>
212                                 <persona-model-id>${modelInvariantId}</persona-model-id>
213                                 <persona-model-version>${modelVersion}</persona-model-version>
214                                 <relationship-list>
215                                         <relationship>
216                         <related-to>service-instance</related-to>
217                         <related-link>${siRelatedLink}</related-link>
218                         <relationship-data>
219                                 <relationship-key>customer.global-customer-id</relationship-key>
220                                 <relationship-value>${globalCustId}</relationship-value>
221                         </relationship-data>
222                         <relationship-data>
223                                 <relationship-key>service-subscription.service-type</relationship-key>
224                                 <relationship-value>${serviceType}</relationship-value>
225                         </relationship-data>
226                                         <relationship-data>
227                                 <relationship-key>service-instance.service-instance-id</relationship-key>
228                                 <relationship-value>${serviceInstanceId}</relationship-value>
229                         </relationship-data>
230                         </relationship>
231                                 </relationship-list>
232                         </generic-vnf>"""
233
234                         execution.setVariable("CREVI_genericVnfPayload", payload)
235
236                 }catch(Exception ex) {
237                         utils.log("DEBUG", "Error Occured in CreateVnfInfra PrepareCreateGenericVnf Process " + ex.getMessage(), isDebugEnabled)
238                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra PrepareCreateGenericVnf Process")
239                 }
240                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCreateGenericVnf Process ***", isDebugEnabled)
241         }
242
243         public void prepareCompletionHandlerRequest(Execution execution){
244                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
245                 execution.setVariable("prefix",Prefix)
246
247                 utils.log("DEBUG", " *** STARTED CreateVnfInfra PrepareCompletionHandlerRequest Process *** ", isDebugEnabled)
248
249                 try {
250                         String requestInfo = execution.getVariable("CREVI_requestInfo")
251                         String vnfId = execution.getVariable("CREVI_vnfId")
252                         requestInfo = utils.removeXmlPreamble(requestInfo)
253
254                         String request =
255                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
256                                                                         xmlns:ns="http://ecomp.att.com/mso/request/types/v1">
257                                                         ${requestInfo}
258                                                         <status-message>Vnf has been created successfully.</status-message>
259                                                         <vnfId>${vnfId}</vnfId>
260                                                         <mso-bpel-name>CreateVnfInfra</mso-bpel-name>
261                                                 </aetgt:MsoCompletionRequest>"""
262
263                         execution.setVariable("CREVI_completionHandlerRequest", request)
264                         utils.log("DEBUG", "Completion Handler Request is: " + request, isDebugEnabled)
265
266                         execution.setVariable("WorkflowResponse", "Success") // for junits
267
268                 } catch (Exception ex) {
269                         utils.log("DEBUG", "Error Occured in CreateVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage(), isDebugEnabled)
270                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra PrepareCompletionHandlerRequest Process")
271
272                 }
273                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCompletionHandlerRequest Process ***", isDebugEnabled)
274         }
275
276         public void sendErrorResponse(Execution execution){
277                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
278                 execution.setVariable("prefix",Prefix)
279
280                 utils.log("DEBUG", " *** STARTED CreateVnfInfra sendErrorResponse Process *** ", isDebugEnabled)
281                 try {
282                         def sentSyncResponse = execution.getVariable("CREVI_sentSyncResponse")
283                         if(sentSyncResponse == false){
284                                 WorkflowException wfex = execution.getVariable("WorkflowException")
285                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
286
287                                 utils.logAudit(response)
288                                 sendWorkflowResponse(execution, 500, response)
289                         }else{
290                                 utils.log("DEBUG", "Not Sending Error Response.  Sync Response Already Sent", isDebugEnabled)
291                         }
292
293                 } catch (Exception ex) {
294                         utils.log("DEBUG", "Error Occured in CreateVnfInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
295                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra sendErrorResponse Process")
296
297                 }
298                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra sendErrorResponse Process ***", isDebugEnabled)
299         }
300
301         public void prepareFalloutRequest(Execution execution){
302                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
303                 execution.setVariable("prefix",Prefix)
304
305                 utils.log("DEBUG", " *** STARTED CreateVnfInfra prepareFalloutRequest Process *** ", isDebugEnabled)
306
307                 try {
308                         WorkflowException wfex = execution.getVariable("WorkflowException")
309                         utils.log("DEBUG", " Incoming Workflow Exception: " + wfex.toString(), isDebugEnabled)
310                         String requestInfo = execution.getVariable("CREVI_requestInfo")
311                         utils.log("DEBUG", " Incoming Request Info: " + requestInfo, isDebugEnabled)
312
313                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
314
315                         execution.setVariable("CREVI_falloutRequest", falloutRequest)
316
317
318                 } catch (Exception ex) {
319                         utils.log("DEBUG", "Error Occured in CreateVnfInfra prepareFalloutRequest Process " + ex.getMessage(), isDebugEnabled)
320                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra prepareFalloutRequest Process")
321
322                 }
323                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra prepareFalloutRequest Process ***", isDebugEnabled)
324         }
325
326 }