Replaced all tabs with spaces in java and pom.xml
[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.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     @Autowired
49     private ExceptionBuilder exceptionUtil;
50     @Autowired
51     private ExtractPojosForBB extractPojosForBB;
52     @Autowired
53     private CatalogDbClient catalogDbClient;
54     @Autowired
55     private ApplicationControllerAction appCClient;
56
57     public void setParamsForGenericVnfHealthCheck(BuildingBlockExecution execution) {
58
59         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
60
61         try {
62             ControllerSelectionReference controllerSelectionReference;
63             GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
64             String vnfId = vnf.getVnfId();
65             String vnfName = vnf.getVnfName();
66             String vnfType = vnf.getVnfType();
67             String oamIpAddress = vnf.getIpv4OamAddress();
68             String actionCategory = Action.HealthCheck.toString();
69             controllerSelectionReference =
70                     catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
71             String controllerName = controllerSelectionReference.getControllerName();
72
73             execution.setVariable("vnfId", vnfId);
74             execution.setVariable("vnfName", vnfName);
75             execution.setVariable("oamIpAddress", oamIpAddress);
76             execution.setVariable("vnfHostIpAddress", oamIpAddress);
77             execution.setVariable("msoRequestId", gBBInput.getRequestContext().getMsoRequestId());
78             execution.setVariable("action", actionCategory);
79             execution.setVariable("controllerType", controllerName);
80
81         } catch (Exception ex) {
82             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
83         }
84     }
85
86     public void callAppcClient(BuildingBlockExecution execution) {
87         logger.trace("Start runAppcCommand ");
88         String appcCode = "1002";
89         String appcMessage = "";
90         try {
91             Action action = null;
92             action = Action.valueOf(execution.getVariable("action"));
93             String msoRequestId = execution.getVariable("msoRequestId");
94             String vnfId = execution.getVariable("vnfId");
95             Optional<String> payload = null;
96             if (execution.getVariable("payload") != null) {
97                 String pay = execution.getVariable("payload");
98                 payload = Optional.of(pay);
99             }
100             String controllerType = execution.getVariable("controllerType");
101             HashMap<String, String> payloadInfo = new HashMap<String, String>();
102             payloadInfo.put("vnfName", execution.getVariable("vnfName"));
103             payloadInfo.put("vfModuleId", execution.getVariable("vfModuleId"));
104             payloadInfo.put("oamIpAddress", execution.getVariable("oamIpAddress"));
105             payloadInfo.put("vnfHostIpAddress", execution.getVariable("vnfHostIpAddress"));
106
107             logger.debug("Running APP-C action: {}", action.toString());
108             logger.debug("VNFID: {}", vnfId);
109             // PayloadInfo contains extra information that adds on to payload before making request to appc
110             appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
111             appcCode = appCClient.getErrorCode();
112             appcMessage = appCClient.getErrorMessage();
113         } catch (BpmnError ex) {
114             logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
115                     "Caught exception in GenericVnfHealthCheck", "BPMN", ErrorCode.UnknownError.getValue(), ex);
116             appcMessage = ex.getMessage();
117             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
118         } catch (Exception e) {
119             if (e instanceof java.util.concurrent.TimeoutException) {
120                 appcMessage = "Request to APPC timed out. ";
121                 logger.error("{} {} {} {} {}", MessageEnum.RA_CONNECTION_EXCEPTION.toString(),
122                         "Caught timedOut exception in runAppcCommand in GenericVnfHealthCheck", "BPMN",
123                         ErrorCode.UnknownError.getValue(), "APPC Error", e);
124                 throw e;
125             } else {
126                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
127                         "Caught exception in runAppcCommand in GenericVnfHealthCheck", "BPMN",
128                         ErrorCode.UnknownError.getValue(), "APPC Error", e);
129                 appcMessage = e.getMessage();
130                 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
131             }
132         }
133         logger.error("Error Message: " + appcMessage);
134         logger.error("ERROR CODE: " + appcCode);
135         logger.trace("End of runAppCommand ");
136         if (appcCode != null && !appcCode.equals("0")) {
137             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
138         }
139     }
140 }