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