f2d3099965a9087fb928cf68b6c5988ae01bbb7f
[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 for the status code for a failed outcome
61      */
62     @SuppressWarnings("nls")
63     public static final String OUTCOME_FAILURE = "failure";
64     /**
65      * The logger to be used
66      */
67     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestHealthcheckAdapterImpl.class);
68     /**
69      * A reference to the adapter configuration object.
70      */
71     private Configuration configuration;
72     /**
73      * This default constructor is used as a work around because the activator
74      * wasnt getting called
75      */
76     public RestHealthcheckAdapterImpl() {
77         initialize();
78     }
79     @Override
80     public String getAdapterName() {
81         return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
82     }
83     public void checkHealth(Map<String, String> params, SvcLogicContext ctx) {
84         logger.info("VNF rest health check");
85         String uri=params.get("VNF.URI");
86         String endPoint=params.get("VNF.endpoint");
87         String tUrl=uri+"/"+endPoint;
88         RequestContext rc = new RequestContext(ctx);
89         rc.isAlive();
90         try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
91             HttpGet httpGet = new HttpGet(tUrl);
92             HttpResponse response ;
93             response = httpClient.execute(httpGet);
94             int responseCode=response.getStatusLine().getStatusCode();
95             HttpEntity entity = response.getEntity();
96             String responseOutput=EntityUtils.toString(entity);
97             if(responseCode==200)
98             {
99                 doSuccess(rc,responseCode,responseOutput);
100             }
101             else
102             {
103                 doHealthCheckFailure(rc,responseCode,responseOutput);
104             }
105         } catch (Exception ex) {
106             doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
107         }
108     }
109     @SuppressWarnings("static-method")
110     private void doFailure(RequestContext rc, HttpStatus code, String message) {
111         SvcLogicContext svcLogic = rc.getSvcLogicContext();
112         String msg = (message == null) ? code.getReasonPhrase() : message;
113         if (msg.contains("\n")) {
114             msg = msg.substring(msg.indexOf("\n"));
115         }
116         String status;
117         try {
118             status = Integer.toString(code.getStatusCode());
119         } catch (Exception e) {
120             status = "500";
121         }
122         svcLogic.setStatus(OUTCOME_FAILURE);
123         svcLogic.setAttribute("healthcheck.result.code", "200");
124         svcLogic.setAttribute("healthcheck.result.message", status+" "+msg);
125     }
126     /**
127      * @param rc
128      *            The request context that manages the state and recovery of the
129      *            request for the life of its processing.
130      */
131     @SuppressWarnings("static-method")
132     private void doHealthCheckFailure(RequestContext rc, int code, String message) {
133         SvcLogicContext svcLogic = rc.getSvcLogicContext();
134         String msg = Integer.toString(code)+" "+message;
135         svcLogic.setAttribute("healthcheck.result.code", "200");
136         svcLogic.setAttribute("healthcheck.result.message", msg);
137     }
138     @SuppressWarnings("static-method")
139     private void doSuccess(RequestContext rc, int code, String message) {
140         SvcLogicContext svcLogic = rc.getSvcLogicContext();
141         String msg = Integer.toString(code)+" "+message;
142         svcLogic.setAttribute("healthcheck.result.code", "400");
143         svcLogic.setAttribute("healthcheck.result.message", msg);
144     }
145     /**
146      * initialize the provider adapter by building the context cache
147      */
148     private void initialize() {
149         logger.info("init rest health check adapter!!!!!");
150     }
151
152
153 }