Merge changes I4e542ffd,Ibb86da2d
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / 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.openecomp.mso.bpmn.infrastructure.scripts;
22
23 import static org.apache.commons.lang3.StringUtils.*;
24 import groovy.xml.XmlUtil
25 import groovy.json.*
26
27 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
28 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
29 import org.openecomp.mso.bpmn.core.domain.ModelInfo
30 import org.openecomp.mso.bpmn.core.json.JsonUtils
31 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
32 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
34 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
35 import org.openecomp.mso.bpmn.core.RollbackData
36 import org.openecomp.mso.bpmn.core.WorkflowException
37 import org.openecomp.mso.rest.APIResponse;
38 import org.openecomp.mso.rest.RESTClient
39 import org.openecomp.mso.rest.RESTConfig
40
41 import java.util.UUID;
42
43 import org.camunda.bpm.engine.delegate.BpmnError
44 import org.camunda.bpm.engine.runtime.Execution
45 import org.json.JSONObject;
46 import org.apache.commons.lang3.*
47 import org.apache.commons.codec.binary.Base64;
48 import org.springframework.web.util.UriUtils;
49
50 /**
51  * This groovy class supports the <class>DoCreateServiceInstance.bpmn</class> process.
52  *
53  * Inputs:
54  * @param - msoRequestId
55  * @param - globalSubscriberId
56  * @param - subscriptionServiceType
57  * @param - serviceInstanceId
58  * @param - serviceInstanceName - O
59  * @param - serviceModelInfo
60  * @param - productFamilyId
61  * @param - disableRollback
62  * @param - failExists - TODO
63  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
64  * @param - sdncVersion ("1610")
65  * @param - serviceDecomposition - Decomposition for R1710 
66  * (if macro provides serviceDecompsition then serviceModelInfo, serviceInstanceId & serviceInstanceName will be ignored)
67  *
68  * Outputs:
69  * @param - rollbackData (localRB->null)
70  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
71  * @param - WorkflowException
72  * @param - serviceInstanceName - (GET from AAI if null in input)
73  *
74  */
75 public class DoCreateE2EServiceInstance extends AbstractServiceTaskProcessor {
76
77         String Prefix="DCRESI_"
78         ExceptionUtil exceptionUtil = new ExceptionUtil()
79         JsonUtils jsonUtil = new JsonUtils()
80
81         public void preProcessRequest (Execution execution) {
82                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
83                 String msg = ""
84                 utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
85
86                 try {
87                         execution.setVariable("prefix", Prefix)
88                         //Inputs
89                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
90                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
91                         utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)
92                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
93                         String serviceType = execution.getVariable("serviceType")
94                         utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)
95                         //requestDetails.requestParameters. for SDNC assignTopology
96                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
97
98                         if (isBlank(globalSubscriberId)) {
99                                 msg = "Input globalSubscriberId is null"
100                                 utils.log("INFO", msg, isDebugEnabled)
101                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
102                         }
103                         
104                         if (isBlank(serviceType)) {
105                                 msg = "Input serviceType is null"
106                                 utils.log("INFO", msg, isDebugEnabled)
107                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
108                         }
109                         
110                         if (productFamilyId == null) {
111                                 execution.setVariable("productFamilyId", "")
112                         }
113                         
114                         String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
115                         if (isBlank(sdncCallbackUrl)) {
116                                 msg = "URN_mso_workflow_sdncadapter_callback is null"
117                                 utils.log("INFO", msg, isDebugEnabled)
118                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
119                         }
120                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
121                         utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
122
123                         //requestDetails.modelInfo.for AAI PUT servieInstanceData                       
124                         //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData 
125                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
126                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
127                         String uuiRequest = execution.getVariable("uuiRequest")
128                         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceDefId")
129                         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.templateId")
130                         String serviceModelName = jsonUtil.getJsonValue(uuiRequest, "service.parameters.templateName")
131                         execution.setVariable("serviceModelName", serviceModelName)
132                         //aai serviceType and Role can be setted as fixed value now.
133                         String aaiServiceType = "voLTE type"
134                         String aaiServiceRole = "voLTE role"
135                         
136                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)
137                         execution.setVariable("modelUuid", modelUuid)
138
139                         //AAI PUT
140                         String oStatus = execution.getVariable("initialStatus") ?: ""
141                         if ("TRANSPORT".equalsIgnoreCase(serviceType))
142                         {
143                                 oStatus = "Created"
144                         }
145
146                         String statusLine = isBlank(oStatus) ? "" : "<orchestration-status>${oStatus}</orchestration-status>"
147                                 
148                         AaiUtil aaiUriUtil = new AaiUtil(this)
149                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
150                         String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
151                         String serviceInstanceData =
152                                         """<service-instance xmlns=\"${namespace}\">
153                                 <service-instance-id>${serviceInstanceId}</service-instance-id>
154                                 <service-instance-name>${serviceInstanceName}</service-instance-name>
155                                         <service-type>${aaiServiceType}</service-type>
156                                         <service-role>${aaiServiceRole}</service-role>
157                                         ${statusLine}
158                                     <model-invariant-id>${modelInvariantUuid}</model-invariant-id>
159                                     <model-version-id>${modelUuid}</model-version-id>
160                                         </service-instance>""".trim()                                   
161                         execution.setVariable("serviceInstanceData", serviceInstanceData)
162                         utils.logAudit(serviceInstanceData)
163                         utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
164                         utils.log("INFO", " 'payload' to create Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
165
166                 } catch (BpmnError e) {
167                         throw e;
168                 } catch (Exception ex){
169                         msg = "Exception in preProcessRequest " + ex.getMessage()
170                         utils.log("INFO", msg, isDebugEnabled)
171                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
172                 }
173                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
174         }
175
176         public void postProcessAAIGET(Execution execution) {
177                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
178                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
179                 String msg = ""
180
181                 try {
182                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
183                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
184                         if(succInAAI != true){
185                                 utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceName, isDebugEnabled)
186                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
187                                 utils.logAudit("workflowException: " + workflowException)
188                                 if(workflowException != null){
189                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
190                                 }
191                                 else
192                                 {
193                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
194                                         utils.log("INFO", msg, isDebugEnabled)
195                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
196                                 }
197                         }
198                         else
199                         {
200                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
201                                 if(foundInAAI == true){
202                                         utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
203                                         msg = "ServiceInstance already exists in AAI:" + serviceInstanceName
204                                         utils.log("INFO", msg, isDebugEnabled)
205                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
206                                 }
207                         }
208                 } catch (BpmnError e) {
209                         throw e;
210                 } catch (Exception ex) {
211                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET. " + ex.getMessage()
212                         utils.log("INFO", msg, isDebugEnabled)
213                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
214                 }
215                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
216         }
217
218         public void postProcessAAIPUT(Execution execution) {
219                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
220                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
221                 String msg = ""
222                 try {
223                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
224                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
225                         if(succInAAI != true){
226                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
227                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
228                                 utils.logAudit("workflowException: " + workflowException)
229                                 if(workflowException != null){
230                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
231                                 }
232                         }
233                         else
234                         {
235                                 //start rollback set up
236                                 RollbackData rollbackData = new RollbackData()
237                                 def disableRollback = execution.getVariable("disableRollback")
238                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
239                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
240                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
241                                 rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
242                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
243                                 execution.setVariable("rollbackData", rollbackData)
244                         }
245
246                 } catch (BpmnError e) {
247                         throw e;
248                 } catch (Exception ex) {
249                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
250                         utils.log("INFO", msg, isDebugEnabled)
251                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
252                 }
253                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
254         }
255         
256         public void postProcessAAIGET2(Execution execution) {
257                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
258                 utils.log("INFO"," ***** postProcessAAIGET2 ***** ", isDebugEnabled)
259                 String msg = ""
260
261                 try {
262                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
263                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
264                         if(succInAAI != true){
265                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET2", + serviceInstanceName, isDebugEnabled)
266                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
267                                 utils.logAudit("workflowException: " + workflowException)
268                                 if(workflowException != null){
269                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
270                                 }
271                                 else
272                                 {
273                                         msg = "Failure in postProcessAAIGET2 GENGS_SuccessIndicator:" + succInAAI
274                                         utils.log("INFO", msg, isDebugEnabled)
275                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
276                                 }
277                         }
278                         else
279                         {
280                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
281                                 if(foundInAAI == true){
282                                         String aaiService = execution.getVariable("GENGS_service")
283                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "service-instance-name"))) {
284                                                 execution.setVariable("serviceInstanceName",  utils.getNodeText1(aaiService, "service-instance-name"))
285                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
286                                         }
287                                 }
288                         }
289                 } catch (BpmnError e) {
290                         throw e;
291                 } catch (Exception ex) {
292                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET2 " + ex.getMessage()
293                         utils.log("INFO", msg, isDebugEnabled)
294                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
295                 }
296                 utils.log("INFO"," *** Exit postProcessAAIGET2 *** ", isDebugEnabled)
297         }
298
299         public void preProcessRollback (Execution execution) {
300                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
301                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)
302                 try {
303                         
304                         Object workflowException = execution.getVariable("WorkflowException");
305
306                         if (workflowException instanceof WorkflowException) {
307                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
308                                 execution.setVariable("prevWorkflowException", workflowException);
309                                 //execution.setVariable("WorkflowException", null);
310                         }
311                 } catch (BpmnError e) {
312                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)
313                 } catch(Exception ex) {
314                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
315                         utils.log("INFO", msg, isDebugEnabled)
316                 }
317                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)
318         }
319
320         public void postProcessRollback (Execution execution) {
321                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
322                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)
323                 String msg = ""
324                 try {
325                         Object workflowException = execution.getVariable("prevWorkflowException");
326                         if (workflowException instanceof WorkflowException) {
327                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)
328                                 execution.setVariable("WorkflowException", workflowException);
329                         }
330                         execution.setVariable("rollbackData", null)
331                 } catch (BpmnError b) {
332                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)
333                         throw b;
334                 } catch(Exception ex) {
335                         msg = "Exception in postProcessRollback. " + ex.getMessage()
336                         utils.log("INFO", msg, isDebugEnabled)
337                 }
338                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)
339         }
340
341         public void preInitResourcesOperStatus(Execution execution){
342         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
343
344         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
345         try{
346             String serviceId = execution.getVariable("serviceInstanceId")
347             String operationId = execution.getVariable("operationId")
348             String operationType = execution.getVariable("operationType")
349             String resourceTemplateUUIDs = ""
350             String result = "processing"
351             String progress = "0"
352             String reason = ""
353             String operationContent = "Prepare service creation"
354             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
355             serviceId = UriUtils.encode(serviceId,"UTF-8")
356             execution.setVariable("serviceInstanceId", serviceId)
357             execution.setVariable("operationId", operationId)
358             execution.setVariable("operationType", operationType)
359             String incomingRequest = execution.getVariable("uuiRequest")
360             String resourcesStr = jsonUtil.getJsonValue(incomingRequest, "service.parameters.resources")  
361             List<String> resourceList = jsonUtil.StringArrayToList(execution, resourcesStr)   
362             for(String resource : resourceList){
363                     resourceTemplateUUIDs  = resourceTemplateUUIDs + jsonUtil.getJsonValue(resource, "resourceId") + ":"
364             }           
365
366             def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
367             execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
368             utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
369
370             String payload =
371                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
372                         xmlns:ns="http://org.openecomp.mso/requestsdb">
373                         <soapenv:Header/>
374                         <soapenv:Body>
375                             <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
376                             <serviceId>${serviceId}</serviceId>
377                             <operationId>${operationId}</operationId>
378                             <operationType>${operationType}</operationType>
379                             <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>
380                         </ns:initResourceOperationStatus>
381                     </soapenv:Body>
382                 </soapenv:Envelope>"""
383
384             payload = utils.formatXml(payload)
385             execution.setVariable("CVFMI_initResOperStatusRequest", payload)
386             utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
387             utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
388
389         }catch(Exception e){
390             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
391             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
392         }
393         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)  
394         }
395         
396         /**
397          * prepare resource create request
398          */
399         public void preResourceRequest(execution){
400             String resourceType = execution.getVariable("resourceType")
401             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
402             String serviceInstanceName = execution.getVariable("serviceInstanceName")
403             String nsServiceName = resourceType + "_" + serviceInstanceName
404             execution.setVariable("nsServiceName", nsServiceName)
405             utils.log("INFO", "Prepare VFC Request nsServiceName:" + nsServiceName, isDebugEnabled)
406         String globalSubscriberId = execution.getVariable("globalSubscriberId")
407         String serviceType = execution.getVariable("serviceType")
408         String serviceId = execution.getVariable("serviceInstanceId")
409         execution.setVariable("serviceId", serviceId)
410         String operationId = execution.getVariable("operationId")
411         String incomingRequest = execution.getVariable("uuiRequest")
412         String resourcesStr = jsonUtil.getJsonValue(incomingRequest, "service.parameters.resources")  
413         String nsServiceDescription = jsonUtil.getJsonValue(incomingRequest, "service.description")  
414         execution.setVariable("nsServiceDescription", nsServiceDescription)
415         utils.log("INFO", "Prepare VFC Request nsServiceDescription:" + nsServiceDescription, isDebugEnabled)
416         List<String> resourceList = jsonUtil.StringArrayToList(execution, resourcesStr)   
417         for(String resource : resourceList){
418             String resourceName = jsonUtil.getJsonValue(resource, "resourceName")  
419             if(StringUtils.containsIgnoreCase(resourceName, resourceType)){
420                 String resourceUUID  = jsonUtil.getJsonValue(resource, "resourceId")
421                 String resourceInvariantUUID  = jsonUtil.getJsonValue(resource, "resourceDefId")
422                 String resourceParameters = jsonUtil.getJsonValue(resource, "nsParameters")                
423                 execution.setVariable("resourceUUID", resourceUUID)
424                 execution.setVariable("resourceInvariantUUID", resourceInvariantUUID)
425                 execution.setVariable("resourceParameters", resourceParameters)
426                 utils.log("INFO", "Prepare VFC Request resourceType:" + resourceType, isDebugEnabled)
427                 utils.log("INFO", "Prepare VFC Request resourceUUID:" + resourceUUID, isDebugEnabled)
428                 utils.log("INFO", "Prepare VFC Request resourceParameters:" + resourceParameters, isDebugEnabled)
429             } 
430         }
431             utils.log("INFO", "Prepare VFC Request finished", isDebugEnabled)
432         }
433         
434            /**
435      * post config request.
436      */
437         public void postConfigRequest(execution){
438             //now do noting
439         }
440 }