Merge "Added logic to call aria adapter"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteResourcesV1.groovy
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.Resource
36 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
37 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
38 import org.openecomp.mso.bpmn.core.json.JsonUtils
39 import org.openecomp.mso.bpmn.infrastructure.properties.BPMNProperties
40
41 import static org.apache.commons.lang3.StringUtils.isBlank
42 import static org.apache.commons.lang3.StringUtils.isBlank
43
44
45 /**
46  * input for script :
47  * msoRequestId
48  * isDebugLogEnabled
49  * globalSubscriberId
50  * serviceType
51  * serviceInstanceId
52  * URN_mso_workflow_sdncadapter_callback
53  * serviceInputParams
54  * deleteResourceList
55  * resourceInstanceIDs
56  *
57  * output from script:
58  *
59  */
60
61 public class DoDeleteResourcesV1 extends AbstractServiceTaskProcessor {
62
63     String Prefix="DDR_"
64     ExceptionUtil exceptionUtil = new ExceptionUtil()
65     JsonUtils jsonUtil = new JsonUtils()
66     CatalogDbUtils cutils = new CatalogDbUtils()
67
68     public void preProcessRequest (DelegateExecution execution) {
69         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
70         utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
71         String msg = ""
72
73         try {
74             String requestId = execution.getVariable("msoRequestId")
75             execution.setVariable("prefix",Prefix)
76
77             //Inputs
78             //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
79             String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
80             if (globalSubscriberId == null)
81             {
82                 execution.setVariable("globalSubscriberId", "")
83             }
84
85             //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
86             String serviceType = execution.getVariable("serviceType")
87             if (serviceType == null)
88             {
89                 execution.setVariable("serviceType", "")
90             }
91
92             //Generated in parent for AAI PUT
93             String serviceInstanceId = execution.getVariable("serviceInstanceId")
94             if (isBlank(serviceInstanceId)){
95                 msg = "Input serviceInstanceId is null"
96                 utils.log("INFO", msg, isDebugEnabled)
97                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
98             }
99
100             String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
101             if (isBlank(sdncCallbackUrl)) {
102                 msg = "URN_mso_workflow_sdncadapter_callback is null"
103                 utils.log("INFO", msg, isDebugEnabled)
104                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105             }
106             execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
107             utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
108
109             StringBuilder sbParams = new StringBuilder()
110             Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
111             if (paramsMap != null)
112             {
113                 sbParams.append("<service-input-parameters>")
114                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
115                     String paramsXml
116                     String paramName = entry.getKey()
117                     String paramValue = entry.getValue()
118                     paramsXml =
119                             """ <param>
120                                                         <name>${paramName}</name>
121                                                         <value>${paramValue}</value>
122                                                         </param>
123                                                         """
124                     sbParams.append(paramsXml)
125                 }
126                 sbParams.append("</service-input-parameters>")
127             }
128             String siParamsXml = sbParams.toString()
129             if (siParamsXml == null)
130                 siParamsXml = ""
131             execution.setVariable("siParamsXml", siParamsXml)
132
133         } catch (BpmnError e) {
134             throw e;
135         } catch (Exception ex){
136             msg = "Exception in preProcessRequest " + ex.getMessage()
137             utils.log("INFO", msg, isDebugEnabled)
138             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
139         }
140         utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
141     }
142
143     public void sequenceResource(execution){
144         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
145
146         utils.log("INFO", " ======== STARTED sequenceResource Process ======== ", isDebugEnabled)
147         List<String> nsResources = new ArrayList<String>()
148         List<String> wanResources = new ArrayList<String>()
149         List<String> resourceSequence = new  ArrayList<String>()
150
151         // get delete resource list and order list
152         List<Resource> delResourceList = execution.getVariable("deleteResourceList")
153         // existing resource list
154         List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
155
156         for(ServiceInstance rc_e : existResourceList){
157
158             String muuid = rc_e.getModelInfo().getModelUuid()
159             String mIuuid = rc_e.getModelInfo().getModelInvariantUuid()
160             String mCuuid = rc_e.getModelInfo().getModelCustomizationUuid()
161             rcType = rc_e.getInstanceName()
162
163             for(Resource rc_d : delResourceList){
164
165                 if(rc_d.getModelInfo().getModelUuid() == muuid
166                         && rc_d.getModelInfo().getModelInvariantUuid() == mIuuid
167                         && rc_d.getModelInfo().getModelCustomizationUuid() == mCuuid) {
168
169                     if(StringUtils.containsIgnoreCase(rcType, "overlay")
170                             || StringUtils.containsIgnoreCase(rcType, "underlay")){
171                         wanResources.add(rcType)
172                     }else{
173                         nsResources.add(rcType)
174                     }
175
176                 }
177             }
178
179         }
180
181         resourceSequence.addAll(wanResources)
182         resourceSequence.addAll(nsResources)
183         String isContainsWanResource = wanResources.isEmpty() ? "false" : "true"
184         execution.setVariable("isContainsWanResource", isContainsWanResource)
185         execution.setVariable("currentResourceIndex", 0)
186         execution.setVariable("resourceSequence", resourceSequence)
187         utils.log("INFO", "resourceSequence: " + resourceSequence, isDebugEnabled)
188         execution.setVariable("wanResources", wanResources)
189         utils.log("INFO", " ======== END sequenceResource Process ======== ", isDebugEnabled)
190     }
191
192     /**
193      * prepare delete parameters
194      */
195     public void preResourceDelete(execution, resourceName){
196
197         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
198
199         utils.log("INFO", " ======== STARTED preResourceDelete Process ======== ", isDebugEnabled)
200
201         List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
202
203         for(ServiceInstance rc_e : existResourceList){
204
205             if(StringUtils.containsIgnoreCase(rc_e.getInstanceName(), resourceName)) {
206
207                 String resourceInstanceUUID = rc_e.getInstanceId()
208                 String resourceTemplateUUID = rc_e.getModelInfo().getModelUuid()
209                 execution.setVariable("resourceInstanceId", resourceInstanceUUID)
210                 execution.setVariable("resourceTemplateId", resourceTemplateUUID)
211                 execution.setVariable("resourceType", resourceName)
212                 utils.log("INFO", "Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + "  resourceInstanceId: "
213                         + resourceInstanceUUID + " resourceType: " + resourceName, isDebugEnabled)
214             }
215         }
216
217         utils.log("INFO", " ======== END preResourceDelete Process ======== ", isDebugEnabled)
218     }
219
220
221     /**
222      * Execute delete workflow for resource
223      */
224     public void executeResourceDelete(execution) {
225         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
226         utils.log("INFO", "======== Start executeResourceDelete Process ======== ", isDebugEnabled)
227         String requestId = execution.getVariable("msoRequestId")
228         String serviceInstanceId = execution.getVariable("serviceInstanceId")
229         String serviceType = execution.getVariable("serviceType")
230         ResourceInput resourceInput = execution.getVariable("resourceInput")
231         String requestAction = resourceInput.getOperationType()
232         JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceUuid(), requestAction)
233         String recipeUri = resourceRecipe.getString("orchestrationUri")
234         String recipeTimeOut = resourceRecipe.getString("recipeTimeout")
235         String recipeParamXsd = resourceRecipe.get("paramXSD")
236         HttpResponse resp = BpmnRestClient.post(recipeUri, requestId, recipeTimeout, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
237         utils.log("INFO", " ======== END executeResourceDelete Process ======== ", isDebugEnabled)
238     }
239
240
241     public void parseNextResource(execution){
242         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
243         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)
244         def currentIndex = execution.getVariable("currentResourceIndex")
245         def nextIndex =  currentIndex + 1
246         execution.setVariable("currentResourceIndex", nextIndex)
247         List<String> resourceSequence = execution.getVariable("resourceSequence")
248         if(nextIndex >= resourceSequence.size()){
249             execution.setVariable("allResourceFinished", "true")
250         }else{
251             execution.setVariable("allResourceFinished", "false")
252         }
253         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)
254     }
255 }