e034393151df2dc97f2ae7abd6f9bc40231a60dc
[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             String sdnc_svcAction = "activate"
82             operationType = "SOTNConnectivity"
83
84             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"
85             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)
86             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)
87             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
88             execution.setVariable("mso-request-id", requestId)
89             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
90         } catch (BpmnError e) {
91             throw e;
92         } catch (Exception ex){
93             msg = "Exception in preProcessRequest " + ex.getMessage()
94             msoLogger.debug(msg)
95             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
96         }
97     }
98
99     public void prepareUpdateAfterActivateSDNCResource(DelegateExecution execution) {
100         msoLogger.trace("started prepareUpdateAfterActivateSDNCResource ")
101
102         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
103         String operType = resourceInputObj.getOperationType()
104         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
105         String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
106         String operationId = resourceInputObj.getOperationId()
107         String progress = "100"
108         String status = "finished"
109         String statusDescription = "SDCN resource creation completed"
110
111         execution.getVariable("operationId")
112
113         String body = """
114                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
115                         xmlns:ns="http://org.onap.so/requestsdb">
116                         <soapenv:Header/>
117                 <soapenv:Body>
118                     <ns:updateResourceOperationStatus>
119                                <operType>${MsoUtils.xmlEscape(operType)}</operType>
120                                <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
121                                <progress>${MsoUtils.xmlEscape(progress)}</progress>
122                                <resourceTemplateUUID>${MsoUtils.xmlEscape(resourceCustomizationUuid)}</resourceTemplateUUID>
123                                <serviceId>${MsoUtils.xmlEscape(ServiceInstanceId)}</serviceId>
124                                <status>${MsoUtils.xmlEscape(status)}</status>
125                                <statusDescription>${MsoUtils.xmlEscape(statusDescription)}</statusDescription>
126                     </ns:updateResourceOperationStatus>
127                 </soapenv:Body>
128                 </soapenv:Envelope>""";
129
130         setProgressUpdateVariables(execution, body)
131     }
132
133     public void prepareSDNCRequest (DelegateExecution execution) {
134         msoLogger.trace("Started prepareSDNCRequest ")
135
136         try {
137             // get variables
138             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")
139             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")
140             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
141             String createNetworkInput = execution.getVariable(Prefix + "networkRequest")
142
143             String hdrRequestId = execution.getVariable("mso-request-id")
144             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")
145             String source = execution.getVariable("source")
146             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")
147             ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
148             String serviceType = resourceInputObj.getServiceType()
149             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()
150             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()
151             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelVersion()
152             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelName()
153             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()
154             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();
155             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
156             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
157             String modelName = resourceInputObj.getResourceModelInfo().getModelName()
158             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()
159             String resourceInputPrameters = resourceInputObj.getResourceParameters()
160             String networkInputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
161             //here convert json string to xml string
162             String netowrkInputParameters = XML.toString(new JSONObject(customizeResourceParam(networkInputParametersJson)))
163             // 1. prepare assign topology via SDNC Adapter SUBFLOW call
164             String sndcTopologyCreateRequest =
165                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
166                                                               xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1" 
167                                                               xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
168                                  <sdncadapter:RequestHeader>
169                                     <sdncadapter:RequestId>${MsoUtils.xmlEscape(hdrRequestId)}</sdncadapter:RequestId>
170                                     <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
171                                     <sdncadapter:SvcAction>${MsoUtils.xmlEscape(sdnc_svcAction)}</sdncadapter:SvcAction>
172                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
173                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
174                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
175                                  </sdncadapter:RequestHeader>
176                                  <sdncadapterworkflow:SDNCRequestData>
177                                      <request-information>
178                                         <request-id>${MsoUtils.xmlEscape(hdrRequestId)}</request-id>
179                                         <request-action>${MsoUtils.xmlEscape(sdnc_requestAction)}</request-action>
180                                         <source>${MsoUtils.xmlEscape(source)}</source>
181                                         <notification-url></notification-url>
182                                         <order-number></order-number>
183                                         <order-version></order-version>
184                                      </request-information>
185                                      <service-information>
186                                         <service-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-id>
187                                         <subscription-service-type>${MsoUtils.xmlEscape(serviceType)}</subscription-service-type>
188                                         <onap-model-information>
189                                              <model-invariant-uuid>${MsoUtils.xmlEscape(serviceModelInvariantUuid)}</model-invariant-uuid>
190                                              <model-uuid>${MsoUtils.xmlEscape(serviceModelUuid)}</model-uuid>
191                                              <model-version>${MsoUtils.xmlEscape(serviceModelVersion)}</model-version>
192                                              <model-name>${MsoUtils.xmlEscape(serviceModelName)}</model-name>
193                                         </onap-model-information>
194                                         <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
195                                         <global-customer-id>${MsoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
196                                      </service-information>
197                                      <network-information>
198                                         <onap-model-information>
199                                              <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-uuid>
200                                              <model-customization-uuid>${MsoUtils.xmlEscape(modelCustomizationUuid)}</model-customization-uuid>
201                                              <model-uuid>${MsoUtils.xmlEscape(modelUuid)}</model-uuid>
202                                              <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
203                                              <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
204                                         </onap-model-information>
205                                      </network-information>
206                                      <network-request-input>
207                                        <network-input-parameters>${MsoUtils.xmlEscape(netowrkInputParameters)}</network-input-parameters>
208                                      </network-request-input>
209                                 </sdncadapterworkflow:SDNCRequestData>
210                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()
211
212             String sndcTopologyCreateRequesAsString = utils.formatXml(sndcTopologyCreateRequest)
213             msoLogger.debug(sndcTopologyCreateRequesAsString)
214             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyCreateRequesAsString)
215             msoLogger.debug("sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyCreateRequesAsString)
216
217         } catch (Exception ex) {
218             String exceptionMessage = " Bpmn error encountered in CreateSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()
219             msoLogger.debug(exceptionMessage)
220             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
221
222         }
223         msoLogger.trace("Exit prepareSDNCRequest ")
224     }
225
226     public void postCreateSDNCCall(DelegateExecution execution) {
227         msoLogger.trace("started postCreateSDNCCall ")
228
229         String responseCode = execution.getVariable(Prefix + "sdncCreateReturnCode")
230         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")
231
232         msoLogger.info("response from sdnc, response code :" + responseCode + "  response object :" + responseObj)
233     }
234
235     public void sendSyncResponse(DelegateExecution execution) {
236         msoLogger.trace("started sendSyncResponse ")
237
238         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
239         utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
240
241         try {
242             String operationStatus = "finished"
243             // RESTResponse for main flow
244             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
245             utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)
246             sendWorkflowResponse(execution, 202, resourceOperationResp)
247             execution.setVariable("sentSyncResponse", true)
248
249         } catch (Exception ex) {
250             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
251             utils.log("DEBUG", msg, isDebugEnabled)
252             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
253         }
254         utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
255     }
256 }