Merge "Replace ecomp tag with onap"
[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("requestId")\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     \r
125     /**\r
126      * Pre Process the BPMN Flow Request\r
127      * Inclouds:\r
128      * generate the nsOperationKey\r
129      * generate the nsParameters\r
130      */\r
131     public void prepareSDNCRequest (DelegateExecution execution) {\r
132         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
133         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
134 \r
135         try {\r
136             // get variables\r
137             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")        \r
138             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")\r
139             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")\r
140             String createNetworkInput = execution.getVariable(Prefix + "networkRequest")\r
141 \r
142             String hdrRequestId = execution.getVariable("mso-request-id")\r
143             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")\r
144             String source = execution.getVariable("source")\r
145             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")\r
146             ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
147             String serviceType = resourceInputObj.getServiceType()\r
148             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()\r
149             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()\r
150             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelVersion()\r
151             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelName()\r
152             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()\r
153             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();\r
154             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
155             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()\r
156             String modelName = resourceInputObj.getResourceModelInfo().getModelName()\r
157             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()\r
158             String resourceInputPrameters = resourceInputObj.getResourceParameters()\r
159             String netowrkInputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")\r
160             //here convert json string to xml string\r
161             String netowrkInputParameters = XML.toString(new JSONObject(netowrkInputParametersJson))\r
162             // 1. prepare assign topology via SDNC Adapter SUBFLOW call\r
163             String sndcTopologyCreateRequest =\r
164                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
165                                                               xmlns:sdncadapter="http://org.openecomp.mso/workflow/sdnc/adapter/schema/v1" \r
166                                                               xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">\r
167                                  <sdncadapter:RequestHeader>\r
168                                     <sdncadapter:RequestId>${hdrRequestId}</sdncadapter:RequestId>\r
169                                     <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>\r
170                                     <sdncadapter:SvcAction>${sdnc_svcAction}</sdncadapter:SvcAction>\r
171                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>\r
172                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>\r
173                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>\r
174                                  </sdncadapter:RequestHeader>\r
175                                  <sdncadapterworkflow:SDNCRequestData>\r
176                                      <request-information>\r
177                                         <request-id>${hdrRequestId}</request-id>\r
178                                         <request-action>${sdnc_requestAction}</request-action>\r
179                                         <source>${source}</source>\r
180                                         <notification-url></notification-url>\r
181                                         <order-number></order-number>\r
182                                         <order-version></order-version>\r
183                                      </request-information>\r
184                                      <service-information>\r
185                                         <service-id>${serviceInstanceId}</service-id>\r
186                                         <subscription-service-type>${serviceType}</subscription-service-type>\r
187                                         <onap-model-information>\r
188                                              <model-invariant-uuid>${serviceModelInvariantUuid}</model-invariant-uuid>\r
189                                              <model-uuid>${serviceModelUuid}</model-uuid>\r
190                                              <model-version>${serviceModelVersion}</model-version>\r
191                                              <model-name>${serviceModelName}</model-name>\r
192                                         </onap-model-information>\r
193                                         <service-instance-id>${serviceInstanceId}</service-instance-id>\r
194                                         <global-customer-id>${globalCustomerId}</global-customer-id>\r
195                                      </service-information>\r
196                                      <network-information>\r
197                                         <ecomp-model-information>\r
198                                              <model-invariant-uuid>${modelInvariantUuid}</model-invariant-uuid>\r
199                                              <model-customization-uuid>${modelCustomizationUuid}</model-customization-uuid>\r
200                                              <model-uuid>${modelUuid}</model-uuid>\r
201                                              <model-version>${modelVersion}</model-version>\r
202                                              <model-name>${modelName}</model-name>\r
203                                         </ecomp-model-information>\r
204                                      </network-information>\r
205                                      <network-request-input>\r
206                                        <network-input-parameters>${netowrkInputParameters}</network-input-parameters>\r
207                                      </network-request-input>\r
208                                 </sdncadapterworkflow:SDNCRequestData>\r
209                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()\r
210             \r
211             String sndcTopologyCreateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)\r
212             utils.logAudit(sndcTopologyCreateRequesAsString)\r
213             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyCreateRequesAsString)\r
214             utils.log("DEBUG","sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyCreateRequesAsString, isDebugEnabled)\r
215 \r
216         } catch (Exception ex) {\r
217             String exceptionMessage = " Bpmn error encountered in CreateSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()\r
218             utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
219             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
220 \r
221         }\r
222        utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
223         }\r
224 \r
225     private void setProgressUpdateVariables(DelegateExecution execution, String body) {\r
226         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")\r
227         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)\r
228         execution.setVariable("CVFMI_updateResOperStatusRequest", body)\r
229     }\r
230 \r
231     public void prepareUpdateBeforeCreateSDNCResource(DelegateExecution execution) {\r
232         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
233         String operType = resourceInputObj.getOperationType()\r
234         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
235         String ServiceInstanceId = resourceInputObj.getServiceInstanceId()\r
236         String operationId = resourceInputObj.getOperationId()\r
237         String progress = "20"\r
238         String status = "processing"\r
239         String statusDescription = "SDCN resource creation invoked"\r
240 \r
241         execution.getVariable("operationId")\r
242 \r
243         String body = """\r
244                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
245                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
246                         <soapenv:Header/>\r
247                 <soapenv:Body>\r
248                     <ns:updateResourceOperationStatus>\r
249                                <operType>${operType}</operType>\r
250                                <operationId>${operationId}</operationId>\r
251                                <progress>${progress}</progress>\r
252                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>\r
253                                <serviceId>${ServiceInstanceId}</serviceId>\r
254                                <status>${status}</status>\r
255                                <statusDescription>${statusDescription}</statusDescription>\r
256                     </ns:updateResourceOperationStatus>\r
257                 </soapenv:Body>\r
258                 </soapenv:Envelope>""";\r
259 \r
260         setProgressUpdateVariables(execution, body)\r
261 \r
262     }\r
263 \r
264     public void prepareUpdateAfterCreateSDNCResource(execution) {\r
265         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
266         String operType = resourceInputObj.getOperationType()\r
267         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
268         String ServiceInstanceId = resourceInputObj.getServiceInstanceId()\r
269         String operationId = resourceInputObj.getOperationId()\r
270         String progress = "100"\r
271         String status = "Created"\r
272         String statusDescription = "SDCN resource creation completed"\r
273 \r
274         execution.getVariable("operationId")\r
275 \r
276         String body = """\r
277                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
278                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
279                         <soapenv:Header/>\r
280                 <soapenv:Body>\r
281                     <ns:updateResourceOperationStatus>\r
282                                <operType>${operType}</operType>\r
283                                <operationId>${operationId}</operationId>\r
284                                <progress>${progress}</progress>\r
285                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>\r
286                                <serviceId>${ServiceInstanceId}</serviceId>\r
287                                <status>${status}</status>\r
288                                <statusDescription>${statusDescription}</statusDescription>\r
289                     </ns:updateResourceOperationStatus>\r
290                 </soapenv:Body>\r
291                 </soapenv:Envelope>""";\r
292 \r
293         setProgressUpdateVariables(execution, body)\r
294     }\r
295 \r
296     public void postCreateSDNCCall(DelegateExecution execution){\r
297         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
298         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
299         String responseCode = execution.getVariable(Prefix + "sdncCreateReturnCode")\r
300         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")\r
301         \r
302         utils.log("INFO","response from sdnc, response code :" + responseCode + "  response object :" + responseObj,  isDebugEnabled)\r
303         utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
304     }\r
305 }\r