666bbf5273366f53401e1d38f650a411259a2ed8
[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 sdncTopologyDeleteRequest = ""
132
133             switch (modelName) {
134                 case ~/^Site$/:
135                     sdncTopologyDeleteRequest = ""
136                     break
137
138                 case ~/^SOTNAttachment$/:
139                     sdncTopologyDeleteRequest = ""
140                     break
141                 default:
142                     sdncTopologyDeleteRequest = """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
143                                                               xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1" 
144                                                               xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
145                                  <sdncadapter:RequestHeader>
146                                     <sdncadapter:RequestId>${MsoUtils.xmlEscape(hdrRequestId)}</sdncadapter:RequestId>
147                                     <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
148                                     <sdncadapter:SvcAction>${MsoUtils.xmlEscape(sdnc_svcAction)}</sdncadapter:SvcAction>
149                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
150                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
151                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
152                                  </sdncadapter:RequestHeader>
153                                  <sdncadapterworkflow:SDNCRequestData>
154                                      <request-information>
155                                         <request-id>${MsoUtils.xmlEscape(hdrRequestId)}</request-id>
156                                         <request-action>${MsoUtils.xmlEscape(sdnc_requestAction)}</request-action>
157                                         <source>${MsoUtils.xmlEscape(source)}</source>
158                                         <notification-url></notification-url>
159                                         <order-number></order-number>
160                                         <order-version></order-version>
161                                      </request-information>
162                                      <service-information>
163                                         <service-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-id>
164                                         <subscription-service-type>${MsoUtils.xmlEscape(serviceType)}</subscription-service-type>
165                                         <onap-model-information>
166                                              <model-invariant-uuid>${MsoUtils.xmlEscape(serviceModelInvariantUuid)}</model-invariant-uuid>
167                                              <model-uuid>${MsoUtils.xmlEscape(serviceModelUuid)}</model-uuid>
168                                              <model-version>${MsoUtils.xmlEscape(serviceModelVersion)}</model-version>
169                                              <model-name>${MsoUtils.xmlEscape(serviceModelName)}</model-name>
170                                         </onap-model-information>
171                                         <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
172                                         <global-customer-id>${MsoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
173                                      </service-information>
174                                      <network-information>
175                                         <onap-model-information>
176                                              <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-uuid>
177                                              <model-customization-uuid>${MsoUtils.xmlEscape(modelCustomizationUuid)}</model-customization-uuid>
178                                              <model-uuid>${MsoUtils.xmlEscape(modelUuid)}</model-uuid>
179                                              <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
180                                              <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
181                                         </onap-model-information>
182                                      </network-information>
183                                      <network-request-input>
184                                        <network-input-parameters></network-input-parameters>
185                                      </network-request-input>
186                                 </sdncadapterworkflow:SDNCRequestData>
187                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()
188             }
189
190             String sndcTopologyDeleteRequesAsString = utils.formatXml(sdncTopologyDeleteRequest)
191             utils.logAudit(sndcTopologyDeleteRequesAsString)
192             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyDeleteRequesAsString)
193             msoLogger.info("sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyDeleteRequesAsString)
194
195         } catch (Exception ex) {
196             String exceptionMessage = " Bpmn error encountered in DeleteSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()
197             msoLogger.debug(exceptionMessage)
198             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
199
200         }
201         msoLogger.info(" ***** Exit prepareSDNCRequest *****")
202     }
203
204     public void prepareUpdateAfterDeActivateSDNCResource(DelegateExecution execution) {
205         msoLogger.info("***** started prepareUpdateAfterDeActivateSDNCResource *****")
206
207         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
208         String operType = resourceInputObj.getOperationType()
209         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
210         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
211         String operationId = resourceInputObj.getOperationId()
212         String progress = "100"
213         String status = "finished"
214         String statusDescription = "SDCN resource delete completed"
215
216         //String operationId = execution.getVariable("operationId")
217
218         String body = """
219                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
220                         xmlns:ns="http://org.onap.so/requestsdb">
221                         <soapenv:Header/>
222                 <soapenv:Body>
223                     <ns:updateResourceOperationStatus>
224                                <operType>${MsoUtils.xmlEscape(operType)}</operType>
225                                <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
226                                <progress>${MsoUtils.xmlEscape(progress)}</progress>
227                                <resourceTemplateUUID>${MsoUtils.xmlEscape(resourceCustomizationUuid)}</resourceTemplateUUID>
228                                <serviceId>${MsoUtils.xmlEscape(serviceInstanceId)}</serviceId>
229                                <status>${MsoUtils.xmlEscape(status)}</status>
230                                <statusDescription>${MsoUtils.xmlEscape(statusDescription)}</statusDescription>
231                     </ns:updateResourceOperationStatus>
232                 </soapenv:Body>
233                 </soapenv:Envelope>""";
234
235         setProgressUpdateVariables(execution, body)
236     }
237
238     public void postDeactivateSDNCCall(DelegateExecution execution) {
239         msoLogger.info(" ***** started postDeactivateSDNCCall *****")
240         String responseCode = execution.getVariable(Prefix + "sdncDeleteReturnCode")
241         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")
242
243         msoLogger.info("response from sdnc, response code :" + responseCode + "  response object :" + responseObj)
244         msoLogger.info(" ***** Exit prepareSDNCRequest *****")
245     }
246
247     public void sendSyncResponse(DelegateExecution execution) {
248         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
249         msoLogger.info(" ***** started sendSyncResponse *****")
250
251         try {
252             String operationStatus = "finished"
253             // RESTResponse for main flow
254             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
255             msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
256             sendWorkflowResponse(execution, 202, resourceOperationResp)
257             execution.setVariable("sentSyncResponse", true)
258
259         } catch (Exception ex) {
260             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
261             msoLogger.debug(msg)
262             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
263         }
264         msoLogger.debug(" ***** Exit sendSyncResopnse *****")
265     }
266
267 }