Containerization feature of SO
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCUnassignTasks.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.sdnc.tasks;
22
23 import org.onap.so.bpmn.common.BuildingBlockExecution;
24 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
29 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
30 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
31 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
32 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
33 import org.onap.so.client.exception.ExceptionBuilder;
34 import org.onap.so.client.orchestration.SDNCServiceInstanceResources;
35 import org.onap.so.client.orchestration.SDNCVfModuleResources;
36 import org.onap.so.client.orchestration.SDNCVnfResources;
37 import org.onap.so.db.catalog.beans.OrchestrationStatus;
38 import org.onap.so.logger.MsoLogger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
43 import org.onap.so.client.orchestration.SDNCNetworkResources;
44
45 @Component
46 public class SDNCUnassignTasks {
47         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCUnassignTasks.class);
48         @Autowired
49         private SDNCServiceInstanceResources sdncSIResources;
50         @Autowired
51         private SDNCVfModuleResources sdncVfModuleResources;
52         @Autowired
53         private SDNCVnfResources sdncVnfResources;
54         @Autowired
55         private ExceptionBuilder exceptionUtil;
56         @Autowired
57         private ExtractPojosForBB extractPojosForBB;
58         @Autowired
59         private SDNCNetworkResources sdncNetworkResources;
60
61         public void unassignServiceInstance(BuildingBlockExecution execution) {
62                 try {
63                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
64                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID)); 
65                         
66                         if (serviceInstance.getOrchestrationStatus() == OrchestrationStatus.INVENTORIED) {
67                                 return;  // If INVENTORIED then SDNC unassign is not necessary
68                         }
69                         
70                         RequestContext requestContext = gBBInput.getRequestContext();
71                         Customer customer = gBBInput.getCustomer();
72                         String sdncUnassignResponse = sdncSIResources.unassignServiceInstance(serviceInstance, customer, requestContext);
73                         execution.setVariable("sdncUnassignResponse", sdncUnassignResponse);
74                 } catch (Exception ex) {
75                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
76                 }
77         }
78         
79         public void unassignVfModule(BuildingBlockExecution execution) {                
80                 try {
81                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID)); 
82                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID)); 
83                         VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
84                         
85                         if (OrchestrationStatus.INVENTORIED == vfModule.getOrchestrationStatus() || OrchestrationStatus.PENDING_CREATE == vfModule.getOrchestrationStatus()) {
86                                 return;  // If INVENTORIED or PENDING_CREATE then SDNC unassign is not necessary
87                         }
88                         
89                         execution.setVariable("sdncVfModuleRollback", false);
90                 
91                         String response = sdncVfModuleResources.unassignVfModule(vfModule, vnf, serviceInstance);               
92                         execution.setVariable("SDNCResponse", response);
93                         execution.setVariable("sdncVfModuleRollback", true);
94                 } catch (Exception ex) {                        
95                     exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
96                 }
97         }
98         
99         public void unassignVnf(BuildingBlockExecution execution)  {
100                 try {
101                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
102                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
103                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
104                         
105                         if (OrchestrationStatus.INVENTORIED == vnf.getOrchestrationStatus() || OrchestrationStatus.CREATED == vnf.getOrchestrationStatus()) {
106                                 return; // If INVENTORIED or CREATED then SDNC unassign is not necessary
107                         }
108                         
109                         RequestContext requestContext = gBBInput.getRequestContext();
110                         Customer customer = gBBInput.getCustomer();
111                         CloudRegion cloudRegion = gBBInput.getCloudRegion();
112                         execution.setVariable("sdncVnfRollback", false);
113                         String response = sdncVnfResources.unassignVnf(vnf, serviceInstance, customer, cloudRegion, requestContext);
114                         execution.setVariable("sdncUnassignVnfResponse", response);
115                         execution.setVariable("sdncVnfRollback", true);
116                 } catch (Exception ex) {
117                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
118                 }
119         }
120
121         public void unassignNetwork(BuildingBlockExecution execution) throws Exception {
122                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
123                 L3Network network =  extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID, execution.getLookupMap().get(ResourceKey.NETWORK_ID));
124                 
125                 if (OrchestrationStatus.INVENTORIED == network.getOrchestrationStatus()) {
126                         return; // If INVENTORIED then SDNC unassign is not necessary
127                 }
128                 
129                 ServiceInstance serviceInstance =  extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
130                 Customer customer = gBBInput.getCustomer();
131                 RequestContext requestContext = gBBInput.getRequestContext();           
132                 CloudRegion cloudRegion = gBBInput.getCloudRegion();
133                 String cloudRegionSdnc = execution.getVariable("cloudRegionSdnc");
134                 cloudRegion.setLcpCloudRegionId(cloudRegionSdnc);
135                 try {
136                         String response = sdncNetworkResources.unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
137                         execution.setVariable("SDNCUnAssignNetworkResponse", response);
138                         execution.setVariable("isRollbackNeeded", true);
139                 } catch (Exception ex) {
140                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
141                 }
142         }
143 }