1adb11243cdc2ac30fbf3a20d7fcd13a1aeeeece
[appc.git] / appc-adapters / appc-rest-healthcheck-adapter / appc-rest-healthcheck-adapter-bundle / src / main / java / org / openecomp / appc / adapter / restHealthcheck / impl / RestHealthcheckAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.restHealthcheck.impl;
26
27 import java.net.URI;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33 import java.util.Set;
34 import java.util.regex.Pattern;
35
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;
47
48
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;
66
67 import org.glassfish.grizzly.http.util.HttpStatus;
68 import org.slf4j.MDC;
69
70 import java.net.InetAddress;
71 import java.util.Locale;
72 import java.util.UUID;
73 import static com.att.eelf.configuration.Configuration.*;
74
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;
83
84 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
85
86         /**
87          * The constant used to define the adapter name in the mapped diagnostic
88          * context
89          */
90
91
92         @SuppressWarnings("nls")
93         public static final String MDC_ADAPTER = "adapter";
94
95         /**
96          * The constant used to define the service name in the mapped diagnostic
97          * context
98          */
99         @SuppressWarnings("nls")
100         public static final String MDC_SERVICE = "service";
101
102         /**
103          * The constant for the status code for a failed outcome
104          */
105         @SuppressWarnings("nls")
106         public static final String OUTCOME_FAILURE = "failure";
107
108         /**
109          * The constant for the status code for a successful outcome
110          */
111         @SuppressWarnings("nls")
112         public static final String OUTCOME_SUCCESS = "success";
113
114         /**
115          * A constant for the property token "provider" used in the structured
116          * property specifications
117          */
118         @SuppressWarnings("nls")
119         public static final String PROPERTY_PROVIDER = "provider";
120
121         /**
122          * A constant for the property token "identity" used in the structured
123          * property specifications
124          */
125         @SuppressWarnings("nls")
126         public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
127
128         /**
129          * A constant for the property token "name" used in the structured property
130          * specifications
131          */
132         @SuppressWarnings("nls")
133         public static final String PROPERTY_PROVIDER_NAME = "name";
134
135         /**
136          * A constant for the property token "tenant" used in the structured
137          * property specifications
138          */
139         @SuppressWarnings("nls")
140         public static final String PROPERTY_PROVIDER_TENANT = "tenant";
141
142         /**
143          * A constant for the property token "tenant name" used in the structured
144          * property specifications
145          */
146         @SuppressWarnings("nls")
147         public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
148
149         /**
150          * A constant for the property token "password" used in the structured
151          * property specifications
152          */
153         @SuppressWarnings("nls")
154         public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
155
156         /**
157          * A constant for the property token "userid" used in the structured
158          * property specifications
159          */
160         @SuppressWarnings("nls")
161         public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
162
163         /**
164          * A constant for the property token "type" used in the structured property
165          * specifications
166          */
167         @SuppressWarnings("nls")
168         public static final String PROPERTY_PROVIDER_TYPE = "type";
169
170
171         @SuppressWarnings("nls")
172         public static final String PING_SERVICE = "pingServer";
173
174         /**
175          * The logger to be used
176          */
177         private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
178
179         /**
180          * The constant for a left parenthesis
181          */
182         private static final char LPAREN = '(';
183
184         /**
185          * The constant for a new line control code
186          */
187         private static final char NL = '\n';
188
189         /**
190          * The constant for a single quote
191          */
192         private static final char QUOTE = '\'';
193
194         /**
195          * The constant for a right parenthesis
196          */
197         private static final char RPAREN = ')';
198
199         /**
200          * The constant for a space
201          */
202         private static final char SPACE = ' ';
203
204         /**
205          * A reference to the adapter configuration object.
206          */
207         private Configuration configuration;
208
209         /**
210          * A cache of providers that are predefined.
211          */
212         // private Map<String /* provider name */, ProviderCache> providerCache;
213
214         /**
215          * This default constructor is used as a work around because the activator
216          * wasnt getting called
217          */
218         /**
219          * A cache of providers that are predefined.
220          */
221         // private Map<String /* provider name */, ProviderCache> providerCache;
222
223         /**
224          * This default constructor is used as a work around because the activator
225          * wasnt getting called
226          */
227         public RestHealthcheckAdapterImpl() {
228                 initialize();
229
230         }
231
232
233         public RestHealthcheckAdapterImpl(boolean initialize) {
234
235                 if (initialize) {
236                         initialize();
237
238                 }
239         }
240
241
242         public RestHealthcheckAdapterImpl(Properties props) {
243                 initialize();
244
245         }
246
247
248         @Override
249         public String getAdapterName() {
250                 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
251         }
252
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);
259                 rc.isAlive();
260
261                 try {
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)
270                         {
271                                 doSuccess(rc,responseCode,responseOutput);
272                         }
273                         else
274                         {
275                                 doHealthCheckFailure(rc,responseCode,responseOutput);
276                         }
277                 } catch (Exception ex) {
278                         doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
279                 }
280         }
281
282
283
284
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"));
291                 }
292
293                 String status;
294                 try {
295                         status = Integer.toString(code.getStatusCode());
296                 } catch (Exception e) {
297                         status = "500";
298                 }
299                 svcLogic.setStatus(OUTCOME_FAILURE);
300                 svcLogic.setAttribute("healthcheck.result.code", "200");
301                 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
302         }
303
304
305         /**
306          * @param rc
307          *            The request context that manages the state and recovery of the
308          *            request for the life of its processing.
309          */
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);
316
317         }
318
319
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);
326
327         }
328
329
330         /**
331          * initialize the provider adapter by building the context cache
332          */
333         private void initialize() {
334
335
336                 logger.info("init rest health check adapter!!!!!");
337         }
338
339 }