Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteResources.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved. 
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.bpmn.infrastructure.scripts
21
22 import org.json.JSONArray;
23
24 import static org.apache.commons.lang3.StringUtils.*;
25 import groovy.xml.XmlUtil
26 import groovy.json.*
27
28 import org.openecomp.mso.bpmn.core.domain.ModelInfo
29 import org.openecomp.mso.bpmn.core.domain.Resource
30 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
31 import org.openecomp.mso.bpmn.core.json.JsonUtils
32 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
34 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
35 import org.openecomp.mso.bpmn.core.WorkflowException
36 import org.openecomp.mso.rest.APIResponse;
37 import org.openecomp.mso.rest.RESTClient
38 import org.openecomp.mso.rest.RESTConfig
39
40 import java.util.List;
41 import java.util.UUID;
42 import javax.xml.parsers.DocumentBuilder
43 import javax.xml.parsers.DocumentBuilderFactory
44
45 import org.camunda.bpm.engine.delegate.BpmnError
46 import org.camunda.bpm.engine.delegate.DelegateExecution
47 import org.camunda.bpm.engine.runtime.Execution
48 import org.json.JSONObject;
49 import org.apache.commons.lang3.*
50 import org.apache.commons.codec.binary.Base64;
51 import org.springframework.web.util.UriUtils;
52 import org.w3c.dom.Document
53 import org.w3c.dom.Element
54 import org.w3c.dom.Node
55 import org.w3c.dom.NodeList
56 import org.xml.sax.InputSource
57
58 import com.fasterxml.jackson.jaxrs.json.annotation.JSONP.Def;
59
60 /**
61  * This groovy class supports the <class>DoDeleteResources.bpmn</class> process.
62  * 
63  * Inputs:
64  * @param - msoRequestId
65  * @param - globalSubscriberId - O
66  * @param - subscriptionServiceType - O
67  * @param - serviceInstanceId
68  * @param - serviceInstanceName - O
69  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
70  * @param - sdncVersion 
71  * @param - failNotFound - TODO
72  * @param - serviceInputParams - TODO 
73  *
74  * @param - delResourceList
75  * @param - serviceRelationShip
76  *
77  * Outputs:
78  * @param - WorkflowException
79  * 
80  * Rollback - Deferred
81  */
82 public class DoDeleteResources extends AbstractServiceTaskProcessor {
83
84         String Prefix="DDELR_"
85         ExceptionUtil exceptionUtil = new ExceptionUtil()
86         JsonUtils jsonUtil = new JsonUtils()
87
88         public void preProcessRequest (DelegateExecution execution) {
89                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
90                 utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
91                 String msg = ""
92                 
93                 List<ServiceInstance> realNSRessources = new ArrayList<ServiceInstance>() 
94         
95         // related ns from AAI
96         String serviceRelationShip = execution.getVariable("serviceRelationShip")
97         def jsonSlurper = new JsonSlurper()
98         def jsonOutput = new JsonOutput()        
99         List<String> nsSequence = new ArrayList<String>() 
100         List relationShipList =  jsonSlurper.parseText(serviceRelationShip)
101         if (relationShipList != null) {
102            relationShipList.each {
103                String resourceType = it.resourceType
104                nsSequence.add(resourceType)
105            }
106         }       
107      
108         execution.setVariable("currentNSIndex", 0)
109         execution.setVariable("nsSequence", nsSequence)
110         execution.setVariable("realNSRessources", realNSRessources)
111         utils.log("INFO", "nsSequence: " + nsSequence, isDebugEnabled) 
112
113                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
114     }
115
116    public void getCurrentNS(execution){
117        def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
118        utils.log("INFO", "======== Start getCurrentNS Process ======== ", isDebugEnabled)    
119        
120        def currentIndex = execution.getVariable("currentNSIndex")
121        List<String> nsSequence = execution.getVariable("nsSequence") 
122        String nsResourceType =  nsSequence.get(currentIndex)
123        
124        // GET AAI by Name, not ID, for process convenient
125        execution.setVariable("GENGS_type", "service-instance")
126        execution.setVariable("GENGS_serviceInstanceId", "") 
127        execution.setVariable("GENGS_serviceInstanceName", nsResourceType)       
128
129        utils.log("INFO", "======== COMPLETED getCurrentNS Process ======== ", isDebugEnabled)  
130    }    
131         
132         public void postProcessAAIGET(DelegateExecution execution) {
133                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
134                 utils.log("INFO"," ***** postProcessAAIGET2 ***** ", isDebugEnabled)
135                 String msg = ""
136
137                 try {
138                         String nsResourceName = execution.getVariable("GENGS_serviceInstanceName")
139                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
140                         if(!succInAAI){
141                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + nsResourceName, isDebugEnabled)
142                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
143                                 utils.logAudit("workflowException: " + workflowException)
144                                 if(workflowException != null){
145                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
146                                 }
147                                 else
148                                 {
149                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
150                                         utils.log("INFO", msg, isDebugEnabled)
151                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
152                                 }
153                         }
154                         else
155                         {
156                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
157                                 if(foundInAAI){
158                                         String aaiService = execution.getVariable("GENGS_service")
159                                         if (!isBlank(aaiService)) {
160                                             String svcId = utils.getNodeText1(aaiService, "service-instance-id")
161                                                 //String mn = utils.getNodeText1(aaiService, "model-name")                                              
162                                                 String mIuuid = utils.getNodeText1(aaiService, "model-invariant-id")
163                                                 String muuid = utils.getNodeText1(aaiService, "model-version-id")
164                                                 String mCuuid = utils.getNodeText1(aaiService, "model-customization-uuid")
165                                                 ServiceInstance rc = new ServiceInstance()
166                                                 ModelInfo modelInfo = new ModelInfo()
167                                                 //modelInfo.setModelName(mn)
168                                                 modelInfo.setModelUuid(muuid)                                           
169                                                 modelInfo.setModelInvariantUuid(mIuuid)
170                                                 modelInfo.getModelCustomizationUuid(mCuuid)
171                                                 rc.setModelInfo(modelInfo)
172                                                 rc.setInstanceId(svcId)
173                                                 rc.setInstanceName(nsResourceName)
174
175                                                 List<ServiceInstance> realNSRessources = execution.getVariable("realNSRessources")
176                                                 realNSRessources.add(rc)
177                                                 execution.setVariable("realNSRessources", realNSRessources)
178                                                 
179                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
180                                         }
181                                 }
182                         }
183                 } catch (BpmnError e) {
184                         throw e;
185                 } catch (Exception ex) {
186                         msg = "Exception in DoDeleteResources.postProcessAAIGET " + ex.getMessage()
187                         utils.log("INFO", msg, isDebugEnabled)
188                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
189                 }
190                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
191         }
192         
193     public void parseNextNS(execution){
194        def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
195        utils.log("INFO", "======== Start parseNextNS Process ======== ", isDebugEnabled)    
196        def currentIndex = execution.getVariable("currentNSIndex")
197        def nextIndex =  currentIndex + 1
198        execution.setVariable("currentNSIndex", nextIndex)
199        List<String> nsSequence = execution.getVariable("nsSequence")    
200        if(nextIndex >= nsSequence.size()){
201            execution.setVariable("allNsFinished", "true")
202        }else{
203            execution.setVariable("allNsFinished", "false")
204        }
205        utils.log("INFO", "======== COMPLETED parseNextNS Process ======== ", isDebugEnabled)            
206    }   
207
208    
209    public void sequenceResource(execution){
210        def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
211
212        utils.log("INFO", " ======== STARTED sequenceResource Process ======== ", isDebugEnabled)
213        List<String> nsResources = new ArrayList<String>()
214        List<String> wanResources = new ArrayList<String>()
215        List<String> resourceSequence = new  ArrayList<String>()
216        
217        // get delete resource list and order list
218        List<Resource> delResourceList = execution.getVariable("delResourceList")
219        // existing resource list
220        List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
221        
222        for(ServiceInstance rc_e : existResourceList){
223        
224            String muuid = rc_e.getModelInfo().getModelUuid()
225            String mIuuid = rc_e.getModelInfo().getModelInvariantUuid()
226            String mCuuid = rc_e.getModelInfo().getModelCustomizationUuid()
227            rcType = rc_e.getInstanceName()
228            
229            for(Resource rc_d : delResourceList){                 
230            
231                 if(rc_d.getModelInfo().getModelUuid() == muuid 
232                 && rc_d.getModelInfo().getModelInvariantUuid() == mIuuid 
233                 && rc_d.getModelInfo().getModelCustomizationUuid() == mCuuid) {
234                 
235                    if(StringUtils.containsIgnoreCase(rcType, "overlay") 
236                    || StringUtils.containsIgnoreCase(rcType, "underlay")){                   
237                        wanResources.add(rcType)
238                    }else{
239                        nsResources.add(rcType)
240                    }
241                    
242                }
243            }
244            
245        }
246   
247        resourceSequence.addAll(wanResources)
248        resourceSequence.addAll(nsResources)
249        String isContainsWanResource = wanResources.isEmpty() ? "false" : "true"
250        execution.setVariable("isContainsWanResource", isContainsWanResource)
251        execution.setVariable("currentResourceIndex", 0)
252        execution.setVariable("resourceSequence", resourceSequence)
253        utils.log("INFO", "resourceSequence: " + resourceSequence, isDebugEnabled)  
254        execution.setVariable("wanResources", wanResources)
255        utils.log("INFO", " ======== END sequenceResource Process ======== ", isDebugEnabled)
256    }
257    
258    public void getCurrentResource(execution){
259        def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
260        utils.log("INFO", "======== Start getCurrentResoure Process ======== ", isDebugEnabled)    
261        def currentIndex = execution.getVariable("currentResourceIndex")
262        List<String> resourceSequence = execution.getVariable("resourceSequence")  
263        List<String> wanResources = execution.getVariable("wanResources")  
264        String resourceName =  resourceSequence.get(currentIndex)
265        execution.setVariable("resourceType",resourceName)
266        if(wanResources.contains(resourceName)){
267            execution.setVariable("controllerInfo", "SDN-C")
268        }else{
269            execution.setVariable("controllerInfo", "VF-C")
270        }
271        utils.log("INFO", "======== COMPLETED getCurrentResoure Process ======== ", isDebugEnabled)  
272    }
273    
274    /**
275     * prepare delete parameters
276     */
277    public void preResourceDelete(execution, resourceName){
278
279        def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
280
281        utils.log("INFO", " ======== STARTED preResourceDelete Process ======== ", isDebugEnabled)
282        
283        List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
284            
285        for(ServiceInstance rc_e : existResourceList){
286            
287             if(StringUtils.containsIgnoreCase(rc_e.getInstanceName(), resourceName)) {
288                 
289                                    String resourceInstanceUUID = rc_e.getInstanceId()
290                                    String resourceTemplateUUID = rc_e.getModelInfo().getModelUuid()
291                                    execution.setVariable("resourceInstanceId", resourceInstanceUUID)
292                                    execution.setVariable("resourceTemplateId", resourceTemplateUUID)                               
293                                    execution.setVariable("resourceType", resourceName)
294                                utils.log("INFO", "Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + "  resourceInstanceId: " 
295                                    + resourceInstanceUUID + " resourceType: " + resourceName, isDebugEnabled)
296            }
297        }       
298     
299        utils.log("INFO", " ======== END preResourceDelete Process ======== ", isDebugEnabled)
300    }
301    
302    public void parseNextResource(execution){
303        def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
304        utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)    
305        def currentIndex = execution.getVariable("currentResourceIndex")
306        def nextIndex =  currentIndex + 1
307        execution.setVariable("currentResourceIndex", nextIndex)
308        List<String> resourceSequence = execution.getVariable("resourceSequence")    
309        if(nextIndex >= resourceSequence.size()){
310            execution.setVariable("allResourceFinished", "true")
311        }else{
312            execution.setVariable("allResourceFinished", "false")
313        }
314        utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)            
315    }
316    
317      /**
318       * post config request.
319       */
320      public void postConfigRequest(execution){
321          //to do
322      } 
323    
324 }
325