8508f077655235fd9792564fcd635fc24b7d293e
[so.git] /
1
2 /*-
3  * ============LICENSE_START=======================================================
4  * ONAP - SO
5  * ================================================================================
6  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.openecomp.mso.bpmn.infrastructure.scripts
22
23 import groovy.json.JsonSlurper
24 import org.apache.commons.lang3.StringUtils
25 import org.apache.http.HttpResponse
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.json.JSONObject
29 import org.openecomp.mso.bpmn.common.recipe.BpmnRestClient
30 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
31 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder
32 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
34 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
35 import org.openecomp.mso.bpmn.core.domain.AllottedResource
36 import org.openecomp.mso.bpmn.core.domain.ModelInfo
37 import org.openecomp.mso.bpmn.core.domain.NetworkResource
38 import org.openecomp.mso.bpmn.core.domain.Resource
39 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
40 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
41 import org.openecomp.mso.bpmn.core.domain.VnfResource
42 import org.openecomp.mso.bpmn.core.json.JsonUtils
43 import org.openecomp.mso.bpmn.infrastructure.properties.BPMNProperties
44
45 import static org.apache.commons.lang3.StringUtils.isBlank
46
47 /**
48  * input for script :
49  * msoRequestId
50  * isDebugLogEnabled
51  * globalSubscriberId
52  * serviceType
53  * serviceInstanceId
54  * URN_mso_workflow_sdncadapter_callback
55  * serviceInputParams
56  * deleteResourceList
57  * resourceInstanceIDs
58  *
59  * output from script:
60  *
61  */
62
63 public class DoDeleteResourcesV1 extends AbstractServiceTaskProcessor {
64
65     String Prefix="DDR_"
66     ExceptionUtil exceptionUtil = new ExceptionUtil()
67     JsonUtils jsonUtil = new JsonUtils()
68     CatalogDbUtils cutils = new CatalogDbUtils()
69
70     public void preProcessRequest (DelegateExecution execution) {
71         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
72         utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
73         String msg = ""
74
75         try {
76             String requestId = execution.getVariable("msoRequestId")
77             execution.setVariable("prefix",Prefix)
78
79             //Inputs
80             //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
81             String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
82             if (globalSubscriberId == null)
83             {
84                 execution.setVariable("globalSubscriberId", "")
85             }
86
87             //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
88             String serviceType = execution.getVariable("serviceType")
89             if (serviceType == null)
90             {
91                 execution.setVariable("serviceType", "")
92             }
93
94             //Generated in parent for AAI PUT
95             String serviceInstanceId = execution.getVariable("serviceInstanceId")
96             if (isBlank(serviceInstanceId)){
97                 msg = "Input serviceInstanceId is null"
98                 utils.log("INFO", msg, isDebugEnabled)
99                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100             }
101
102             String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
103             if (isBlank(sdncCallbackUrl)) {
104                 msg = "URN_mso_workflow_sdncadapter_callback is null"
105                 utils.log("INFO", msg, isDebugEnabled)
106                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
107             }
108             execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
109             utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
110
111             StringBuilder sbParams = new StringBuilder()
112             Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
113             if (paramsMap != null)
114             {
115                 sbParams.append("<service-input-parameters>")
116                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
117                     String paramsXml
118                     String paramName = entry.getKey()
119                     String paramValue = entry.getValue()
120                     paramsXml =
121                             """ <param>
122                                                         <name>${paramName}</name>
123                                                         <value>${paramValue}</value>
124                                                         </param>
125                                                         """
126                     sbParams.append(paramsXml)
127                 }
128                 sbParams.append("</service-input-parameters>")
129             }
130             String siParamsXml = sbParams.toString()
131             if (siParamsXml == null)
132                 siParamsXml = ""
133             execution.setVariable("siParamsXml", siParamsXml)
134
135         } catch (BpmnError e) {
136             throw e;
137         } catch (Exception ex){
138             msg = "Exception in preProcessRequest " + ex.getMessage()
139             utils.log("INFO", msg, isDebugEnabled)
140             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
141         }
142         utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
143     }
144
145     public void sequenceResource(DelegateExecution execution){
146         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
147
148         utils.log("INFO", " ======== STARTED sequenceResource Process ======== ", isDebugEnabled)
149         List<Resource> sequencedResourceList = new ArrayList<Resource>()
150         List<Resource> wanResources = new ArrayList<Resource>()
151
152         // get delete resource list and order list
153         List<Resource> delResourceList = execution.getVariable("deleteResourceList")
154         
155         def resourceSequence = BPMNProperties.getResourceSequenceProp()
156
157         if(resourceSequence != null) {
158             for (resourceType in resourceSequence.reverse()) {
159                 for (resource in delResourceList) {
160                     if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
161                         sequencedResourceList.add(resource)
162
163                         if (resource instanceof NetworkResource) {
164                             wanResources.add(resource)
165                         }
166                     }
167                 }
168             }
169         }else {
170             //define sequenced resource list, we deploy vf first and then network and then ar
171             //this is defaule sequence
172             List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
173             List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
174             for (Resource rc : delResourceList) {
175                 if (rc instanceof VnfResource) {
176                     vnfResourceList.add(rc)
177                 } else if (rc instanceof NetworkResource) {
178                         wanResources.add(rc)
179                 } else if (rc instanceof AllottedResource) {
180                     arResourceList.add(rc)
181                 }
182             }
183
184             sequencedResourceList.addAll(arResourceList)
185             sequencedResourceList.addAll(wanResources)
186             sequencedResourceList.addAll(vnfResourceList)
187         }
188
189         String isContainsWanResource = wanResources.isEmpty() ? "false" : "true"
190         execution.setVariable("isContainsWanResource", isContainsWanResource)
191         execution.setVariable("currentResourceIndex", 0)
192         execution.setVariable("sequencedResourceList", sequencedResourceList)
193         utils.log("INFO", "resourceSequence: " + resourceSequence, isDebugEnabled)
194         utils.log("INFO", " ======== END sequenceResource Process ======== ", isDebugEnabled)
195     }
196
197     /**
198      * prepare delete parameters
199      */
200     public void preResourceDelete(DelegateExecution execution){
201
202         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
203
204         utils.log("INFO", " ======== STARTED preResourceDelete Process ======== ", isDebugEnabled)
205
206         List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
207
208         int currentIndex = execution.getVariable("currentResourceIndex")
209         Resource curResource = sequencedResourceList.get(currentIndex);
210
211         String resourceInstanceUUID = curResource.getResourceId()
212         String resourceTemplateUUID = curResource.getModelInfo().getModelUuid()
213         execution.setVariable("resourceInstanceId", resourceInstanceUUID)
214         execution.setVariable("currentResource", curResource)
215         utils.log("INFO", "Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + "  resourceInstanceId: "
216                 + resourceInstanceUUID + " resourceModelName: " + curResource.getModelInfo().getModelName(), isDebugEnabled)
217
218         utils.log("INFO", " ======== END preResourceDelete Process ======== ", isDebugEnabled)
219     }
220
221
222     /**
223      * Execute delete workflow for resource
224      */
225     public void executeResourceDelete(DelegateExecution execution) {
226         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
227         utils.log("INFO", "======== Start executeResourceDelete Process ======== ", isDebugEnabled)
228         String requestId = execution.getVariable("msoRequestId")
229         String serviceInstanceId = execution.getVariable("serviceInstanceId")
230         String serviceType = execution.getVariable("serviceType")
231
232         String resourceInstanceId = execution.getVariable("resourceInstanceId")
233
234         Resource currentResource = execution.getVariable("currentResource")
235         String action = "deleteInstance"
236         JSONObject resourceRecipe = cutils.getResourceRecipe(execution, currentResource.getModelInfo().getModelUuid(), action)
237         String recipeUri = resourceRecipe.getString("orchestrationUri")
238         int recipeTimeout = resourceRecipe.getInt("recipeTimeout")
239         String recipeParamXsd = resourceRecipe.get("paramXSD")
240
241
242         ResourceInput resourceInput = new ResourceInput();
243         resourceInput.setServiceInstanceId(serviceInstanceId)
244         resourceInput.setResourceInstanceName(currentResource.getResourceInstanceName())
245         resourceInput.setResourceInstancenUuid(currentResource.getResourceId())
246         resourceInput.setOperationId(execution.getVariable("operationId"))
247         String globalSubscriberId = execution.getVariable("globalSubscriberId") 
248         resourceInput.setGlobalSubscriberId(globalSubscriberId)
249         resourceInput.setResourceModelInfo(currentResource.getModelInfo());
250             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
251                 resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
252         resourceInput.setServiceType(serviceType)
253
254         String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + recipeUri
255
256         HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeout, action, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
257         utils.log("INFO", " ======== END executeResourceDelete Process ======== ", isDebugEnabled)
258     }
259
260
261     public void parseNextResource(DelegateExecution execution){
262         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
263         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)
264         def currentIndex = execution.getVariable("currentResourceIndex")
265         def nextIndex =  currentIndex + 1
266         execution.setVariable("currentResourceIndex", nextIndex)
267         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")
268         if(nextIndex >= sequencedResourceList.size()){
269             execution.setVariable("allResourceFinished", "true")
270         }else{
271             execution.setVariable("allResourceFinished", "false")
272         }
273         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)
274     }
275     
276     public void prepareFinishedProgressForResource(execution) {
277
278         String serviceInstanceId = execution.getVariable("serviceInstanceId")
279         String serviceType = execution.getVariable("serviceType")
280         String resourceInstanceId = execution.getVariable("resourceInstanceId")
281         Resource currentResource = execution.getVariable("currentResource")
282         String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid()
283         String resourceModelName = currentResource.getModelInfo().getModelName()
284         String operationType = execution.getVariable("operationType")
285         String progress = "100"
286         String status = "finished"
287         String statusDescription = "The resource instance does not exist for " + resourceModelName
288         String operationId = execution.getVariable("operationId")
289
290         String body = """
291                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
292                         xmlns:ns="http://org.openecomp.mso/requestsdb">
293                         <soapenv:Header/>
294                 <soapenv:Body>
295                     <ns:updateResourceOperationStatus>
296                                <operType>${operationType}</operType>
297                                <operationId>${operationId}</operationId>
298                                <progress>${progress}</progress>
299                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
300                                <serviceId>${ServiceInstanceId}</serviceId>
301                                <status>${status}</status>
302                                <statusDescription>${statusDescription}</statusDescription>
303                     </ns:updateResourceOperationStatus>
304                 </soapenv:Body>
305                 </soapenv:Envelope>""";
306
307         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
308         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
309         execution.setVariable("CVFMI_updateResOperStatusRequest", body)
310     }
311
312     public void prepareServiceTopologyDeletion(DelegateExecution execution) {
313         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
314         utils.log("INFO"," ***** prepareServiceTopologyDeletion "  + " *****", isDebugEnabled)
315
316         ModelInfo serviceModelInfo = execution.getVariable("serviceModelInfo")
317
318         execution.setVariable("modelInvariantUuid", serviceModelInfo.getModelInvariantUuid())
319         execution.setVariable("modelVersion", serviceModelInfo.getModelVersion())
320         execution.setVariable("modelUuid", serviceModelInfo.getModelUuid())
321         execution.setVariable("serviceModelName", serviceModelInfo.getModelName())
322
323         // set operation type and resource type is required to form request body
324         execution.setVariable("operationType", "DELETE")
325         execution.setVariable("resourceType", "SDNC-SERVICE-TOPOLOGY")
326
327         utils.log("INFO"," ***** prepareServiceTopologyDeletion "  + " *****", isDebugEnabled)
328     }
329 }