Fixing EtSI BB ClassCastException
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorVnfmNodeTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
21
22 import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
23 import java.util.Optional;
24 import org.onap.aai.domain.yang.GenericVnf;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.exception.GenericVnfNotFoundException;
27 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.onap.so.client.orchestration.AAIVnfResources;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33
34
35 /**
36  * 
37  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
38  * @author Waqas Ikram (waqas.ikram@est.tech)
39  *
40  */
41 public abstract class MonitorVnfmNodeTask {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorVnfmNodeTask.class);
44
45     private final ExtractPojosForBB extractPojosForBB;
46     private final ExceptionBuilder exceptionUtil;
47     private final AAIVnfResources aaiVnfResources;
48
49     @Autowired
50     public MonitorVnfmNodeTask(final ExtractPojosForBB extractPojosForBB, final ExceptionBuilder exceptionUtil,
51             final AAIVnfResources aaiVnfResources) {
52         this.exceptionUtil = exceptionUtil;
53         this.extractPojosForBB = extractPojosForBB;
54         this.aaiVnfResources = aaiVnfResources;
55     }
56
57     /**
58      * Check the final status of vnf in A&AI
59      * 
60      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
61      */
62     public void getNodeStatus(final BuildingBlockExecution execution) {
63         try {
64             LOGGER.debug("Executing getNodeStatus  ...");
65
66             final org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf =
67                     extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
68
69             final String vnfId = vnf.getVnfId();
70             LOGGER.debug("Query A&AI for generic VNF using vnfID: {}", vnfId);
71             final Optional<GenericVnf> aaiGenericVnfOptional = aaiVnfResources.getGenericVnf(vnfId);
72
73             if (!aaiGenericVnfOptional.isPresent()) {
74                 throw new GenericVnfNotFoundException("Unable to find generic vnf in A&AI using vnfID: " + vnfId);
75             }
76             final GenericVnf genericVnf = aaiGenericVnfOptional.get();
77             final String orchestrationStatus = genericVnf.getOrchestrationStatus();
78             LOGGER.debug("Found generic vnf with orchestration status : {}", orchestrationStatus);
79
80             execution.setVariable(getNodeStatusVariableName(), isOrchestrationStatusValid(orchestrationStatus));
81
82         } catch (final Exception exception) {
83             LOGGER.error("Unable to get vnf from AAI", exception);
84             exceptionUtil.buildAndThrowWorkflowException(execution, 1220, exception);
85         }
86     }
87
88     /**
89      * Get variable to store in execution context
90      * 
91      * @return the variable name
92      */
93     public abstract String getNodeStatusVariableName();
94
95     /**
96      * @param orchestrationStatus the orchestration status from A&AI
97      * @return true if valid
98      */
99     public abstract boolean isOrchestrationStatusValid(final String orchestrationStatus);
100
101
102     /**
103      * Log and throw exception on timeout for job status
104      * 
105      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
106      */
107     public void timeOutLogFailue(final BuildingBlockExecution execution) {
108         final String message = "Node operation time out";
109         LOGGER.error(message);
110         exceptionUtil.buildAndThrowWorkflowException(execution, 1221, message);
111     }
112 }