f29db9252f03e412c1ea9b94708f0a6d2dec4745
[so.git] /
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                         // target model uuid\r
130                         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")\r
131                         execution.setVariable("modelUuid", modelUuid)\r
132                         \r
133                         utils.log("INFO","modelUuid: " + modelUuid, isDebugEnabled)\r
134                                 \r
135                 } catch (BpmnError e) {\r
136                         throw e;\r
137                 } catch (Exception ex){\r
138                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
139                         utils.log("INFO", msg, isDebugEnabled)\r
140                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
141                 }\r
142                 utils.log("INFO", "======== COMPLETED preProcessRequest Process ======== ", isDebugEnabled)  \r
143         }\r
144 \r
145         \r
146         public void preInitResourcesOperStatus(DelegateExecution execution){\r
147         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
148 \r
149         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)\r
150         try{\r
151             String serviceId = execution.getVariable("serviceInstanceId")\r
152             String operationId = execution.getVariable("operationId")\r
153             String operationType = execution.getVariable("operationType")\r
154             String resourceTemplateUUIDs = ""\r
155             String result = "processing"\r
156             String progress = "0"\r
157             String reason = ""\r
158             String operationContent = "Prepare service updating"\r
159             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)\r
160             serviceId = UriUtils.encode(serviceId,"UTF-8")\r
161             execution.setVariable("serviceInstanceId", serviceId)\r
162             execution.setVariable("operationId", operationId)\r
163             execution.setVariable("operationType", operationType)\r
164 \r
165                         List<Resource> resourceList = new ArrayList<String>()\r
166                         List<Resource> addResourceList =  execution.getVariable("addResourceList")\r
167                         List<Resource> delResourceList =  execution.getVariable("delResourceList")\r
168                         resourceList.addAll(addResourceList)\r
169                         resourceList.addAll(delResourceList)\r
170                         for(Resource resource : resourceList){\r
171                                 resourceTemplateUUIDs  = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"\r
172                         }\r
173                         \r
174                         def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"\r
175                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)\r
176                         utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)\r
177 \r
178                         String payload =\r
179                                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
180                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
181                         <soapenv:Header/>\r
182                         <soapenv:Body>\r
183                             <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">\r
184                                                                 <serviceId>${serviceId}</serviceId>\r
185                                                                 <operationId>${operationId}</operationId>\r
186                                                                 <operationType>${operationType}</operationType>\r
187                                                                 <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>\r
188                             </ns:initResourceOperationStatus>\r
189                         </soapenv:Body>\r
190                         </soapenv:Envelope>"""\r
191 \r
192                         payload = utils.formatXml(payload)\r
193                         execution.setVariable("CVFMI_initResOperStatusRequest", payload)\r
194                         utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)\r
195                         utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)\r
196 \r
197         }catch(Exception e){\r
198             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)\r
199             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())\r
200         }\r
201         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)  \r
202     }\r
203     \r
204 \r
205     public void preProcessForAddResource(DelegateExecution execution) {\r
206         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
207                 utils.log("INFO"," ***** preProcessForAddResource ***** ", isDebugEnabled)\r
208                 \r
209             execution.setVariable("operationType", "create")\r
210                 \r
211                 execution.setVariable("hasResourcetoAdd", false)\r
212                 List<Resource> addResourceList =  execution.getVariable("addResourceList")\r
213                 if(addResourceList != null && !addResourceList.isEmpty()) {\r
214                         execution.setVariable("hasResourcetoAdd", true)                 \r
215                 }\r
216         \r
217                 utils.log("INFO"," *** Exit preProcessForAddResource *** ", isDebugEnabled)\r
218     }\r
219 \r
220     public void postProcessForAddResource(DelegateExecution execution) {\r
221         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
222                 utils.log("INFO"," ***** postProcessForAddResource ***** ", isDebugEnabled)\r
223                 \r
224                 execution.setVariable("operationType", "update")\r
225 \r
226                 utils.log("INFO"," *** Exit postProcessForAddResource *** ", isDebugEnabled)\r
227     }\r
228     \r
229     public void preProcessForDeleteResource(DelegateExecution execution) {\r
230         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
231                 utils.log("INFO"," ***** preProcessForDeleteResource ***** ", isDebugEnabled)\r
232                 \r
233                 execution.setVariable("operationType", "delete")\r
234                 \r
235                 execution.setVariable("hasResourcetoDelete", false)\r
236                 List<Resource> delResourceList =  execution.getVariable("delResourceList")\r
237                 if(delResourceList != null && !delResourceList.isEmpty()) {\r
238                         execution.setVariable("hasResourcetoDelete", true)\r
239                 }                       \r
240                 \r
241                 execution.setVariable("resourceInstanceIDs", execution.getVariable("serviceRelationShip"))\r
242                 \r
243                 utils.log("INFO"," *** Exit preProcessForDeleteResource *** ", isDebugEnabled)\r
244 \r
245     }\r
246 \r
247     public void postProcessForDeleteResource(DelegateExecution execution) {\r
248         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
249                 utils.log("INFO"," ***** postProcessForDeleteResource ***** ", isDebugEnabled)\r
250                 \r
251                 execution.setVariable("operationType", "update")\r
252 \r
253                 utils.log("INFO"," *** Exit postProcessForDeleteResource *** ", isDebugEnabled)\r
254     } \r
255     \r
256         public void preProcessAAIGET(DelegateExecution execution) {\r
257         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   \r
258         }\r
259         \r
260         public void postProcessAAIGET(DelegateExecution execution) {\r
261                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
262                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)\r
263                 String msg = ""\r
264 \r
265                 try {\r
266                         String serviceInstanceName = execution.getVariable("serviceInstanceName")\r
267                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")\r
268                         if(!succInAAI){\r
269                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + serviceInstanceName, isDebugEnabled)\r
270                                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
271                                 utils.logAudit("workflowException: " + workflowException)\r
272                                 if(workflowException != null){\r
273                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())\r
274                                 }\r
275                                 else\r
276                                 {\r
277                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI\r
278                                         utils.log("INFO", msg, isDebugEnabled)\r
279                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)\r
280                                 }\r
281                         }\r
282                         else\r
283                         {\r
284                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")\r
285                                 if(foundInAAI){\r
286                                         String aaiService = execution.getVariable("GENGS_service")\r
287                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "resource-version"))) {\r
288                                                 execution.setVariable("serviceInstanceVersion",  utils.getNodeText1(aaiService, "resource-version"))\r
289                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)\r
290                                         }\r
291                                 }\r
292                         }\r
293                 } catch (BpmnError e) {\r
294                         throw e;\r
295                 } catch (Exception ex) {\r
296                         msg = "Exception in DoUpdateE2EServiceInstance.postProcessAAIGET " + ex.getMessage()\r
297                         utils.log("INFO", msg, isDebugEnabled)\r
298                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
299                 }\r
300                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)\r
301         }\r
302 \r
303         public void preProcessAAIPUT(DelegateExecution execution) {             \r
304                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'\r
305                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
306                 utils.log("INFO","Entered " + method, isDebugEnabled)\r
307                 String msg = ""\r
308                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)\r
309 \r
310                 String modelUuid = execution.getVariable("modelUuid")\r
311                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion")\r
312                 execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)\r
313 \r
314                 AaiUtil aaiUriUtil = new AaiUtil(this)\r
315                 utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled) \r
316                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)\r
317                 utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)\r
318                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)\r
319                 utils.log("INFO","namespace: " + namespace, isDebugEnabled)\r
320 \r
321                 //update target model to aai\r
322                 String serviceInstanceData =\r
323                                 """<service-instance xmlns=\"${namespace}\">\r
324                                <model-version-id">${modelUuid}</model-version-id>\r
325                                  </service-instance>""".trim()\r
326 \r
327                 execution.setVariable("serviceInstanceData", serviceInstanceData)\r
328                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)\r
329                 utils.logAudit(serviceInstanceData)\r
330                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)\r
331                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)\r
332         \r
333                 utils.log("INFO", "Exited " + method, isDebugEnabled)\r
334         }       \r
335         \r
336         public void postProcessAAIPUT(DelegateExecution execution) {\r
337                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
338                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)\r
339                 String msg = ""\r
340                 try {\r
341                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
342                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")\r
343                         if(!succInAAI){\r
344                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)\r
345                                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
346                                 utils.logAudit("workflowException: " + workflowException)\r
347                                 if(workflowException != null){\r
348                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())\r
349                                 }\r
350                         }\r
351                         else\r
352                         {\r
353                                 //start rollback set up\r
354                                 RollbackData rollbackData = new RollbackData()\r
355                                 def disableRollback = execution.getVariable("disableRollback")\r
356                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())\r
357                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")\r
358                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)\r
359                                 rollbackData.put("SERVICEINSTANCE", "serviceType", execution.getVariable("serviceType"))\r
360                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))\r
361                                 execution.setVariable("rollbackData", rollbackData)\r
362                         }\r
363 \r
364                 } catch (BpmnError e) {\r
365                         throw e;\r
366                 } catch (Exception ex) {\r
367                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()\r
368                         utils.log("INFO", msg, isDebugEnabled)\r
369                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
370                 }\r
371                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)\r
372         }       \r
373 \r
374         public void preProcessRollback (DelegateExecution execution) {\r
375                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
376                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)\r
377                 try {\r
378                         \r
379                         Object workflowException = execution.getVariable("WorkflowException");\r
380 \r
381                         if (workflowException instanceof WorkflowException) {\r
382                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)\r
383                                 execution.setVariable("prevWorkflowException", workflowException);\r
384                                 //execution.setVariable("WorkflowException", null);\r
385                         }\r
386                 } catch (BpmnError e) {\r
387                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)\r
388                 } catch(Exception ex) {\r
389                         String msg = "Exception in preProcessRollback. " + ex.getMessage()\r
390                         utils.log("INFO", msg, isDebugEnabled)\r
391                 }\r
392                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)\r
393         }\r
394 \r
395         public void postProcessRollback (DelegateExecution execution) {\r
396                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
397                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)\r
398                 String msg = ""\r
399                 try {\r
400                         Object workflowException = execution.getVariable("prevWorkflowException");\r
401                         if (workflowException instanceof WorkflowException) {\r
402                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)\r
403                                 execution.setVariable("WorkflowException", workflowException);\r
404                         }\r
405                         execution.setVariable("rollbackData", null)\r
406                 } catch (BpmnError b) {\r
407                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)\r
408                         throw b;\r
409                 } catch(Exception ex) {\r
410                         msg = "Exception in postProcessRollback. " + ex.getMessage()\r
411                         utils.log("INFO", msg, isDebugEnabled)\r
412                 }\r
413                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)\r
414         }\r
415 \r
416         \r
417         public void postConfigRequest(execution){\r
418             //now do noting\r
419         }\r
420 \r
421         \r
422 }\r
423         \r