Merge "image correction in the Workspace section"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / UnassignNetworkBB.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.flowspecific.tasks;
22
23 import java.util.Optional;
24
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
27 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
28 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
29 import org.onap.so.client.aai.entities.AAIResultWrapper;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.client.orchestration.AAINetworkResources;
32 import org.onap.so.logger.MsoLogger;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class UnassignNetworkBB {
38
39         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, UnassignNetworkBB.class);
40         
41         private static String MESSAGE_CANNOT_PERFORM_UNASSIGN = "Cannot perform Unassign Network. Network is still related to ";        
42         private static String MESSAGE_ERROR_ROLLBACK = " Rollback is not possible. Please restore data manually.";      
43         
44         @Autowired
45         private ExceptionBuilder exceptionUtil;
46
47         @Autowired
48         private NetworkBBUtils networkBBUtils;
49         
50         @Autowired
51         private ExtractPojosForBB extractPojosForBB;
52         
53         @Autowired
54         private AAINetworkResources aaiNetworkResources;
55
56         /**
57          * BPMN access method to prepare overall error messages.
58          * 
59          * @param execution - BuildingBlockExecution
60          * @param relatedToValue - String, ex: vf-module
61          * @return void - nothing
62          * @throws Exception
63          */
64         
65         public void checkRelationshipRelatedTo(BuildingBlockExecution execution, String relatedToValue) throws Exception {
66                 try {
67                         L3Network l3network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID,
68                                 execution.getLookupMap().get(ResourceKey.NETWORK_ID));
69                         AAIResultWrapper aaiResultWrapper = aaiNetworkResources.queryNetworkWrapperById(l3network);
70                         Optional<org.onap.aai.domain.yang.L3Network> network = aaiResultWrapper.asBean(org.onap.aai.domain.yang.L3Network.class);
71                         if (networkBBUtils.isRelationshipRelatedToExists(network, relatedToValue)) {
72                                 String msg = MESSAGE_CANNOT_PERFORM_UNASSIGN + relatedToValue;
73                                 execution.setVariable("ErrorUnassignNetworkBB", msg);
74                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
75                         }
76                 } catch (Exception ex) {
77                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
78                 }
79         }       
80         
81         /**
82          * BPMN access method to getCloudRegionId
83          * 
84          * @param execution - BuildingBlockExecution
85          * @return void - nothing
86          * @throws Exception
87          */
88         
89         public void getCloudSdncRegion(BuildingBlockExecution execution) throws Exception {
90                 try {
91                         String cloudRegionSdnc = networkBBUtils.getCloudRegion(execution, SourceSystem.SDNC);
92                         execution.setVariable("cloudRegionSdnc", cloudRegionSdnc);
93                 } catch (Exception ex) {
94                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
95                 }
96         }               
97         
98         /**
99          * BPMN access method to prepare overall error messages.
100          * 
101          * @param execution - BuildingBlockExecution
102          * @return void - nothing
103          */
104         public void errorEncountered(BuildingBlockExecution execution) {
105                 String msg;
106                 boolean isRollbackNeeded = execution.getVariable("isRollbackNeeded") != null ? execution.getVariable("isRollbackNeeded") : false;
107                 if (isRollbackNeeded == true) {
108                         msg = execution.getVariable("ErrorUnassignNetworkBB") + MESSAGE_ERROR_ROLLBACK;
109                 } else {
110                         msg = execution.getVariable("ErrorUnassignNetworkBB");
111                 }
112                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
113         }       
114         
115         
116 }