Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / CreateSDNCNetworkResource.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - 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 \r
21 package org.openecomp.mso.bpmn.infrastructure.scripts\r
22 \r
23 import org.json.JSONObject\r
24 import org.json.XML;\r
25 \r
26 import static org.apache.commons.lang3.StringUtils.*;\r
27 import groovy.xml.XmlUtil\r
28 import groovy.json.*\r
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor \r
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
31 import org.openecomp.mso.bpmn.common.recipe.ResourceInput;\r
32 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder \r
33 import org.openecomp.mso.bpmn.core.WorkflowException \r
34 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
35 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder\r
36 import org.openecomp.mso.rest.APIResponse\r
37 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
38 \r
39 import java.util.UUID;\r
40 \r
41 import org.camunda.bpm.engine.delegate.BpmnError \r
42 import org.camunda.bpm.engine.delegate.DelegateExecution\r
43 import org.apache.commons.lang3.*\r
44 import org.apache.commons.codec.binary.Base64;\r
45 import org.springframework.web.util.UriUtils \r
46 import org.openecomp.mso.rest.RESTClient \r
47 import org.openecomp.mso.rest.RESTConfig\r
48 import org.openecomp.mso.rest.APIResponse;\r
49 \r
50 /**\r
51  * This groovy class supports the <class>CreateSDNCCNetworkResource.bpmn</class> process.\r
52  * flow for SDNC Network Resource Create\r
53  */\r
54 public class CreateSDNCNetworkResource extends AbstractServiceTaskProcessor {\r
55 \r
56     String Prefix="CRESDNCRES_"\r
57             \r
58     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
59 \r
60     JsonUtils jsonUtil = new JsonUtils()\r
61 \r
62     SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()\r
63     \r
64     public void preProcessRequest(DelegateExecution execution){\r
65         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
66         utils.log("INFO"," ***** Started preProcessRequest *****",  isDebugEnabled)\r
67         try {           \r
68             \r
69             //get bpmn inputs from resource request.\r
70             String requestId = execution.getVariable("mso-request-id")\r
71             String requestAction = execution.getVariable("requestAction")\r
72             utils.log("INFO","The requestAction is: " + requestAction,  isDebugEnabled)\r
73             String recipeParamsFromRequest = execution.getVariable("recipeParams")\r
74             utils.log("INFO","The recipeParams is: " + recipeParamsFromRequest,  isDebugEnabled)\r
75             String resourceInput = execution.getVariable("resourceInput")\r
76             utils.log("INFO","The resourceInput is: " + resourceInput,  isDebugEnabled)\r
77             //Get ResourceInput Object\r
78             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)\r
79             execution.setVariable(Prefix + "resourceInput", resourceInputObj)\r
80             \r
81             //Deal with recipeParams\r
82             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")\r
83             String resourceName = resourceInputObj.getResourceInstanceName()            \r
84             //For sdnc requestAction default is "createNetworkInstance"\r
85             String operationType = "Network"    \r
86             if(!StringUtils.isBlank(recipeParamsFromRequest)){\r
87                 //the operationType from worflow(first node) is second priority.\r
88                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")\r
89             }\r
90             if(!StringUtils.isBlank(recipeParamsFromWf)){\r
91                 //the operationType from worflow(first node) is highest priority.\r
92                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")\r
93             }\r
94             \r
95             \r
96             //For sdnc, generate svc_action and request_action\r
97             String sdnc_svcAction = "create"\r
98             if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "overlay")){\r
99                 //This will be resolved in R3.\r
100                 sdnc_svcAction ="activate"\r
101                 operationType = "NCINetwork"        \r
102             }\r
103             if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "underlay")){\r
104                 //This will be resolved in R3.\r
105                 operationType ="Network"\r
106             }        \r
107             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"                    \r
108             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)        \r
109             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)\r
110             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())\r
111             execution.setVariable("mso-request-id", requestId)\r
112             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())\r
113             //TODO Here build networkrequest\r
114             \r
115         } catch (BpmnError e) {\r
116             throw e;\r
117         } catch (Exception ex){\r
118             msg = "Exception in preProcessRequest " + ex.getMessage()\r
119             utils.log("DEBUG", msg, isDebugEnabled)\r
120             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
121         }\r
122     }\r
123 \r
124     String customizeResourceParam(String netowrkInputParametersJson) {\r
125         List<Map<String, Object>> paramList = new ArrayList();\r
126         JSONObject jsonObject = new JSONObject(netowrkInputParametersJson);\r
127         Iterator iterator = jsonObject.keys();\r
128         while (iterator.hasNext()) {\r
129             String key = iterator.next();\r
130             HashMap<String, String> hashMap = new HashMap();\r
131             hashMap.put("name", key);\r
132             hashMap.put("value", jsonObject.get(key))\r
133             paramList.add(hashMap)\r
134         }\r
135         Map<String, List<Map<String, Object>>> paramMap = new HashMap();\r
136         paramMap.put("param", paramList);\r
137 \r
138         return  new JSONObject(paramMap).toString();\r
139     }\r
140     \r
141     /**\r
142      * Pre Process the BPMN Flow Request\r
143      * Inclouds:\r
144      * generate the nsOperationKey\r
145      * generate the nsParameters\r
146      */\r
147     public void prepareSDNCRequest (DelegateExecution execution) {\r
148         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
149         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
150 \r
151         try {\r
152             // get variables\r
153             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")        \r
154             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")\r
155             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")\r
156             String createNetworkInput = execution.getVariable(Prefix + "networkRequest")\r
157 \r
158             String hdrRequestId = execution.getVariable("mso-request-id")\r
159             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")\r
160             String source = execution.getVariable("source")\r
161             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")\r
162             ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
163             String serviceType = resourceInputObj.getServiceType()\r
164             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()\r
165             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()\r
166             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelVersion()\r
167             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelName()\r
168             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()\r
169             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();\r
170             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
171             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()\r
172             String modelName = resourceInputObj.getResourceModelInfo().getModelName()\r
173             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()\r
174             String resourceInputPrameters = resourceInputObj.getResourceParameters()\r
175             String netowrkInputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")\r
176             //here convert json string to xml string\r
177             String netowrkInputParameters = XML.toString(new JSONObject(customizeResourceParam(netowrkInputParametersJson)))\r
178             // 1. prepare assign topology via SDNC Adapter SUBFLOW call\r
179             String sndcTopologyCreateRequest =\r
180                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
181                                                               xmlns:sdncadapter="http://org.openecomp.mso/workflow/sdnc/adapter/schema/v1" \r
182                                                               xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">\r
183                                  <sdncadapter:RequestHeader>\r
184                                     <sdncadapter:RequestId>${hdrRequestId}</sdncadapter:RequestId>\r
185                                     <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>\r
186                                     <sdncadapter:SvcAction>${sdnc_svcAction}</sdncadapter:SvcAction>\r
187                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>\r
188                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>\r
189                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>\r
190                                  </sdncadapter:RequestHeader>\r
191                                  <sdncadapterworkflow:SDNCRequestData>\r
192                                      <request-information>\r
193                                         <request-id>${hdrRequestId}</request-id>\r
194                                         <request-action>${sdnc_requestAction}</request-action>\r
195                                         <source>${source}</source>\r
196                                         <notification-url></notification-url>\r
197                                         <order-number></order-number>\r
198                                         <order-version></order-version>\r
199                                      </request-information>\r
200                                      <service-information>\r
201                                         <service-id>${serviceInstanceId}</service-id>\r
202                                         <subscription-service-type>${serviceType}</subscription-service-type>\r
203                                         <onap-model-information>\r
204                                              <model-invariant-uuid>${serviceModelInvariantUuid}</model-invariant-uuid>\r
205                                              <model-uuid>${serviceModelUuid}</model-uuid>\r
206                                              <model-version>${serviceModelVersion}</model-version>\r
207                                              <model-name>${serviceModelName}</model-name>\r
208                                         </onap-model-information>\r
209                                         <service-instance-id>${serviceInstanceId}</service-instance-id>\r
210                                         <global-customer-id>${globalCustomerId}</global-customer-id>\r
211                                      </service-information>\r
212                                      <network-information>\r
213                                         <onap-model-information>\r
214                                              <model-invariant-uuid>${modelInvariantUuid}</model-invariant-uuid>\r
215                                              <model-customization-uuid>${modelCustomizationUuid}</model-customization-uuid>\r
216                                              <model-uuid>${modelUuid}</model-uuid>\r
217                                              <model-version>${modelVersion}</model-version>\r
218                                              <model-name>${modelName}</model-name>\r
219                                         </onap-model-information>\r
220                                      </network-information>\r
221                                      <network-request-input>\r
222                                        <network-input-parameters>${netowrkInputParameters}</network-input-parameters>\r
223                                      </network-request-input>\r
224                                 </sdncadapterworkflow:SDNCRequestData>\r
225                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()\r
226             \r
227             String sndcTopologyCreateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)\r
228             utils.logAudit(sndcTopologyCreateRequesAsString)\r
229             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyCreateRequesAsString)\r
230             utils.log("DEBUG","sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyCreateRequesAsString, isDebugEnabled)\r
231 \r
232         } catch (Exception ex) {\r
233             String exceptionMessage = " Bpmn error encountered in CreateSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()\r
234             utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
235             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
236 \r
237         }\r
238        utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
239         }\r
240 \r
241     private void setProgressUpdateVariables(DelegateExecution execution, String body) {\r
242         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")\r
243         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)\r
244         execution.setVariable("CVFMI_updateResOperStatusRequest", body)\r
245     }\r
246 \r
247     public void prepareUpdateBeforeCreateSDNCResource(DelegateExecution execution) {\r
248         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
249         String operType = resourceInputObj.getOperationType()\r
250         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
251         String ServiceInstanceId = resourceInputObj.getServiceInstanceId()\r
252         String operationId = resourceInputObj.getOperationId()\r
253         String progress = "20"\r
254         String status = "processing"\r
255         String statusDescription = "SDCN resource creation invoked"\r
256 \r
257         execution.getVariable("operationId")\r
258 \r
259         String body = """\r
260                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
261                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
262                         <soapenv:Header/>\r
263                 <soapenv:Body>\r
264                     <ns:updateResourceOperationStatus>\r
265                                <operType>${operType}</operType>\r
266                                <operationId>${operationId}</operationId>\r
267                                <progress>${progress}</progress>\r
268                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>\r
269                                <serviceId>${ServiceInstanceId}</serviceId>\r
270                                <status>${status}</status>\r
271                                <statusDescription>${statusDescription}</statusDescription>\r
272                     </ns:updateResourceOperationStatus>\r
273                 </soapenv:Body>\r
274                 </soapenv:Envelope>""";\r
275 \r
276         setProgressUpdateVariables(execution, body)\r
277 \r
278     }\r
279 \r
280     public void prepareUpdateAfterCreateSDNCResource(execution) {\r
281         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
282         String operType = resourceInputObj.getOperationType()\r
283         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
284         String ServiceInstanceId = resourceInputObj.getServiceInstanceId()\r
285         String operationId = resourceInputObj.getOperationId()\r
286         String progress = "100"\r
287         String status = "finished"\r
288         String statusDescription = "SDCN resource creation completed"\r
289 \r
290         execution.getVariable("operationId")\r
291 \r
292         String body = """\r
293                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
294                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
295                         <soapenv:Header/>\r
296                 <soapenv:Body>\r
297                     <ns:updateResourceOperationStatus>\r
298                                <operType>${operType}</operType>\r
299                                <operationId>${operationId}</operationId>\r
300                                <progress>${progress}</progress>\r
301                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>\r
302                                <serviceId>${ServiceInstanceId}</serviceId>\r
303                                <status>${status}</status>\r
304                                <statusDescription>${statusDescription}</statusDescription>\r
305                     </ns:updateResourceOperationStatus>\r
306                 </soapenv:Body>\r
307                 </soapenv:Envelope>""";\r
308 \r
309         setProgressUpdateVariables(execution, body)\r
310     }\r
311 \r
312     public void postCreateSDNCCall(DelegateExecution execution){\r
313         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
314         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
315         String responseCode = execution.getVariable(Prefix + "sdncCreateReturnCode")\r
316         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")\r
317         \r
318         utils.log("INFO","response from sdnc, response code :" + responseCode + "  response object :" + responseObj,  isDebugEnabled)\r
319         utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
320     }\r
321     \r
322         public void sendSyncResponse (DelegateExecution execution) {\r
323                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
324                 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)\r
325 \r
326                 try {\r
327                         String operationStatus = "finished"\r
328                         // RESTResponse for main flow\r
329                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()\r
330                         utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)\r
331                         sendWorkflowResponse(execution, 202, resourceOperationResp)\r
332                         execution.setVariable("sentSyncResponse", true)\r
333 \r
334                 } catch (Exception ex) {\r
335                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()\r
336                         utils.log("DEBUG", msg, isDebugEnabled)\r
337                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
338                 }\r
339                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)\r
340         }\r
341 }\r