2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.restHealthcheck.impl;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Properties;
32 import java.util.regex.Pattern;
34 import org.openecomp.appc.Constants;
35 import org.openecomp.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
36 import org.openecomp.appc.configuration.Configuration;
37 import org.openecomp.appc.configuration.ConfigurationFactory;
38 import org.openecomp.appc.exceptions.APPCException;
39 import org.openecomp.appc.exceptions.UnknownProviderException;
40 import org.openecomp.appc.i18n.Msg;
41 import org.openecomp.appc.pool.Pool;
42 import org.openecomp.appc.pool.PoolExtensionException;
43 import org.openecomp.appc.util.StructuredPropertyHelper;
44 import org.openecomp.appc.util.StructuredPropertyHelper.Node;
47 import com.att.cdp.exceptions.ContextConnectionException;
48 import com.att.cdp.exceptions.ResourceNotFoundException;
49 import com.att.cdp.exceptions.TimeoutException;
50 import com.att.cdp.exceptions.ZoneException;
51 import com.att.cdp.pal.util.StringHelper;
52 import com.att.cdp.zones.ComputeService;
53 import com.att.cdp.zones.Context;
54 import com.att.cdp.zones.ImageService;
55 import com.att.cdp.zones.Provider;
56 import com.att.cdp.zones.model.Image;
57 import com.att.cdp.zones.model.Server;
58 import com.att.cdp.zones.model.Server.Status;
59 import com.att.cdp.zones.model.ServerBootSource;
60 import com.att.eelf.configuration.EELFLogger;
61 import com.att.eelf.configuration.EELFManager;
62 import com.att.eelf.i18n.EELFResourceManager;
63 import org.openecomp.sdnc.sli.SvcLogicContext;
65 import org.glassfish.grizzly.http.util.HttpStatus;
68 import java.net.InetAddress;
69 import java.util.Locale;
70 import java.util.UUID;
71 import static com.att.eelf.configuration.Configuration.*;
73 import org.apache.http.*;
74 import org.apache.http.client.*;
75 import org.apache.http.client.methods.*;
76 import org.apache.http.impl.client.*;
77 import org.apache.http.util.EntityUtils;
78 import java.io.IOException;
79 import org.apache.http.entity.StringEntity;
80 import java.net.InetAddress;
82 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
85 * The constant used to define the adapter name in the mapped diagnostic
90 @SuppressWarnings("nls")
91 public static final String MDC_ADAPTER = "adapter";
94 * The constant used to define the service name in the mapped diagnostic
97 @SuppressWarnings("nls")
98 public static final String MDC_SERVICE = "service";
101 * The constant for the status code for a failed outcome
103 @SuppressWarnings("nls")
104 public static final String OUTCOME_FAILURE = "failure";
107 * The constant for the status code for a successful outcome
109 @SuppressWarnings("nls")
110 public static final String OUTCOME_SUCCESS = "success";
113 * A constant for the property token "provider" used in the structured
114 * property specifications
116 @SuppressWarnings("nls")
117 public static final String PROPERTY_PROVIDER = "provider";
120 * A constant for the property token "identity" used in the structured
121 * property specifications
123 @SuppressWarnings("nls")
124 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
127 * A constant for the property token "name" used in the structured property
130 @SuppressWarnings("nls")
131 public static final String PROPERTY_PROVIDER_NAME = "name";
134 * A constant for the property token "tenant" used in the structured
135 * property specifications
137 @SuppressWarnings("nls")
138 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
141 * A constant for the property token "tenant name" used in the structured
142 * property specifications
144 @SuppressWarnings("nls")
145 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
148 * A constant for the property token "password" used in the structured
149 * property specifications
151 @SuppressWarnings("nls")
152 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
155 * A constant for the property token "userid" used in the structured
156 * property specifications
158 @SuppressWarnings("nls")
159 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
162 * A constant for the property token "type" used in the structured property
165 @SuppressWarnings("nls")
166 public static final String PROPERTY_PROVIDER_TYPE = "type";
169 @SuppressWarnings("nls")
170 public static final String PING_SERVICE = "pingServer";
173 * The logger to be used
175 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
178 * The constant for a left parenthesis
180 private static final char LPAREN = '(';
183 * The constant for a new line control code
185 private static final char NL = '\n';
188 * The constant for a single quote
190 private static final char QUOTE = '\'';
193 * The constant for a right parenthesis
195 private static final char RPAREN = ')';
198 * The constant for a space
200 private static final char SPACE = ' ';
203 * A reference to the adapter configuration object.
205 private Configuration configuration;
208 * A cache of providers that are predefined.
210 // private Map<String /* provider name */, ProviderCache> providerCache;
213 * This default constructor is used as a work around because the activator
214 * wasnt getting called
217 * A cache of providers that are predefined.
219 // private Map<String /* provider name */, ProviderCache> providerCache;
222 * This default constructor is used as a work around because the activator
223 * wasnt getting called
225 public RestHealthcheckAdapterImpl() {
231 public RestHealthcheckAdapterImpl(boolean initialize) {
240 public RestHealthcheckAdapterImpl(Properties props) {
247 public String getAdapterName() {
248 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
251 public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
252 logger.info("VNF rest health check");
253 String uri=params.get("VNF.URI");
254 String endPoint=params.get("VNF.endpoint");
255 String tUrl=uri+"/"+endPoint;
256 RequestContext rc = new RequestContext(ctx);
260 HttpGet httpGet = new HttpGet(tUrl);
261 HttpClient httpClient = HttpClients.createDefault();
262 HttpResponse response = null;
263 response = httpClient.execute(httpGet);
264 int responseCode=response.getStatusLine().getStatusCode();
265 HttpEntity entity = response.getEntity();
266 String responseOutput=EntityUtils.toString(entity);
267 if(responseCode==200)
269 doSuccess(rc,responseCode,responseOutput);
273 doHealthCheckFailure(rc,responseCode,responseOutput);
275 } catch (Exception ex) {
276 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
283 @SuppressWarnings("static-method")
284 private void doFailure(RequestContext rc, HttpStatus code, String message) {
285 SvcLogicContext svcLogic = rc.getSvcLogicContext();
286 String msg = (message == null) ? code.getReasonPhrase() : message;
287 if (msg.contains("\n")) {
288 msg = msg.substring(msg.indexOf("\n"));
293 status = Integer.toString(code.getStatusCode());
294 } catch (Exception e) {
297 svcLogic.setStatus(OUTCOME_FAILURE);
298 svcLogic.setAttribute("healthcheck.result.code", "200");
299 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
305 * The request context that manages the state and recovery of the
306 * request for the life of its processing.
308 @SuppressWarnings("static-method")
309 private void doHealthCheckFailure(RequestContext rc, int code, String message) {
310 SvcLogicContext svcLogic = rc.getSvcLogicContext();
311 String msg = Integer.toString(code)+" "+message;
312 svcLogic.setAttribute("healthcheck.result.code", "200");
313 svcLogic.setAttribute("healthcheck.result.message", msg);
318 @SuppressWarnings("static-method")
319 private void doSuccess(RequestContext rc, int code, String message) {
320 SvcLogicContext svcLogic = rc.getSvcLogicContext();
321 String msg = Integer.toString(code)+" "+message;
322 svcLogic.setAttribute("healthcheck.result.code", "400");
323 svcLogic.setAttribute("healthcheck.result.message", msg);
329 * initialize the provider adapter by building the context cache
331 private void initialize() {
334 logger.info("init rest health check adapter!!!!!");