cb50fbbee64891419925bb3cacb5865b43ec4f0f
[so.git] / bpmn / so-bpmn-infrastructure-common / 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 preProcessAAIPUT(DelegateExecution execution) {
268                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
269                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
270                 utils.log("INFO","Entered " + method, isDebugEnabled)
271                 String msg = ""
272                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)
273
274
275                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion")
276                 //execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
277
278                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
279                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
280                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
281                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
282                 //aai serviceType and Role can be setted as fixed value now.
283                 String aaiServiceType = "E2E Service"
284                 String aaiServiceRole = "E2E Service"
285                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
286                 String modelUuid = execution.getVariable("modelUuid")
287
288
289                 AaiUtil aaiUriUtil = new AaiUtil(this)
290                 utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled)
291                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
292                 utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)
293                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
294                 utils.log("INFO","namespace: " + namespace, isDebugEnabled)
295
296                 //update target model to aai
297                 String serviceInstanceData =
298                                 """<service-instance xmlns=\"${namespace}\">
299                     <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
300                     <service-instance-name>${MsoUtils.xmlEscape(serviceInstanceName)}</service-instance-name>
301                     <service-type>${MsoUtils.xmlEscape(aaiServiceType)}</service-type>
302                     <service-role>${MsoUtils.xmlEscape(aaiServiceRole)}</service-role>
303                     <resource-version>${MsoUtils.xmlEscape(serviceInstanceVersion)}</resource-version>
304                     <model-invariant-id>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-id>
305                     <model-version-id>${MsoUtils.xmlEscape(modelUuid)}</model-version-id>
306                                  </service-instance>""".trim()
307
308                 execution.setVariable("serviceInstanceData", serviceInstanceData)
309                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)
310                 utils.logAudit(serviceInstanceData)
311                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
312                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
313
314                 utils.log("INFO", "Exited " + method, isDebugEnabled)
315         }
316
317         public void postProcessAAIPUT(DelegateExecution execution) {
318                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
319                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
320                 String msg = ""
321                 try {
322                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
323                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
324                         if(!succInAAI){
325                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
326                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
327                                 utils.logAudit("workflowException: " + workflowException)
328                                 if(workflowException != null){
329                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
330                                 }
331                         }
332                         else
333                         {
334                                 //start rollback set up
335                                 RollbackData rollbackData = new RollbackData()
336                                 def disableRollback = execution.getVariable("disableRollback")
337                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
338                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
339                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
340                                 rollbackData.put("SERVICEINSTANCE", "serviceType", execution.getVariable("serviceType"))
341                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
342                                 execution.setVariable("rollbackData", rollbackData)
343                         }
344
345                 } catch (BpmnError e) {
346                         throw e;
347                 } catch (Exception ex) {
348                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
349                         utils.log("INFO", msg, isDebugEnabled)
350                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
351                 }
352                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
353         }
354
355         public void preProcessRollback (DelegateExecution execution) {
356                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
357                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)
358                 try {
359
360                         Object workflowException = execution.getVariable("WorkflowException");
361
362                         if (workflowException instanceof WorkflowException) {
363                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
364                                 execution.setVariable("prevWorkflowException", workflowException);
365                                 //execution.setVariable("WorkflowException", null);
366                         }
367                 } catch (BpmnError e) {
368                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)
369                 } catch(Exception ex) {
370                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
371                         utils.log("INFO", msg, isDebugEnabled)
372                 }
373                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)
374         }
375
376         public void postProcessRollback (DelegateExecution execution) {
377                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
378                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)
379                 String msg = ""
380                 try {
381                         Object workflowException = execution.getVariable("prevWorkflowException");
382                         if (workflowException instanceof WorkflowException) {
383                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)
384                                 execution.setVariable("WorkflowException", workflowException);
385                         }
386                         execution.setVariable("rollbackData", null)
387                 } catch (BpmnError b) {
388                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)
389                         throw b;
390                 } catch(Exception ex) {
391                         msg = "Exception in postProcessRollback. " + ex.getMessage()
392                         utils.log("INFO", msg, isDebugEnabled)
393                 }
394                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)
395         }
396
397
398         public void postConfigRequest(execution){
399             //now do noting
400         }
401
402
403 }
404