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