Add or Delete a PNF to an Active Service
[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.Pnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
43 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
44 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
45 import org.onap.so.client.exception.ExceptionBuilder;
46 import org.onap.so.client.orchestration.AAIConfigurationResources;
47 import org.onap.so.client.orchestration.AAIInstanceGroupResources;
48 import org.onap.so.client.orchestration.AAINetworkResources;
49 import org.onap.so.client.orchestration.AAIServiceInstanceResources;
50 import org.onap.so.client.orchestration.AAIVfModuleResources;
51 import org.onap.so.client.orchestration.AAIVnfResources;
52 import org.onap.so.client.orchestration.AAIPnfResources;
53 import org.onap.so.client.orchestration.AAIVolumeGroupResources;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Component;
58
59 @Component
60 public class AAIDeleteTasks {
61     private static final Logger logger = LoggerFactory.getLogger(AAIDeleteTasks.class);
62
63     private static String contrailNetworkPolicyFqdnList = "contrailNetworkPolicyFqdnList";
64     private static String networkPolicyFqdnParam = "network-policy-fqdn";
65
66     @Autowired
67     private ExceptionBuilder exceptionUtil;
68     @Autowired
69     private ExtractPojosForBB extractPojosForBB;
70     @Autowired
71     private AAIServiceInstanceResources aaiSIResources;
72     @Autowired
73     private AAIVnfResources aaiVnfResources;
74     @Autowired
75     private AAIPnfResources aaiPnfResources;
76     @Autowired
77     private AAIVfModuleResources aaiVfModuleResources;
78     @Autowired
79     private AAINetworkResources aaiNetworkResources;
80     @Autowired
81     private AAIVolumeGroupResources aaiVolumeGroupResources;
82     @Autowired
83     private AAIConfigurationResources aaiConfigurationResources;
84     @Autowired
85     private AAIInstanceGroupResources aaiInstanceGroupResources;
86
87     /**
88      * BPMN access method to delete the VfModule from A&AI.
89      *
90      * It will extract the genericVnf & VfModule from the BBObject.
91      *
92      * Before deleting it set the aaiVfModuleRollback as false & then it will delete the VfModule.
93      *
94      * @param execution
95      * @throws Exception
96      */
97     public void deleteVfModule(BuildingBlockExecution execution) throws Exception {
98         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
99         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
100
101         execution.setVariable("aaiVfModuleRollback", false);
102         try {
103             aaiVfModuleResources.deleteVfModule(vfModule, genericVnf);
104             execution.setVariable("aaiVfModuleRollback", true);
105         } catch (Exception ex) {
106             logger.error("Exception occurred in AAIDeleteTasks deleteVfModule process", ex);
107             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
108         }
109     }
110
111     /**
112      * BPMN access method to delete the Vnf from A&AI.
113      *
114      * It will extract the genericVnf from the BBObject.
115      *
116      * Before deleting it set the aaiVnfRollback as false & then it will delete the Vnf.
117      *
118      * @param execution
119      * @throws Exception
120      */
121     public void deleteVnf(BuildingBlockExecution execution) throws Exception {
122         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
123
124         execution.setVariable("aaiVnfRollback", false);
125         try {
126             aaiVnfResources.deleteVnf(genericVnf);
127             execution.setVariable("aaiVnfRollback", true);
128         } catch (Exception ex) {
129             logger.error("Exception occurred in AAIDeleteTasks deleteVnf process", ex);
130             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
131         }
132     }
133
134
135
136     public void deletePnf(BuildingBlockExecution execution) throws Exception {
137         Pnf pnf = extractPojosForBB.extractByKey(execution, ResourceKey.PNF);
138         execution.setVariable("aaiPnfRollback", false);
139         try {
140             aaiPnfResources.deletePnf(pnf);
141             execution.setVariable("aaiPnfRollback", true);
142         } catch (Exception ex) {
143             logger.error("Exception occurred in AAIDeleteTasks deletePnf process", ex);
144             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
145         }
146     }
147
148     /**
149      * BPMN access method to delete the ServiceInstance from A&AI.
150      *
151      * It will extract the serviceInstance from the BBObject.
152      *
153      * @param execution
154      * @throws Exception
155      */
156     public void deleteServiceInstance(BuildingBlockExecution execution) throws Exception {
157         try {
158             ServiceInstance serviceInstance =
159                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
160             aaiSIResources.deleteServiceInstance(serviceInstance);
161         } catch (Exception ex) {
162             logger.error("Exception occurred in AAIDeleteTasks deleteServiceInstance process", ex);
163             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
164         }
165
166     }
167
168     /**
169      * BPMN access method to delete the l3network from A&AI.
170      *
171      * It will extract the l3network from the BBObject.
172      *
173      * After deleting the l3network it set the isRollbackNeeded as true.
174      *
175      * @param execution
176      * @throws Exception
177      */
178     public void deleteNetwork(BuildingBlockExecution execution) throws Exception {
179         try {
180             L3Network l3network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
181             aaiNetworkResources.deleteNetwork(l3network);
182             execution.setVariable("isRollbackNeeded", true);
183         } catch (Exception ex) {
184             logger.error("Exception occurred in AAIDeleteTasks deleteNetwork process", ex);
185             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
186         }
187     }
188
189     /**
190      * BPMN access method to delete the Collection from A&AI.
191      *
192      * It will extract the serviceInstance from the BBObject.
193      *
194      * Then it will get the collection from serviceinstance.
195      *
196      * @param execution
197      * @throws Exception
198      */
199     public void deleteCollection(BuildingBlockExecution execution) throws Exception {
200         try {
201             ServiceInstance serviceInstance =
202                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
203             aaiNetworkResources.deleteCollection(serviceInstance.getCollection());
204         } catch (Exception ex) {
205             logger.error("Exception occurred in AAIDeleteTasks deleteCollection process", ex);
206             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
207         }
208     }
209
210     /**
211      * BPMN access method to delete the InstanceGroup from A&AI.
212      *
213      * It will extract the serviceInstance from the BBObject.
214      *
215      * Then it will get the Instance group from serviceInstance.
216      *
217      * @param execution
218      * @throws Exception
219      */
220     public void deleteInstanceGroup(BuildingBlockExecution execution) throws Exception {
221         try {
222             ServiceInstance serviceInstance =
223                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
224             aaiNetworkResources.deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
225         } catch (Exception ex) {
226             logger.error("Exception occurred in AAIDeleteTasks deleteInstanceGroup process", ex);
227             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
228         }
229     }
230
231     /**
232      * BPMN access method to delete the VolumeGroup from A&AI.
233      *
234      * It will extract the volumeGroup from the BBObject and cloudRegion from execution object .
235      *
236      * Then it will delete from A&AI.
237      *
238      * @param execution
239      * @throws Exception
240      */
241     public void deleteVolumeGroup(BuildingBlockExecution execution) {
242         try {
243             VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
244             CloudRegion cloudRegion = execution.getGeneralBuildingBlock().getCloudRegion();
245             aaiVolumeGroupResources.deleteVolumeGroup(volumeGroup, cloudRegion);
246         } catch (Exception ex) {
247             logger.error("Exception occurred in AAIDeleteTasks deleteVolumeGroup process", ex);
248             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
249         }
250     }
251
252     /**
253      * BPMN access method to delete the Configuration from A&AI.
254      *
255      * It will extract the configuration from the BBObject.
256      *
257      * Then it will delete from A&AI.
258      *
259      * @param execution
260      */
261     public void deleteConfiguration(BuildingBlockExecution execution) {
262         try {
263             Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
264             aaiConfigurationResources.deleteConfiguration(configuration);
265         } catch (Exception ex) {
266             logger.error("Exception occurred in AAIDeleteTasks deleteConfiguration process", ex);
267             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
268         }
269     }
270
271     /**
272      * BPMN access method to delete the InstanceGroupVnf from A&AI.
273      *
274      * It will extract the instanceGroup from the BBObject.
275      *
276      * Then it will delete from A&AI.
277      *
278      * @param execution
279      */
280     public void deleteInstanceGroupVnf(BuildingBlockExecution execution) {
281         try {
282             InstanceGroup instanceGroup = extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
283             aaiInstanceGroupResources.deleteInstanceGroup(instanceGroup);
284         } catch (Exception ex) {
285             logger.error("Exception occurred in AAIDeleteTasks deleteInstanceGroupVnf process", ex);
286             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
287         }
288     }
289
290
291     public void deleteNetworkPolicies(BuildingBlockExecution execution) {
292         try {
293             String fqdns = execution.getVariable(contrailNetworkPolicyFqdnList);
294             if (fqdns != null && !fqdns.isEmpty()) {
295                 String[] fqdnList = fqdns.split(",");
296                 int fqdnCount = fqdnList.length;
297                 if (fqdnCount > 0) {
298                     for (int i = 0; i < fqdnCount; i++) {
299                         String fqdn = fqdnList[i];
300                         AAIPluralResourceUri uri =
301                                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicies());
302                         uri.queryParam(networkPolicyFqdnParam, fqdn);
303                         Optional<NetworkPolicies> oNetPolicies = aaiNetworkResources.getNetworkPolicies(uri);
304                         if (oNetPolicies.isPresent()) {
305                             NetworkPolicies networkPolicies = oNetPolicies.get();
306                             List<NetworkPolicy> networkPolicyList = networkPolicies.getNetworkPolicy();
307                             if (networkPolicyList != null && !networkPolicyList.isEmpty()) {
308                                 NetworkPolicy networkPolicy = networkPolicyList.get(0);
309                                 String networkPolicyId = networkPolicy.getNetworkPolicyId();
310                                 logger.debug("Deleting network-policy with network-policy-id {}", networkPolicyId);
311                                 aaiNetworkResources.deleteNetworkPolicy(networkPolicyId);
312                             }
313                         }
314                     }
315                 }
316             }
317         } catch (Exception ex) {
318             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
319         }
320     }
321 }