933d45066a05f9ca83392b7f8c77bc7a11229c9d
[so.git] /
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 static org.apache.commons.lang3.StringUtils.*;\r
24 import groovy.xml.XmlUtil\r
25 import groovy.json.*\r
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor \r
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
28 import org.openecomp.mso.bpmn.common.recipe.ResourceInput;\r
29 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder \r
30 import org.openecomp.mso.bpmn.core.WorkflowException \r
31 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
32 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder\r
33 import org.openecomp.mso.rest.APIResponse\r
34 \r
35 import java.util.UUID;\r
36 \r
37 import org.camunda.bpm.engine.delegate.BpmnError \r
38 import org.camunda.bpm.engine.delegate.DelegateExecution\r
39 import org.apache.commons.lang3.*\r
40 import org.apache.commons.codec.binary.Base64;\r
41 import org.springframework.web.util.UriUtils \r
42 import org.openecomp.mso.rest.RESTClient \r
43 import org.openecomp.mso.rest.RESTConfig\r
44 import org.openecomp.mso.rest.APIResponse;\r
45 \r
46 /**\r
47  * This groovy class supports the <class>CreateSDNCCNetworkResource.bpmn</class> process.\r
48  * flow for SDNC Network Resource Create\r
49  */\r
50 public class CreateSDNCCNetworkResource extends AbstractServiceTaskProcessor {\r
51 \r
52     String Prefix="CRESDNCRES_"\r
53             \r
54     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
55 \r
56     JsonUtils jsonUtil = new JsonUtils()\r
57     \r
58     public void preProcessRequest(DelegateExecution execution){\r
59         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
60         utils.log("INFO"," ***** Started preProcessRequest *****",  isDebugEnabled)\r
61         try {           \r
62             \r
63             //get bpmn inputs from resource request.\r
64             String requestId = execution.getVariable("requestId")\r
65             String requestAction = execution.getVariable("requestAction")\r
66             utils.log("INFO","The requestAction is: " + requestAction,  isDebugEnabled)\r
67             String recipeParamsFromRequest = execution.getVariable("recipeParams")\r
68             utils.log("INFO","The recipeParams is: " + recipeParams,  isDebugEnabled)\r
69             String resourceInput = execution.getVariable("requestInput")\r
70             utils.log("INFO","The resourceInput is: " + resourceInput,  isDebugEnabled)\r
71             //Get ResourceInput Object\r
72             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, resourceInputObj)\r
73             execution.setVariable(Prefix + "resourceInput", resourceInputObj)\r
74             \r
75             //Deal with recipeParams\r
76             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")\r
77             String resourceName = resourceInputObj.getResourceInstanceName()            \r
78             //For sdnc requestAction default is "createNetworkInstance"\r
79             String operationType = "Network"    \r
80             if(!StringUtils.isBlank(recipeParamsFromRequest)){\r
81                 //the operationType from worflow(first node) is second priority.\r
82                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")\r
83             }\r
84             if(!StringUtils.isBlank(recipeParamsFromWf)){\r
85                 //the operationType from worflow(first node) is highest priority.\r
86                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")\r
87             }\r
88             \r
89             \r
90             //For sdnc, generate svc_action and request_action\r
91             String sdnc_svcAction = "create"\r
92             if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "overlay")){\r
93                 //This will be resolved in R3.\r
94                 sdnc_svcAction ="activate"\r
95                 operationType = "NCINetwork"        \r
96             }\r
97             if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "underlay")){\r
98                 //This will be resolved in R3.\r
99                 operationType ="Network"\r
100             }        \r
101             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"                    \r
102             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)        \r
103             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)\r
104             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())\r
105             execution.setVariable("mso-request-id", requestId)\r
106             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())\r
107             //TODO Here build networkrequest\r
108             \r
109         } catch (BpmnError e) {\r
110             throw e;\r
111         } catch (Exception ex){\r
112             msg = "Exception in preProcessRequest " + ex.getMessage()\r
113             utils.log("DEBUG", msg, isDebugEnabled)\r
114             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
115         }\r
116     }\r
117     \r
118     \r
119     /**\r
120      * Pre Process the BPMN Flow Request\r
121      * Inclouds:\r
122      * generate the nsOperationKey\r
123      * generate the nsParameters\r
124      */\r
125     public void prepareSDNCRequest (DelegateExecution execution) {\r
126         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
127         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
128 \r
129         try {\r
130             // get variables\r
131             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")        \r
132             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")\r
133             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")\r
134             String createNetworkInput = execution.getVariable(Prefix + "networkRequest")\r
135 \r
136             String hdrRequestId = execution.getVariable("mso-request-id")\r
137             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")\r
138             String source = execution.getVariable("source")\r
139             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")\r
140             ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")\r
141             String serviceType = resourceInputObj.getServiceType()\r
142             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()\r
143             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()\r
144             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelName()\r
145             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelVersion()\r
146             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()\r
147             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();\r
148             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()\r
149             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()\r
150             String modelName = resourceInputObj.getResourceModelInfo().getModelName()\r
151             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()\r
152             String resourceInputPrameters = resourceInputObj.getResourceParameters()\r
153             String netowrkInputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")\r
154             //here convert json string to xml string\r
155             String netowrkInputParameters = jsonUtil.json2xml(netowrkInputParametersJson)\r
156             // 1. prepare assign topology via SDNC Adapter SUBFLOW call\r
157             String sndcTopologyCreateRequest = sdncAdapterUtils.sdncTopologyRequestRsrc(execution, createNetworkInput, serviceInstanceId, sdncCallback, sdnc_svcAction, sdnc_requestAction, null, null, null)\r
158                     String content =\r
159                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
160                                                               xmlns:sdncadapter="http://org.openecomp.mso/workflow/sdnc/adapter/schema/v1" \r
161                                                               xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">\r
162                                  <sdncadapter:RequestHeader>\r
163                                     <sdncadapter:RequestId>${hdrRequestId}</sdncadapter:RequestId>\r
164                                     <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>\r
165                                     <sdncadapter:SvcAction>${sdnc_svcAction}</sdncadapter:SvcAction>\r
166                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>\r
167                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>\r
168                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>\r
169                                  </sdncadapter:RequestHeader>\r
170                                  <sdncadapterworkflow:SDNCRequestData>\r
171                                      <request-information>\r
172                                         <request-id>${hdrRequestId}</request-id>\r
173                                         <request-action>${sdnc_requestAction}</request-action>\r
174                                         <source>${source}</source>\r
175                                         <notification-url></notification-url>\r
176                                         <order-number></order-number>\r
177                                         <order-version></order-version>\r
178                                      </request-information>\r
179                                      <service-information>\r
180                                         <service-id>${sdnc_service_id}</service-id>\r
181                                         <subscription-service-type>${serviceType}</subscription-service-type>\r
182                                         <ecomp-model-information>\r
183                                              <model-invariant-uuid>${serviceModelInvariantUuid}</model-invariant-uuid>\r
184                                              <model-uuid>${serviceModelUuid}</model-uuid>\r
185                                              <model-version>${serviceModelVersion}</model-version>\r
186                                              <model-name>${serviceModelName}</model-name>\r
187                                         </ecomp-model-information>\r
188                                         <service-instance-id>${serviceInstanceId}</service-instance-id>\r
189                                         <global-customer-id>${globalCustomerId}</global-customer-id>\r
190                                      </service-information>\r
191                                      <network-information>\r
192                                         <ecomp-model-information>\r
193                                              <model-invariant-uuid>${modelInvariantUuid}</model-invariant-uuid>\r
194                                              <model-customization-uuid>${modelCustomizationUuid}</model-customization-uuid>\r
195                                              <model-uuid>${modelUuid}</model-uuid>\r
196                                              <model-version>${modelVersion}</model-version>\r
197                                              <model-name>${modelName}</model-name>\r
198                                         </ecomp-model-information>\r
199                                      </network-information>\r
200                                      <network-request-input>\r
201                                        <network-input-parameters>${netowrkInputParameters}</network-input-parameters>\r
202                                      </network-request-input>\r
203                                 </sdncadapterworkflow:SDNCRequestData>\r
204                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()\r
205             \r
206             String sndcTopologyCreateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)\r
207             utils.logAudit(sndcTopologyCreateRequesAsString)\r
208             execution.setVariable(Prefix + "createSDNCRequest", sndcTopologyCreateRequesAsString)\r
209             utils.log("DEBUG", Prefix + "createSDNCRequest - " + "\n" +  sndcTopologyCreateRequesAsString, isDebugEnabled)\r
210 \r
211         } catch (Exception ex) {\r
212             String exceptionMessage = " Bpmn error encountered in CreateSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()\r
213             utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
214             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
215 \r
216         }\r
217        utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
218         }\r
219 \r
220     public void postCreateSDNCCall(DelegateExecution execution){\r
221         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
222         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)\r
223         String responseCode = execution.getVariable(Prefix + "sdncCreateReturnCode")\r
224         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")\r
225         \r
226         utils.log("INFO","response from sdnc, response code :" + responseCode + "  response object :" + responseObj,  isDebugEnabled)\r
227         utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)\r
228     }\r
229 }\r