Handle exception
[appc.git] / appc-adapters / appc-rest-healthcheck-adapter / appc-rest-healthcheck-adapter-bundle / src / main / java / org / onap / appc / adapter / restHealthcheck / impl / RestHealthcheckAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * Modifications Copyright (C) 2019 IBM
11  * =============================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  * 
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  * 
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  * 
24  * ============LICENSE_END=========================================================
25  */
26
27 package org.onap.appc.adapter.restHealthcheck.impl;
28
29 import java.util.Map;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.util.EntityUtils;
36 import org.glassfish.grizzly.http.util.HttpStatus;
37 import org.onap.appc.Constants;
38 import org.onap.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
39 import org.onap.appc.configuration.Configuration;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43
44 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
45
46     /**
47      * The constant for the status code for a failed outcome
48      */
49     @SuppressWarnings("nls")
50     public static final String OUTCOME_FAILURE = "failure";
51     /**
52      * The logger to be used
53      */
54     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
55     /**
56      * A reference to the adapter configuration object.
57      */
58     private Configuration configuration;
59     /**
60      * This default constructor is used as a work around because the activator
61      * wasnt getting called
62      */
63     public RestHealthcheckAdapterImpl() {
64         initialize();
65     }
66     @Override
67     public String getAdapterName() {
68         return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
69     }
70     public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
71         logger.info("VNF rest health check");
72         String uri = params.get("VNF.URI");
73         String endPoint = params.get("VNF.endpoint");
74         String tUrl = uri + "/" + endPoint;
75         RequestContext rc = new RequestContext(ctx);
76         rc.isAlive();
77         try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
78             HttpGet httpGet = new HttpGet(tUrl);
79             HttpResponse response ;
80             response = httpClient.execute(httpGet);
81             int responseCode=response.getStatusLine().getStatusCode();
82             HttpEntity entity = response.getEntity();
83             String responseOutput = EntityUtils.toString(entity);
84             if(responseCode == 200)
85             {
86                 doSuccess(rc, responseCode, responseOutput);
87             }
88             else
89             {
90                 doHealthCheckFailure(rc, responseCode, responseOutput);
91             }
92         } catch (Exception ex) {
93             doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
94         }
95     }
96     @SuppressWarnings("static-method")
97     private void doFailure(RequestContext rc, HttpStatus code, String message) {
98         SvcLogicContext svcLogic = rc.getSvcLogicContext();
99         String msg = (message == null) ? code.getReasonPhrase() : message;
100         if (msg.contains("\n")) {
101             msg = msg.substring(msg.indexOf("\n"));
102         }
103         String status;
104         try {
105             status = Integer.toString(code.getStatusCode());
106         } catch (Exception e) {
107             status = "500";
108             logger.error("Error while casting Integer to String", e);
109         }
110         svcLogic.setStatus(OUTCOME_FAILURE);
111         svcLogic.setAttribute("healthcheck.result.code", "200");
112         svcLogic.setAttribute("healthcheck.result.message", status + " " + msg);
113     }
114     /**
115      * @param rc
116      *            The request context that manages the state and recovery of the
117      *            request for the life of its processing.
118      */
119     @SuppressWarnings("static-method")
120     private void doHealthCheckFailure(RequestContext rc, int code, String message) {
121         SvcLogicContext svcLogic = rc.getSvcLogicContext();
122         String msg = Integer.toString(code) + " " + message;
123         svcLogic.setAttribute("healthcheck.result.code", "200");
124         svcLogic.setAttribute("healthcheck.result.message", msg);
125     }
126     @SuppressWarnings("static-method")
127     private void doSuccess(RequestContext rc, int code, String message) {
128         SvcLogicContext svcLogic = rc.getSvcLogicContext();
129         String msg = Integer.toString(code) + " " + message;
130         svcLogic.setAttribute("healthcheck.result.code", "400");
131         svcLogic.setAttribute("healthcheck.result.message", msg);
132     }
133     /**
134      * initialize the provider adapter by building the context cache
135      */
136     private void initialize() {
137         logger.info("init rest health check adapter!!!!!");
138     }
139
140
141 }