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