6cf2eaf397740663268e7494846f33f7fc52a1dd
[appc.git] /
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 import org.apache.http.impl.client.CloseableHttpClient;
36
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;
48
49
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;
67
68 import org.glassfish.grizzly.http.util.HttpStatus;
69 import org.slf4j.MDC;
70
71 import java.net.InetAddress;
72 import java.util.Locale;
73 import java.util.UUID;
74 import static com.att.eelf.configuration.Configuration.*;
75
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;
84
85 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
86
87         /**
88          * The constant used to define the adapter name in the mapped diagnostic
89          * context
90          */
91
92
93         @SuppressWarnings("nls")
94         public static final String MDC_ADAPTER = "adapter";
95
96         /**
97          * The constant used to define the service name in the mapped diagnostic
98          * context
99          */
100         @SuppressWarnings("nls")
101         public static final String MDC_SERVICE = "service";
102
103         /**
104          * The constant for the status code for a failed outcome
105          */
106         @SuppressWarnings("nls")
107         public static final String OUTCOME_FAILURE = "failure";
108
109         /**
110          * The constant for the status code for a successful outcome
111          */
112         @SuppressWarnings("nls")
113         public static final String OUTCOME_SUCCESS = "success";
114
115         /**
116          * A constant for the property token "provider" used in the structured
117          * property specifications
118          */
119         @SuppressWarnings("nls")
120         public static final String PROPERTY_PROVIDER = "provider";
121
122         /**
123          * A constant for the property token "identity" used in the structured
124          * property specifications
125          */
126         @SuppressWarnings("nls")
127         public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
128
129         /**
130          * A constant for the property token "name" used in the structured property
131          * specifications
132          */
133         @SuppressWarnings("nls")
134         public static final String PROPERTY_PROVIDER_NAME = "name";
135
136         /**
137          * A constant for the property token "tenant" used in the structured
138          * property specifications
139          */
140         @SuppressWarnings("nls")
141         public static final String PROPERTY_PROVIDER_TENANT = "tenant";
142
143         /**
144          * A constant for the property token "tenant name" used in the structured
145          * property specifications
146          */
147         @SuppressWarnings("nls")
148         public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
149
150         /**
151          * A constant for the property token "password" used in the structured
152          * property specifications
153          */
154         @SuppressWarnings("nls")
155         public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
156
157         /**
158          * A constant for the property token "userid" used in the structured
159          * property specifications
160          */
161         @SuppressWarnings("nls")
162         public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
163
164         /**
165          * A constant for the property token "type" used in the structured property
166          * specifications
167          */
168         @SuppressWarnings("nls")
169         public static final String PROPERTY_PROVIDER_TYPE = "type";
170
171
172         @SuppressWarnings("nls")
173         public static final String PING_SERVICE = "pingServer";
174
175         /**
176          * The logger to be used
177          */
178         private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
179
180         /**
181          * The constant for a left parenthesis
182          */
183         private static final char LPAREN = '(';
184
185         /**
186          * The constant for a new line control code
187          */
188         private static final char NL = '\n';
189
190         /**
191          * The constant for a single quote
192          */
193         private static final char QUOTE = '\'';
194
195         /**
196          * The constant for a right parenthesis
197          */
198         private static final char RPAREN = ')';
199
200         /**
201          * The constant for a space
202          */
203         private static final char SPACE = ' ';
204
205         /**
206          * A reference to the adapter configuration object.
207          */
208         private Configuration configuration;
209
210         /**
211          * A cache of providers that are predefined.
212          */
213         // private Map<String /* provider name */, ProviderCache> providerCache;
214
215         /**
216          * This default constructor is used as a work around because the activator
217          * wasnt getting called
218          */
219         /**
220          * A cache of providers that are predefined.
221          */
222         // private Map<String /* provider name */, ProviderCache> providerCache;
223
224         /**
225          * This default constructor is used as a work around because the activator
226          * wasnt getting called
227          */
228         public RestHealthcheckAdapterImpl() {
229                 initialize();
230
231         }
232
233
234         public RestHealthcheckAdapterImpl(boolean initialize) {
235
236                 if (initialize) {
237                         initialize();
238
239                 }
240         }
241
242
243         public RestHealthcheckAdapterImpl(Properties props) {
244                 initialize();
245
246         }
247
248
249         @Override
250         public String getAdapterName() {
251                 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
252         }
253
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);
260                 rc.isAlive();
261
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)
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 }