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