Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoUpdateE2EServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.so.bpmn.infrastructure.scripts;
21
22 import static org.apache.commons.lang3.StringUtils.*;
23
24 import org.apache.commons.lang3.*
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.so.bpmn.common.scripts.AaiUtil
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.common.scripts.MsoUtils
31 import org.onap.so.bpmn.core.RollbackData
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.bpmn.core.domain.Resource
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.springframework.web.util.UriUtils;
36
37 import groovy.json.*
38
39
40 /**
41  * This groovy class supports the <class>DoUpdateE2EServiceInstance.bpmn</class> process.
42  *
43  * Inputs:
44  * @param - msoRequestId
45  * @param - globalSubscriberId
46  * @param - serviceType
47  * @param - serviceInstanceId
48  * @param - serviceInstanceName
49  * @param - serviceModelInfo
50  * @param - productFamilyId
51  * @param - uuiRequest
52  * @param - serviceDecomposition_Target
53  * @param - serviceDecomposition_Original
54  * @param - addResourceList
55  * @param - delResourceList
56  * 
57  * Outputs:
58  * @param - rollbackData (localRB->null)
59  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
60  * @param - WorkflowException
61  */
62 public class DoUpdateE2EServiceInstance extends AbstractServiceTaskProcessor {
63
64         String Prefix="DUPDSI_"
65         private static final String DebugFlag = "isDebugEnabled"
66         
67         ExceptionUtil exceptionUtil = new ExceptionUtil()
68         JsonUtils jsonUtil = new JsonUtils()
69
70         public void preProcessRequest (DelegateExecution execution) {
71                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
72                 utils.log("INFO"," ***** Enter DoUpdateE2EServiceInstance preProcessRequest *****",  isDebugEnabled)
73                 
74                 String msg = "" 
75
76                 try {
77                         execution.setVariable("prefix", Prefix)
78                         //Inputs
79                         //for AAI GET & PUT & SDNC assignToplology
80                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
81                         utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)
82                         
83                         //for AAI PUT & SDNC assignTopology
84                         String serviceType = execution.getVariable("serviceType")
85                         utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)
86                         
87                         //for SDNC assignTopology
88                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
89
90                         if (isBlank(globalSubscriberId)) {
91                                 msg = "Input globalSubscriberId is null"
92                                 utils.log("INFO", msg, isDebugEnabled)
93                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
94                         }
95                         
96                         if (isBlank(serviceType)) {
97                                 msg = "Input serviceType is null"
98                                 utils.log("INFO", msg, isDebugEnabled)
99                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100                         }
101                         
102                         //Generated in parent for AAI 
103                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
104                         if (isBlank(serviceInstanceId)){
105                                 msg = "Input serviceInstanceId is null"
106                                 utils.log("INFO", msg, isDebugEnabled)
107                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
108                         }
109
110                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
111                         
112                         // user params
113                         String uuiRequest = execution.getVariable("uuiRequest")
114             
115                         // target model Invariant uuid
116                         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
117                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)            
118                         utils.log("INFO", "modelInvariantUuid: " + modelInvariantUuid, isDebugEnabled)            
119             
120                         // target model uuid
121                         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
122                         execution.setVariable("modelUuid", modelUuid)
123                         
124                         utils.log("INFO","modelUuid: " + modelUuid, isDebugEnabled)
125                                 
126                 } catch (BpmnError e) {
127                         throw e;
128                 } catch (Exception ex){
129                         msg = "Exception in preProcessRequest " + ex.getMessage()
130                         utils.log("INFO", msg, isDebugEnabled)
131                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
132                 }
133                 utils.log("INFO", "======== COMPLETED preProcessRequest Process ======== ", isDebugEnabled)  
134         }
135
136         
137         public void preInitResourcesOperStatus(DelegateExecution execution){
138         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
139
140         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
141         try{
142             String serviceId = execution.getVariable("serviceInstanceId")
143             String operationId = execution.getVariable("operationId")
144             String operationType = execution.getVariable("operationType")
145             String resourceTemplateUUIDs = ""
146             String result = "processing"
147             String progress = "0"
148             String reason = ""
149             String operationContent = "Prepare service updating"
150             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
151             serviceId = UriUtils.encode(serviceId,"UTF-8")
152             execution.setVariable("serviceInstanceId", serviceId)
153             execution.setVariable("operationId", operationId)
154             execution.setVariable("operationType", operationType)
155
156                         List<Resource> resourceList = new ArrayList<String>()
157                         List<Resource> addResourceList =  execution.getVariable("addResourceList")
158                         List<Resource> delResourceList =  execution.getVariable("delResourceList")
159                         resourceList.addAll(addResourceList)
160                         resourceList.addAll(delResourceList)
161                         for(Resource resource : resourceList){
162                                 resourceTemplateUUIDs  = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"
163                         }
164                         
165                         def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
166                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
167                         utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
168
169                         String payload =
170                                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
171                         xmlns:ns="http://org.onap.so/requestsdb">
172                         <soapenv:Header/>
173                         <soapenv:Body>
174                             <ns:initResourceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
175                                                                 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
176                                                                 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
177                                                                 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
178                                                                 <resourceTemplateUUIDs>${MsoUtils.xmlEscape(resourceTemplateUUIDs)}</resourceTemplateUUIDs>
179                             </ns:initResourceOperationStatus>
180                         </soapenv:Body>
181                         </soapenv:Envelope>"""
182
183                         payload = utils.formatXml(payload)
184                         execution.setVariable("CVFMI_initResOperStatusRequest", payload)
185                         utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
186                         utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
187
188         }catch(Exception e){
189             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
190             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
191         }
192         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)  
193     }
194     
195
196     public void preProcessForAddResource(DelegateExecution execution) {
197         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
198                 utils.log("INFO"," ***** preProcessForAddResource ***** ", isDebugEnabled)
199                 
200             execution.setVariable("operationType", "create")
201                 
202                 execution.setVariable("hasResourcetoAdd", false)
203                 List<Resource> addResourceList =  execution.getVariable("addResourceList")
204                 if(addResourceList != null && !addResourceList.isEmpty()) {
205                         execution.setVariable("hasResourcetoAdd", true)                 
206                 }
207         
208                 utils.log("INFO"," *** Exit preProcessForAddResource *** ", isDebugEnabled)
209     }
210
211     public void postProcessForAddResource(DelegateExecution execution) {
212         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
213                 utils.log("INFO"," ***** postProcessForAddResource ***** ", isDebugEnabled)
214                 
215                 execution.setVariable("operationType", "update")
216
217                 utils.log("INFO"," *** Exit postProcessForAddResource *** ", isDebugEnabled)
218     }
219     
220         public void preProcessForDeleteResource(DelegateExecution execution) {
221                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
222                 utils.log("INFO"," ***** preProcessForDeleteResource ***** ", isDebugEnabled)
223
224                 execution.setVariable("operationType", "delete")
225
226                 def  hasResourcetoDelete = false
227                 List<Resource> delResourceList =  execution.getVariable("delResourceList")
228                 if(delResourceList != null && !delResourceList.isEmpty()) {
229                         hasResourcetoDelete = true
230                 }
231                 execution.setVariable("hasResourcetoDelete", hasResourcetoDelete)
232
233                 if(hasResourcetoDelete) {
234                         def jsonSlurper = new JsonSlurper()
235                         String serviceRelationShip = execution.getVariable("serviceRelationShip")                       
236                         List relationShipList =  jsonSlurper.parseText(serviceRelationShip)
237
238                         //Set the real resource instance id to the decomosed resource list
239                         for(Resource resource: delResourceList){
240                                 //reset the resource instance id , because in the decompose flow ,its a random one.
241                                 resource.setResourceId("");
242                                 //match the resource-instance-name and the model name
243                                 if (relationShipList != null) {
244                                         relationShipList.each {
245                                                 if(StringUtils.containsIgnoreCase(it.resourceType, resource.getModelInfo().getModelName())){
246                                                         resource.setResourceId(it.resourceInstanceId);
247                                                 }
248                                         }
249                                 }
250                         }
251                 }
252                 
253                 execution.setVariable("deleteResourceList", delResourceList)
254
255                 utils.log("INFO"," *** Exit preProcessForDeleteResource *** ", isDebugEnabled)
256         }
257
258     public void postProcessForDeleteResource(DelegateExecution execution) {
259         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
260                 utils.log("INFO"," ***** postProcessForDeleteResource ***** ", isDebugEnabled)
261                 
262                 execution.setVariable("operationType", "update")
263
264                 utils.log("INFO"," *** Exit postProcessForDeleteResource *** ", isDebugEnabled)
265     } 
266     
267         public void preProcessAAIGET(DelegateExecution execution) {
268         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
269         }
270         
271         public void postProcessAAIGET(DelegateExecution execution) {
272                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
273                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
274                 String msg = ""
275
276                 try {
277                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
278                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
279                         if(!succInAAI){
280                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + serviceInstanceName, isDebugEnabled)
281                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
282                                 utils.logAudit("workflowException: " + workflowException)
283                                 if(workflowException != null){
284                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
285                                 }
286                                 else
287                                 {
288                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
289                                         utils.log("INFO", msg, isDebugEnabled)
290                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
291                                 }
292                         }
293                         else
294                         {
295                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
296                                 if(foundInAAI){
297                                         String aaiService = execution.getVariable("GENGS_service")
298                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "resource-version"))) {
299                                                 execution.setVariable("serviceInstanceVersion",  utils.getNodeText(aaiService, "resource-version"))
300                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
301                                         }
302                                 }
303                         }
304                 } catch (BpmnError e) {
305                         throw e;
306                 } catch (Exception ex) {
307                         msg = "Exception in DoUpdateE2EServiceInstance.postProcessAAIGET " + ex.getMessage()
308                         utils.log("INFO", msg, isDebugEnabled)
309                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
310                 }
311                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
312         }
313
314         public void preProcessAAIPUT(DelegateExecution execution) {             
315                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
316                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
317                 utils.log("INFO","Entered " + method, isDebugEnabled)
318                 String msg = ""
319                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)
320
321
322                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion")
323                 //execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
324         
325                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
326                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
327                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
328                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
329                 //aai serviceType and Role can be setted as fixed value now.
330                 String aaiServiceType = "E2E Service"
331                 String aaiServiceRole = "E2E Service"
332                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
333                 String modelUuid = execution.getVariable("modelUuid")
334
335
336                 AaiUtil aaiUriUtil = new AaiUtil(this)
337                 utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled) 
338                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
339                 utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)
340                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
341                 utils.log("INFO","namespace: " + namespace, isDebugEnabled)
342
343                 //update target model to aai
344                 String serviceInstanceData =
345                                 """<service-instance xmlns=\"${namespace}\">
346                     <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
347                     <service-instance-name>${MsoUtils.xmlEscape(serviceInstanceName)}</service-instance-name>
348                     <service-type>${MsoUtils.xmlEscape(aaiServiceType)}</service-type>
349                     <service-role>${MsoUtils.xmlEscape(aaiServiceRole)}</service-role>
350                     <resource-version>${MsoUtils.xmlEscape(serviceInstanceVersion)}</resource-version>
351                     <model-invariant-id>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-id>
352                     <model-version-id>${MsoUtils.xmlEscape(modelUuid)}</model-version-id>                    
353                                  </service-instance>""".trim()
354
355                 execution.setVariable("serviceInstanceData", serviceInstanceData)
356                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)
357                 utils.logAudit(serviceInstanceData)
358                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
359                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
360         
361                 utils.log("INFO", "Exited " + method, isDebugEnabled)
362         }       
363         
364         public void postProcessAAIPUT(DelegateExecution execution) {
365                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
366                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
367                 String msg = ""
368                 try {
369                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
370                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
371                         if(!succInAAI){
372                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
373                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
374                                 utils.logAudit("workflowException: " + workflowException)
375                                 if(workflowException != null){
376                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
377                                 }
378                         }
379                         else
380                         {
381                                 //start rollback set up
382                                 RollbackData rollbackData = new RollbackData()
383                                 def disableRollback = execution.getVariable("disableRollback")
384                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
385                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
386                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
387                                 rollbackData.put("SERVICEINSTANCE", "serviceType", execution.getVariable("serviceType"))
388                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
389                                 execution.setVariable("rollbackData", rollbackData)
390                         }
391
392                 } catch (BpmnError e) {
393                         throw e;
394                 } catch (Exception ex) {
395                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
396                         utils.log("INFO", msg, isDebugEnabled)
397                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
398                 }
399                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
400         }       
401
402         public void preProcessRollback (DelegateExecution execution) {
403                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
404                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)
405                 try {
406                         
407                         Object workflowException = execution.getVariable("WorkflowException");
408
409                         if (workflowException instanceof WorkflowException) {
410                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
411                                 execution.setVariable("prevWorkflowException", workflowException);
412                                 //execution.setVariable("WorkflowException", null);
413                         }
414                 } catch (BpmnError e) {
415                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)
416                 } catch(Exception ex) {
417                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
418                         utils.log("INFO", msg, isDebugEnabled)
419                 }
420                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)
421         }
422
423         public void postProcessRollback (DelegateExecution execution) {
424                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
425                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)
426                 String msg = ""
427                 try {
428                         Object workflowException = execution.getVariable("prevWorkflowException");
429                         if (workflowException instanceof WorkflowException) {
430                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)
431                                 execution.setVariable("WorkflowException", workflowException);
432                         }
433                         execution.setVariable("rollbackData", null)
434                 } catch (BpmnError b) {
435                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)
436                         throw b;
437                 } catch(Exception ex) {
438                         msg = "Exception in postProcessRollback. " + ex.getMessage()
439                         utils.log("INFO", msg, isDebugEnabled)
440                 }
441                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)
442         }
443
444         
445         public void postConfigRequest(execution){
446             //now do noting
447         }
448
449         
450 }
451