2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.restHealthcheck.impl;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Properties;
31 import java.util.regex.Pattern;
33 import org.openecomp.appc.Constants;
34 import org.openecomp.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
35 import org.openecomp.appc.configuration.Configuration;
36 import org.openecomp.appc.configuration.ConfigurationFactory;
37 import org.openecomp.appc.exceptions.APPCException;
38 import org.openecomp.appc.exceptions.UnknownProviderException;
39 import org.openecomp.appc.i18n.Msg;
40 import org.openecomp.appc.pool.Pool;
41 import org.openecomp.appc.pool.PoolExtensionException;
42 import org.openecomp.appc.util.StructuredPropertyHelper;
43 import org.openecomp.appc.util.StructuredPropertyHelper.Node;
46 import com.att.cdp.exceptions.ContextConnectionException;
47 import com.att.cdp.exceptions.ResourceNotFoundException;
48 import com.att.cdp.exceptions.TimeoutException;
49 import com.att.cdp.exceptions.ZoneException;
50 import com.att.cdp.pal.util.StringHelper;
51 import com.att.cdp.zones.ComputeService;
52 import com.att.cdp.zones.Context;
53 import com.att.cdp.zones.ImageService;
54 import com.att.cdp.zones.Provider;
55 import com.att.cdp.zones.model.Image;
56 import com.att.cdp.zones.model.Server;
57 import com.att.cdp.zones.model.Server.Status;
58 import com.att.cdp.zones.model.ServerBootSource;
59 import com.att.eelf.configuration.EELFLogger;
60 import com.att.eelf.configuration.EELFManager;
61 import com.att.eelf.i18n.EELFResourceManager;
62 import org.openecomp.sdnc.sli.SvcLogicContext;
64 import org.glassfish.grizzly.http.util.HttpStatus;
67 import java.net.InetAddress;
68 import java.util.Locale;
69 import java.util.UUID;
70 import static com.att.eelf.configuration.Configuration.*;
72 import org.apache.http.*;
73 import org.apache.http.client.*;
74 import org.apache.http.client.methods.*;
75 import org.apache.http.impl.client.*;
76 import org.apache.http.util.EntityUtils;
77 import java.io.IOException;
78 import org.apache.http.entity.StringEntity;
79 import java.net.InetAddress;
81 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
84 * The constant used to define the adapter name in the mapped diagnostic
89 @SuppressWarnings("nls")
90 public static final String MDC_ADAPTER = "adapter";
93 * The constant used to define the service name in the mapped diagnostic
96 @SuppressWarnings("nls")
97 public static final String MDC_SERVICE = "service";
100 * The constant for the status code for a failed outcome
102 @SuppressWarnings("nls")
103 public static final String OUTCOME_FAILURE = "failure";
106 * The constant for the status code for a successful outcome
108 @SuppressWarnings("nls")
109 public static final String OUTCOME_SUCCESS = "success";
112 * A constant for the property token "provider" used in the structured
113 * property specifications
115 @SuppressWarnings("nls")
116 public static final String PROPERTY_PROVIDER = "provider";
119 * A constant for the property token "identity" used in the structured
120 * property specifications
122 @SuppressWarnings("nls")
123 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
126 * A constant for the property token "name" used in the structured property
129 @SuppressWarnings("nls")
130 public static final String PROPERTY_PROVIDER_NAME = "name";
133 * A constant for the property token "tenant" used in the structured
134 * property specifications
136 @SuppressWarnings("nls")
137 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
140 * A constant for the property token "tenant name" used in the structured
141 * property specifications
143 @SuppressWarnings("nls")
144 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
147 * A constant for the property token "password" used in the structured
148 * property specifications
150 @SuppressWarnings("nls")
151 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
154 * A constant for the property token "userid" used in the structured
155 * property specifications
157 @SuppressWarnings("nls")
158 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
161 * A constant for the property token "type" used in the structured property
164 @SuppressWarnings("nls")
165 public static final String PROPERTY_PROVIDER_TYPE = "type";
168 @SuppressWarnings("nls")
169 public static final String PING_SERVICE = "pingServer";
172 * The logger to be used
174 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
177 * The constant for a left parenthesis
179 private static final char LPAREN = '(';
182 * The constant for a new line control code
184 private static final char NL = '\n';
187 * The constant for a single quote
189 private static final char QUOTE = '\'';
192 * The constant for a right parenthesis
194 private static final char RPAREN = ')';
197 * The constant for a space
199 private static final char SPACE = ' ';
202 * A reference to the adapter configuration object.
204 private Configuration configuration;
207 * A cache of providers that are predefined.
209 // private Map<String /* provider name */, ProviderCache> providerCache;
212 * This default constructor is used as a work around because the activator
213 * wasnt getting called
216 * A cache of providers that are predefined.
218 // private Map<String /* provider name */, ProviderCache> providerCache;
221 * This default constructor is used as a work around because the activator
222 * wasnt getting called
224 public RestHealthcheckAdapterImpl() {
230 public RestHealthcheckAdapterImpl(boolean initialize) {
239 public RestHealthcheckAdapterImpl(Properties props) {
246 public String getAdapterName() {
247 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
250 public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
251 logger.info("VNF rest health check");
252 String uri=params.get("VNF.URI");
253 String endPoint=params.get("VNF.endpoint");
254 String tUrl=uri+"/"+endPoint;
255 RequestContext rc = new RequestContext(ctx);
259 HttpGet httpGet = new HttpGet(tUrl);
260 HttpClient httpClient = HttpClients.createDefault();
261 HttpResponse response = null;
262 response = httpClient.execute(httpGet);
263 int responseCode=response.getStatusLine().getStatusCode();
264 HttpEntity entity = response.getEntity();
265 String responseOutput=EntityUtils.toString(entity);
266 if(responseCode==200)
268 doSuccess(rc,responseCode,responseOutput);
272 doHealthCheckFailure(rc,responseCode,responseOutput);
274 } catch (Exception ex) {
275 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
282 @SuppressWarnings("static-method")
283 private void doFailure(RequestContext rc, HttpStatus code, String message) {
284 SvcLogicContext svcLogic = rc.getSvcLogicContext();
285 String msg = (message == null) ? code.getReasonPhrase() : message;
286 if (msg.contains("\n")) {
287 msg = msg.substring(msg.indexOf("\n"));
292 status = Integer.toString(code.getStatusCode());
293 } catch (Exception e) {
296 svcLogic.setStatus(OUTCOME_FAILURE);
297 svcLogic.setAttribute("healthcheck.result.code", "200");
298 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
304 * The request context that manages the state and recovery of the
305 * request for the life of its processing.
307 @SuppressWarnings("static-method")
308 private void doHealthCheckFailure(RequestContext rc, int code, String message) {
309 SvcLogicContext svcLogic = rc.getSvcLogicContext();
310 String msg = Integer.toString(code)+" "+message;
311 svcLogic.setAttribute("healthcheck.result.code", "200");
312 svcLogic.setAttribute("healthcheck.result.message", msg);
317 @SuppressWarnings("static-method")
318 private void doSuccess(RequestContext rc, int code, String message) {
319 SvcLogicContext svcLogic = rc.getSvcLogicContext();
320 String msg = Integer.toString(code)+" "+message;
321 svcLogic.setAttribute("healthcheck.result.code", "400");
322 svcLogic.setAttribute("healthcheck.result.message", msg);
328 * initialize the provider adapter by building the context cache
330 private void initialize() {
333 logger.info("init rest health check adapter!!!!!");