Merge "Added CreateVcpePesCustServiceV2 workflow for pnf"
[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.ModelInfo
36 import org.openecomp.mso.bpmn.core.domain.NetworkResource
37 import org.openecomp.mso.bpmn.core.domain.Resource
38 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
39 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
40 import org.openecomp.mso.bpmn.core.json.JsonUtils
41 import org.openecomp.mso.bpmn.infrastructure.properties.BPMNProperties
42
43 import static org.apache.commons.lang3.StringUtils.isBlank
44 import static org.apache.commons.lang3.StringUtils.isBlank
45
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(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<String> wanResources = new ArrayList<String>()
151
152         // get delete resource list and order list
153         List<Resource> delResourceList = execution.getVariable("deleteResourceList")
154         // existing resource list
155         List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
156
157         def resourceSequence = BPMNProperties.getResourceSequenceProp()
158
159         for (resourceType in resourceSequence.reverse()) {
160             for (resource in delResourceList) {
161                 if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
162                     sequencedResourceList.add(resource)
163
164                     if (resource instanceof NetworkResource) {
165                         wanResources.add(resource)
166                     }
167                 }
168             }
169         }
170
171         String isContainsWanResource = wanResources.isEmpty() ? "false" : "true"
172         execution.setVariable("isContainsWanResource", isContainsWanResource)
173         execution.setVariable("currentResourceIndex", 0)
174         execution.setVariable("resourceSequence", resourceSequence)
175         utils.log("INFO", "resourceSequence: " + resourceSequence, isDebugEnabled)
176         execution.setVariable("wanResources", wanResources)
177         utils.log("INFO", " ======== END sequenceResource Process ======== ", isDebugEnabled)
178     }
179
180     /**
181      * prepare delete parameters
182      */
183     public void preResourceDelete(execution, resourceName){
184
185         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
186
187         utils.log("INFO", " ======== STARTED preResourceDelete Process ======== ", isDebugEnabled)
188
189         List<Resource> existResourceList = execution.getVariable("deleteResourceList")
190
191         int currentIndex = execution.getVariable("currentResourceIndex")
192         Resource curResource = existResourceList.get(currentIndex);
193
194         String resourceInstanceUUID = curResource.getResourceId()
195         String resourceTemplateUUID = curResource.getModelInfo().getModelUuid()
196         execution.setVariable("resourceInstanceId", resourceInstanceUUID)
197         execution.setVariable("resourceUuid", resourceTemplateUUID)
198         execution.setVariable("resourceType", curResource.getModelInfo().getModelName())
199         execution.setVariable("currentResource", curResource)
200         utils.log("INFO", "Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + "  resourceInstanceId: "
201                 + resourceInstanceUUID + " resourceType: " + resourceName, isDebugEnabled)
202
203         utils.log("INFO", " ======== END preResourceDelete Process ======== ", isDebugEnabled)
204     }
205
206
207     /**
208      * Execute delete workflow for resource
209      */
210     public void executeResourceDelete(execution, resourceName) {
211         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
212         utils.log("INFO", "======== Start executeResourceDelete Process ======== ", isDebugEnabled)
213         String requestId = execution.getVariable("msoRequestId")
214         String serviceInstanceId = execution.getVariable("serviceInstanceId")
215         String serviceType = execution.getVariable("serviceType")
216
217         String resourceInstanceId = execution.getVariable("resourceInstanceId")
218         String resourceUuid = execution.getVariable("resourceUuid")
219
220         String requestAction = execution.getVariable("operationType")
221         JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceUuid, requestAction)
222         String recipeUri = resourceRecipe.getString("orchestrationUri")
223         int recipeTimeout = resourceRecipe.getInt("recipeTimeout")
224         String recipeParamXsd = resourceRecipe.get("paramXSD")
225
226         Resource currentResource = execution.getVariable("currentResource")
227
228         ResourceInput resourceInput = new ResourceInput();
229         resourceInput.setServiceInstanceId(serviceInstanceId)
230         resourceInput.setResourceInstanceName(currentResource.getResourceInstanceName())
231         resourceInput.setGlobalSubscriberId("globalSubscriberId")
232         ModelInfo modelInfo = new ModelInfo()
233         modelInfo.setModelCustomizationUuid(currentResource.getModelInfo().getModelCustomizationUuid())
234         modelInfo.setModelUuid(currentResource.getModelInfo().getModelCustomizationUuid())
235         modelInfo.setModelInvariantUuid(currentResource.getModelInfo().getModelInvariantUuid())
236         resourceInput.setServiceModelInfo(modelInfo)
237         resourceInput.setServiceType(serviceType)
238
239         String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + recipeUri
240
241         HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeout, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
242         utils.log("INFO", " ======== END executeResourceDelete Process ======== ", isDebugEnabled)
243     }
244
245
246     public void parseNextResource(execution){
247         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
248         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)
249         def currentIndex = execution.getVariable("currentResourceIndex")
250         def nextIndex =  currentIndex + 1
251         execution.setVariable("currentResourceIndex", nextIndex)
252         List<String> resourceSequence = execution.getVariable("resourceSequence")
253         if(nextIndex >= resourceSequence.size()){
254             execution.setVariable("allResourceFinished", "true")
255         }else{
256             execution.setVariable("allResourceFinished", "false")
257         }
258         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)
259     }
260 }