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