Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / 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.onap.so.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.onap.so.bpmn.core.domain.ModelInfo
29 import org.onap.so.bpmn.core.domain.Resource
30 import org.onap.so.bpmn.core.domain.ServiceInstance
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
34 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
35 import org.onap.so.bpmn.core.WorkflowException
36 import org.onap.so.rest.APIResponse;
37 import org.onap.so.rest.RESTClient
38 import org.onap.so.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 import org.onap.so.logger.MessageEnum
60 import org.onap.so.logger.MsoLogger
61
62 /**
63  * This groovy class supports the <class>DoDeleteResources.bpmn</class> process.
64  * 
65  * Inputs:
66  * @param - msoRequestId
67  * @param - globalSubscriberId - O
68  * @param - subscriptionServiceType - O
69  * @param - serviceInstanceId
70  * @param - serviceInstanceName - O
71  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
72  * @param - sdncVersion 
73  * @param - failNotFound - TODO
74  * @param - serviceInputParams - TODO 
75  *
76  * @param - delResourceList
77  * @param - serviceRelationShip
78  *
79  * Outputs:
80  * @param - WorkflowException
81  * 
82  * Rollback - Deferred
83  */
84 public class DoDeleteResources extends AbstractServiceTaskProcessor {
85         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoDeleteResources.class);
86
87         String Prefix="DDELR_"
88         ExceptionUtil exceptionUtil = new ExceptionUtil()
89         JsonUtils jsonUtil = new JsonUtils()
90
91         public void preProcessRequest (DelegateExecution execution) {
92                 msoLogger.trace("preProcessRequest ")
93                 String msg = ""
94                 
95                 List<ServiceInstance> realNSRessources = new ArrayList<ServiceInstance>() 
96         
97         // related ns from AAI
98         String serviceRelationShip = execution.getVariable("serviceRelationShip")
99         def jsonSlurper = new JsonSlurper()
100         def jsonOutput = new JsonOutput()        
101         List<String> nsSequence = new ArrayList<String>() 
102         List relationShipList =  jsonSlurper.parseText(serviceRelationShip)
103         if (relationShipList != null) {
104            relationShipList.each {
105                String resourceType = it.resourceType
106                nsSequence.add(resourceType)
107            }
108         }       
109      
110         execution.setVariable("currentNSIndex", 0)
111         execution.setVariable("nsSequence", nsSequence)
112         execution.setVariable("realNSRessources", realNSRessources)
113         msoLogger.info("nsSequence: " + nsSequence) 
114
115                 msoLogger.trace("Exit preProcessRequest ")
116     }
117
118    public void getCurrentNS(execution){
119        msoLogger.trace("Start getCurrentNS Process ")    
120        
121        def currentIndex = execution.getVariable("currentNSIndex")
122        List<String> nsSequence = execution.getVariable("nsSequence") 
123        String nsResourceType =  nsSequence.get(currentIndex)
124        
125        // GET AAI by Name, not ID, for process convenient
126        execution.setVariable("GENGS_type", "service-instance")
127        execution.setVariable("GENGS_serviceInstanceId", "") 
128        execution.setVariable("GENGS_serviceInstanceName", nsResourceType)       
129
130        msoLogger.trace("COMPLETED getCurrentNS Process ")  
131    }    
132         
133         public void postProcessAAIGET(DelegateExecution execution) {
134                 msoLogger.trace("postProcessAAIGET2 ")
135                 String msg = ""
136
137                 try {
138                         String nsResourceName = execution.getVariable("GENGS_serviceInstanceName")
139                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
140                         if(!succInAAI){
141                                 msoLogger.info("Error getting Service-instance from AAI in postProcessAAIGET", + nsResourceName)
142                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
143                                 msoLogger.debug("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                                         msoLogger.info(msg)
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.getNodeText(aaiService, "service-instance-id")
161                                                 //String mn = utils.getNodeText(aaiService, "model-name")                                               
162                                                 String mIuuid = utils.getNodeText(aaiService, "model-invariant-id")
163                                                 String muuid = utils.getNodeText(aaiService, "model-version-id")
164                                                 String mCuuid = utils.getNodeText(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                                                 msoLogger.info("Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"))
180                                         }
181                                 }
182                         }
183                 } catch (BpmnError e) {
184                         throw e;
185                 } catch (Exception ex) {
186                         msg = "Exception in DoDeleteResources.postProcessAAIGET " + ex.getMessage()
187                         msoLogger.info(msg)
188                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
189                 }
190                 msoLogger.trace("Exit postProcessAAIGET ")
191         }
192         
193     public void parseNextNS(execution){
194        msoLogger.trace("Start parseNextNS Process ")    
195        def currentIndex = execution.getVariable("currentNSIndex")
196        def nextIndex =  currentIndex + 1
197        execution.setVariable("currentNSIndex", nextIndex)
198        List<String> nsSequence = execution.getVariable("nsSequence")    
199        if(nextIndex >= nsSequence.size()){
200            execution.setVariable("allNsFinished", "true")
201        }else{
202            execution.setVariable("allNsFinished", "false")
203        }
204        msoLogger.trace("COMPLETED parseNextNS Process ")            
205    }   
206
207    
208    public void sequenceResource(execution){
209        msoLogger.trace("STARTED sequenceResource Process ")
210        List<String> nsResources = new ArrayList<String>()
211        List<String> wanResources = new ArrayList<String>()
212        List<String> resourceSequence = new  ArrayList<String>()
213        
214        // get delete resource list and order list
215        List<Resource> delResourceList = execution.getVariable("delResourceList")
216        // existing resource list
217        List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
218        
219        for(ServiceInstance rc_e : existResourceList){
220        
221            String muuid = rc_e.getModelInfo().getModelUuid()
222            String mIuuid = rc_e.getModelInfo().getModelInvariantUuid()
223            String mCuuid = rc_e.getModelInfo().getModelCustomizationUuid()
224            rcType = rc_e.getInstanceName()
225            
226            for(Resource rc_d : delResourceList){                 
227            
228                 if(rc_d.getModelInfo().getModelUuid() == muuid 
229                 && rc_d.getModelInfo().getModelInvariantUuid() == mIuuid 
230                 && rc_d.getModelInfo().getModelCustomizationUuid() == mCuuid) {
231                 
232                    if(StringUtils.containsIgnoreCase(rcType, "overlay") 
233                    || StringUtils.containsIgnoreCase(rcType, "underlay")){                   
234                        wanResources.add(rcType)
235                    }else{
236                        nsResources.add(rcType)
237                    }
238                    
239                }
240            }
241            
242        }
243   
244        resourceSequence.addAll(wanResources)
245        resourceSequence.addAll(nsResources)
246        String isContainsWanResource = wanResources.isEmpty() ? "false" : "true"
247        execution.setVariable("isContainsWanResource", isContainsWanResource)
248        execution.setVariable("currentResourceIndex", 0)
249        execution.setVariable("resourceSequence", resourceSequence)
250        msoLogger.info("resourceSequence: " + resourceSequence)  
251        execution.setVariable("wanResources", wanResources)
252        msoLogger.trace("END sequenceResource Process ")
253    }
254    
255    public void getCurrentResource(execution){
256        msoLogger.trace("Start getCurrentResoure Process ")    
257        def currentIndex = execution.getVariable("currentResourceIndex")
258        List<String> resourceSequence = execution.getVariable("resourceSequence")  
259        List<String> wanResources = execution.getVariable("wanResources")  
260        String resourceName =  resourceSequence.get(currentIndex)
261        execution.setVariable("resourceType",resourceName)
262        if(wanResources.contains(resourceName)){
263            execution.setVariable("controllerInfo", "SDN-C")
264        }else{
265            execution.setVariable("controllerInfo", "VF-C")
266        }
267        msoLogger.trace("COMPLETED getCurrentResoure Process ")  
268    }
269    
270    /**
271     * prepare delete parameters
272     */
273    public void preResourceDelete(execution, resourceName){
274
275        msoLogger.trace("STARTED preResourceDelete Process ")
276        
277        List<ServiceInstance> existResourceList = execution.getVariable("realNSRessources")
278            
279        for(ServiceInstance rc_e : existResourceList){
280            
281             if(StringUtils.containsIgnoreCase(rc_e.getInstanceName(), resourceName)) {
282                 
283                                    String resourceInstanceUUID = rc_e.getInstanceId()
284                                    String resourceTemplateUUID = rc_e.getModelInfo().getModelUuid()
285                                    execution.setVariable("resourceInstanceId", resourceInstanceUUID)
286                                    execution.setVariable("resourceTemplateId", resourceTemplateUUID)                               
287                                    execution.setVariable("resourceType", resourceName)
288                                msoLogger.info("Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + "  resourceInstanceId: " + resourceInstanceUUID + " resourceType: " + resourceName)
289            }
290        }       
291     
292        msoLogger.trace("END preResourceDelete Process ")
293    }
294    
295    public void parseNextResource(execution){
296        msoLogger.trace("Start parseNextResource Process ")    
297        def currentIndex = execution.getVariable("currentResourceIndex")
298        def nextIndex =  currentIndex + 1
299        execution.setVariable("currentResourceIndex", nextIndex)
300        List<String> resourceSequence = execution.getVariable("resourceSequence")    
301        if(nextIndex >= resourceSequence.size()){
302            execution.setVariable("allResourceFinished", "true")
303        }else{
304            execution.setVariable("allResourceFinished", "false")
305        }
306        msoLogger.trace("COMPLETED parseNextResource Process ")            
307    }
308    
309      /**
310       * post config request.
311       */
312      public void postConfigRequest(execution){
313          //to do
314      } 
315    
316 }
317