[SO] Macro flow for PNF-Modify operation
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / aai / tasks / AAIFlagTasks.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 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
28 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
29 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.client.orchestration.AAIPnfResources;
32 import org.onap.so.client.orchestration.AAIVnfResources;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class AAIFlagTasks {
40     private static final Logger logger = LoggerFactory.getLogger(AAIFlagTasks.class);
41
42
43     @Autowired
44     private AAIVnfResources aaiVnfResources;
45
46     @Autowired
47     private AAIPnfResources aaiPnfResources;
48     @Autowired
49     private ExceptionBuilder exceptionUtil;
50     @Autowired
51     private ExtractPojosForBB extractPojosForBB;
52
53     public void checkVnfInMaintFlag(BuildingBlockExecution execution) {
54         boolean inMaint = false;
55         try {
56             GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
57             String vnfId = vnf.getVnfId();
58             inMaint = aaiVnfResources.checkInMaintFlag(vnfId);
59         } catch (Exception ex) {
60             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
61         }
62         if (inMaint) {
63             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "VNF is in maintenance in A&AI");
64         }
65     }
66
67     public void modifyVnfInMaintFlag(BuildingBlockExecution execution, boolean inMaint) {
68         try {
69             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
70
71             GenericVnf copiedGenericVnf = genericVnf.shallowCopyId();
72
73             copiedGenericVnf.setInMaint(inMaint);
74             genericVnf.setInMaint(inMaint);
75
76             aaiVnfResources.updateObjectVnf(copiedGenericVnf);
77         } catch (Exception ex) {
78             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
79         }
80     }
81
82     public void checkPnfInMaintFlag(BuildingBlockExecution execution) {
83         boolean inMaint = false;
84         try {
85             Pnf pnf = extractPojosForBB.extractByKey(execution, ResourceKey.PNF);
86             String pnfName = pnf.getPnfName();
87             inMaint = aaiPnfResources.checkInMaintFlag(pnfName);
88         } catch (Exception ex) {
89             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
90         }
91         if (inMaint) {
92             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "PNF is already in maintenance in A&AI");
93         }
94     }
95
96     public void modifyPnfInMaintFlag(BuildingBlockExecution execution, boolean inMaint) {
97         try {
98             Pnf pnf = extractPojosForBB.extractByKey(execution, ResourceKey.PNF);
99             logger.info("In modifyPnfInMaintFlag pnfname: {}", pnf.getPnfName());
100             Pnf copiedPnf = pnf.shallowCopyId();
101             copiedPnf.setPnfName(pnf.getPnfName());
102
103             copiedPnf.setInMaint(inMaint);
104             pnf.setInMaint(inMaint);
105             logger.info("In modifyPnfInMaintFlag if block pnfInMaint: {}, copiedPnfInMaint: {}", pnf.isInMaint(),
106                     copiedPnf.isInMaint());
107             aaiPnfResources.updateObjectPnf(copiedPnf);
108
109         } catch (Exception ex) {
110             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
111         }
112     }
113
114
115     public void checkVnfClosedLoopDisabledFlag(BuildingBlockExecution execution) {
116         boolean isClosedLoopDisabled = false;
117         try {
118             GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
119             String vnfId = vnf.getVnfId();
120             isClosedLoopDisabled = aaiVnfResources.checkVnfClosedLoopDisabledFlag(vnfId);
121         } catch (Exception ex) {
122             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
123         }
124         if (isClosedLoopDisabled) {
125             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "VNF Close Loop Disabled in A&AI");
126         }
127     }
128
129     public void modifyVnfClosedLoopDisabledFlag(BuildingBlockExecution execution, boolean closedLoopDisabled) {
130         try {
131             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
132
133             GenericVnf copiedGenericVnf = genericVnf.shallowCopyId();
134             copiedGenericVnf.setClosedLoopDisabled(closedLoopDisabled);
135             genericVnf.setClosedLoopDisabled(closedLoopDisabled);
136
137             aaiVnfResources.updateObjectVnf(copiedGenericVnf);
138         } catch (Exception ex) {
139             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
140         }
141     }
142
143     public void checkVnfPserversLockedFlag(BuildingBlockExecution execution) {
144         boolean inPserversLocked = false;
145         try {
146             GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
147             String vnfId = vnf.getVnfId();
148             inPserversLocked = aaiVnfResources.checkVnfPserversLockedFlag(vnfId);
149         } catch (Exception ex) {
150             logger.warn("Exception on checking pservers: " + ex.getMessage());
151         }
152         if (inPserversLocked) {
153             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "VNF PServers in Locked in A&AI");
154         }
155     }
156 }