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