Merge "move comparemodel before syncresponse updateE2E"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoUpdateE2EServiceInstance.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved. \r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
21 \r
22 import static org.apache.commons.lang3.StringUtils.*;\r
23 import groovy.xml.XmlUtil\r
24 import groovy.json.*\r
25 import groovy.util.XmlParser\r
26 \r
27 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition\r
28 import org.openecomp.mso.bpmn.core.domain.ServiceInstance\r
29 import org.openecomp.mso.bpmn.core.domain.ModelInfo\r
30 import org.openecomp.mso.bpmn.core.domain.Resource\r
31 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
32 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
33 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
34 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
35 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
36 import org.openecomp.mso.bpmn.core.RollbackData\r
37 import org.openecomp.mso.bpmn.core.WorkflowException\r
38 import org.openecomp.mso.rest.APIResponse;\r
39 import org.openecomp.mso.rest.RESTClient\r
40 import org.openecomp.mso.rest.RESTConfig\r
41 \r
42 import java.util.UUID;\r
43 import javax.xml.parsers.DocumentBuilder\r
44 import javax.xml.parsers.DocumentBuilderFactory\r
45 \r
46 import org.camunda.bpm.engine.delegate.BpmnError\r
47 import org.camunda.bpm.engine.delegate.DelegateExecution\r
48 import org.json.JSONObject;\r
49 import org.json.JSONArray;\r
50 import org.apache.commons.lang3.*\r
51 import org.apache.commons.codec.binary.Base64;\r
52 import org.springframework.web.util.UriUtils;\r
53 \r
54 \r
55 /**\r
56  * This groovy class supports the <class>DoUpdateE2EServiceInstance.bpmn</class> process.\r
57  *\r
58  * Inputs:\r
59  * @param - msoRequestId\r
60  * @param - globalSubscriberId\r
61  * @param - serviceType\r
62  * @param - serviceInstanceId\r
63  * @param - serviceInstanceName\r
64  * @param - serviceModelInfo\r
65  * @param - productFamilyId\r
66  * @param - uuiRequest\r
67  * @param - serviceDecomposition_Target\r
68  * @param - serviceDecomposition_Original\r
69  * @param - addResourceList\r
70  * @param - delResourceList\r
71  * \r
72  * Outputs:\r
73  * @param - rollbackData (localRB->null)\r
74  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)\r
75  * @param - WorkflowException\r
76  */\r
77 public class DoUpdateE2EServiceInstance extends AbstractServiceTaskProcessor {\r
78 \r
79         String Prefix="DUPDSI_"\r
80         private static final String DebugFlag = "isDebugEnabled"\r
81         \r
82         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
83         JsonUtils jsonUtil = new JsonUtils()\r
84 \r
85         public void preProcessRequest (DelegateExecution execution) {\r
86                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
87                 utils.log("INFO"," ***** Enter DoUpdateE2EServiceInstance preProcessRequest *****",  isDebugEnabled)\r
88                 \r
89                 String msg = "" \r
90 \r
91                 try {\r
92                         execution.setVariable("prefix", Prefix)\r
93                         //Inputs\r
94                         //for AAI GET & PUT & SDNC assignToplology\r
95                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId\r
96                         utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)\r
97                         \r
98                         //for AAI PUT & SDNC assignTopology\r
99                         String serviceType = execution.getVariable("serviceType")\r
100                         utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)\r
101                         \r
102                         //for SDNC assignTopology\r
103                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId\r
104 \r
105                         if (isBlank(globalSubscriberId)) {\r
106                                 msg = "Input globalSubscriberId is null"\r
107                                 utils.log("INFO", msg, isDebugEnabled)\r
108                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
109                         }\r
110                         \r
111                         if (isBlank(serviceType)) {\r
112                                 msg = "Input serviceType is null"\r
113                                 utils.log("INFO", msg, isDebugEnabled)\r
114                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
115                         }\r
116                         \r
117                         //Generated in parent for AAI \r
118                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
119                         if (isBlank(serviceInstanceId)){\r
120                                 msg = "Input serviceInstanceId is null"\r
121                                 utils.log("INFO", msg, isDebugEnabled)\r
122                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
123                         }\r
124 \r
125                         String serviceInstanceName = execution.getVariable("serviceInstanceName")\r
126                         \r
127                         // user params\r
128                         String uuiRequest = execution.getVariable("uuiRequest")\r
129                         utils.log("INFO","uuiRequest: " + uuiRequest, isDebugEnabled)\r
130                                 \r
131                 } catch (BpmnError e) {\r
132                         throw e;\r
133                 } catch (Exception ex){\r
134                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
135                         utils.log("INFO", msg, isDebugEnabled)\r
136                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
137                 }\r
138                 utils.log("INFO", "======== COMPLETED preProcessRequest Process ======== ", isDebugEnabled)  \r
139         }\r
140 \r
141         \r
142         public void preInitResourcesOperStatus(DelegateExecution execution){\r
143         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
144 \r
145         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)\r
146         try{\r
147             String serviceId = execution.getVariable("serviceInstanceId")\r
148             String operationId = execution.getVariable("operationId")\r
149             String operationType = execution.getVariable("operationType")\r
150             String resourceTemplateUUIDs = ""\r
151             String result = "processing"\r
152             String progress = "0"\r
153             String reason = ""\r
154             String operationContent = "Prepare service updating"\r
155             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)\r
156             serviceId = UriUtils.encode(serviceId,"UTF-8")\r
157             execution.setVariable("serviceInstanceId", serviceId)\r
158             execution.setVariable("operationId", operationId)\r
159             execution.setVariable("operationType", operationType)\r
160 \r
161                         List<Resource> resourceList = new ArrayList<String>()\r
162                         List<Resource> addResourceList =  execution.getVariable("addResourceList")\r
163                         List<Resource> delResourceList =  execution.getVariable("delResourceList")\r
164                         resourceList.addAll(addResourceList)\r
165                         resourceList.addAll(delResourceList)\r
166                         for(Resource resource : resourceList){\r
167                                 resourceTemplateUUIDs  = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"\r
168                         }\r
169                         \r
170                         def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"\r
171                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)\r
172                         utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)\r
173 \r
174                         String payload =\r
175                                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
176                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
177                         <soapenv:Header/>\r
178                         <soapenv:Body>\r
179                             <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">\r
180                                                                 <serviceId>${serviceId}</serviceId>\r
181                                                                 <operationId>${operationId}</operationId>\r
182                                                                 <operationType>${operationType}</operationType>\r
183                                                                 <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>\r
184                             </ns:initResourceOperationStatus>\r
185                         </soapenv:Body>\r
186                         </soapenv:Envelope>"""\r
187 \r
188                         payload = utils.formatXml(payload)\r
189                         execution.setVariable("CVFMI_initResOperStatusRequest", payload)\r
190                         utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)\r
191                         utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)\r
192 \r
193         }catch(Exception e){\r
194             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)\r
195             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())\r
196         }\r
197         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)  \r
198     }\r
199     \r
200 \r
201     public void preProcessForAddResource(DelegateExecution execution) {\r
202         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
203                 utils.log("INFO"," ***** preProcessForAddResource ***** ", isDebugEnabled)\r
204                 \r
205             execution.setVariable("operationType", "create")\r
206                 \r
207                 def hasResourcetoAdd = false\r
208                 List<Resource> addResourceList =  execution.getVariable("addResourceList")\r
209                 if(addResourceList != null && !addResourceList.isEmpty()) {\r
210                         hasResourcetoAdd = true                 \r
211                 }\r
212                 execution.setVariable("hasResourcetoAdd", hasResourcetoAdd)\r
213                 \r
214         \r
215                 utils.log("INFO"," *** Exit preProcessForAddResource *** ", isDebugEnabled)\r
216     }\r
217 \r
218     public void postProcessForAddResource(DelegateExecution execution) {\r
219         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
220                 utils.log("INFO"," ***** postProcessForAddResource ***** ", isDebugEnabled)\r
221                 \r
222                 execution.setVariable("operationType", "update")\r
223 \r
224                 utils.log("INFO"," *** Exit postProcessForAddResource *** ", isDebugEnabled)\r
225     }\r
226     \r
227     public void preProcessForDeleteResource(DelegateExecution execution) {\r
228         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
229                 utils.log("INFO"," ***** preProcessForDeleteResource ***** ", isDebugEnabled)\r
230                 \r
231                 execution.setVariable("operationType", "delete")\r
232                 \r
233                 def hasResourcetoDelete = false\r
234                 List<Resource> delResourceList =  execution.getVariable("delResourceList")\r
235                 if(delResourceList != null && !delResourceList.isEmpty()) {\r
236                         hasResourcetoDelete = true\r
237                 }\r
238                 execution.setVariable("hasResourcetoDelete", hasResourcetoDelete)\r
239                 \r
240                 execution.setVariable("resourceInstanceIDs", execution.getVariable("serviceRelationShip"))\r
241                 \r
242                 utils.log("INFO"," *** Exit preProcessForDeleteResource *** ", isDebugEnabled)\r
243 \r
244     }\r
245 \r
246     public void postProcessForDeleteResource(DelegateExecution execution) {\r
247         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
248                 utils.log("INFO"," ***** postProcessForDeleteResource ***** ", isDebugEnabled)\r
249                 \r
250                 execution.setVariable("operationType", "update")\r
251 \r
252                 utils.log("INFO"," *** Exit postProcessForDeleteResource *** ", isDebugEnabled)\r
253     } \r
254     \r
255         public void preProcessAAIGET(DelegateExecution execution) {\r
256         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   \r
257         }\r
258         \r
259         public void postProcessAAIGET(DelegateExecution execution) {\r
260                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
261                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)\r
262                 String msg = ""\r
263 \r
264                 try {\r
265                         String serviceInstanceName = execution.getVariable("serviceInstanceName")\r
266                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")\r
267                         if(!succInAAI){\r
268                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + serviceInstanceName, isDebugEnabled)\r
269                                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
270                                 utils.logAudit("workflowException: " + workflowException)\r
271                                 if(workflowException != null){\r
272                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())\r
273                                 }\r
274                                 else\r
275                                 {\r
276                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI\r
277                                         utils.log("INFO", msg, isDebugEnabled)\r
278                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)\r
279                                 }\r
280                         }\r
281                         else\r
282                         {\r
283                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")\r
284                                 if(foundInAAI){\r
285                                         String aaiService = execution.getVariable("GENGS_service")\r
286                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "resource-version"))) {\r
287                                                 execution.setVariable("serviceInstanceVersion",  utils.getNodeText1(aaiService, "resource-version"))\r
288                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)\r
289                                         }\r
290                                 }\r
291                         }\r
292                 } catch (BpmnError e) {\r
293                         throw e;\r
294                 } catch (Exception ex) {\r
295                         msg = "Exception in DoUpdateE2EServiceInstance.postProcessAAIGET " + ex.getMessage()\r
296                         utils.log("INFO", msg, isDebugEnabled)\r
297                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
298                 }\r
299                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)\r
300         }\r
301 \r
302         public void preProcessAAIPUT(DelegateExecution execution) {             \r
303                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'\r
304                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
305                 utils.log("INFO","Entered " + method, isDebugEnabled)\r
306                 String msg = ""\r
307                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)\r
308 \r
309                 String modelUuid = execution.getVariable("modelUuid")\r
310                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion")\r
311                 execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)\r
312 \r
313                 AaiUtil aaiUriUtil = new AaiUtil(this)\r
314                 utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled) \r
315                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)\r
316                 utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)\r
317                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)\r
318                 utils.log("INFO","namespace: " + namespace, isDebugEnabled)\r
319 \r
320                 String serviceInstanceData =\r
321                                 """<service-instance xmlns=\"${namespace}\">\r
322                                <model-version-id">${modelUuid}</model-version-id>\r
323                                  </service-instance>""".trim()\r
324 \r
325                 execution.setVariable("serviceInstanceData", serviceInstanceData)\r
326                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)\r
327                 utils.logAudit(serviceInstanceData)\r
328                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)\r
329                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)\r
330         \r
331                 utils.log("INFO", "Exited " + method, isDebugEnabled)\r
332         }       \r
333         \r
334         public void postProcessAAIPUT(DelegateExecution execution) {\r
335                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
336                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)\r
337                 String msg = ""\r
338                 try {\r
339                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
340                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")\r
341                         if(!succInAAI){\r
342                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)\r
343                                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
344                                 utils.logAudit("workflowException: " + workflowException)\r
345                                 if(workflowException != null){\r
346                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())\r
347                                 }\r
348                         }\r
349                         else\r
350                         {\r
351                                 //start rollback set up\r
352                                 RollbackData rollbackData = new RollbackData()\r
353                                 def disableRollback = execution.getVariable("disableRollback")\r
354                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())\r
355                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")\r
356                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)\r
357                                 rollbackData.put("SERVICEINSTANCE", "serviceType", execution.getVariable("serviceType"))\r
358                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))\r
359                                 execution.setVariable("rollbackData", rollbackData)\r
360                         }\r
361 \r
362                 } catch (BpmnError e) {\r
363                         throw e;\r
364                 } catch (Exception ex) {\r
365                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()\r
366                         utils.log("INFO", msg, isDebugEnabled)\r
367                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
368                 }\r
369                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)\r
370         }       \r
371 \r
372         public void preProcessRollback (DelegateExecution execution) {\r
373                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
374                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)\r
375                 try {\r
376                         \r
377                         Object workflowException = execution.getVariable("WorkflowException");\r
378 \r
379                         if (workflowException instanceof WorkflowException) {\r
380                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)\r
381                                 execution.setVariable("prevWorkflowException", workflowException);\r
382                                 //execution.setVariable("WorkflowException", null);\r
383                         }\r
384                 } catch (BpmnError e) {\r
385                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)\r
386                 } catch(Exception ex) {\r
387                         String msg = "Exception in preProcessRollback. " + ex.getMessage()\r
388                         utils.log("INFO", msg, isDebugEnabled)\r
389                 }\r
390                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)\r
391         }\r
392 \r
393         public void postProcessRollback (DelegateExecution execution) {\r
394                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
395                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)\r
396                 String msg = ""\r
397                 try {\r
398                         Object workflowException = execution.getVariable("prevWorkflowException");\r
399                         if (workflowException instanceof WorkflowException) {\r
400                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)\r
401                                 execution.setVariable("WorkflowException", workflowException);\r
402                         }\r
403                         execution.setVariable("rollbackData", null)\r
404                 } catch (BpmnError b) {\r
405                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)\r
406                         throw b;\r
407                 } catch(Exception ex) {\r
408                         msg = "Exception in postProcessRollback. " + ex.getMessage()\r
409                         utils.log("INFO", msg, isDebugEnabled)\r
410                 }\r
411                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)\r
412         }\r
413 \r
414         \r
415         public void postConfigRequest(execution){\r
416             //now do noting\r
417         }\r
418 \r
419         \r
420 }\r
421         \r