[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateVnf.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 package org.openecomp.mso.bpmn.infrastructure.scripts
21
22 import static org.apache.commons.lang3.StringUtils.*
23
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.runtime.Execution
26 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
27 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
29 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
30 import org.openecomp.mso.bpmn.common.scripts.VidUtils
31 import org.openecomp.mso.bpmn.core.WorkflowException
32 import org.openecomp.mso.bpmn.core.json.JsonUtils
33
34
35 /**
36  * This class supports the DoCreateVnf building block subflow
37  * with the creation of a generic vnf for
38  * infrastructure.
39  *
40  */
41 class DoCreateVnf extends AbstractServiceTaskProcessor {
42
43         String Prefix="DoCVNF_"
44         ExceptionUtil exceptionUtil = new ExceptionUtil()
45         JsonUtils jsonUtil = new JsonUtils()
46         VidUtils vidUtils = new VidUtils(this)
47
48         /**
49          * This method gets and validates the incoming
50          * request.
51          *
52          * @param - execution
53          *
54          */
55         public void preProcessRequest(Execution execution) {
56                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
57                 execution.setVariable("prefix",Prefix)
58                 utils.log("DEBUG", " *** STARTED DoCreateVnf PreProcessRequest Process*** ", isDebugEnabled)
59
60                 // DISABLE SDNC INTERACTION FOR NOW
61                 execution.setVariable("SDNCInteractionEnabled", false)
62
63
64                 /*******************/
65                 try{
66                         // Get Variables
67
68                         String cloudConfiguration = execution.getVariable("cloudConfiguration")
69                         String vnfModelInfo = execution.getVariable("vnfModelInfo")
70
71                         String requestId = execution.getVariable("requestId")
72                         execution.setVariable("DoCVNF_requestId", requestId)
73                         execution.setVariable("mso-request-id", requestId)
74                         utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
75
76                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
77                         execution.setVariable("DoCVNF_serviceInstanceId", serviceInstanceId)
78                         utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
79
80                         String vnfType = execution.getVariable("vnfType")
81                         execution.setVariable("DoCVNF_vnfType", vnfType)
82                         utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
83
84                         String vnfName = execution.getVariable("vnfName")
85                         execution.setVariable("DoCVNF_vnfName", vnfName)
86                         utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
87
88                         String serviceId = execution.getVariable("productFamilyId")
89                         execution.setVariable("DoCVNF_serviceId", serviceId)
90                         utils.log("DEBUG", "Incoming Service Id is: " + serviceId, isDebugEnabled)
91
92                         String source = "VID"
93                         execution.setVariable("DoCVNF_source", source)
94                         utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
95
96                         String suppressRollback = execution.getVariable("disableRollback")
97                         execution.setVariable("DoCVNF_suppressRollback", suppressRollback)
98                         utils.log("DEBUG", "Incoming Suppress Rollback is: " + suppressRollback, isDebugEnabled)
99
100                         String modelInvariantId = jsonUtil.getJsonValue(vnfModelInfo, "modelInvariantId")
101                         execution.setVariable("DoCVNF_modelInvariantId", modelInvariantId)
102                         utils.log("DEBUG", "Incoming Invariant Id is: " + modelInvariantId, isDebugEnabled)
103
104                         String modelVersionId = jsonUtil.getJsonValue(vnfModelInfo, "modelVersionId")
105                         if (modelVersionId == null) {
106                                 modelVersionId = ""
107                         }
108                         execution.setVariable("DoCVNF_modelVersionId", modelVersionId)
109                         utils.log("DEBUG", "Incoming Version Id is: " + modelVersionId, isDebugEnabled)
110
111                         String modelVersion = jsonUtil.getJsonValue(vnfModelInfo, "modelVersion")
112                         execution.setVariable("DoCVNF_modelVersion", modelVersion)
113                         utils.log("DEBUG", "Incoming Model Version is: " + modelVersion, isDebugEnabled)
114
115                         String modelName = jsonUtil.getJsonValue(vnfModelInfo, "modelName")
116                         execution.setVariable("DoCVNF_modelName", modelName)
117                         utils.log("DEBUG", "Incoming Model Name is: " + modelName, isDebugEnabled)
118
119                         String modelCustomizationId = jsonUtil.getJsonValue(vnfModelInfo, "modelCustomizationId")
120                         if (modelCustomizationId == null) {
121                                 modelCustomizationId = ""
122                         }
123                         execution.setVariable("DoCVNF_modelCustomizationId", modelCustomizationId)
124                         utils.log("DEBUG", "Incoming Model Customization Id is: " + modelCustomizationId, isDebugEnabled)
125
126                         String cloudSiteId = jsonUtil.getJsonValue(cloudConfiguration, "lcpCloudRegionId")
127                         execution.setVariable("DoCVNF_cloudSiteId", cloudSiteId)
128                         utils.log("DEBUG", "Incoming Cloud Site Id is: " + cloudSiteId, isDebugEnabled)
129
130                         String tenantId = jsonUtil.getJsonValue(cloudConfiguration, "tenantId")
131                         execution.setVariable("DoCVNF_tenantId", tenantId)
132                         utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId, isDebugEnabled)
133
134                         //For Completion Handler & Fallout Handler
135                         String requestInfo =
136                                 """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
137                                         <request-id>${requestId}</request-id>
138                                         <action>CREATE</action>
139                                         <source>${source}</source>
140                                    </request-info>"""
141
142                         execution.setVariable("DoCVNF_requestInfo", requestInfo)
143                         //TODO: Orch Status - TBD, will come from SDN-C Response in 1702
144                         String orchStatus = "Created"
145                         execution.setVariable("DoCVNF_orchStatus", orchStatus)
146
147                         //TODO: Equipment Role - Should come from SDN-C Response in 1702
148                         String equipmentRole = " "
149                         execution.setVariable("DoCVNF_equipmentRole", equipmentRole)
150                         String vnfId = execution.getVariable("testVnfId") // for junits
151                         if(isBlank(vnfId)){
152                                 vnfId = execution.getVariable("vnfId")
153                                 if (isBlank(vnfId)) {
154                                         vnfId = UUID.randomUUID().toString()
155                                         utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
156                                 }
157                         }
158                         execution.setVariable("DoCVNF_vnfId", vnfId)
159
160                         // Setting for Sub Flow Calls
161                         execution.setVariable("DoCVNF_type", "generic-vnf")
162                         execution.setVariable("GENGS_type", "service-instance")
163
164                         String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
165                         if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
166                                 def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
167                                 logError(msg)
168                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
169                         }
170                         execution.setVariable("DoCVNF_sdncCallbackUrl", sdncCallbackUrl)
171                         utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)
172                         logDebug("SDNC Callback URL is: " + sdncCallbackUrl, isDebugEnabled)
173
174                 }catch(BpmnError b){
175                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
176                         throw b
177                 }catch(Exception e){
178                         utils.log("DEBUG", " Error Occured in DoCreateVnf PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
179                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
180
181                 }
182                 utils.log("DEBUG", "*** COMPLETED DoCreateVnf PreProcessRequest Process ***", isDebugEnabled)
183         }
184
185
186         public void prepareCreateGenericVnf (Execution execution) {
187                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
188                 execution.setVariable("prefix",Prefix)
189
190                 utils.log("DEBUG", " *** STARTED DoCreateVnf PrepareCreateGenericVnf Process *** ", isDebugEnabled)
191                 try {
192                         //Get Vnf Info
193                         String vnfId = execution.getVariable("DoCVNF_vnfId")
194                         def vnfName = execution.getVariable("DoCVNF_vnfName")
195                         def vnfType = execution.getVariable("DoCVNF_vnfType")
196                         def serviceId = execution.getVariable("DoCVNF_serviceId")
197                         def orchStatus = execution.getVariable("DoCVNF_orchStatus")
198                         def modelInvariantId = execution.getVariable("DoCVNF_modelInvariantId")
199                         def modelVersion = execution.getVariable("DoCVNF_modelVersion")
200                         def modelCustomizationId = execution.getVariable("DoCVNF_modelCustomizationId")
201                         // TODO: 1702 Variable
202                         def equipmentRole = execution.getVariable("DoCVNF_equipmentRole")
203
204                         //Get Service Instance Info
205                         def serviceInstanceId = execution.getVariable("DoCVNF_serviceInstanceId")
206                         String siRelatedLink = execution.getVariable("GENGS_siResourceLink")
207
208                         int custStart = siRelatedLink.indexOf("customer/")
209                         int custEnd = siRelatedLink.indexOf("/service-subscriptions")
210                         String globalCustId = siRelatedLink.substring(custStart + 9, custEnd)
211                         int serviceStart = siRelatedLink.indexOf("service-subscription/")
212                         int serviceEnd = siRelatedLink.indexOf("/service-instances/")
213                         String serviceType = siRelatedLink.substring(serviceStart + 21, serviceEnd)
214
215                         //Get Namespace
216                         AaiUtil aaiUtil = new AaiUtil(this)
217                         def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
218                         String namespace = aaiUtil.getNamespaceFromUri(aai_uri)
219
220                         String payload =
221                                         """<generic-vnf xmlns="${namespace}">
222                                 <vnf-id>${vnfId}</vnf-id>
223                                 <vnf-name>${vnfName}</vnf-name>
224                                 <service-id>${serviceId}</service-id>
225                                 <vnf-type>${vnfType}</vnf-type>
226                                 <orchestration-status>${orchStatus}</orchestration-status>
227                                 <persona-model-id>${modelInvariantId}</persona-model-id>
228                                 <persona-model-version>${modelVersion}</persona-model-version>
229                                 <persona-model-customization-id>${modelCustomizationId}</persona-model-customization-id>
230                                 <relationship-list>
231                                         <relationship>
232                         <related-to>service-instance</related-to>
233                         <related-link>${siRelatedLink}</related-link>
234                         <relationship-data>
235                                 <relationship-key>customer.global-customer-id</relationship-key>
236                                 <relationship-value>${globalCustId}</relationship-value>
237                         </relationship-data>
238                         <relationship-data>
239                                 <relationship-key>service-subscription.service-type</relationship-key>
240                                 <relationship-value>${serviceType}</relationship-value>
241                         </relationship-data>
242                                         <relationship-data>
243                                 <relationship-key>service-instance.service-instance-id</relationship-key>
244                                 <relationship-value>${serviceInstanceId}</relationship-value>
245                         </relationship-data>
246                         </relationship>
247                                 </relationship-list>
248                         </generic-vnf>"""
249
250                         execution.setVariable("DoCVNF_genericVnfPayload", payload)
251
252                 }catch(Exception ex) {
253                         utils.log("DEBUG", "Error Occured in DoCreateVnf PrepareCreateGenericVnf Process " + ex.getMessage(), isDebugEnabled)
254                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PrepareCreateGenericVnf Process")
255                 }
256                 utils.log("DEBUG", "*** COMPLETED DoCreateVnf PrepareCreateGenericVnf Process ***", isDebugEnabled)
257         }
258
259         public void preProcessSDNCAssignRequest(Execution execution){
260                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
261                 execution.setVariable("prefix", Prefix)
262                 logDebug(" ======== STARTED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
263                 def vnfId = execution.getVariable("DoCVNF_vnfId")
264                 def serviceInstanceId = execution.getVariable("DoCVNF_serviceInstanceId")
265                 logDebug("NEW VNF ID: " + vnfId, isDebugLogEnabled)
266                 utils.logAudit("NEW VNF ID: " + vnfId)
267
268                 try{
269                         //Build SDNC Request
270
271                         String assignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "assign")
272
273                         assignSDNCRequest = utils.formatXml(assignSDNCRequest)
274                         execution.setVariable("DoCVNF_assignSDNCRequest", assignSDNCRequest)
275                         logDebug("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest, isDebugLogEnabled)
276                         utils.logAudit("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest)
277
278                 }catch(Exception e){
279                         utils.log("ERROR", "Exception Occured Processing preProcessSDNCAssignRequest. Exception is:\n" + e, isDebugLogEnabled)
280                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareProvision Method:\n" + e.getMessage())
281                 }
282                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
283         }
284
285         public void preProcessSDNCActivateRequest(Execution execution) {
286                 def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
287                         'execution=' + execution.getId() +
288                         ')'
289                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
290                 logDebug('Entered ' + method, isDebugLogEnabled)
291                 execution.setVariable("prefix", Prefix)
292                 logDebug(" ======== STARTED preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
293                 try{
294                         String vnfId = execution.getVariable("DoCVNF_vnfId")
295                         String serviceInstanceId = execution.getVariable("DoCVNF_serviceInstanceId")
296
297                         String activateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "activate")
298
299                         execution.setVariable("DoCVNF_activateSDNCRequest", activateSDNCRequest)
300                         logDebug("Outgoing CommitSDNCRequest is: \n" + activateSDNCRequest, isDebugLogEnabled)
301                         utils.logAudit("Outgoing CommitSDNCRequest is: \n"  + activateSDNCRequest)
302
303                 }catch(Exception e){
304                         log.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e, isDebugLogEnabled)
305                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
306                 }
307                 logDebug("======== COMPLETED  preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
308         }
309
310         public String buildSDNCRequest(Execution execution, String svcInstId, String action){
311
312                                 String uuid = execution.getVariable('testReqId') // for junits
313                                 if(uuid==null){
314                                         uuid = execution.getVariable("mso-request-id") + "-" +          System.currentTimeMillis()
315                                 }
316                                 def callbackURL = execution.getVariable("DoCVNF_sdncCallbackUrl")
317                                 def requestId = execution.getVariable("DoCVNF_requestId")
318                                 def serviceId = execution.getVariable("DoCVNF_serviceId")
319                                 def vnfType = execution.getVariable("DoCVNF_vnfType")
320                                 def vnfName = execution.getVariable("DoCVNF_vnfName")
321                                 def tenantId = execution.getVariable("DoCVNF_tenantId")
322                                 def source = execution.getVariable("DoCVNF_source")
323                                 def vnfId = execution.getVariable("DoCVNF_vnfId")
324                                 def cloudSiteId = execution.getVariable("DoCVNF_cloudSiteId")
325                                 def modelInvariantId = execution.getVariable("DoCVNF_modelInvariantId")
326                                 def modelVersionId = execution.getVariable("DoCVNF_modelVersionId")
327                                 def modelVersion = execution.getVariable("DoCVNF_modelVersion")
328                                 def modelName = execution.getVariable("DoCVNF_modelName")
329
330                                 String sdncVNFParamsXml = ""
331
332                                 if(execution.getVariable("DoCVNF_vnfParamsExistFlag") == true){
333                                         sdncVNFParamsXml = buildSDNCParamsXml(execution)
334                                 }else{
335                                         sdncVNFParamsXml = ""
336                                 }
337
338                                 String sdncRequest =
339                                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
340                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
341                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
342            <sdncadapter:RequestHeader>
343                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
344                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
345                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
346                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
347                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
348                 </sdncadapter:RequestHeader>
349         <sdncadapterworkflow:SDNCRequestData>
350                 <request-information>
351                         <request-id>${requestId}</request-id>
352                         <request-action>VNFActivateRequest</request-action>
353                         <source>${source}</source>
354                         <notification-url/>
355                         <order-number/>
356                         <order-version/>
357                 </request-information>
358                 <service-information>
359                         <service-id>${serviceId}</service-id>
360                         <service-type>${serviceId}</service-type>
361                         <service-instance-id>${svcInstId}</service-instance-id>
362                         <subscriber-name>notsurewecare</subscriber-name>
363                 </service-information>
364                 <vnf-information>
365                         <vnf-id>${vnfId}</vnf-id>
366                         <vnf-type>${vnfType}</vnf-type>
367                         <ecomp-model-information>
368                                                  <model-invariant-uuid>${modelInvariantId}</model-invariant-uuid>
369                                                  <model-uuid>${modelVersionId}</model-uuid>
370                                                  <model-version>${modelVersion}</model-version>
371                                                  <model-name>${modelName}</model-name>
372                         </ecomp-model-information>
373                 </vnf-information>
374                 <vnf-request-information>               
375                         <vnf-name>${vnfName}</vnf-name>                 
376                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
377                         <tenant>${tenantId}</tenant>
378                 ${sdncVNFParamsXml}
379                 </vnf-request-information>
380         </sdncadapterworkflow:SDNCRequestData>
381         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
382
383                         utils.logAudit("sdncRequest:  " + sdncRequest)
384                         return sdncRequest
385         }
386
387         public void validateSDNCResponse(Execution execution, String response, String method){
388                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
389                 execution.setVariable("prefix",Prefix)
390                 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
391
392                 WorkflowException workflowException = execution.getVariable("WorkflowException")
393                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
394
395                 utils.logAudit("workflowException: " + workflowException)
396
397                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
398                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
399
400                 utils.logAudit("SDNCResponse: " + response)
401
402                 String sdncResponse = response
403                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
404                         logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)
405
406                 }else{
407                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
408                         throw new BpmnError("MSOWorkflowException")
409                 }
410                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
411         }
412
413
414 }