Merge "Sniro BB now supports sole service proxies"
[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.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
39 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
40 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
41 import org.onap.so.client.aai.AAIObjectPlurals;
42 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
43 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
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     public void deleteVfModule(BuildingBlockExecution execution) throws Exception {
84         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
85         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
86
87         execution.setVariable("aaiVfModuleRollback", false);
88         try {
89             aaiVfModuleResources.deleteVfModule(vfModule, genericVnf);
90             execution.setVariable("aaiVfModuleRollback", true);
91         } catch (Exception ex) {
92             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
93         }
94     }
95
96     public void deleteVnf(BuildingBlockExecution execution) throws Exception {
97         GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
98
99         execution.setVariable("aaiVnfRollback", false);
100         try {
101             aaiVnfResources.deleteVnf(genericVnf);
102             execution.setVariable("aaiVnfRollback", true);
103         } catch (Exception ex) {
104             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
105         }
106     }
107
108     public void deleteServiceInstance(BuildingBlockExecution execution) throws Exception {
109         try {
110             ServiceInstance serviceInstance =
111                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
112             aaiSIResources.deleteServiceInstance(serviceInstance);
113         } catch (Exception ex) {
114             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
115         }
116
117     }
118
119     public void deleteNetwork(BuildingBlockExecution execution) throws Exception {
120         try {
121             L3Network l3network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
122             aaiNetworkResources.deleteNetwork(l3network);
123             execution.setVariable("isRollbackNeeded", true);
124         } catch (Exception ex) {
125             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
126         }
127     }
128
129     public void deleteCollection(BuildingBlockExecution execution) throws Exception {
130         try {
131             ServiceInstance serviceInstance =
132                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
133             aaiNetworkResources.deleteCollection(serviceInstance.getCollection());
134         } catch (Exception ex) {
135             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
136         }
137     }
138
139     public void deleteInstanceGroup(BuildingBlockExecution execution) throws Exception {
140         try {
141             ServiceInstance serviceInstance =
142                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
143             aaiNetworkResources.deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
144         } catch (Exception ex) {
145             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
146         }
147     }
148
149     public void deleteVolumeGroup(BuildingBlockExecution execution) {
150         try {
151             VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
152             CloudRegion cloudRegion = execution.getGeneralBuildingBlock().getCloudRegion();
153             aaiVolumeGroupResources.deleteVolumeGroup(volumeGroup, cloudRegion);
154         } catch (Exception ex) {
155             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
156         }
157     }
158
159     public void deleteConfiguration(BuildingBlockExecution execution) {
160         try {
161             Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
162             aaiConfigurationResources.deleteConfiguration(configuration);
163         } catch (Exception ex) {
164             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
165         }
166     }
167
168     public void deleteInstanceGroupVnf(BuildingBlockExecution execution) {
169         try {
170             InstanceGroup instanceGroup = extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
171             aaiInstanceGroupResources.deleteInstanceGroup(instanceGroup);
172         } catch (Exception ex) {
173             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
174         }
175     }
176
177     public void deleteNetworkPolicies(BuildingBlockExecution execution) {
178         try {
179             String fqdns = execution.getVariable(contrailNetworkPolicyFqdnList);
180             if (fqdns != null && !fqdns.isEmpty()) {
181                 String[] fqdnList = fqdns.split(",");
182                 int fqdnCount = fqdnList.length;
183                 if (fqdnCount > 0) {
184                     for (int i = 0; i < fqdnCount; i++) {
185                         String fqdn = fqdnList[i];
186                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY);
187                         uri.queryParam(networkPolicyFqdnParam, fqdn);
188                         Optional<NetworkPolicies> oNetPolicies = aaiNetworkResources.getNetworkPolicies(uri);
189                         if (oNetPolicies.isPresent()) {
190                             NetworkPolicies networkPolicies = oNetPolicies.get();
191                             List<NetworkPolicy> networkPolicyList = networkPolicies.getNetworkPolicy();
192                             if (networkPolicyList != null && !networkPolicyList.isEmpty()) {
193                                 NetworkPolicy networkPolicy = networkPolicyList.get(0);
194                                 String networkPolicyId = networkPolicy.getNetworkPolicyId();
195                                 logger.debug("Deleting network-policy with network-policy-id {}", networkPolicyId);
196                                 aaiNetworkResources.deleteNetworkPolicy(networkPolicyId);
197                             }
198                         }
199                     }
200                 }
201             }
202         } catch (Exception ex) {
203             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
204         }
205     }
206 }