6e19a3469a04f5f8edc629d2826bfc7218118b73
[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 import static org.apache.commons.lang3.StringUtils.isBlank
47
48
49 /**
50  * input for script :
51  * msoRequestId
52  * isDebugLogEnabled
53  * globalSubscriberId
54  * serviceType
55  * serviceInstanceId
56  * URN_mso_workflow_sdncadapter_callback
57  * serviceInputParams
58  * deleteResourceList
59  * resourceInstanceIDs
60  *
61  * output from script:
62  *
63  */
64
65 public class DoDeleteResourcesV1 extends AbstractServiceTaskProcessor {
66
67     String Prefix="DDR_"
68     ExceptionUtil exceptionUtil = new ExceptionUtil()
69     JsonUtils jsonUtil = new JsonUtils()
70     CatalogDbUtils cutils = new CatalogDbUtils()
71
72     public void preProcessRequest (DelegateExecution execution) {
73         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
74         utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
75         String msg = ""
76
77         try {
78             String requestId = execution.getVariable("msoRequestId")
79             execution.setVariable("prefix",Prefix)
80
81             //Inputs
82             //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
83             String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
84             if (globalSubscriberId == null)
85             {
86                 execution.setVariable("globalSubscriberId", "")
87             }
88
89             //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
90             String serviceType = execution.getVariable("serviceType")
91             if (serviceType == null)
92             {
93                 execution.setVariable("serviceType", "")
94             }
95
96             //Generated in parent for AAI PUT
97             String serviceInstanceId = execution.getVariable("serviceInstanceId")
98             if (isBlank(serviceInstanceId)){
99                 msg = "Input serviceInstanceId is null"
100                 utils.log("INFO", msg, isDebugEnabled)
101                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
102             }
103
104             String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
105             if (isBlank(sdncCallbackUrl)) {
106                 msg = "URN_mso_workflow_sdncadapter_callback is null"
107                 utils.log("INFO", msg, isDebugEnabled)
108                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109             }
110             execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
111             utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
112
113             StringBuilder sbParams = new StringBuilder()
114             Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
115             if (paramsMap != null)
116             {
117                 sbParams.append("<service-input-parameters>")
118                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
119                     String paramsXml
120                     String paramName = entry.getKey()
121                     String paramValue = entry.getValue()
122                     paramsXml =
123                             """ <param>
124                                                         <name>${paramName}</name>
125                                                         <value>${paramValue}</value>
126                                                         </param>
127                                                         """
128                     sbParams.append(paramsXml)
129                 }
130                 sbParams.append("</service-input-parameters>")
131             }
132             String siParamsXml = sbParams.toString()
133             if (siParamsXml == null)
134                 siParamsXml = ""
135             execution.setVariable("siParamsXml", siParamsXml)
136
137         } catch (BpmnError e) {
138             throw e;
139         } catch (Exception ex){
140             msg = "Exception in preProcessRequest " + ex.getMessage()
141             utils.log("INFO", msg, isDebugEnabled)
142             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
143         }
144         utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
145     }
146
147     public void sequenceResource(execution){
148         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
149
150         utils.log("INFO", " ======== STARTED sequenceResource Process ======== ", isDebugEnabled)
151         List<Resource> sequencedResourceList = new ArrayList<Resource>()
152         List<String> wanResources = new ArrayList<String>()
153
154         // get delete resource list and order list
155         List<Resource> delResourceList = execution.getVariable("deleteResourceList")
156         // existing resource list
157         List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
158
159         def resourceSequence = BPMNProperties.getResourceSequenceProp()
160
161         if(resourceSequence != null) {
162             for (resourceType in resourceSequence.reverse()) {
163                 for (resource in delResourceList) {
164                     if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
165                         sequencedResourceList.add(resource)
166
167                         if (resource instanceof NetworkResource) {
168                             wanResources.add(resource)
169                         }
170                     }
171                 }
172             }
173         }else {
174             //define sequenced resource list, we deploy vf first and then network and then ar
175             //this is defaule sequence
176             List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
177             List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
178
179             for (Resource rc : delResourceList) {
180                 if (rc instanceof VnfResource) {
181                     vnfResourceList.add(rc)
182                 } else if (rc instanceof NetworkResource) {
183                     networkResourceList.add(rc)
184                 } else if (rc instanceof AllottedResource) {
185                     arResourceList.add(rc)
186                 }
187             }
188
189             sequencedResourceList.addAll(arResourceList)
190             sequencedResourceList.addAll(networkResourceList)
191             sequencedResourceList.addAll(vnfResourceList)
192         }
193
194         String isContainsWanResource = wanResources.isEmpty() ? "false" : "true"
195         execution.setVariable("isContainsWanResource", isContainsWanResource)
196         execution.setVariable("currentResourceIndex", 0)
197         execution.setVariable("resourceSequence", resourceSequence)
198         utils.log("INFO", "resourceSequence: " + resourceSequence, isDebugEnabled)
199         execution.setVariable("wanResources", wanResources)
200         utils.log("INFO", " ======== END sequenceResource Process ======== ", isDebugEnabled)
201     }
202
203     /**
204      * prepare delete parameters
205      */
206     public void preResourceDelete(execution, resourceName){
207
208         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
209
210         utils.log("INFO", " ======== STARTED preResourceDelete Process ======== ", isDebugEnabled)
211
212         List<Resource> existResourceList = execution.getVariable("deleteResourceList")
213
214         int currentIndex = execution.getVariable("currentResourceIndex")
215         Resource curResource = existResourceList.get(currentIndex);
216
217         String resourceInstanceUUID = curResource.getResourceId()
218         String resourceTemplateUUID = curResource.getModelInfo().getModelUuid()
219         execution.setVariable("resourceInstanceId", resourceInstanceUUID)
220         execution.setVariable("resourceUuid", resourceTemplateUUID)
221         execution.setVariable("resourceType", curResource.getModelInfo().getModelName())
222         execution.setVariable("currentResource", curResource)
223         utils.log("INFO", "Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + "  resourceInstanceId: "
224                 + resourceInstanceUUID + " resourceType: " + resourceName, isDebugEnabled)
225
226         utils.log("INFO", " ======== END preResourceDelete Process ======== ", isDebugEnabled)
227     }
228
229
230     /**
231      * Execute delete workflow for resource
232      */
233     public void executeResourceDelete(execution, resourceName) {
234         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
235         utils.log("INFO", "======== Start executeResourceDelete Process ======== ", isDebugEnabled)
236         String requestId = execution.getVariable("msoRequestId")
237         String serviceInstanceId = execution.getVariable("serviceInstanceId")
238         String serviceType = execution.getVariable("serviceType")
239
240         String resourceInstanceId = execution.getVariable("resourceInstanceId")
241         String resourceUuid = execution.getVariable("resourceUuid")
242
243         String requestAction = execution.getVariable("operationType")
244         JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceUuid, requestAction)
245         String recipeUri = resourceRecipe.getString("orchestrationUri")
246         int recipeTimeout = resourceRecipe.getInt("recipeTimeout")
247         String recipeParamXsd = resourceRecipe.get("paramXSD")
248
249         Resource currentResource = execution.getVariable("currentResource")
250
251         ResourceInput resourceInput = new ResourceInput();
252         resourceInput.setServiceInstanceId(serviceInstanceId)
253         resourceInput.setResourceInstanceName(currentResource.getResourceInstanceName())
254         resourceInput.setGlobalSubscriberId("globalSubscriberId")
255         ModelInfo modelInfo = new ModelInfo()
256         modelInfo.setModelCustomizationUuid(currentResource.getModelInfo().getModelCustomizationUuid())
257         modelInfo.setModelUuid(currentResource.getModelInfo().getModelCustomizationUuid())
258         modelInfo.setModelInvariantUuid(currentResource.getModelInfo().getModelInvariantUuid())
259         resourceInput.setServiceModelInfo(modelInfo)
260         resourceInput.setServiceType(serviceType)
261
262         String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + recipeUri
263
264         HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeout, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
265         utils.log("INFO", " ======== END executeResourceDelete Process ======== ", isDebugEnabled)
266     }
267
268
269     public void parseNextResource(execution){
270         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
271         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)
272         def currentIndex = execution.getVariable("currentResourceIndex")
273         def nextIndex =  currentIndex + 1
274         execution.setVariable("currentResourceIndex", nextIndex)
275         List<String> resourceSequence = execution.getVariable("resourceSequence")
276         if(nextIndex >= resourceSequence.size()){
277             execution.setVariable("allResourceFinished", "true")
278         }else{
279             execution.setVariable("allResourceFinished", "false")
280         }
281         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)
282     }
283 }