2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.restHealthcheck.impl;
28 import java.util.Properties;
29 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.onap.appc.Constants;
32 import org.onap.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.pool.PoolExtensionException;
35 import org.onap.appc.util.StructuredPropertyHelper;
38 import com.att.cdp.zones.ImageService;
39 import com.att.cdp.zones.Provider;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.att.eelf.i18n.EELFResourceManager;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.glassfish.grizzly.http.util.HttpStatus;
47 import static com.att.eelf.configuration.Configuration.*;
49 import org.apache.http.*;
50 import org.apache.http.client.*;
51 import org.apache.http.client.methods.*;
52 import org.apache.http.impl.client.*;
53 import org.apache.http.util.EntityUtils;
54 import java.io.IOException;
55 import java.net.InetAddress;
57 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
60 * The constant used to define the adapter name in the mapped diagnostic
65 @SuppressWarnings("nls")
66 public static final String MDC_ADAPTER = "adapter";
69 * The constant used to define the service name in the mapped diagnostic
72 @SuppressWarnings("nls")
73 public static final String MDC_SERVICE = "service";
76 * The constant for the status code for a failed outcome
78 @SuppressWarnings("nls")
79 public static final String OUTCOME_FAILURE = "failure";
82 * The constant for the status code for a successful outcome
84 @SuppressWarnings("nls")
85 public static final String OUTCOME_SUCCESS = "success";
88 * A constant for the property token "provider" used in the structured
89 * property specifications
91 @SuppressWarnings("nls")
92 public static final String PROPERTY_PROVIDER = "provider";
95 * A constant for the property token "identity" used in the structured
96 * property specifications
98 @SuppressWarnings("nls")
99 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
102 * A constant for the property token "name" used in the structured property
105 @SuppressWarnings("nls")
106 public static final String PROPERTY_PROVIDER_NAME = "name";
109 * A constant for the property token "tenant" used in the structured
110 * property specifications
112 @SuppressWarnings("nls")
113 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
116 * A constant for the property token "tenant name" used in the structured
117 * property specifications
119 @SuppressWarnings("nls")
120 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
123 * A constant for the property token "password" used in the structured
124 * property specifications
126 @SuppressWarnings("nls")
127 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
130 * A constant for the property token "userid" used in the structured
131 * property specifications
133 @SuppressWarnings("nls")
134 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
137 * A constant for the property token "type" used in the structured property
140 @SuppressWarnings("nls")
141 public static final String PROPERTY_PROVIDER_TYPE = "type";
144 @SuppressWarnings("nls")
145 public static final String PING_SERVICE = "pingServer";
148 * The logger to be used
150 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
153 * The constant for a left parenthesis
155 private static final char LPAREN = '(';
158 * The constant for a new line control code
160 private static final char NL = '\n';
163 * The constant for a single quote
165 private static final char QUOTE = '\'';
168 * The constant for a right parenthesis
170 private static final char RPAREN = ')';
173 * The constant for a space
175 private static final char SPACE = ' ';
178 * A reference to the adapter configuration object.
180 private Configuration configuration;
183 * A cache of providers that are predefined.
185 // private Map<String /* provider name */, ProviderCache> providerCache;
188 * This default constructor is used as a work around because the activator
189 * wasnt getting called
192 * A cache of providers that are predefined.
194 // private Map<String /* provider name */, ProviderCache> providerCache;
197 * This default constructor is used as a work around because the activator
198 * wasnt getting called
200 public RestHealthcheckAdapterImpl() {
206 public RestHealthcheckAdapterImpl(boolean initialize) {
215 public RestHealthcheckAdapterImpl(Properties props) {
222 public String getAdapterName() {
223 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
226 public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
227 logger.info("VNF rest health check");
228 String uri=params.get("VNF.URI");
229 String endPoint=params.get("VNF.endpoint");
230 String tUrl=uri+"/"+endPoint;
231 RequestContext rc = new RequestContext(ctx);
234 try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
235 HttpGet httpGet = new HttpGet(tUrl);
236 HttpResponse response = null;
237 response = httpClient.execute(httpGet);
238 int responseCode=response.getStatusLine().getStatusCode();
239 HttpEntity entity = response.getEntity();
240 String responseOutput=EntityUtils.toString(entity);
241 if(responseCode==200)
243 doSuccess(rc,responseCode,responseOutput);
247 doHealthCheckFailure(rc,responseCode,responseOutput);
249 } catch (Exception ex) {
250 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
257 @SuppressWarnings("static-method")
258 private void doFailure(RequestContext rc, HttpStatus code, String message) {
259 SvcLogicContext svcLogic = rc.getSvcLogicContext();
260 String msg = (message == null) ? code.getReasonPhrase() : message;
261 if (msg.contains("\n")) {
262 msg = msg.substring(msg.indexOf("\n"));
267 status = Integer.toString(code.getStatusCode());
268 } catch (Exception e) {
271 svcLogic.setStatus(OUTCOME_FAILURE);
272 svcLogic.setAttribute("healthcheck.result.code", "200");
273 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
279 * The request context that manages the state and recovery of the
280 * request for the life of its processing.
282 @SuppressWarnings("static-method")
283 private void doHealthCheckFailure(RequestContext rc, int code, String message) {
284 SvcLogicContext svcLogic = rc.getSvcLogicContext();
285 String msg = Integer.toString(code)+" "+message;
286 svcLogic.setAttribute("healthcheck.result.code", "200");
287 svcLogic.setAttribute("healthcheck.result.message", msg);
292 @SuppressWarnings("static-method")
293 private void doSuccess(RequestContext rc, int code, String message) {
294 SvcLogicContext svcLogic = rc.getSvcLogicContext();
295 String msg = Integer.toString(code)+" "+message;
296 svcLogic.setAttribute("healthcheck.result.code", "400");
297 svcLogic.setAttribute("healthcheck.result.message", msg);
303 * initialize the provider adapter by building the context cache
305 private void initialize() {
308 logger.info("init rest health check adapter!!!!!");