508131279aa051c842bcbd2bb3f53cc09ccae4e7
[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
199         }catch(Exception e){
200             logger.info( "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e)
201             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
202         }
203         logger.info( "======== COMPLETED preInitResourcesOperStatus Process ======== ")
204     }
205
206
207     public void preProcessForAddResource(DelegateExecution execution) {
208         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
209                 logger.info(" ***** preProcessForAddResource ***** ")
210
211             execution.setVariable("operationType", "create")
212
213                 execution.setVariable("hasResourcetoAdd", false)
214                 List<Resource> addResourceList =  execution.getVariable("addResourceList")
215                 if(addResourceList != null && !addResourceList.isEmpty()) {
216                         execution.setVariable("hasResourcetoAdd", true)
217                 }
218
219                 logger.info(" *** Exit preProcessForAddResource *** ")
220     }
221
222     public void postProcessForAddResource(DelegateExecution execution) {
223         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
224                 logger.info(" ***** postProcessForAddResource ***** ")
225
226                 execution.setVariable("operationType", "update")
227
228                 logger.info(" *** Exit postProcessForAddResource *** ")
229     }
230
231         public void preProcessForDeleteResource(DelegateExecution execution) {
232                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
233                 logger.info(" ***** preProcessForDeleteResource ***** ")
234
235                 execution.setVariable("operationType", "delete")
236
237                 def  hasResourcetoDelete = false
238                 List<Resource> delResourceList =  execution.getVariable("delResourceList")
239                 if(delResourceList != null && !delResourceList.isEmpty()) {
240                         hasResourcetoDelete = true
241                 }
242                 execution.setVariable("hasResourcetoDelete", hasResourcetoDelete)
243
244                 if(hasResourcetoDelete) {
245                         def jsonSlurper = new JsonSlurper()
246                         String serviceRelationShip = execution.getVariable("serviceRelationShip")
247                         List relationShipList =  jsonSlurper.parseText(serviceRelationShip)
248
249                         //Set the real resource instance id to the decomosed resource list
250                         for(Resource resource: delResourceList){
251                                 //reset the resource instance id , because in the decompose flow ,its a random one.
252                                 resource.setResourceId("");
253                                 //match the resource-instance-name and the model name
254                                 if (relationShipList != null) {
255                                         relationShipList.each {
256                                                 if(StringUtils.containsIgnoreCase(it.resourceType, resource.getModelInfo().getModelName())){
257                                                         resource.setResourceId(it.resourceInstanceId);
258                                                 }
259                                         }
260                                 }
261                         }
262                 }
263
264                 execution.setVariable("deleteResourceList", delResourceList)
265
266                 logger.info(" *** Exit preProcessForDeleteResource *** ")
267         }
268
269     public void postProcessForDeleteResource(DelegateExecution execution) {
270         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
271                 logger.info(" ***** postProcessForDeleteResource ***** ")
272
273                 execution.setVariable("operationType", "update")
274
275                 logger.info(" *** Exit postProcessForDeleteResource *** ")
276     }
277
278         public void preProcessAAIPUT(DelegateExecution execution) {
279                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
280                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
281                 logger.info("Entered " + method)
282                 String msg = ""
283
284                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion")
285
286                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
287                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
288                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
289                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
290                 //aai serviceType and Role can be setted as fixed value now.
291                 String aaiServiceType = "E2E Service"
292                 String aaiServiceRole = "E2E Service"
293                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
294                 String modelUuid = execution.getVariable("modelUuid")
295
296                 org.onap.aai.domain.yang.ServiceInstance si = new org.onap.aai.domain.yang.ServiceInstance()
297                 si.setServiceInstanceId(serviceInstanceId)
298                 si.setServiceInstanceName(serviceInstanceName)
299                 si.setServiceType(aaiServiceType)
300                 si.setServiceRole(aaiServiceRole)
301                 si.setModelInvariantId(modelInvariantUuid)
302                 si.setModelVersionId(modelUuid)
303
304                 execution.setVariable("serviceInstanceData", si)
305
306                 logger.info( "Exited " + method)
307         }
308
309         public void updateServiceInstance(DelegateExecution execution) {
310                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
311                 logger.info(" ***** createServiceInstance ***** ")
312                 String msg = ""
313                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
314                 try {
315                         org.onap.aai.domain.yang.ServiceInstance si = execution.getVariable("serviceInstanceData")
316
317             AAIResourcesClient client = new AAIResourcesClient()
318             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
319                         client.update(uri, si)
320
321                 } catch (BpmnError e) {
322                         throw e;
323                 } catch (Exception ex) {
324                         RollbackData rollbackData = new RollbackData()
325                         def disableRollback = execution.getVariable("disableRollback")
326                         rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
327                         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
328                         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
329                         rollbackData.put("SERVICEINSTANCE", "serviceType", execution.getVariable("serviceType"))
330                         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
331                         execution.setVariable("rollbackData", rollbackData)
332
333                         msg = "Exception in DoCreateServiceInstance.createServiceInstance. " + ex.getMessage()
334                         logger.info( msg)
335                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
336                 }
337                 logger.info(" *** Exit createServiceInstance *** ")
338         }
339
340         public void preProcessRollback (DelegateExecution execution) {
341                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
342                 logger.info(" ***** preProcessRollback ***** ")
343                 try {
344
345                         Object workflowException = execution.getVariable("WorkflowException");
346
347                         if (workflowException instanceof WorkflowException) {
348                                 logger.info( "Prev workflowException: " + workflowException.getErrorMessage())
349                                 execution.setVariable("prevWorkflowException", workflowException);
350                                 //execution.setVariable("WorkflowException", null);
351                         }
352                 } catch (BpmnError e) {
353                         logger.info( "BPMN Error during preProcessRollback")
354                 } catch(Exception ex) {
355                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
356                         logger.info( msg)
357                 }
358                 logger.info(" *** Exit preProcessRollback *** ")
359         }
360
361         public void postProcessRollback (DelegateExecution execution) {
362                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
363                 logger.info(" ***** postProcessRollback ***** ")
364                 String msg = ""
365                 try {
366                         Object workflowException = execution.getVariable("prevWorkflowException");
367                         if (workflowException instanceof WorkflowException) {
368                                 logger.info( "Setting prevException to WorkflowException: ")
369                                 execution.setVariable("WorkflowException", workflowException);
370                         }
371                         execution.setVariable("rollbackData", null)
372                 } catch (BpmnError b) {
373                         logger.info( "BPMN Error during postProcessRollback")
374                         throw b;
375                 } catch(Exception ex) {
376                         msg = "Exception in postProcessRollback. " + ex.getMessage()
377                         logger.info( msg)
378                 }
379                 logger.info(" *** Exit postProcessRollback *** ")
380         }
381
382
383         public void postConfigRequest(execution){
384             //now do noting
385         }
386
387
388 }
389