31bda63a497366574c480563dfea62be70bf1f57
[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.logger.MsoLogger
28 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
29 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder
30 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
31 import org.camunda.bpm.engine.delegate.DelegateExecution
32 import org.openecomp.mso.bpmn.core.json.JsonUtils
33 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
34 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
35
36 /**
37  * This groovy class supports the <class>ActivateSDNCCNetworkResource.bpmn</class> process.
38  * flow for SDNC Network Resource Activate
39  */
40 public class DeActivateSDNCNetworkResource extends AbstractServiceTaskProcessor {
41     private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL,
42             CreateSDNCNetworkResource.class);
43     String Prefix = "DEACTSDNCRES_"
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.info(" ***** started  preProcessRequest*****")
53
54         try {
55
56             //get bpmn inputs from resource request.
57             String requestId = execution.getVariable("mso-request-id")
58             String requestAction = execution.getVariable("requestAction")
59             msoLogger.info("The requestAction is: " + requestAction)
60             String recipeParamsFromRequest = execution.getVariable("recipeParams")
61             msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
62             String resourceInput = execution.getVariable("resourceInput")
63             msoLogger.info("The resourceInput is: " + resourceInput)
64             //Get ResourceInput Object
65             org.onap.so.bpmn.common.recipe.ResourceInput resourceInputObj = org.onap.so.bpmn.common.resource.ResourceRequestBuilder.getJsonObject(resourceInput, org.onap.so.bpmn.common.recipe.ResourceInput.class)
66             execution.setVariable(Prefix + "resourceInput", resourceInputObj)
67
68             //Deal with recipeParams
69             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
70             String resourceModelName = resourceInputObj.getResourceModelInfo().getModelName()
71             //For sdnc requestAction default is "NetworkInstance"
72             String operationType = "Network"
73             if(!StringUtils.isBlank(recipeParamsFromRequest) && "null" != recipeParamsFromRequest){
74                 //the operationType from worflow(first node) is second priority.
75                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")
76             }
77             if(!StringUtils.isBlank(recipeParamsFromWf)){
78                 //the operationType from worflow(first node) is highest priority.
79                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")
80             }
81
82
83             // TODO: based on the resource type decide action and operation type
84             String sdnc_svcAction = "deactivate"
85             operationType = "SOTNConnectivity"
86
87             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"
88             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)
89             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)
90             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
91             execution.setVariable("mso-request-id", requestId)
92             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
93             //TODO Here build networkrequest
94
95         } catch (BpmnError e) {
96             throw e;
97         } catch (Exception ex){
98             msg = "Exception in preProcessRequest " + ex.getMessage()
99             msoLogger.debug(msg)
100             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
101         }
102     }
103
104     public void prepareSDNCRequest(DelegateExecution execution) {
105         msoLogger.info(" ***** started prepareSDNCRequest *****")
106
107         try {
108             // get variables
109             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")
110             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")
111             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
112             String deleteNetworkInput = execution.getVariable(Prefix + "networkRequest")
113
114             String hdrRequestId = execution.getVariable("mso-request-id")
115             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")
116             String source = execution.getVariable("source")
117             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")
118             org.onap.so.bpmn.common.recipe.ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
119             String serviceType = resourceInputObj.getServiceType()
120             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()
121             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()
122             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelVersion()
123             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelName()
124             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()
125             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();
126             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
127             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
128             String modelName = resourceInputObj.getResourceModelInfo().getModelName()
129             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()
130             // 1. prepare assign topology via SDNC Adapter SUBFLOW call
131             String sndcTopologyDeleteRequest =
132                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
133                                                               xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1" 
134                                                               xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
135                                  <sdncadapter:RequestHeader>
136                                     <sdncadapter:RequestId>${MsoUtils.xmlEscape(hdrRequestId)}</sdncadapter:RequestId>
137                                     <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
138                                     <sdncadapter:SvcAction>${MsoUtils.xmlEscape(sdnc_svcAction)}</sdncadapter:SvcAction>
139                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
140                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
141                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
142                                  </sdncadapter:RequestHeader>
143                                  <sdncadapterworkflow:SDNCRequestData>
144                                      <request-information>
145                                         <request-id>${MsoUtils.xmlEscape(hdrRequestId)}</request-id>
146                                         <request-action>${MsoUtils.xmlEscape(sdnc_requestAction)}</request-action>
147                                         <source>${MsoUtils.xmlEscape(source)}</source>
148                                         <notification-url></notification-url>
149                                         <order-number></order-number>
150                                         <order-version></order-version>
151                                      </request-information>
152                                      <service-information>
153                                         <service-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-id>
154                                         <subscription-service-type>${MsoUtils.xmlEscape(serviceType)}</subscription-service-type>
155                                         <onap-model-information>
156                                              <model-invariant-uuid>${MsoUtils.xmlEscape(serviceModelInvariantUuid)}</model-invariant-uuid>
157                                              <model-uuid>${MsoUtils.xmlEscape(serviceModelUuid)}</model-uuid>
158                                              <model-version>${MsoUtils.xmlEscape(serviceModelVersion)}</model-version>
159                                              <model-name>${MsoUtils.xmlEscape(serviceModelName)}</model-name>
160                                         </onap-model-information>
161                                         <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
162                                         <global-customer-id>${MsoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
163                                      </service-information>
164                                      <network-information>
165                                         <onap-model-information>
166                                              <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-uuid>
167                                              <model-customization-uuid>${MsoUtils.xmlEscape(modelCustomizationUuid)}</model-customization-uuid>
168                                              <model-uuid>${MsoUtils.xmlEscape(modelUuid)}</model-uuid>
169                                              <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
170                                              <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
171                                         </onap-model-information>
172                                      </network-information>
173                                      <network-request-input>
174                                        <network-input-parameters></network-input-parameters>
175                                      </network-request-input>
176                                 </sdncadapterworkflow:SDNCRequestData>
177                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()
178
179             String sndcTopologyDeleteRequesAsString = utils.formatXml(sndcTopologyDeleteRequest)
180             utils.logAudit(sndcTopologyDeleteRequesAsString)
181             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyDeleteRequesAsString)
182             msoLogger.info("sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyDeleteRequesAsString)
183
184         } catch (Exception ex) {
185             String exceptionMessage = " Bpmn error encountered in DeleteSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()
186             msoLogger.debug(exceptionMessage)
187             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
188
189         }
190         msoLogger.info(" ***** Exit prepareSDNCRequest *****")
191     }
192
193     public void prepareUpdateAfterDeActivateSDNCResource(DelegateExecution execution) {
194         msoLogger.info("***** started prepareUpdateAfterDeActivateSDNCResource *****")
195
196         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
197         String operType = resourceInputObj.getOperationType()
198         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
199         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
200         String operationId = resourceInputObj.getOperationId()
201         String progress = "100"
202         String status = "finished"
203         String statusDescription = "SDCN resource delete completed"
204
205         //String operationId = execution.getVariable("operationId")
206
207         String body = """
208                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
209                         xmlns:ns="http://org.onap.so/requestsdb">
210                         <soapenv:Header/>
211                 <soapenv:Body>
212                     <ns:updateResourceOperationStatus>
213                                <operType>${MsoUtils.xmlEscape(operType)}</operType>
214                                <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
215                                <progress>${MsoUtils.xmlEscape(progress)}</progress>
216                                <resourceTemplateUUID>${MsoUtils.xmlEscape(resourceCustomizationUuid)}</resourceTemplateUUID>
217                                <serviceId>${MsoUtils.xmlEscape(serviceInstanceId)}</serviceId>
218                                <status>${MsoUtils.xmlEscape(status)}</status>
219                                <statusDescription>${MsoUtils.xmlEscape(statusDescription)}</statusDescription>
220                     </ns:updateResourceOperationStatus>
221                 </soapenv:Body>
222                 </soapenv:Envelope>""";
223
224         setProgressUpdateVariables(execution, body)
225     }
226
227     public void postDeactivateSDNCCall(DelegateExecution execution) {
228         msoLogger.info(" ***** started postDeactivateSDNCCall *****")
229         String responseCode = execution.getVariable(Prefix + "sdncDeleteReturnCode")
230         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")
231
232         msoLogger.info("response from sdnc, response code :" + responseCode + "  response object :" + responseObj)
233         msoLogger.info(" ***** Exit prepareSDNCRequest *****")
234     }
235
236     public void sendSyncResponse(DelegateExecution execution) {
237         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
238         msoLogger.info(" ***** started sendSyncResponse *****")
239
240         try {
241             String operationStatus = "finished"
242             // RESTResponse for main flow
243             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
244             msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
245             sendWorkflowResponse(execution, 202, resourceOperationResp)
246             execution.setVariable("sentSyncResponse", true)
247
248         } catch (Exception ex) {
249             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
250             msoLogger.debug(msg)
251             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
252         }
253         msoLogger.debug(" ***** Exit sendSyncResopnse *****")
254     }
255
256 }