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