2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.restHealthcheck.impl;
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;
43 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
46 * The constant for the status code for a failed outcome
48 @SuppressWarnings("nls")
49 public static final String OUTCOME_FAILURE = "failure";
51 * The logger to be used
53 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
55 * A reference to the adapter configuration object.
57 private Configuration configuration;
59 * This default constructor is used as a work around because the activator
60 * wasnt getting called
62 public RestHealthcheckAdapterImpl() {
66 public String getAdapterName() {
67 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
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);
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)
85 doSuccess(rc, responseCode, responseOutput);
89 doHealthCheckFailure(rc, responseCode, responseOutput);
91 } catch (Exception ex) {
92 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
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"));
104 status = Integer.toString(code.getStatusCode());
105 } catch (Exception e) {
108 svcLogic.setStatus(OUTCOME_FAILURE);
109 svcLogic.setAttribute("healthcheck.result.code", "200");
110 svcLogic.setAttribute("healthcheck.result.message", status + " " + msg);
114 * The request context that manages the state and recovery of the
115 * request for the life of its processing.
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);
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);
132 * initialize the provider adapter by building the context cache
134 private void initialize() {
135 logger.info("init rest health check adapter!!!!!");