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