removed extra argument from extractByKey method
[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
29 import org.onap.aai.domain.yang.NetworkPolicies;
30 import org.onap.aai.domain.yang.NetworkPolicy;
31 import org.onap.so.bpmn.common.BuildingBlockExecution;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
40 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
41 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
42 import org.onap.so.client.aai.AAIObjectPlurals;
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
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.AAIVolumeGroupResources;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Component;
57
58 @Component
59 public class AAIDeleteTasks {
60         private static final Logger logger = LoggerFactory.getLogger(AAIDeleteTasks.class);
61         
62         private static String CONTRAIL_NETWORK_POLICY_FQDN_LIST = "contrailNetworkPolicyFqdnList";
63         private static String NETWORK_POLICY_FQDN_PARAM = "network-policy-fqdn";
64         
65         @Autowired
66         private ExceptionBuilder exceptionUtil;
67         @Autowired
68         private ExtractPojosForBB extractPojosForBB;
69         @Autowired
70         private AAIServiceInstanceResources aaiSIResources;
71         @Autowired
72         private AAIVnfResources aaiVnfResources;
73         @Autowired
74         private AAIVfModuleResources aaiVfModuleResources;
75         @Autowired
76         private AAINetworkResources aaiNetworkResources;
77         @Autowired
78         private AAIVolumeGroupResources aaiVolumeGroupResources;
79         @Autowired
80         private AAIConfigurationResources aaiConfigurationResources;
81         @Autowired
82         private AAIInstanceGroupResources aaiInstanceGroupResources;
83         
84         public void deleteVfModule(BuildingBlockExecution execution) throws Exception {         
85                 GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
86                 VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
87                 
88                 execution.setVariable("aaiVfModuleRollback", false);
89                 try {
90                         aaiVfModuleResources.deleteVfModule(vfModule, genericVnf);
91                         execution.setVariable("aaiVfModuleRollback", true);
92                 } catch (Exception ex) {                        
93                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
94                 }       
95         }
96
97         public void deleteVnf(BuildingBlockExecution execution) throws Exception {              
98                 GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
99                 
100                 execution.setVariable("aaiVnfRollback", false);
101                 try {
102                         aaiVnfResources.deleteVnf(genericVnf);
103                         execution.setVariable("aaiVnfRollback", true);
104                 } catch (Exception ex) {                        
105                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
106                 }       
107         }
108         
109         public void deleteServiceInstance(BuildingBlockExecution execution) throws Exception {
110                 try {
111                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID); 
112                         aaiSIResources.deleteServiceInstance(serviceInstance);
113                 }
114                 catch(Exception ex) {
115                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
116                 }
117                 
118         }
119         
120         public void deleteNetwork(BuildingBlockExecution execution) throws Exception {
121                 try {
122                         L3Network l3network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID); 
123                         aaiNetworkResources.deleteNetwork(l3network);
124                         execution.setVariable("isRollbackNeeded", true);
125                 }
126                 catch(Exception ex) {
127                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
128                 }
129         }
130         
131         public void deleteCollection(BuildingBlockExecution execution) throws Exception {
132                 try {
133                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID); 
134                         aaiNetworkResources.deleteCollection(serviceInstance.getCollection());
135                 }
136                 catch(Exception ex) {
137                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
138                 }
139         }
140         
141         public void deleteInstanceGroup(BuildingBlockExecution execution) throws Exception {
142                 try {
143                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID); 
144                         aaiNetworkResources.deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
145                 }
146                 catch(Exception ex) {
147                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
148                 }
149         }
150         
151         public void deleteVolumeGroup(BuildingBlockExecution execution) {
152                 try {
153                         VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
154                         CloudRegion cloudRegion = execution.getGeneralBuildingBlock().getCloudRegion();
155                         aaiVolumeGroupResources.deleteVolumeGroup(volumeGroup, cloudRegion);
156                 } catch (Exception ex) {
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
158                 }
159         }
160         public void deleteConfiguration(BuildingBlockExecution execution) {
161                 try {
162                         Configuration configuration = extractPojosForBB.extractByKey(execution, ResourceKey.CONFIGURATION_ID);
163                         aaiConfigurationResources.deleteConfiguration(configuration);
164                 } catch (Exception ex) {
165                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
166                 }
167         }
168         
169         public void deleteInstanceGroupVnf(BuildingBlockExecution execution) {
170                 try {
171                         InstanceGroup instanceGroup = extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
172                         aaiInstanceGroupResources.deleteInstanceGroup(instanceGroup);
173                 } catch (Exception ex) {
174                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
175                 }
176         }
177         
178         public void deleteNetworkPolicies(BuildingBlockExecution execution) {
179                 try{                    
180                         String fqdns = execution.getVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST);
181                         if (fqdns != null && !fqdns.isEmpty()) {
182                                 String fqdnList[] = fqdns.split(",");
183                                 int fqdnCount = fqdnList.length;
184                                 if (fqdnCount > 0) {
185                                         for (int i=0; i < fqdnCount; i++) {
186                                                 String fqdn = fqdnList[i];
187                                                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY);
188                                                 uri.queryParam(NETWORK_POLICY_FQDN_PARAM, fqdn);
189                                                 Optional<NetworkPolicies> oNetPolicies = aaiNetworkResources.getNetworkPolicies(uri);
190                                                 if(oNetPolicies.isPresent()) {
191                                                         NetworkPolicies networkPolicies = oNetPolicies.get();
192                                                         List<NetworkPolicy> networkPolicyList = networkPolicies.getNetworkPolicy();
193                                                         if (networkPolicyList != null && !networkPolicyList.isEmpty()) {
194                                                                 NetworkPolicy networkPolicy = networkPolicyList.get(0);
195                                                                 String networkPolicyId = networkPolicy.getNetworkPolicyId();
196                                                                 logger.debug("Deleting network-policy with network-policy-id {}", networkPolicyId);
197                                                                 aaiNetworkResources.deleteNetworkPolicy(networkPolicyId);                                                               
198                                                         }                                                       
199                                                 }
200                                         }
201                                 }
202                         }                       
203                 } catch (Exception ex) {
204                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
205                 }       
206         }
207 }