f0db5eda2eaeb1b503c7fc71fba1fb358e3b8107
[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 = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
70                         String controllerName = controllerSelectionReference.getControllerName();
71                         
72                         execution.setVariable("vnfId", vnfId);
73                         execution.setVariable("vnfName", vnfName);
74                         execution.setVariable("oamIpAddress", oamIpAddress);
75                         execution.setVariable("vnfHostIpAddress", oamIpAddress);
76                         execution.setVariable("msoRequestId", gBBInput.getRequestContext().getMsoRequestId());
77                         execution.setVariable("action", actionCategory);
78                         execution.setVariable("controllerType", controllerName);
79                         
80                 } catch (Exception ex) {
81                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
82                 }
83         }
84         
85         public void callAppcClient(BuildingBlockExecution execution) {
86                 logger.trace("Start runAppcCommand ");
87                 String appcCode = "1002";
88                 String appcMessage = "";
89                 try {
90                         Action action = null;
91                         action = Action.valueOf(execution.getVariable("action"));
92                         String msoRequestId = execution.getVariable("msoRequestId");
93                         String vnfId = execution.getVariable("vnfId");
94                         Optional<String> payload = null;
95                         if(execution.getVariable("payload") != null){
96                                 String pay = execution.getVariable("payload");
97                                 payload =  Optional.of(pay);
98                         }
99                         String controllerType = execution.getVariable("controllerType");
100                         HashMap<String, String> payloadInfo = new HashMap<String, String>();
101                         payloadInfo.put("vnfName", execution.getVariable("vnfName"));
102                         payloadInfo.put("vfModuleId",execution.getVariable("vfModuleId"));
103                         payloadInfo.put("oamIpAddress",execution.getVariable("oamIpAddress"));
104                         payloadInfo.put("vnfHostIpAddress",execution.getVariable("vnfHostIpAddress"));
105                         
106                         logger.debug("Running APP-C action: {}", action.toString());
107                         logger.debug("VNFID: {}", vnfId);
108                         //PayloadInfo contains extra information that adds on to payload before making request to appc
109                         appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
110                         appcCode = appCClient.getErrorCode();
111                         appcMessage = appCClient.getErrorMessage();
112         } catch (BpmnError ex) {
113                         logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
114                                 "Caught exception in GenericVnfHealthCheck", "BPMN", ErrorCode.UnknownError.getValue(), ex);
115                         appcMessage = ex.getMessage();
116             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
117                 } catch (Exception e) {
118                         if (e instanceof java.util.concurrent.TimeoutException )
119                         {
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                         }
126                         else {
127                                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(),
128                                         "Caught exception in runAppcCommand in GenericVnfHealthCheck", "BPMN",
129                                         ErrorCode.UnknownError.getValue(), "APPC Error", e);
130                                 appcMessage = e.getMessage();
131                                 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
132                         }
133                 }
134                 logger.error("Error Message: " + appcMessage);
135                 logger.error("ERROR CODE: " + appcCode);
136                 logger.trace("End of runAppCommand ");
137                 if (appcCode != null && !appcCode.equals("0")) {
138                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
139                 }
140         }
141 }