2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.restHealthcheck.impl;
27 import java.util.Properties;
28 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.onap.appc.Constants;
31 import org.onap.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
32 import org.onap.appc.configuration.Configuration;
33 import org.onap.appc.pool.PoolExtensionException;
34 import org.onap.appc.util.StructuredPropertyHelper;
37 import com.att.cdp.zones.ImageService;
38 import com.att.cdp.zones.Provider;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.att.eelf.i18n.EELFResourceManager;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.glassfish.grizzly.http.util.HttpStatus;
46 import static com.att.eelf.configuration.Configuration.*;
48 import org.apache.http.*;
49 import org.apache.http.client.*;
50 import org.apache.http.client.methods.*;
51 import org.apache.http.impl.client.*;
52 import org.apache.http.util.EntityUtils;
53 import java.io.IOException;
54 import java.net.InetAddress;
56 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
59 * The constant for the status code for a failed outcome
61 @SuppressWarnings("nls")
62 public static final String OUTCOME_FAILURE = "failure";
64 * The logger to be used
66 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
68 * A reference to the adapter configuration object.
70 private Configuration configuration;
72 * This default constructor is used as a work around because the activator
73 * wasnt getting called
75 public RestHealthcheckAdapterImpl() {
79 public String getAdapterName() {
80 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
82 public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
83 logger.info("VNF rest health check");
84 String uri=params.get("VNF.URI");
85 String endPoint=params.get("VNF.endpoint");
86 String tUrl=uri+"/"+endPoint;
87 RequestContext rc = new RequestContext(ctx);
89 try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
90 HttpGet httpGet = new HttpGet(tUrl);
91 HttpResponse response ;
92 response = httpClient.execute(httpGet);
93 int responseCode=response.getStatusLine().getStatusCode();
94 HttpEntity entity = response.getEntity();
95 String responseOutput=EntityUtils.toString(entity);
98 doSuccess(rc,responseCode,responseOutput);
102 doHealthCheckFailure(rc,responseCode,responseOutput);
104 } catch (Exception ex) {
105 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
108 @SuppressWarnings("static-method")
109 private void doFailure(RequestContext rc, HttpStatus code, String message) {
110 SvcLogicContext svcLogic = rc.getSvcLogicContext();
111 String msg = (message == null) ? code.getReasonPhrase() : message;
112 if (msg.contains("\n")) {
113 msg = msg.substring(msg.indexOf("\n"));
117 status = Integer.toString(code.getStatusCode());
118 } catch (Exception e) {
121 svcLogic.setStatus(OUTCOME_FAILURE);
122 svcLogic.setAttribute("healthcheck.result.code", "200");
123 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
127 * The request context that manages the state and recovery of the
128 * request for the life of its processing.
130 @SuppressWarnings("static-method")
131 private void doHealthCheckFailure(RequestContext rc, int code, String message) {
132 SvcLogicContext svcLogic = rc.getSvcLogicContext();
133 String msg = Integer.toString(code)+" "+message;
134 svcLogic.setAttribute("healthcheck.result.code", "200");
135 svcLogic.setAttribute("healthcheck.result.message", msg);
137 @SuppressWarnings("static-method")
138 private void doSuccess(RequestContext rc, int code, String message) {
139 SvcLogicContext svcLogic = rc.getSvcLogicContext();
140 String msg = Integer.toString(code)+" "+message;
141 svcLogic.setAttribute("healthcheck.result.code", "400");
142 svcLogic.setAttribute("healthcheck.result.message", msg);
145 * initialize the provider adapter by building the context cache
147 private void initialize() {
148 logger.info("init rest health check adapter!!!!!");