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