8f0c809e1f57f0015508702e48c8cdb320c678cf
[so.git] /
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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
23
24 import java.util.HashMap;
25 import java.util.Optional;
26 import org.camunda.bpm.engine.delegate.BpmnError;
27 import org.onap.appc.client.lcm.model.Action;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
30 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
32 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
33 import org.onap.so.client.appc.ApplicationControllerAction;
34 import org.onap.so.client.exception.ExceptionBuilder;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
37 import org.onap.so.logger.ErrorCode;
38 import org.onap.so.logger.MessageEnum;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class GenericVnfHealthCheck {
46
47     private static final Logger logger = LoggerFactory.getLogger(GenericVnfHealthCheck.class);
48     public static final String VNF_NAME = "vnfName";
49     public static final String OAM_IP_ADDRESS = "oamIpAddress";
50     public static final String VNF_HOST_IP_ADDRESS = "vnfHostIpAddress";
51     @Autowired
52     private ExceptionBuilder exceptionUtil;
53     @Autowired
54     private ExtractPojosForBB extractPojosForBB;
55     @Autowired
56     private CatalogDbClient catalogDbClient;
57     @Autowired
58     private ApplicationControllerAction appCClient;
59
60     public void setParamsForGenericVnfHealthCheck(BuildingBlockExecution execution) {
61
62         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
63
64         try {
65             ControllerSelectionReference controllerSelectionReference;
66             GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
67             String vnfId = vnf.getVnfId();
68             String vnfName = vnf.getVnfName();
69             String vnfType = vnf.getVnfType();
70             String oamIpAddress = vnf.getIpv4OamAddress();
71             String actionCategory = Action.HealthCheck.toString();
72             controllerSelectionReference =
73                     catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
74             String controllerName = controllerSelectionReference.getControllerName();
75
76             execution.setVariable("vnfId", vnfId);
77             execution.setVariable(VNF_NAME, vnfName);
78             execution.setVariable(OAM_IP_ADDRESS, oamIpAddress);
79             execution.setVariable(VNF_HOST_IP_ADDRESS, oamIpAddress);
80             execution.setVariable("msoRequestId", gBBInput.getRequestContext().getMsoRequestId());
81             execution.setVariable("action", actionCategory);
82             execution.setVariable("controllerType", controllerName);
83
84         } catch (Exception ex) {
85             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
86         }
87     }
88
89     public void callAppcClient(BuildingBlockExecution execution) {
90         logger.trace("Start runAppcCommand ");
91         String appcCode = "1002";
92         String appcMessage = "";
93         try {
94             Action action = null;
95             action = Action.valueOf(execution.getVariable("action"));
96             String msoRequestId = execution.getVariable("msoRequestId");
97             String vnfId = execution.getVariable("vnfId");
98             Optional<String> payload = null;
99             if (execution.getVariable("payload") != null) {
100                 String pay = execution.getVariable("payload");
101                 payload = Optional.of(pay);
102             }
103             String controllerType = execution.getVariable("controllerType");
104             HashMap<String, String> payloadInfo = new HashMap<>();
105             payloadInfo.put(VNF_NAME, execution.getVariable(VNF_NAME));
106             payloadInfo.put("vfModuleId", execution.getVariable("vfModuleId"));
107             payloadInfo.put(OAM_IP_ADDRESS, execution.getVariable(OAM_IP_ADDRESS));
108             payloadInfo.put(VNF_HOST_IP_ADDRESS, execution.getVariable(VNF_HOST_IP_ADDRESS));
109
110             logger.debug("Running APP-C action: {}", action.toString());
111             logger.debug("VNFID: {}", vnfId);
112             // PayloadInfo contains extra information that adds on to payload before making request to appc
113             appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
114             appcCode = appCClient.getErrorCode();
115             appcMessage = appCClient.getErrorMessage();
116         } catch (BpmnError ex) {
117             logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
118                     "Caught exception in GenericVnfHealthCheck", "BPMN", ErrorCode.UnknownError.getValue(), ex);
119             appcMessage = ex.getMessage();
120             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
121         } catch (Exception e) {
122             if (e instanceof java.util.concurrent.TimeoutException) {
123                 appcMessage = "Request to APPC timed out. ";
124                 logger.error("{} {} {} {} {}", MessageEnum.RA_CONNECTION_EXCEPTION.toString(),
125                         "Caught timedOut exception in runAppcCommand in GenericVnfHealthCheck", "BPMN",
126                         ErrorCode.UnknownError.getValue(), "APPC Error", e);
127                 throw e;
128             } else {
129                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
130                         "Caught exception in runAppcCommand in GenericVnfHealthCheck", "BPMN",
131                         ErrorCode.UnknownError.getValue(), "APPC Error", e);
132                 appcMessage = e.getMessage();
133                 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
134             }
135         }
136         logger.error("Error Message: " + appcMessage);
137         logger.error("ERROR CODE: " + appcCode);
138         logger.trace("End of runAppCommand ");
139         if (appcCode != null && !("0").equals(appcCode)) {
140             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
141         }
142     }
143 }