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