69e973d7c36eabe1cdad187f2f5125abeee0d5d6
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.infrastructure.scripts
22
23 import org.apache.commons.lang3.StringUtils
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.json.JSONObject
26 import org.json.XML
27 import org.onap.so.bpmn.common.recipe.ResourceInput
28 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.camunda.bpm.engine.delegate.DelegateExecution
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
34 import org.onap.so.logger.MsoLogger
35
36 /**
37  * This groovy class supports the <class>ActivateSDNCCNetworkResource.bpmn</class> process.
38  * flow for SDNC Network Resource Activate
39  */
40 public class ActivateSDNCNetworkResource extends AbstractServiceTaskProcessor {
41     private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateSDNCNetworkResource.class);
42     
43     String Prefix = "ACTSDNCRES_"
44
45     ExceptionUtil exceptionUtil = new ExceptionUtil()
46
47     JsonUtils jsonUtil = new JsonUtils()
48
49     SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
50
51     public void preProcessRequest(DelegateExecution execution) {
52         msoLogger.trace("Started preProcessRequest ")
53
54         try {
55             //get bpmn inputs from resource request.
56             String requestId = execution.getVariable("mso-request-id")
57             String requestAction = execution.getVariable("requestAction")
58             msoLogger.info("The requestAction is: " + requestAction)
59             String recipeParamsFromRequest = execution.getVariable("recipeParams")
60             msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
61             String resourceInput = execution.getVariable("resourceInput")
62             msoLogger.info("The resourceInput is: " + resourceInput)
63             //Get ResourceInput Object
64             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
65             execution.setVariable(Prefix + "resourceInput", resourceInputObj)
66
67             //Deal with recipeParams
68             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
69             String resourceName = resourceInputObj.getResourceInstanceName()
70             //For sdnc requestAction default is "createNetworkInstance"
71             String operationType = "Network"
72             if(!StringUtils.isBlank(recipeParamsFromRequest)){
73                 //the operationType from worflow(first node) is second priority.
74                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")
75             }
76             if(!StringUtils.isBlank(recipeParamsFromWf)){
77                 //the operationType from worflow(first node) is highest priority.
78                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")
79             }
80
81             // TODO: based on the resource type decide action and operation type
82             String sdnc_svcAction = "activate"
83             operationType = "SOTNConnectivity"
84
85             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"
86             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)
87             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)
88             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
89             execution.setVariable("mso-request-id", requestId)
90             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
91         } catch (BpmnError e) {
92             throw e;
93         } catch (Exception ex){
94             msg = "Exception in preProcessRequest " + ex.getMessage()
95             msoLogger.debug(msg)
96             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
97         }
98     }
99
100     public void prepareUpdateAfterActivateSDNCResource(DelegateExecution execution) {
101         msoLogger.trace("started prepareUpdateAfterActivateSDNCResource ")
102
103         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
104         String operType = resourceInputObj.getOperationType()
105         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
106         String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
107         String operationId = resourceInputObj.getOperationId()
108         String progress = "100"
109         String status = "finished"
110         String statusDescription = "SDCN resource creation completed"
111
112         execution.getVariable("operationId")
113
114         String body = """
115                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
116                         xmlns:ns="http://org.onap.so/requestsdb">
117                         <soapenv:Header/>
118                 <soapenv:Body>
119                     <ns:updateResourceOperationStatus>
120                                <operType>${MsoUtils.xmlEscape(operType)}</operType>
121                                <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
122                                <progress>${MsoUtils.xmlEscape(progress)}</progress>
123                                <resourceTemplateUUID>${MsoUtils.xmlEscape(resourceCustomizationUuid)}</resourceTemplateUUID>
124                                <serviceId>${MsoUtils.xmlEscape(ServiceInstanceId)}</serviceId>
125                                <status>${MsoUtils.xmlEscape(status)}</status>
126                                <statusDescription>${MsoUtils.xmlEscape(statusDescription)}</statusDescription>
127                     </ns:updateResourceOperationStatus>
128                 </soapenv:Body>
129                 </soapenv:Envelope>""";
130
131         setProgressUpdateVariables(execution, body)
132     }
133
134     public void prepareSDNCRequest (DelegateExecution execution) {
135         msoLogger.trace("Started prepareSDNCRequest ")
136
137         try {
138             // get variables
139             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")
140             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")
141             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
142             String createNetworkInput = execution.getVariable(Prefix + "networkRequest")
143
144             String hdrRequestId = execution.getVariable("mso-request-id")
145             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")
146             String source = execution.getVariable("source")
147             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")
148             ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
149             String serviceType = resourceInputObj.getServiceType()
150             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()
151             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()
152             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelVersion()
153             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelName()
154             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()
155             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();
156             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
157             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
158             String modelName = resourceInputObj.getResourceModelInfo().getModelName()
159             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()
160             String resourceInputPrameters = resourceInputObj.getResourceParameters()
161             String networkInputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
162             //here convert json string to xml string
163             String netowrkInputParameters = XML.toString(new JSONObject(customizeResourceParam(networkInputParametersJson)))
164             // 1. prepare assign topology via SDNC Adapter SUBFLOW call
165             String sndcTopologyCreateRequest =
166                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
167                                                               xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1" 
168                                                               xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
169                                  <sdncadapter:RequestHeader>
170                                     <sdncadapter:RequestId>${MsoUtils.xmlEscape(hdrRequestId)}</sdncadapter:RequestId>
171                                     <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
172                                     <sdncadapter:SvcAction>${MsoUtils.xmlEscape(sdnc_svcAction)}</sdncadapter:SvcAction>
173                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
174                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
175                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
176                                  </sdncadapter:RequestHeader>
177                                  <sdncadapterworkflow:SDNCRequestData>
178                                      <request-information>
179                                         <request-id>${MsoUtils.xmlEscape(hdrRequestId)}</request-id>
180                                         <request-action>${MsoUtils.xmlEscape(sdnc_requestAction)}</request-action>
181                                         <source>${MsoUtils.xmlEscape(source)}</source>
182                                         <notification-url></notification-url>
183                                         <order-number></order-number>
184                                         <order-version></order-version>
185                                      </request-information>
186                                      <service-information>
187                                         <service-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-id>
188                                         <subscription-service-type>${MsoUtils.xmlEscape(serviceType)}</subscription-service-type>
189                                         <onap-model-information>
190                                              <model-invariant-uuid>${MsoUtils.xmlEscape(serviceModelInvariantUuid)}</model-invariant-uuid>
191                                              <model-uuid>${MsoUtils.xmlEscape(serviceModelUuid)}</model-uuid>
192                                              <model-version>${MsoUtils.xmlEscape(serviceModelVersion)}</model-version>
193                                              <model-name>${MsoUtils.xmlEscape(serviceModelName)}</model-name>
194                                         </onap-model-information>
195                                         <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
196                                         <global-customer-id>${MsoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
197                                      </service-information>
198                                      <network-information>
199                                         <onap-model-information>
200                                              <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-uuid>
201                                              <model-customization-uuid>${MsoUtils.xmlEscape(modelCustomizationUuid)}</model-customization-uuid>
202                                              <model-uuid>${MsoUtils.xmlEscape(modelUuid)}</model-uuid>
203                                              <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
204                                              <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
205                                         </onap-model-information>
206                                      </network-information>
207                                      <network-request-input>
208                                        <network-input-parameters>${MsoUtils.xmlEscape(netowrkInputParameters)}</network-input-parameters>
209                                      </network-request-input>
210                                 </sdncadapterworkflow:SDNCRequestData>
211                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()
212
213             String sndcTopologyCreateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)
214             msoLogger.debug(sndcTopologyCreateRequesAsString)
215             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyCreateRequesAsString)
216             msoLogger.debug("sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyCreateRequesAsString)
217
218         } catch (Exception ex) {
219             String exceptionMessage = " Bpmn error encountered in CreateSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()
220             msoLogger.debug(exceptionMessage)
221             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
222
223         }
224         msoLogger.trace("Exit prepareSDNCRequest ")
225     }
226
227     public void postCreateSDNCCall(DelegateExecution execution) {
228         msoLogger.trace("started postCreateSDNCCall ")
229
230         String responseCode = execution.getVariable(Prefix + "sdncCreateReturnCode")
231         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")
232
233         msoLogger.info("response from sdnc, response code :" + responseCode + "  response object :" + responseObj)
234     }
235
236     public void sendSyncResponse(DelegateExecution execution) {
237         msoLogger.trace("started sendSyncResponse ")
238
239         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
240         utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
241
242         try {
243             String operationStatus = "finished"
244             // RESTResponse for main flow
245             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
246             utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)
247             sendWorkflowResponse(execution, 202, resourceOperationResp)
248             execution.setVariable("sentSyncResponse", true)
249
250         } catch (Exception ex) {
251             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
252             utils.log("DEBUG", msg, isDebugEnabled)
253             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
254         }
255         utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
256     }
257 }