ca67d5dcddc336428ea912b3d8e53e780dddd81e
[so.git] /
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                         //aai serviceType and Role can be setted as fixed value now.
131                         String aaiServiceType = "voLTE type"
132                         String aaiServiceRole = "voLTE role"
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>${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>${serviceInstanceId}</service-instance-id>
152                                 <service-instance-name>${serviceInstanceName}</service-instance-name>
153                                         <service-type>${aaiServiceType}</service-type>
154                                         <service-role>${aaiServiceRole}</service-role>
155                                         ${statusLine}
156                                     <model-invariant-id>${modelInvariantUuid}</model-invariant-id>
157                                     <model-version-id>${modelUuid}</model-version-id>
158                                         </service-instance>""".trim()                                   
159                         execution.setVariable("serviceInstanceData", serviceInstanceData)
160                         utils.logAudit(serviceInstanceData)
161                         utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
162                         utils.log("INFO", " 'payload' to create Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
163
164                 } catch (BpmnError e) {
165                         throw e;
166                 } catch (Exception ex){
167                         msg = "Exception in preProcessRequest " + ex.getMessage()
168                         utils.log("INFO", msg, isDebugEnabled)
169                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
170                 }
171                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
172         }
173
174         public void postProcessAAIGET(Execution execution) {
175                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
176                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
177                 String msg = ""
178
179                 try {
180                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
181                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
182                         if(succInAAI != true){
183                                 utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceName, isDebugEnabled)
184                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
185                                 utils.logAudit("workflowException: " + workflowException)
186                                 if(workflowException != null){
187                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
188                                 }
189                                 else
190                                 {
191                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
192                                         utils.log("INFO", msg, isDebugEnabled)
193                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
194                                 }
195                         }
196                         else
197                         {
198                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
199                                 if(foundInAAI == true){
200                                         utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
201                                         msg = "ServiceInstance already exists in AAI:" + serviceInstanceName
202                                         utils.log("INFO", msg, isDebugEnabled)
203                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
204                                 }
205                         }
206                 } catch (BpmnError e) {
207                         throw e;
208                 } catch (Exception ex) {
209                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET. " + ex.getMessage()
210                         utils.log("INFO", msg, isDebugEnabled)
211                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
212                 }
213                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
214         }
215
216         public void postProcessAAIPUT(Execution execution) {
217                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
218                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
219                 String msg = ""
220                 try {
221                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
222                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
223                         if(succInAAI != true){
224                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
225                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
226                                 utils.logAudit("workflowException: " + workflowException)
227                                 if(workflowException != null){
228                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
229                                 }
230                         }
231                         else
232                         {
233                                 //start rollback set up
234                                 RollbackData rollbackData = new RollbackData()
235                                 def disableRollback = execution.getVariable("disableRollback")
236                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
237                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
238                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
239                                 rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
240                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
241                                 execution.setVariable("rollbackData", rollbackData)
242                         }
243
244                 } catch (BpmnError e) {
245                         throw e;
246                 } catch (Exception ex) {
247                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
248                         utils.log("INFO", msg, isDebugEnabled)
249                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
250                 }
251                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
252         }
253         
254         public void postProcessAAIGET2(Execution execution) {
255                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
256                 utils.log("INFO"," ***** postProcessAAIGET2 ***** ", isDebugEnabled)
257                 String msg = ""
258
259                 try {
260                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
261                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
262                         if(succInAAI != true){
263                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET2", + serviceInstanceName, isDebugEnabled)
264                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
265                                 utils.logAudit("workflowException: " + workflowException)
266                                 if(workflowException != null){
267                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
268                                 }
269                                 else
270                                 {
271                                         msg = "Failure in postProcessAAIGET2 GENGS_SuccessIndicator:" + succInAAI
272                                         utils.log("INFO", msg, isDebugEnabled)
273                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
274                                 }
275                         }
276                         else
277                         {
278                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
279                                 if(foundInAAI == true){
280                                         String aaiService = execution.getVariable("GENGS_service")
281                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "service-instance-name"))) {
282                                                 execution.setVariable("serviceInstanceName",  utils.getNodeText1(aaiService, "service-instance-name"))
283                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
284                                         }
285                                 }
286                         }
287                 } catch (BpmnError e) {
288                         throw e;
289                 } catch (Exception ex) {
290                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET2 " + ex.getMessage()
291                         utils.log("INFO", msg, isDebugEnabled)
292                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
293                 }
294                 utils.log("INFO"," *** Exit postProcessAAIGET2 *** ", isDebugEnabled)
295         }
296
297         public void preProcessRollback (Execution execution) {
298                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
299                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)
300                 try {
301                         
302                         Object workflowException = execution.getVariable("WorkflowException");
303
304                         if (workflowException instanceof WorkflowException) {
305                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
306                                 execution.setVariable("prevWorkflowException", workflowException);
307                                 //execution.setVariable("WorkflowException", null);
308                         }
309                 } catch (BpmnError e) {
310                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)
311                 } catch(Exception ex) {
312                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
313                         utils.log("INFO", msg, isDebugEnabled)
314                 }
315                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)
316         }
317
318         public void postProcessRollback (Execution execution) {
319                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
320                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)
321                 String msg = ""
322                 try {
323                         Object workflowException = execution.getVariable("prevWorkflowException");
324                         if (workflowException instanceof WorkflowException) {
325                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)
326                                 execution.setVariable("WorkflowException", workflowException);
327                         }
328                         execution.setVariable("rollbackData", null)
329                 } catch (BpmnError b) {
330                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)
331                         throw b;
332                 } catch(Exception ex) {
333                         msg = "Exception in postProcessRollback. " + ex.getMessage()
334                         utils.log("INFO", msg, isDebugEnabled)
335                 }
336                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)
337         }
338
339         public void preInitResourcesOperStatus(Execution execution){
340         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
341
342         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
343         try{
344             String serviceId = execution.getVariable("serviceInstanceId")
345             String operationId = execution.getVariable("operationId")
346             String operationType = execution.getVariable("operationType")
347             String resourceTemplateUUIDs = ""
348             String result = "processing"
349             String progress = "0"
350             String reason = ""
351             String operationContent = "Prepare service creation"
352             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
353             serviceId = UriUtils.encode(serviceId,"UTF-8")
354             execution.setVariable("serviceInstanceId", serviceId)
355             execution.setVariable("operationId", operationId)
356             execution.setVariable("operationType", operationType)
357             String incomingRequest = execution.getVariable("uuiRequest")
358             String resourcesStr = jsonUtil.getJsonValue(incomingRequest, "service.parameters.resources")  
359             List<String> resourceList = jsonUtil.StringArrayToList(execution, resourcesStr)   
360             for(String resource : resourceList){
361                     resourceTemplateUUIDs  = resourceTemplateUUIDs + jsonUtil.getJsonValue(resource, "resourceDefId") + ":"
362             }           
363
364             def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
365             execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
366             utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
367
368             String payload =
369                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
370                         xmlns:ns="http://org.openecomp.mso/requestsdb">
371                         <soapenv:Header/>
372                         <soapenv:Body>
373                             <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
374                             <serviceId>${serviceId}</serviceId>
375                             <operationId>${operationId}</operationId>
376                             <operationType>${operationType}</operationType>
377                             <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>
378                         </ns:initResourceOperationStatus>
379                     </soapenv:Body>
380                 </soapenv:Envelope>"""
381
382             payload = utils.formatXml(payload)
383             execution.setVariable("CVFMI_initResOperStatusRequest", payload)
384             utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
385             utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
386
387         }catch(Exception e){
388             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
389             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
390         }
391         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)  
392         }
393         
394         /**
395          * prepare resource create request
396          */
397         public void preResourceRequest(execution){
398             String resourceType = execution.getVariable("resourceType")
399             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
400             String serviceInstanceName = execution.getVariable("serviceInstanceName")
401             String nsServiceName = resourceType + "_" + serviceInstanceName
402             execution.setVariable("nsServiceName", nsServiceName)
403             utils.log("INFO", "Prepare VFC Request nsServiceName:" + nsServiceName, isDebugEnabled)
404         String globalSubscriberId = execution.getVariable("globalSubscriberId")
405         String serviceType = execution.getVariable("serviceType")
406         String serviceId = execution.getVariable("serviceInstanceId")
407         execution.setVariable("serviceId", serviceId)
408         String operationId = execution.getVariable("operationId")
409         String incomingRequest = execution.getVariable("uuiRequest")
410         String resourcesStr = jsonUtil.getJsonValue(incomingRequest, "service.parameters.resources")  
411         String nsServiceDescription = jsonUtil.getJsonValue(incomingRequest, "service.description")  
412         execution.setVariable("nsServiceDescription", nsServiceDescription)
413         utils.log("INFO", "Prepare VFC Request nsServiceDescription:" + nsServiceDescription, isDebugEnabled)
414         List<String> resourceList = jsonUtil.StringArrayToList(execution, resourcesStr)   
415         for(String resource : resourceList){
416             String resourceName = jsonUtil.getJsonValue(resource, "resourceName")  
417             if(StringUtils.containsIgnoreCase(resourceName, resourceType)){
418                 String resourceUUID  = jsonUtil.getJsonValue(resource, "resourceDefId")
419                 String resourceParameters = jsonUtil.getJsonValue(resource, "nsParameters")                
420                 execution.setVariable("resourceUUID", resourceUUID)
421                 execution.setVariable("resourceParameters", resourceParameters)
422                 utils.log("INFO", "Prepare VFC Request resourceType:" + resourceType, isDebugEnabled)
423                 utils.log("INFO", "Prepare VFC Request resourceUUID:" + resourceUUID, isDebugEnabled)
424                 utils.log("INFO", "Prepare VFC Request resourceParameters:" + resourceParameters, isDebugEnabled)
425             } 
426         }
427             utils.log("INFO", "Prepare VFC Request finished", isDebugEnabled)
428         }
429 }