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.openecomp.appc.adapter.restHealthcheck.impl;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Properties;
34 import java.util.regex.Pattern;
35 import org.apache.http.impl.client.CloseableHttpClient;
37 import org.openecomp.appc.Constants;
38 import org.openecomp.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
39 import org.openecomp.appc.configuration.Configuration;
40 import org.openecomp.appc.configuration.ConfigurationFactory;
41 import org.openecomp.appc.exceptions.APPCException;
42 import org.openecomp.appc.exceptions.UnknownProviderException;
43 import org.openecomp.appc.i18n.Msg;
44 import org.openecomp.appc.pool.Pool;
45 import org.openecomp.appc.pool.PoolExtensionException;
46 import org.openecomp.appc.util.StructuredPropertyHelper;
47 import org.openecomp.appc.util.StructuredPropertyHelper.Node;
50 import com.att.cdp.exceptions.ContextConnectionException;
51 import com.att.cdp.exceptions.ResourceNotFoundException;
52 import com.att.cdp.exceptions.TimeoutException;
53 import com.att.cdp.exceptions.ZoneException;
54 import com.att.cdp.pal.util.StringHelper;
55 import com.att.cdp.zones.ComputeService;
56 import com.att.cdp.zones.Context;
57 import com.att.cdp.zones.ImageService;
58 import com.att.cdp.zones.Provider;
59 import com.att.cdp.zones.model.Image;
60 import com.att.cdp.zones.model.Server;
61 import com.att.cdp.zones.model.Server.Status;
62 import com.att.cdp.zones.model.ServerBootSource;
63 import com.att.eelf.configuration.EELFLogger;
64 import com.att.eelf.configuration.EELFManager;
65 import com.att.eelf.i18n.EELFResourceManager;
66 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
68 import org.glassfish.grizzly.http.util.HttpStatus;
71 import java.net.InetAddress;
72 import java.util.Locale;
73 import java.util.UUID;
74 import static com.att.eelf.configuration.Configuration.*;
76 import org.apache.http.*;
77 import org.apache.http.client.*;
78 import org.apache.http.client.methods.*;
79 import org.apache.http.impl.client.*;
80 import org.apache.http.util.EntityUtils;
81 import java.io.IOException;
82 import org.apache.http.entity.StringEntity;
83 import java.net.InetAddress;
85 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
88 * The constant used to define the adapter name in the mapped diagnostic
93 @SuppressWarnings("nls")
94 public static final String MDC_ADAPTER = "adapter";
97 * The constant used to define the service name in the mapped diagnostic
100 @SuppressWarnings("nls")
101 public static final String MDC_SERVICE = "service";
104 * The constant for the status code for a failed outcome
106 @SuppressWarnings("nls")
107 public static final String OUTCOME_FAILURE = "failure";
110 * The constant for the status code for a successful outcome
112 @SuppressWarnings("nls")
113 public static final String OUTCOME_SUCCESS = "success";
116 * A constant for the property token "provider" used in the structured
117 * property specifications
119 @SuppressWarnings("nls")
120 public static final String PROPERTY_PROVIDER = "provider";
123 * A constant for the property token "identity" used in the structured
124 * property specifications
126 @SuppressWarnings("nls")
127 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
130 * A constant for the property token "name" used in the structured property
133 @SuppressWarnings("nls")
134 public static final String PROPERTY_PROVIDER_NAME = "name";
137 * A constant for the property token "tenant" used in the structured
138 * property specifications
140 @SuppressWarnings("nls")
141 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
144 * A constant for the property token "tenant name" used in the structured
145 * property specifications
147 @SuppressWarnings("nls")
148 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
151 * A constant for the property token "password" used in the structured
152 * property specifications
154 @SuppressWarnings("nls")
155 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
158 * A constant for the property token "userid" used in the structured
159 * property specifications
161 @SuppressWarnings("nls")
162 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
165 * A constant for the property token "type" used in the structured property
168 @SuppressWarnings("nls")
169 public static final String PROPERTY_PROVIDER_TYPE = "type";
172 @SuppressWarnings("nls")
173 public static final String PING_SERVICE = "pingServer";
176 * The logger to be used
178 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
181 * The constant for a left parenthesis
183 private static final char LPAREN = '(';
186 * The constant for a new line control code
188 private static final char NL = '\n';
191 * The constant for a single quote
193 private static final char QUOTE = '\'';
196 * The constant for a right parenthesis
198 private static final char RPAREN = ')';
201 * The constant for a space
203 private static final char SPACE = ' ';
206 * A reference to the adapter configuration object.
208 private Configuration configuration;
211 * A cache of providers that are predefined.
213 // private Map<String /* provider name */, ProviderCache> providerCache;
216 * This default constructor is used as a work around because the activator
217 * wasnt getting called
220 * A cache of providers that are predefined.
222 // private Map<String /* provider name */, ProviderCache> providerCache;
225 * This default constructor is used as a work around because the activator
226 * wasnt getting called
228 public RestHealthcheckAdapterImpl() {
234 public RestHealthcheckAdapterImpl(boolean initialize) {
243 public RestHealthcheckAdapterImpl(Properties props) {
250 public String getAdapterName() {
251 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
254 public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
255 logger.info("VNF rest health check");
256 String uri=params.get("VNF.URI");
257 String endPoint=params.get("VNF.endpoint");
258 String tUrl=uri+"/"+endPoint;
259 RequestContext rc = new RequestContext(ctx);
262 try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
263 HttpGet httpGet = new HttpGet(tUrl);
264 HttpResponse response = null;
265 response = httpClient.execute(httpGet);
266 int responseCode=response.getStatusLine().getStatusCode();
267 HttpEntity entity = response.getEntity();
268 String responseOutput=EntityUtils.toString(entity);
269 if(responseCode==200)
271 doSuccess(rc,responseCode,responseOutput);
275 doHealthCheckFailure(rc,responseCode,responseOutput);
277 } catch (Exception ex) {
278 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
285 @SuppressWarnings("static-method")
286 private void doFailure(RequestContext rc, HttpStatus code, String message) {
287 SvcLogicContext svcLogic = rc.getSvcLogicContext();
288 String msg = (message == null) ? code.getReasonPhrase() : message;
289 if (msg.contains("\n")) {
290 msg = msg.substring(msg.indexOf("\n"));
295 status = Integer.toString(code.getStatusCode());
296 } catch (Exception e) {
299 svcLogic.setStatus(OUTCOME_FAILURE);
300 svcLogic.setAttribute("healthcheck.result.code", "200");
301 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
307 * The request context that manages the state and recovery of the
308 * request for the life of its processing.
310 @SuppressWarnings("static-method")
311 private void doHealthCheckFailure(RequestContext rc, int code, String message) {
312 SvcLogicContext svcLogic = rc.getSvcLogicContext();
313 String msg = Integer.toString(code)+" "+message;
314 svcLogic.setAttribute("healthcheck.result.code", "200");
315 svcLogic.setAttribute("healthcheck.result.message", msg);
320 @SuppressWarnings("static-method")
321 private void doSuccess(RequestContext rc, int code, String message) {
322 SvcLogicContext svcLogic = rc.getSvcLogicContext();
323 String msg = Integer.toString(code)+" "+message;
324 svcLogic.setAttribute("healthcheck.result.code", "400");
325 svcLogic.setAttribute("healthcheck.result.message", msg);
331 * initialize the provider adapter by building the context cache
333 private void initialize() {
336 logger.info("init rest health check adapter!!!!!");