d62fc6f50f38e806197625764f3cdd2b879906df
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / aai / tasks / AAIDeleteTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.aai.tasks;
24
25
26 import java.util.List;
27 import java.util.Optional;
28 import org.onap.aai.domain.yang.NetworkPolicies;
29 import org.onap.aai.domain.yang.NetworkPolicy;
30 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri;
31 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
33 import org.onap.so.bpmn.common.BuildingBlockExecution;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
42 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
43 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
44 import org.onap.so.client.exception.ExceptionBuilder;
45 import org.onap.so.client.orchestration.AAIConfigurationResources;
46 import org.onap.so.client.orchestration.AAIInstanceGroupResources;
47 import org.onap.so.client.orchestration.AAINetworkResources;
48 import org.onap.so.client.orchestration.AAIServiceInstanceResources;
49 import org.onap.so.client.orchestration.AAIVfModuleResources;
50 import org.onap.so.client.orchestration.AAIVnfResources;
51 import org.onap.so.client.orchestration.AAIVolumeGroupResources;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.stereotype.Component;
56
57 @Component
58 public class AAIDeleteTasks {
59     private static final Logger logger = LoggerFactory.getLogger(AAIDeleteTasks.class);
60
61     private static String contrailNetworkPolicyFqdnList = "contrailNetworkPolicyFqdnList";
62     private static String networkPolicyFqdnParam = "network-policy-fqdn";
63
64     @Autowired
65     private ExceptionBuilder exceptionUtil;
66     @Autowired
67     private ExtractPojosForBB extractPojosForBB;
68     @Autowired
69     private AAIServiceInstanceResources aaiSIResources;
70     @Autowired
71     private AAIVnfResources aaiVnfResources;
72     @Autowired
73     private AAIVfModuleResources aaiVfModuleResources;
74     @Autowired
75     private AAINetworkResources aaiNetworkResources;
76     @Autowired
77     private AAIVolumeGroupResources aaiVolumeGroupResources;
78     @Autowired
79     private AAIConfigurationResources aaiConfigurationResources;
80     @Autowired
81     private AAIInstanceGroupResources aaiInstanceGroupResources;
82
83     /**
84      * BPMN access method to delete the VfModule from A&AI.
85      *
86      * It will extract the genericVnf & VfModule from the BBObject.
87      *
88      * Before deleting it set the aaiVfModuleRollback as false & then it will delete the VfModule.
89      *
90      * @param execution
91      * @throws Exception
92      */
93     public void deleteVfModule(BuildingBlockExecution execution) throws Exception {
94         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
95         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
96
97         execution.setVariable("aaiVfModuleRollback", false);
98         try {
99             aaiVfModuleResources.deleteVfModule(vfModule, genericVnf);
100             execution.setVariable("aaiVfModuleRollback", true);
101         } catch (Exception ex) {
102             logger.error("Exception occurred in AAIDeleteTasks deleteVfModule process", ex);
103             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
104         }
105     }
106
107     /**
108      * BPMN access method to delete the Vnf from A&AI.
109      *
110      * It will extract the genericVnf from the BBObject.
111      *
112      * Before deleting it set the aaiVnfRollback as false & then it will delete the Vnf.
113      *
114      * @param execution
115      * @throws Exception
116      */
117     public void deleteVnf(BuildingBlockExecution execution) throws Exception {
118         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
119
120         execution.setVariable("aaiVnfRollback", false);
121         try {
122             aaiVnfResources.deleteVnf(genericVnf);
123             execution.setVariable("aaiVnfRollback", true);
124         } catch (Exception ex) {
125             logger.error("Exception occurred in AAIDeleteTasks deleteVnf process", ex);
126             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
127         }
128     }
129
130     /**
131      * BPMN access method to delete the ServiceInstance from A&AI.
132      *
133      * It will extract the serviceInstance from the BBObject.
134      *
135      * @param execution
136      * @throws Exception
137      */
138     public void deleteServiceInstance(BuildingBlockExecution execution) throws Exception {
139         try {
140             ServiceInstance serviceInstance =
141                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
142             aaiSIResources.deleteServiceInstance(serviceInstance);
143         } catch (Exception ex) {
144             logger.error("Exception occurred in AAIDeleteTasks deleteServiceInstance process", ex);
145             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
146         }
147
148     }
149
150     /**
151      * BPMN access method to delete the l3network from A&AI.
152      *
153      * It will extract the l3network from the BBObject.
154      *
155      * After deleting the l3network it set the isRollbackNeeded as true.
156      *
157      * @param execution
158      * @throws Exception
159      */
160     public void deleteNetwork(BuildingBlockExecution execution) throws Exception {
161         try {
162             L3Network l3network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
163             aaiNetworkResources.deleteNetwork(l3network);
164             execution.setVariable("isRollbackNeeded", true);
165         } catch (Exception ex) {
166             logger.error("Exception occurred in AAIDeleteTasks deleteNetwork process", ex);
167             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
168         }
169     }
170
171     /**
172      * BPMN access method to delete the Collection from A&AI.
173      *
174      * It will extract the serviceInstance from the BBObject.
175      *
176      * Then it will get the collection from serviceinstance.
177      *
178      * @param execution
179      * @throws Exception
180      */
181     public void deleteCollection(BuildingBlockExecution execution) throws Exception {
182         try {
183             ServiceInstance serviceInstance =
184                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
185             aaiNetworkResources.deleteCollection(serviceInstance.getCollection());
186         } catch (Exception ex) {
187             logger.error("Exception occurred in AAIDeleteTasks deleteCollection process", ex);
188             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
189         }
190     }
191
192     /**
193      * BPMN access method to delete the InstanceGroup from A&AI.
194      *
195      * It will extract the serviceInstance from the BBObject.
196      *
197      * Then it will get the Instance group from serviceInstance.
198      *
199      * @param execution
200      * @throws Exception
201      */
202     public void deleteInstanceGroup(BuildingBlockExecution execution) throws Exception {
203         try {
204             ServiceInstance serviceInstance =
205                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
206             aaiNetworkResources.deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
207         } catch (Exception ex) {
208             logger.error("Exception occurred in AAIDeleteTasks deleteInstanceGroup process", ex);
209             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
210         }
211     }
212
213     /**
214      * BPMN access method to delete the VolumeGroup from A&AI.
215      *
216      * It will extract the volumeGroup from the BBObject and cloudRegion from execution object .
217      *
218      * Then it will delete from A&AI.
219      *
220      * @param execution
221      * @throws Exception
222      */
223     public void deleteVolumeGroup(BuildingBlockExecution execution) {
224         try {
225             VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
226             CloudRegion cloudRegion = execution.getGeneralBuildingBlock().getCloudRegion();
227             aaiVolumeGroupResources.deleteVolumeGroup(volumeGroup, cloudRegion);
228         } catch (Exception ex) {
229             logger.error("Exception occurred in AAIDeleteTasks deleteVolumeGroup process", ex);
230             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
231         }
232     }
233
234     /**
235      * BPMN access method to delete the Configuration from A&AI.
236      *
237      * It will extract the configuration from the BBObject.
238      *
239      * Then it will delete from A&AI.
240      *
241      * @param execution
242      */
243     public void deleteConfiguration(BuildingBlockExecution execution) {
244         try {
245             Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
246             aaiConfigurationResources.deleteConfiguration(configuration);
247         } catch (Exception ex) {
248             logger.error("Exception occurred in AAIDeleteTasks deleteConfiguration process", ex);
249             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
250         }
251     }
252
253     /**
254      * BPMN access method to delete the InstanceGroupVnf from A&AI.
255      *
256      * It will extract the instanceGroup from the BBObject.
257      *
258      * Then it will delete from A&AI.
259      *
260      * @param execution
261      */
262     public void deleteInstanceGroupVnf(BuildingBlockExecution execution) {
263         try {
264             InstanceGroup instanceGroup = extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
265             aaiInstanceGroupResources.deleteInstanceGroup(instanceGroup);
266         } catch (Exception ex) {
267             logger.error("Exception occurred in AAIDeleteTasks deleteInstanceGroupVnf process", ex);
268             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
269         }
270     }
271
272
273     public void deleteNetworkPolicies(BuildingBlockExecution execution) {
274         try {
275             String fqdns = execution.getVariable(contrailNetworkPolicyFqdnList);
276             if (fqdns != null && !fqdns.isEmpty()) {
277                 String[] fqdnList = fqdns.split(",");
278                 int fqdnCount = fqdnList.length;
279                 if (fqdnCount > 0) {
280                     for (int i = 0; i < fqdnCount; i++) {
281                         String fqdn = fqdnList[i];
282                         AAIPluralResourceUri uri =
283                                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicies());
284                         uri.queryParam(networkPolicyFqdnParam, fqdn);
285                         Optional<NetworkPolicies> oNetPolicies = aaiNetworkResources.getNetworkPolicies(uri);
286                         if (oNetPolicies.isPresent()) {
287                             NetworkPolicies networkPolicies = oNetPolicies.get();
288                             List<NetworkPolicy> networkPolicyList = networkPolicies.getNetworkPolicy();
289                             if (networkPolicyList != null && !networkPolicyList.isEmpty()) {
290                                 NetworkPolicy networkPolicy = networkPolicyList.get(0);
291                                 String networkPolicyId = networkPolicy.getNetworkPolicyId();
292                                 logger.debug("Deleting network-policy with network-policy-id {}", networkPolicyId);
293                                 aaiNetworkResources.deleteNetworkPolicy(networkPolicyId);
294                             }
295                         }
296                     }
297                 }
298             }
299         } catch (Exception ex) {
300             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
301         }
302     }
303 }