Second part of onap rename
[appc.git] / appc-adapters / appc-rest-healthcheck-adapter / appc-rest-healthcheck-adapter-bundle / src / main / java / org / onap / 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.onap.appc.adapter.restHealthcheck.impl;
26
27 import java.util.Map;
28 import java.util.Properties;
29 import org.apache.http.impl.client.CloseableHttpClient;
30
31 import org.onap.appc.Constants;
32 import org.onap.appc.adapter.restHealthcheck.RestHealthcheckAdapter;
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.pool.PoolExtensionException;
35 import org.onap.appc.util.StructuredPropertyHelper;
36
37
38 import com.att.cdp.zones.ImageService;
39 import com.att.cdp.zones.Provider;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.att.eelf.i18n.EELFResourceManager;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44
45 import org.glassfish.grizzly.http.util.HttpStatus;
46
47 import static com.att.eelf.configuration.Configuration.*;
48
49 import org.apache.http.*;
50 import org.apache.http.client.*;
51 import org.apache.http.client.methods.*;
52 import org.apache.http.impl.client.*;
53 import org.apache.http.util.EntityUtils;
54 import java.io.IOException;
55 import java.net.InetAddress;
56
57 public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter {
58
59         /**
60          * The constant used to define the adapter name in the mapped diagnostic
61          * context
62          */
63
64
65         @SuppressWarnings("nls")
66         public static final String MDC_ADAPTER = "adapter";
67
68         /**
69          * The constant used to define the service name in the mapped diagnostic
70          * context
71          */
72         @SuppressWarnings("nls")
73         public static final String MDC_SERVICE = "service";
74
75         /**
76          * The constant for the status code for a failed outcome
77          */
78         @SuppressWarnings("nls")
79         public static final String OUTCOME_FAILURE = "failure";
80
81         /**
82          * The constant for the status code for a successful outcome
83          */
84         @SuppressWarnings("nls")
85         public static final String OUTCOME_SUCCESS = "success";
86
87         /**
88          * A constant for the property token "provider" used in the structured
89          * property specifications
90          */
91         @SuppressWarnings("nls")
92         public static final String PROPERTY_PROVIDER = "provider";
93
94         /**
95          * A constant for the property token "identity" used in the structured
96          * property specifications
97          */
98         @SuppressWarnings("nls")
99         public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
100
101         /**
102          * A constant for the property token "name" used in the structured property
103          * specifications
104          */
105         @SuppressWarnings("nls")
106         public static final String PROPERTY_PROVIDER_NAME = "name";
107
108         /**
109          * A constant for the property token "tenant" used in the structured
110          * property specifications
111          */
112         @SuppressWarnings("nls")
113         public static final String PROPERTY_PROVIDER_TENANT = "tenant";
114
115         /**
116          * A constant for the property token "tenant name" used in the structured
117          * property specifications
118          */
119         @SuppressWarnings("nls")
120         public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
121
122         /**
123          * A constant for the property token "password" used in the structured
124          * property specifications
125          */
126         @SuppressWarnings("nls")
127         public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
128
129         /**
130          * A constant for the property token "userid" used in the structured
131          * property specifications
132          */
133         @SuppressWarnings("nls")
134         public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
135
136         /**
137          * A constant for the property token "type" used in the structured property
138          * specifications
139          */
140         @SuppressWarnings("nls")
141         public static final String PROPERTY_PROVIDER_TYPE = "type";
142
143
144         @SuppressWarnings("nls")
145         public static final String PING_SERVICE = "pingServer";
146
147         /**
148          * The logger to be used
149          */
150         private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
151
152         /**
153          * The constant for a left parenthesis
154          */
155         private static final char LPAREN = '(';
156
157         /**
158          * The constant for a new line control code
159          */
160         private static final char NL = '\n';
161
162         /**
163          * The constant for a single quote
164          */
165         private static final char QUOTE = '\'';
166
167         /**
168          * The constant for a right parenthesis
169          */
170         private static final char RPAREN = ')';
171
172         /**
173          * The constant for a space
174          */
175         private static final char SPACE = ' ';
176
177         /**
178          * A reference to the adapter configuration object.
179          */
180         private Configuration configuration;
181
182         /**
183          * A cache of providers that are predefined.
184          */
185         // private Map<String /* provider name */, ProviderCache> providerCache;
186
187         /**
188          * This default constructor is used as a work around because the activator
189          * wasnt getting called
190          */
191         /**
192          * A cache of providers that are predefined.
193          */
194         // private Map<String /* provider name */, ProviderCache> providerCache;
195
196         /**
197          * This default constructor is used as a work around because the activator
198          * wasnt getting called
199          */
200         public RestHealthcheckAdapterImpl() {
201                 initialize();
202
203         }
204
205
206         public RestHealthcheckAdapterImpl(boolean initialize) {
207
208                 if (initialize) {
209                         initialize();
210
211                 }
212         }
213
214
215         public RestHealthcheckAdapterImpl(Properties props) {
216                 initialize();
217
218         }
219
220
221         @Override
222         public String getAdapterName() {
223                 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
224         }
225
226         public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
227                 logger.info("VNF rest health check");
228                 String uri=params.get("VNF.URI");
229                 String endPoint=params.get("VNF.endpoint");
230                 String tUrl=uri+"/"+endPoint;
231                 RequestContext rc = new RequestContext(ctx);
232                 rc.isAlive();
233
234                 try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
235                         HttpGet httpGet = new HttpGet(tUrl);
236                         HttpResponse response = null;
237                         response = httpClient.execute(httpGet);
238                         int responseCode=response.getStatusLine().getStatusCode();
239                         HttpEntity entity = response.getEntity();
240                         String responseOutput=EntityUtils.toString(entity);
241                         if(responseCode==200)
242                         {
243                                 doSuccess(rc,responseCode,responseOutput);
244                         }
245                         else
246                         {
247                                 doHealthCheckFailure(rc,responseCode,responseOutput);
248                         }
249                 } catch (Exception ex) {
250                         doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
251                 }
252         }
253
254
255
256
257         @SuppressWarnings("static-method")
258         private void doFailure(RequestContext rc, HttpStatus code, String message) {
259                 SvcLogicContext svcLogic = rc.getSvcLogicContext();
260                 String msg = (message == null) ? code.getReasonPhrase() : message;
261                 if (msg.contains("\n")) {
262                         msg = msg.substring(msg.indexOf("\n"));
263                 }
264
265                 String status;
266                 try {
267                         status = Integer.toString(code.getStatusCode());
268                 } catch (Exception e) {
269                         status = "500";
270                 }
271                 svcLogic.setStatus(OUTCOME_FAILURE);
272                 svcLogic.setAttribute("healthcheck.result.code", "200");
273                 svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
274         }
275
276
277         /**
278          * @param rc
279          *            The request context that manages the state and recovery of the
280          *            request for the life of its processing.
281          */
282         @SuppressWarnings("static-method")
283         private void doHealthCheckFailure(RequestContext rc, int code, String message) {
284                 SvcLogicContext svcLogic = rc.getSvcLogicContext();
285                 String msg = Integer.toString(code)+" "+message;
286                 svcLogic.setAttribute("healthcheck.result.code", "200");
287                 svcLogic.setAttribute("healthcheck.result.message", msg);
288
289         }
290
291
292         @SuppressWarnings("static-method")
293         private void doSuccess(RequestContext rc, int code, String message) {
294                 SvcLogicContext svcLogic = rc.getSvcLogicContext();
295                 String msg = Integer.toString(code)+" "+message;
296                 svcLogic.setAttribute("healthcheck.result.code", "400");
297                 svcLogic.setAttribute("healthcheck.result.message", msg);
298
299         }
300
301
302         /**
303          * initialize the provider adapter by building the context cache
304          */
305         private void initialize() {
306
307
308                 logger.info("init rest health check adapter!!!!!");
309         }
310
311 }