Enhanced Exception handling.
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import java.util.HashMap;
23 import java.util.Optional;
24 import java.net.HttpURLConnection;
25 import java.net.SocketTimeoutException;
26 import org.apache.http.conn.ConnectTimeoutException;
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.MessageEnum;
39 import org.onap.so.logger.MsoLogger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class GenericVnfHealthCheck {
45
46         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenericVnfHealthCheck.class);
47         @Autowired
48         private ExceptionBuilder exceptionUtil;
49         @Autowired
50         private ExtractPojosForBB extractPojosForBB;
51         @Autowired
52         private CatalogDbClient catalogDbClient;
53         @Autowired
54         private ApplicationControllerAction appCClient;
55         
56         public void setParamsForGenericVnfHealthCheck(BuildingBlockExecution execution) {
57                 
58                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
59
60                 try {
61                         ControllerSelectionReference controllerSelectionReference;
62                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
63                         String vnfId = vnf.getVnfId();
64                         String vnfName = vnf.getVnfName();      
65                         String vnfType = vnf.getVnfType();
66                         String oamIpAddress = vnf.getIpv4OamAddress();
67                         String actionCategory = Action.HealthCheck.toString();
68                         controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
69                         String controllerName = controllerSelectionReference.getControllerName();
70                         
71                         execution.setVariable("vnfId", vnfId);
72                         execution.setVariable("vnfName", vnfName);
73                         execution.setVariable("oamIpAddress", oamIpAddress);
74                         execution.setVariable("vnfHostIpAddress", oamIpAddress);
75                         execution.setVariable("msoRequestId", gBBInput.getRequestContext().getMsoRequestId());
76                         execution.setVariable("action", actionCategory);
77                         execution.setVariable("controllerType", controllerName);
78                         
79                 } catch (Exception ex) {
80                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
81                 }
82         }
83         
84         public void callAppcClient(BuildingBlockExecution execution) {
85                 msoLogger.trace("Start runAppcCommand ");
86                 String appcCode = "1002";
87                 String appcMessage = "";
88                 try {
89                         Action action = null;
90                         action = Action.valueOf(execution.getVariable("action"));
91                         String msoRequestId = execution.getVariable("msoRequestId");
92                         String vnfId = execution.getVariable("vnfId");
93                         Optional<String> payload = null;
94                         if(execution.getVariable("payload") != null){
95                                 String pay = execution.getVariable("payload");
96                                 payload =  Optional.of(pay);
97                         }
98                         String controllerType = execution.getVariable("controllerType");
99                         HashMap<String, String> payloadInfo = new HashMap<String, String>();
100                         payloadInfo.put("vnfName", execution.getVariable("vnfName"));
101                         payloadInfo.put("vfModuleId",execution.getVariable("vfModuleId"));
102                         payloadInfo.put("oamIpAddress",execution.getVariable("oamIpAddress"));
103                         payloadInfo.put("vnfHostIpAddress",execution.getVariable("vnfHostIpAddress"));
104                         
105                         msoLogger.debug("Running APP-C action: " + action.toString());
106                         msoLogger.debug("VNFID: " + vnfId);     
107                         //PayloadInfo contains extra information that adds on to payload before making request to appc
108                         appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
109                         appcCode = appCClient.getErrorCode();
110                         appcMessage = appCClient.getErrorMessage();
111         } catch (BpmnError ex) {
112                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Caught exception in GenericVnfHealthCheck", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + ex);
113             appcMessage = ex.getMessage();
114             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
115                 } catch (Exception e) {
116                         if (e instanceof java.util.concurrent.TimeoutException )
117                         {
118                                 appcMessage = "Request to APPC timed out. ";
119                                 msoLogger.error(MessageEnum.RA_CONNECTION_EXCEPTION, "Caught timedOut exception in runAppcCommand in GenericVnfHealthCheck", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "APPC Error", e);
120                                 throw e;
121                         }
122                         else {
123                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "Caught exception in runAppcCommand in GenericVnfHealthCheck", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "APPC Error", e);
124                                 appcMessage = e.getMessage();
125                                 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
126                         }
127                 }
128                 msoLogger.error("Error Message: " + appcMessage);
129                 msoLogger.error("ERROR CODE: " + appcCode);
130                 msoLogger.trace("End of runAppCommand ");
131                 if (appcCode != null && !appcCode.equals("0")) {
132                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
133                 }
134         }
135 }