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 * 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
16 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * ============LICENSE_END=========================================================
27 package org.onap.appc.adapter.restHealthcheck.impl;
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;
44 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
47 * The constant for the status code for a failed outcome
49 @SuppressWarnings("nls")
50 public static final String OUTCOME_FAILURE = "failure";
52 * The logger to be used
54 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
56 * A reference to the adapter configuration object.
58 private Configuration configuration;
60 * This default constructor is used as a work around because the activator
61 * wasnt getting called
63 public RestHealthcheckAdapterImpl() {
67 public String getAdapterName() {
68 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
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);
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)
86 doSuccess(rc, responseCode, responseOutput);
90 doHealthCheckFailure(rc, responseCode, responseOutput);
92 } catch (Exception ex) {
93 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
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"));
105 status = Integer.toString(code.getStatusCode());
106 } catch (Exception e) {
108 logger.error("Error while casting Integer to String", e);
110 svcLogic.setStatus(OUTCOME_FAILURE);
111 svcLogic.setAttribute("healthcheck.result.code", "200");
112 svcLogic.setAttribute("healthcheck.result.message", status + " " + msg);
116 * The request context that manages the state and recovery of the
117 * request for the life of its processing.
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);
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);
134 * initialize the provider adapter by building the context cache
136 private void initialize() {
137 logger.info("init rest health check adapter!!!!!");