5b7fdb2c228b898265bd8a3942d8d975bde855c4
[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.onap.so.client.aai.AAIObjectType
36 import org.onap.so.client.aai.AAIResourcesClient
37 import org.onap.so.client.aai.entities.AAIResultWrapper
38 import org.onap.so.client.aai.entities.uri.AAIResourceUri
39 import org.onap.so.client.aai.entities.uri.AAIUriFactory
40 import org.springframework.web.util.UriUtils;
41
42 import groovy.json.*
43
44
45 /**
46  * This groovy class supports the <class>DoUpdateE2EServiceInstance.bpmn</class> process.
47  *
48  * Inputs:
49  * @param - msoRequestId
50  * @param - globalSubscriberId
51  * @param - serviceType
52  * @param - serviceInstanceId
53  * @param - serviceInstanceName
54  * @param - serviceModelInfo
55  * @param - productFamilyId
56  * @param - uuiRequest
57  * @param - serviceDecomposition_Target
58  * @param - serviceDecomposition_Original
59  * @param - addResourceList
60  * @param - delResourceList
61  *
62  * Outputs:
63  * @param - rollbackData (localRB->null)
64  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
65  * @param - WorkflowException
66  */
67 public class DoUpdateE2EServiceInstance extends AbstractServiceTaskProcessor {
68
69         String Prefix="DUPDSI_"
70         private static final String DebugFlag = "isDebugEnabled"
71
72         ExceptionUtil exceptionUtil = new ExceptionUtil()
73         JsonUtils jsonUtil = new JsonUtils()
74
75         public void preProcessRequest (DelegateExecution execution) {
76                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
77                 utils.log("INFO"," ***** Enter DoUpdateE2EServiceInstance preProcessRequest *****",  isDebugEnabled)
78
79                 String msg = ""
80
81                 try {
82                         execution.setVariable("prefix", Prefix)
83                         //Inputs
84                         //for AAI GET & PUT & SDNC assignToplology
85                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
86                         utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)
87
88                         //for AAI PUT & SDNC assignTopology
89                         String serviceType = execution.getVariable("serviceType")
90                         utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)
91
92                         //for SDNC assignTopology
93                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
94
95                         if (isBlank(globalSubscriberId)) {
96                                 msg = "Input globalSubscriberId is null"
97                                 utils.log("INFO", msg, isDebugEnabled)
98                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
99                         }
100
101                         if (isBlank(serviceType)) {
102                                 msg = "Input serviceType is null"
103                                 utils.log("INFO", msg, isDebugEnabled)
104                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105                         }
106
107                         //Generated in parent for AAI
108                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
109                         if (isBlank(serviceInstanceId)){
110                                 msg = "Input serviceInstanceId is null"
111                                 utils.log("INFO", msg, isDebugEnabled)
112                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
113                         }
114
115                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
116
117                         // user params
118                         String uuiRequest = execution.getVariable("uuiRequest")
119
120                         // target model Invariant uuid
121                         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
122                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)
123                         utils.log("INFO", "modelInvariantUuid: " + modelInvariantUuid, isDebugEnabled)
124
125                         // target model uuid
126                         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
127                         execution.setVariable("modelUuid", modelUuid)
128
129                         utils.log("INFO","modelUuid: " + modelUuid, isDebugEnabled)
130
131                 } catch (BpmnError e) {
132                         throw e;
133                 } catch (Exception ex){
134                         msg = "Exception in preProcessRequest " + ex.getMessage()
135                         utils.log("INFO", msg, isDebugEnabled)
136                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
137                 }
138                 utils.log("INFO", "======== COMPLETED preProcessRequest Process ======== ", isDebugEnabled)
139         }
140
141
142         public void preInitResourcesOperStatus(DelegateExecution execution){
143         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
144
145         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
146         try{
147             String serviceId = execution.getVariable("serviceInstanceId")
148             String operationId = execution.getVariable("operationId")
149             String operationType = execution.getVariable("operationType")
150             String resourceTemplateUUIDs = ""
151             String result = "processing"
152             String progress = "0"
153             String reason = ""
154             String operationContent = "Prepare service updating"
155             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
156             serviceId = UriUtils.encode(serviceId,"UTF-8")
157             execution.setVariable("serviceInstanceId", serviceId)
158             execution.setVariable("operationId", operationId)
159             execution.setVariable("operationType", operationType)
160
161                         List<Resource> resourceList = new ArrayList<String>()
162                         List<Resource> addResourceList =  execution.getVariable("addResourceList")
163                         List<Resource> delResourceList =  execution.getVariable("delResourceList")
164                         resourceList.addAll(addResourceList)
165                         resourceList.addAll(delResourceList)
166                         for(Resource resource : resourceList){
167                                 resourceTemplateUUIDs  = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"
168                         }
169
170                         def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
171                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
172                         utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
173
174                         String payload =
175                                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
176                         xmlns:ns="http://org.onap.so/requestsdb">
177                         <soapenv:Header/>
178                         <soapenv:Body>
179                             <ns:initResourceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
180                                                                 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
181                                                                 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
182                                                                 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
183                                                                 <resourceTemplateUUIDs>${MsoUtils.xmlEscape(resourceTemplateUUIDs)}</resourceTemplateUUIDs>
184                             </ns:initResourceOperationStatus>
185                         </soapenv:Body>
186                         </soapenv:Envelope>"""
187
188                         payload = utils.formatXml(payload)
189                         execution.setVariable("CVFMI_initResOperStatusRequest", payload)
190                         utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
191                         utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
192
193         }catch(Exception e){
194             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
195             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
196         }
197         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
198     }
199
200
201     public void preProcessForAddResource(DelegateExecution execution) {
202         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
203                 utils.log("INFO"," ***** preProcessForAddResource ***** ", isDebugEnabled)
204
205             execution.setVariable("operationType", "create")
206
207                 execution.setVariable("hasResourcetoAdd", false)
208                 List<Resource> addResourceList =  execution.getVariable("addResourceList")
209                 if(addResourceList != null && !addResourceList.isEmpty()) {
210                         execution.setVariable("hasResourcetoAdd", true)
211                 }
212
213                 utils.log("INFO"," *** Exit preProcessForAddResource *** ", isDebugEnabled)
214     }
215
216     public void postProcessForAddResource(DelegateExecution execution) {
217         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
218                 utils.log("INFO"," ***** postProcessForAddResource ***** ", isDebugEnabled)
219
220                 execution.setVariable("operationType", "update")
221
222                 utils.log("INFO"," *** Exit postProcessForAddResource *** ", isDebugEnabled)
223     }
224
225         public void preProcessForDeleteResource(DelegateExecution execution) {
226                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
227                 utils.log("INFO"," ***** preProcessForDeleteResource ***** ", isDebugEnabled)
228
229                 execution.setVariable("operationType", "delete")
230
231                 def  hasResourcetoDelete = false
232                 List<Resource> delResourceList =  execution.getVariable("delResourceList")
233                 if(delResourceList != null && !delResourceList.isEmpty()) {
234                         hasResourcetoDelete = true
235                 }
236                 execution.setVariable("hasResourcetoDelete", hasResourcetoDelete)
237
238                 if(hasResourcetoDelete) {
239                         def jsonSlurper = new JsonSlurper()
240                         String serviceRelationShip = execution.getVariable("serviceRelationShip")
241                         List relationShipList =  jsonSlurper.parseText(serviceRelationShip)
242
243                         //Set the real resource instance id to the decomosed resource list
244                         for(Resource resource: delResourceList){
245                                 //reset the resource instance id , because in the decompose flow ,its a random one.
246                                 resource.setResourceId("");
247                                 //match the resource-instance-name and the model name
248                                 if (relationShipList != null) {
249                                         relationShipList.each {
250                                                 if(StringUtils.containsIgnoreCase(it.resourceType, resource.getModelInfo().getModelName())){
251                                                         resource.setResourceId(it.resourceInstanceId);
252                                                 }
253                                         }
254                                 }
255                         }
256                 }
257
258                 execution.setVariable("deleteResourceList", delResourceList)
259
260                 utils.log("INFO"," *** Exit preProcessForDeleteResource *** ", isDebugEnabled)
261         }
262
263     public void postProcessForDeleteResource(DelegateExecution execution) {
264         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
265                 utils.log("INFO"," ***** postProcessForDeleteResource ***** ", isDebugEnabled)
266
267                 execution.setVariable("operationType", "update")
268
269                 utils.log("INFO"," *** Exit postProcessForDeleteResource *** ", isDebugEnabled)
270     }
271
272         public void preProcessAAIPUT(DelegateExecution execution) {
273                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
274                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
275                 utils.log("INFO","Entered " + method, isDebugEnabled)
276                 String msg = ""
277
278                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion")
279
280                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
281                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
282                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
283                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
284                 //aai serviceType and Role can be setted as fixed value now.
285                 String aaiServiceType = "E2E Service"
286                 String aaiServiceRole = "E2E Service"
287                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
288                 String modelUuid = execution.getVariable("modelUuid")
289
290                 org.onap.aai.domain.yang.ServiceInstance si = new org.onap.aai.domain.yang.ServiceInstance()
291                 si.setServiceInstanceId(serviceInstanceId)
292                 si.setServiceInstanceName(serviceInstanceName)
293                 si.setServiceType(aaiServiceType)
294                 si.setServiceRole(aaiServiceRole)
295                 si.setModelInvariantId(modelInvariantUuid)
296                 si.setModelVersionId(modelUuid)
297
298                 execution.setVariable("serviceInstanceData", si)
299
300                 utils.log("INFO", "Exited " + method, isDebugEnabled)
301         }
302
303         public void updateServiceInstance(DelegateExecution execution) {
304                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
305                 utils.log("INFO"," ***** createServiceInstance ***** ", isDebugEnabled)
306                 String msg = ""
307                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
308                 try {
309                         org.onap.aai.domain.yang.ServiceInstance si = execution.getVariable("serviceInstanceData")
310
311                         AAIResourcesClient client = new AAIResourcesClient()
312                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
313                         client.update(uri, si)
314
315                 } catch (BpmnError e) {
316                         throw e;
317                 } catch (Exception ex) {
318                         RollbackData rollbackData = new RollbackData()
319                         def disableRollback = execution.getVariable("disableRollback")
320                         rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
321                         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
322                         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
323                         rollbackData.put("SERVICEINSTANCE", "serviceType", execution.getVariable("serviceType"))
324                         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
325                         execution.setVariable("rollbackData", rollbackData)
326
327                         msg = "Exception in DoCreateServiceInstance.createServiceInstance. " + ex.getMessage()
328                         utils.log("INFO", msg, isDebugEnabled)
329                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
330                 }
331                 utils.log("INFO"," *** Exit createServiceInstance *** ", isDebugEnabled)
332         }
333
334         public void preProcessRollback (DelegateExecution execution) {
335                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
336                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)
337                 try {
338
339                         Object workflowException = execution.getVariable("WorkflowException");
340
341                         if (workflowException instanceof WorkflowException) {
342                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
343                                 execution.setVariable("prevWorkflowException", workflowException);
344                                 //execution.setVariable("WorkflowException", null);
345                         }
346                 } catch (BpmnError e) {
347                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)
348                 } catch(Exception ex) {
349                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
350                         utils.log("INFO", msg, isDebugEnabled)
351                 }
352                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)
353         }
354
355         public void postProcessRollback (DelegateExecution execution) {
356                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
357                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)
358                 String msg = ""
359                 try {
360                         Object workflowException = execution.getVariable("prevWorkflowException");
361                         if (workflowException instanceof WorkflowException) {
362                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)
363                                 execution.setVariable("WorkflowException", workflowException);
364                         }
365                         execution.setVariable("rollbackData", null)
366                 } catch (BpmnError b) {
367                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)
368                         throw b;
369                 } catch(Exception ex) {
370                         msg = "Exception in postProcessRollback. " + ex.getMessage()
371                         utils.log("INFO", msg, isDebugEnabled)
372                 }
373                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)
374         }
375
376
377         public void postConfigRequest(execution){
378             //now do noting
379         }
380
381
382 }
383