Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateE2EServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved. 
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.so.bpmn.infrastructure.scripts;
22
23 import static org.apache.commons.lang3.StringUtils.*;
24
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.so.bpmn.common.scripts.AaiUtil
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.CatalogDbUtils;
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.domain.ServiceDecomposition
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.logger.MessageEnum
39 import org.onap.so.logger.MsoLogger
40 import org.springframework.web.util.UriUtils;
41
42 import groovy.json.*
43
44
45
46 /**
47  * This groovy class supports the <class>DoCreateServiceInstance.bpmn</class> process.
48  *
49  * Inputs:
50  * @param - msoRequestId
51  * @param - globalSubscriberId
52  * @param - subscriptionServiceType
53  * @param - serviceInstanceId
54  * @param - serviceInstanceName - O
55  * @param - serviceModelInfo
56  * @param - productFamilyId
57  * @param - disableRollback
58  * @param - failExists - TODO
59  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
60  * @param - sdncVersion ("1610")
61  * @param - serviceDecomposition - Decomposition for R1710 
62  * (if macro provides serviceDecompsition then serviceModelInfo, serviceInstanceId & serviceInstanceName will be ignored)
63  *
64  * Outputs:
65  * @param - rollbackData (localRB->null)
66  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
67  * @param - WorkflowException
68  * @param - serviceInstanceName - (GET from AAI if null in input)
69  *
70  */
71 public class DoCreateE2EServiceInstance extends AbstractServiceTaskProcessor {
72         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateE2EServiceInstance.class);
73
74
75         String Prefix="DCRESI_"
76         ExceptionUtil exceptionUtil = new ExceptionUtil()
77         JsonUtils jsonUtil = new JsonUtils()
78         CatalogDbUtils cutils = new CatalogDbUtils()
79
80         public void preProcessRequest (DelegateExecution execution) {
81                 String msg = ""
82                 msoLogger.trace("preProcessRequest ")
83
84                 try {
85                         execution.setVariable("prefix", Prefix)
86                         //Inputs
87                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
88                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
89                         msoLogger.info(" ***** globalSubscriberId *****" + globalSubscriberId)
90                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
91                         String serviceType = execution.getVariable("serviceType")
92                         msoLogger.info(" ***** serviceType *****" + serviceType)
93                         //requestDetails.requestParameters. for SDNC assignTopology
94                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
95
96                         if (isBlank(globalSubscriberId)) {
97                                 msg = "Input globalSubscriberId is null"
98                                 msoLogger.info(msg)
99                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100                         }
101                         
102                         if (isBlank(serviceType)) {
103                                 msg = "Input serviceType is null"
104                                 msoLogger.info(msg)
105                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
106                         }
107                         
108                         if (productFamilyId == null) {
109                                 execution.setVariable("productFamilyId", "")
110                         }
111                         
112                         String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
113                         if (isBlank(sdncCallbackUrl)) {
114                                 msg = "URN_mso_workflow_sdncadapter_callback is null"
115                                 msoLogger.info(msg)
116                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
117                         }
118                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
119                         msoLogger.info("SDNC Callback URL: " + sdncCallbackUrl)
120
121                         //requestDetails.modelInfo.for AAI PUT servieInstanceData                       
122                         //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData 
123                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
124                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
125                         String uuiRequest = execution.getVariable("uuiRequest")
126                         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
127                         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
128                         String serviceModelName = jsonUtil.getJsonValue(uuiRequest, "service.parameters.templateName")
129                         execution.setVariable("serviceModelName", serviceModelName)
130                         //aai serviceType and Role can be setted as fixed value now.
131                         String aaiServiceType = "E2E Service"
132                         String aaiServiceRole = "E2E Service"
133                         
134                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)
135                         execution.setVariable("modelUuid", modelUuid)
136
137                         //AAI PUT
138                         String oStatus = execution.getVariable("initialStatus") ?: ""
139                         if ("TRANSPORT".equalsIgnoreCase(serviceType))
140                         {
141                                 oStatus = "Created"
142                         }
143
144                         String statusLine = isBlank(oStatus) ? "" : "<orchestration-status>${MsoUtils.xmlEscape(oStatus)}</orchestration-status>"
145                                 
146                         AaiUtil aaiUriUtil = new AaiUtil(this)
147                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
148                         String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
149                         String serviceInstanceData =
150                                         """<service-instance xmlns=\"${namespace}\">
151                                 <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
152                                 <service-instance-name>${MsoUtils.xmlEscape(serviceInstanceName)}</service-instance-name>
153                                         <service-type>${MsoUtils.xmlEscape(aaiServiceType)}</service-type>
154                                         <service-role>${MsoUtils.xmlEscape(aaiServiceRole)}</service-role>
155                                         ${statusLine}
156                                     <model-invariant-id>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-id>
157                                     <model-version-id>${MsoUtils.xmlEscape(modelUuid)}</model-version-id>
158                                         </service-instance>""".trim()                                   
159                         execution.setVariable("serviceInstanceData", serviceInstanceData)
160                         msoLogger.debug(serviceInstanceData)
161                         msoLogger.info(" aai_uri " + aai_uri + " namespace:" + namespace)
162                         msoLogger.info(" 'payload' to create Service Instance in AAI - " + "\n" + serviceInstanceData)
163
164                 } catch (BpmnError e) {
165                         throw e;
166                 } catch (Exception ex){
167                         msg = "Exception in preProcessRequest " + ex.getMessage()
168                         msoLogger.info(msg)
169                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
170                 }
171                 msoLogger.trace("Exit preProcessRequest ")
172         }
173         
174    public void prepareDecomposeService(DelegateExecution execution) {
175         try {
176             msoLogger.trace("Inside prepareDecomposeService of create generic e2e service ")
177             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
178             String modelUuid = execution.getVariable("modelUuid")
179             //here modelVersion is not set, we use modelUuid to decompose the service.
180             String serviceModelInfo = """{
181             "modelInvariantUuid":"${modelInvariantUuid}",
182             "modelUuid":"${modelUuid}",
183             "modelVersion":""
184              }"""
185             execution.setVariable("serviceModelInfo", serviceModelInfo)
186
187             msoLogger.trace("Completed prepareDecomposeService of  create generic e2e service ")
188         } catch (Exception ex) {
189             // try error in method block
190             String exceptionMessage = "Bpmn error encountered in  create generic e2e service flow. Unexpected Error from method prepareDecomposeService() - " + ex.getMessage()
191             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
192         }
193      }
194
195     public void processDecomposition(DelegateExecution execution) {
196         msoLogger.trace("Inside processDecomposition() of  create generic e2e service flow ")    
197         try {
198             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
199         } catch (Exception ex) {
200             String exceptionMessage = "Bpmn error encountered in  create generic e2e service flow. processDecomposition() - " + ex.getMessage()
201             msoLogger.debug(exceptionMessage)
202             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
203         }
204     }
205     
206     public void doServicePreOperation(DelegateExecution execution){
207        //we need a service plugin platform here. 
208         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
209         String uuiRequest = execution.getVariable("uuiRequest")         
210         String newUuiRequest = ServicePluginFactory.getInstance().preProcessService(serviceDecomposition, uuiRequest);
211         execution.setVariable("uuiRequest", newUuiRequest)      
212     }
213     
214     public void doServiceHoming(DelegateExecution execution) {
215         //we need a service plugin platform here. 
216         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
217         String uuiRequest = execution.getVariable("uuiRequest")         
218         String newUuiRequest = ServicePluginFactory.getInstance().doServiceHoming(serviceDecomposition, uuiRequest);
219         execution.setVariable("uuiRequest", newUuiRequest)      
220     }
221     
222         public void postProcessAAIGET(DelegateExecution execution) {
223                 msoLogger.trace("postProcessAAIGET ")
224                 String msg = ""
225
226                 try {
227                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
228                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
229                         if(!succInAAI){
230                                 msoLogger.info("Error getting Service-instance from AAI", + serviceInstanceName)
231                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
232                                 msoLogger.debug("workflowException: " + workflowException)
233                                 if(workflowException != null){
234                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
235                                 }
236                                 else
237                                 {
238                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
239                                         msoLogger.info(msg)
240                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
241                                 }
242                         }
243                         else
244                         {
245                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
246                                 if(foundInAAI){
247                                         msoLogger.info("Found Service-instance in AAI")
248                                         msg = "ServiceInstance already exists in AAI:" + serviceInstanceName
249                                         msoLogger.info(msg)
250                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
251                                 }
252                         }
253                 } catch (BpmnError e) {
254                         throw e;
255                 } catch (Exception ex) {
256                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET. " + ex.getMessage()
257                         msoLogger.info(msg)
258                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
259                 }
260                 msoLogger.trace("Exit postProcessAAIGET ")
261         }
262
263         public void postProcessAAIPUT(DelegateExecution execution) {
264                 msoLogger.trace("postProcessAAIPUT ")
265                 String msg = ""
266                 try {
267                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
268                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
269                         if(!succInAAI){
270                                 msoLogger.info("Error putting Service-instance in AAI", + serviceInstanceId)
271                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
272                                 msoLogger.debug("workflowException: " + workflowException)
273                                 if(workflowException != null){
274                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
275                                 }
276                         }
277                         else
278                         {
279                                 //start rollback set up
280                                 RollbackData rollbackData = new RollbackData()
281                                 def disableRollback = execution.getVariable("disableRollback")
282                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
283                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
284                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
285                                 rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
286                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
287                                 execution.setVariable("rollbackData", rollbackData)
288                         }
289
290                 } catch (BpmnError e) {
291                         throw e;
292                 } catch (Exception ex) {
293                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
294                         msoLogger.info(msg)
295                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
296                 }
297                 msoLogger.trace("Exit postProcessAAIPUT ")
298         }
299         
300         public void postProcessAAIGET2(DelegateExecution execution) {
301                 msoLogger.trace("postProcessAAIGET2 ")
302                 String msg = ""
303
304                 try {
305                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
306                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
307                         if(!succInAAI){
308                                 msoLogger.info("Error getting Service-instance from AAI in postProcessAAIGET2", + serviceInstanceName)
309                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
310                                 msoLogger.debug("workflowException: " + workflowException)
311                                 if(workflowException != null){
312                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
313                                 }
314                                 else
315                                 {
316                                         msg = "Failure in postProcessAAIGET2 GENGS_SuccessIndicator:" + succInAAI
317                                         msoLogger.info(msg)
318                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
319                                 }
320                         }
321                         else
322                         {
323                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
324                                 if(foundInAAI){
325                                         String aaiService = execution.getVariable("GENGS_service")
326                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "service-instance-name"))) {
327                                                 execution.setVariable("serviceInstanceName",  utils.getNodeText(aaiService, "service-instance-name"))
328                                                 msoLogger.info("Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"))
329                                         }
330                                 }
331                         }
332                 } catch (BpmnError e) {
333                         throw e;
334                 } catch (Exception ex) {
335                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET2 " + ex.getMessage()
336                         msoLogger.info(msg)
337                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
338                 }
339                 msoLogger.trace("Exit postProcessAAIGET2 ")
340         }
341
342         public void preProcessRollback (DelegateExecution execution) {
343                 msoLogger.trace("preProcessRollback ")
344                 try {
345                         
346                         Object workflowException = execution.getVariable("WorkflowException");
347
348                         if (workflowException instanceof WorkflowException) {
349                                 msoLogger.info("Prev workflowException: " + workflowException.getErrorMessage())
350                                 execution.setVariable("prevWorkflowException", workflowException);
351                                 //execution.setVariable("WorkflowException", null);
352                         }
353                 } catch (BpmnError e) {
354                         msoLogger.info("BPMN Error during preProcessRollback")
355                 } catch(Exception ex) {
356                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
357                         msoLogger.info(msg)
358                 }
359                 msoLogger.trace("Exit preProcessRollback ")
360         }
361
362         public void postProcessRollback (DelegateExecution execution) {
363                 msoLogger.trace("postProcessRollback ")
364                 String msg = ""
365                 try {
366                         Object workflowException = execution.getVariable("prevWorkflowException");
367                         if (workflowException instanceof WorkflowException) {
368                                 msoLogger.info("Setting prevException to WorkflowException: ")
369                                 execution.setVariable("WorkflowException", workflowException);
370                         }
371                         execution.setVariable("rollbackData", null)
372                 } catch (BpmnError b) {
373                         msoLogger.info("BPMN Error during postProcessRollback")
374                         throw b;
375                 } catch(Exception ex) {
376                         msg = "Exception in postProcessRollback. " + ex.getMessage()
377                         msoLogger.info(msg)
378                 }
379                 msoLogger.trace("Exit postProcessRollback ")
380         }
381
382         public void preInitResourcesOperStatus(DelegateExecution execution){
383         msoLogger.trace("STARTED preInitResourcesOperStatus Process ")
384         try{
385             String serviceId = execution.getVariable("serviceInstanceId")
386             String operationId = execution.getVariable("operationId")
387             String operationType = execution.getVariable("operationType")
388             String resourceTemplateUUIDs = ""
389             String result = "processing"
390             String progress = "0"
391             String reason = ""
392             String operationContent = "Prepare service creation"
393             msoLogger.info("Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType)
394             serviceId = UriUtils.encode(serviceId,"UTF-8")
395             execution.setVariable("serviceInstanceId", serviceId)
396             execution.setVariable("operationId", operationId)
397             execution.setVariable("operationType", operationType)
398             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
399             List<Resource>  resourceList = serviceDecomposition.getServiceResources()
400             
401             for(Resource resource : resourceList){
402                     resourceTemplateUUIDs  = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"
403             }           
404
405             def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
406             execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
407             msoLogger.info("DB Adapter Endpoint is: " + dbAdapterEndpoint)
408
409             String payload =
410                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
411                         xmlns:ns="http://org.onap.so/requestsdb">
412                         <soapenv:Header/>
413                         <soapenv:Body>
414                             <ns:initResourceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
415                                                                 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
416                                                                 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
417                                                                 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
418                                                                 <resourceTemplateUUIDs>${MsoUtils.xmlEscape(resourceTemplateUUIDs)}</resourceTemplateUUIDs>
419                             </ns:initResourceOperationStatus>
420                         </soapenv:Body>
421                         </soapenv:Envelope>"""
422
423             payload = utils.formatXml(payload)
424             execution.setVariable("CVFMI_initResOperStatusRequest", payload)
425             msoLogger.info("Outgoing initResourceOperationStatus: \n" + payload)
426             msoLogger.debug("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
427
428         }catch(Exception e){
429             msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing preInitResourcesOperStatus.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e);
430             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
431         }
432         msoLogger.trace("COMPLETED preInitResourcesOperStatus Process ")  
433         }
434
435         // prepare input param for using DoCreateResources.bpmn
436         public void preProcessForAddResource(DelegateExecution execution) {
437                 msoLogger.trace("STARTED preProcessForAddResource Process ")
438                 
439                 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
440                 List<Resource> addResourceList = serviceDecomposition.getServiceResources()
441                 execution.setVariable("addResourceList", addResourceList)
442                 
443                 msoLogger.trace("COMPLETED preProcessForAddResource Process ")
444         }
445
446         public void postProcessForAddResource(DelegateExecution execution) {
447                 // do nothing now
448         
449         }
450
451 }