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;
36 import org.openecomp.appc.Constants;
37 import org.openecomp.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
38 import org.openecomp.appc.configuration.Configuration;
39 import org.openecomp.appc.configuration.ConfigurationFactory;
40 import org.openecomp.appc.exceptions.APPCException;
41 import org.openecomp.appc.exceptions.UnknownProviderException;
42 import org.openecomp.appc.i18n.Msg;
43 import org.openecomp.appc.pool.Pool;
44 import org.openecomp.appc.pool.PoolExtensionException;
45 import org.openecomp.appc.util.StructuredPropertyHelper;
46 import org.openecomp.appc.util.StructuredPropertyHelper.Node;
49 import com.att.cdp.exceptions.ContextConnectionException;
50 import com.att.cdp.exceptions.ResourceNotFoundException;
51 import com.att.cdp.exceptions.TimeoutException;
52 import com.att.cdp.exceptions.ZoneException;
53 import com.att.cdp.pal.util.StringHelper;
54 import com.att.cdp.zones.ComputeService;
55 import com.att.cdp.zones.Context;
56 import com.att.cdp.zones.ImageService;
57 import com.att.cdp.zones.Provider;
58 import com.att.cdp.zones.model.Image;
59 import com.att.cdp.zones.model.Server;
60 import com.att.cdp.zones.model.Server.Status;
61 import com.att.cdp.zones.model.ServerBootSource;
62 import com.att.eelf.configuration.EELFLogger;
63 import com.att.eelf.configuration.EELFManager;
64 import com.att.eelf.i18n.EELFResourceManager;
65 import org.openecomp.sdnc.sli.SvcLogicContext;
67 import org.glassfish.grizzly.http.util.HttpStatus;
70 import java.net.InetAddress;
71 import java.util.Locale;
72 import java.util.UUID;
73 import static com.att.eelf.configuration.Configuration.*;
75 import org.apache.http.*;
76 import org.apache.http.client.*;
77 import org.apache.http.client.methods.*;
78 import org.apache.http.impl.client.*;
79 import org.apache.http.util.EntityUtils;
80 import java.io.IOException;
81 import org.apache.http.entity.StringEntity;
82 import java.net.InetAddress;
84 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
87 * The constant used to define the adapter name in the mapped diagnostic
92 @SuppressWarnings("nls")
93 public static final String MDC_ADAPTER = "adapter";
96 * The constant used to define the service name in the mapped diagnostic
99 @SuppressWarnings("nls")
100 public static final String MDC_SERVICE = "service";
103 * The constant for the status code for a failed outcome
105 @SuppressWarnings("nls")
106 public static final String OUTCOME_FAILURE = "failure";
109 * The constant for the status code for a successful outcome
111 @SuppressWarnings("nls")
112 public static final String OUTCOME_SUCCESS = "success";
115 * A constant for the property token "provider" used in the structured
116 * property specifications
118 @SuppressWarnings("nls")
119 public static final String PROPERTY_PROVIDER = "provider";
122 * A constant for the property token "identity" used in the structured
123 * property specifications
125 @SuppressWarnings("nls")
126 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
129 * A constant for the property token "name" used in the structured property
132 @SuppressWarnings("nls")
133 public static final String PROPERTY_PROVIDER_NAME = "name";
136 * A constant for the property token "tenant" used in the structured
137 * property specifications
139 @SuppressWarnings("nls")
140 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
143 * A constant for the property token "tenant name" used in the structured
144 * property specifications
146 @SuppressWarnings("nls")
147 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
150 * A constant for the property token "password" used in the structured
151 * property specifications
153 @SuppressWarnings("nls")
154 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
157 * A constant for the property token "userid" used in the structured
158 * property specifications
160 @SuppressWarnings("nls")
161 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
164 * A constant for the property token "type" used in the structured property
167 @SuppressWarnings("nls")
168 public static final String PROPERTY_PROVIDER_TYPE = "type";
171 @SuppressWarnings("nls")
172 public static final String PING_SERVICE = "pingServer";
175 * The logger to be used
177 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
180 * The constant for a left parenthesis
182 private static final char LPAREN = '(';
185 * The constant for a new line control code
187 private static final char NL = '\n';
190 * The constant for a single quote
192 private static final char QUOTE = '\'';
195 * The constant for a right parenthesis
197 private static final char RPAREN = ')';
200 * The constant for a space
202 private static final char SPACE = ' ';
205 * A reference to the adapter configuration object.
207 private Configuration configuration;
210 * A cache of providers that are predefined.
212 // private Map<String /* provider name */, ProviderCache> providerCache;
215 * This default constructor is used as a work around because the activator
216 * wasnt getting called
219 * A cache of providers that are predefined.
221 // private Map<String /* provider name */, ProviderCache> providerCache;
224 * This default constructor is used as a work around because the activator
225 * wasnt getting called
227 public RestHealthcheckAdapterImpl() {
233 public RestHealthcheckAdapterImpl(boolean initialize) {
242 public RestHealthcheckAdapterImpl(Properties props) {
249 public String getAdapterName() {
250 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
253 public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
254 logger.info("VNF rest health check");
255 String uri=params.get("VNF.URI");
256 String endPoint=params.get("VNF.endpoint");
257 String tUrl=uri+"/"+endPoint;
258 RequestContext rc = new RequestContext(ctx);
262 HttpGet httpGet = new HttpGet(tUrl);
263 HttpClient httpClient = HttpClients.createDefault();
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!!!!!");