summaryrefslogtreecommitdiffstats
path: root/src/main/org/onap/ecomp/main/consul/ConsulService.java
blob: 69546705b6ffa30f74ad358f89079368626f50c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*******************************************************************************
 * =============LICENSE_START=========================================================
 *
 * =================================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *******************************************************************************/
package org.onap.ecomp.main.consul;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.ecomp.main.APIHConfig;
import org.onap.ecomp.persistence.APIHDBSource;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;


@Path("/healthservices")
public class ConsulService {

	final static EELFLogger logger = EELFManager.getInstance().getLogger(ConsulService.class);
	
	ConsulHelp consulHelp;

	String errorLine = "Bad Request";

	public ConsulService() {
		consulHelp = new ConsulHelp();

	}

	/**
	 * Return the help for all the available API 
	 * 
	 * @param id
	 * @param authString
	 * @param userid
	 * @return JSONObject
	 */	
	@GET
	@Path("/help")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getHelp(@QueryParam("id") String id,@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid) {


		logger.info("Received get help API Request");
		String result = consulHelp.getHelp();
		logger.info("Handled get help API Request");
		return Response.status(200).entity(result)
				.build();
	}
	
	
	@GET
	@Path("/services/{service_name}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getServiceHealth(@Context HttpServletRequest request,@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid,@PathParam("service_name") String service_name) {

		String url = "";
		logger.info("Health request for service with service name  = "
				+ service_name);

		// validate the user and the application for GET request
		if (!APIHConfig.getInstance().validateUser(authString, userid, "GET")) {
			return Response.status(401).entity("Unauthorized").build();
		}

		url = "/health/checks/" + service_name;
		if (request.getQueryString() != null)
			url = "/health/checks/" + service_name + "?"
					+ request.getQueryString();

		JSONObject result = ConsulClient.getInstance().doGET(url);
		logger.info("Handled Health service name API Request");
		return handleResponse(result);

	}
	
	@GET
	@Path("/nodes/{node_name}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getNodeHealth(@Context HttpServletRequest request,@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid,@PathParam("node_name") String node_name) {

		String url = "";
		logger.info("Health request for node with node name  = " + node_name);

		// validate the user and the application for GET request
		if (!APIHConfig.getInstance().validateUser(authString, userid, "GET")) {
			return Response.status(401).entity("Unauthorized").build();
		}

		url = "/health/node/" + node_name;
		if (request.getQueryString() != null)
			url = "/health/node/" + node_name + "?" + request.getQueryString();

		JSONObject result = ConsulClient.getInstance().doGET(url);
		logger.info("Handled Health node name API Request");
		return handleResponse(result);

	}
	
	@GET
	@Path("/nodes")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getNodes(@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid,@Context HttpServletRequest request) {

		String url = "";
		logger.info("Health request for all the nodes");
		// validate the user and the application for GET request

		if (!APIHConfig.getInstance().validateUser(authString, userid, "GET")) {
			return Response.status(401).entity("Unauthorized").build();
		}

		url = "/catalog/nodes";
		if (request.getQueryString() != null)
			url = "/catalog/nodes?" + request.getQueryString();

		JSONObject result = ConsulClient.getInstance().doGET(url);
		logger.info("Handled Health for all node  API Request");
		return handleResponse(result);

	}
	
	@GET
	@Path("/services")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getServices(@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid,@Context HttpServletRequest request) {

		String url = "";
		logger.info("Health request for all the services");
		// validate the user and the application for GET request

		if (!APIHConfig.getInstance().validateUser(authString, userid, "GET")) {
			return Response.status(401).entity("Unauthorized").build();
		}

		url = "/catalog/services";
		if (request.getQueryString() != null)
			url = "/catalog/services?" + request.getQueryString();

		JSONObject result = ConsulClient.getInstance().doGET(url);
		logger.info("Handled Health for all services  API Request");
		return handleResponse(result);

	}
	
	@GET
	@Path("/datacenters")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getDatacenters(@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid) {

		String url = "";
		logger.info("Health request for datacenters");
		// validate the user and the application for GET request

		if (!APIHConfig.getInstance().validateUser(authString, userid, "GET")) {
			return Response.status(401).entity("Unauthorized").build();
		}

		url = "/catalog/datacenters";

		JSONObject result = ConsulClient.getInstance().doGET(url);
		logger.info("Handled Health for datacenters  API Request");
		return handleResponse(result);

	}
	
	@GET
	@Path("/svchist/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getHistoricalData(@PathParam("id") String id,@Context HttpServletRequest request,@HeaderParam("authorization") String authString,@HeaderParam("userid") String userid) {

		if(!APIHConfig.getInstance().validateUser(authString,userid,"PUT")){
			return Response.status(401).entity("Unauthorized").build();
		}
		
		String utc_startDate = "";
		String utc_endDate = "";
		JSONArray resultArray = new JSONArray();
		logger.info("Historical data request received for id " + id);

		if (request.getQueryString() == null)
			return Response.status(400).entity("start date and end date is mandatory").build();

		String[] dates = request.getQueryString().split("&");
		String start_date = "";
		String end_date = "";
		if(dates[0].split("=").length  ==2 && dates[0].split("=")[0].equals("start") && !dates[0].split("=")[1].isEmpty()){
				start_date = dates[0].split("=")[1];
				if (dates[1].split("=").length  ==2 && dates[1].split("=")[0].equals("end") && !dates[1].split("=")[1].isEmpty())
					end_date = dates[1].split("=")[1];
				else
					return Response.status(400).entity("start or end tag/value not found").build();
		}
		else
			return Response.status(400).entity("start or end tag/value not found").build();
		
		try {
			
			logger.info("Health Request received  for start date :" + start_date + " and end date as : " + end_date);
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
			Calendar s = javax.xml.bind.DatatypeConverter.parseDateTime(start_date);
			Calendar e = javax.xml.bind.DatatypeConverter.parseDateTime(end_date);
			Date sDate = s.getTime();
			Date eDate = e.getTime();
			
			
			formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
			utc_startDate = formatter.format(sDate);
			utc_endDate = formatter.format(eDate);
			logger.info("Fetching the health date for start date :" + utc_startDate + " and end date as : " + utc_endDate);
			Connection conn = APIHDBSource.getInstance().getConnection();
			Statement cs = conn.createStatement();
			/**
			String sql = "select date,status,output from healthcheck where id='"
					+ id
					+ "' and date between '"
					+ utc_startDate
					+ "' and '"
					+ utc_endDate + "'";
					*/
			String sql = "select date,status,output from healthcheck h, service s where s.id = h.id " 
					+ " and s.name ='"
					+ id
					+ "' and date between '"
					+ utc_startDate
					+ "' and '"
					+ utc_endDate + "'";
			logger.info("Sending query : " + sql);
			ResultSet rs = cs.executeQuery(sql);
			if (!rs.isBeforeFirst()) {
				return Response
						.status(200)
						//.entity("No health Data found for the selected dates or service")
						.entity(resultArray.toString())
						.build();
			} else {
				while (rs.next()) {
					JSONObject tmp = new JSONObject();
					String returnDate = rs.getString(1).substring(0,19)+ "+0000";
				//	System.out.println("DB return " + returnDate);
					formatter.setTimeZone(TimeZone.getTimeZone(s.getTimeZone().getID()));
					Date ret = formatter.parse(returnDate);
					//System.out.println("After parsing " + ret);
					//System.out.println("After formatting " + formatter.format(ret));

					tmp.put("Date", formatter.format(ret));
					tmp.put("Status", rs.getString(2));
					tmp.put("Output", rs.getString(3));
					resultArray.put(tmp);
				}
				logger.info("Handled historical health date request sucessfully with total records: " + resultArray.length());
				return Response.status(200).entity(resultArray.toString())
						.build();
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return Response.status(400)
					.entity("start_date or end_date is not parsable. Please check documentation for correct format")
					.build();
		} catch (Exception e) {
			e.printStackTrace();
			return Response.status(500)
					.entity("Error fetching the health historical data")
					.build();
		} 
	}
	
	@POST
	@Path("/register")
	@Produces(MediaType.APPLICATION_JSON)
	public Response registerService(InputStream inputStream, @HeaderParam("authorization") String authString,@HeaderParam("userid") String userid) {

		logger.info("Register request for a service");
	
		JSONArray services = new JSONArray();
		JSONObject outputJson = new JSONObject();
		
		//validate the user and the application for POST request
			
		if(!APIHConfig.getInstance().validateUser(authString,userid,"PUT")){
			return Response.status(401).entity("Unauthorized").build();
		}
		
		BufferedReader reader =  new BufferedReader(new InputStreamReader(inputStream));
		StringBuilder check = new StringBuilder();
		String inputLine;
		try {
			while ((inputLine = reader.readLine()) != null)
				check.append(inputLine);
			
			JSONObject incoming = new JSONObject(check.toString());
			services = incoming.optJSONArray("services");
			if(services == null)
				return Response.status(400).entity("services[] tag is mandatory").build();
			
			int service_length = services.length();
			if(service_length == 1){
				outputJson = createServiceObject(services.getJSONObject(0));
			}
			else if(service_length > 1){
				for(int i = 0; i< service_length; i++){
					outputJson = createServiceObject(services.getJSONObject(i));
				}
			}
			else{
				return Response.status(400).entity("no service is defined inside the services tag").build();
			}
			
			logger.info("Handled service register Request");
			return handleResponse(outputJson);				
			
		} catch (Exception e) {
			logger.error("some exception" + e.getMessage());
			return Response.status(400).entity("Error parsing the incoming DATA").build();
		}

		

	}
	
	
	private JSONObject createServiceObject(JSONObject serviceObject) throws JSONException {

		String url = "/agent/service/register";
		JSONObject checkObject = new JSONObject();
		JSONArray checks = new JSONArray();
		JSONArray tags = new JSONArray();
		JSONObject outputJson = new JSONObject();
		
		checks = serviceObject.optJSONArray("checks");
		tags = serviceObject.optJSONArray("tags");
		String service_name = serviceObject.optString("name","");
		String service_port = serviceObject.optString("port","");
		String service_address = serviceObject.optString("address","");
		
		if(checks == null || service_port.isEmpty() || service_address.isEmpty() || service_name.isEmpty())
			return new JSONObject("{\"responseCode\":\"400\",\"responseMsg\":\"Required fields : [checks[], port, address, name]\"}");
		
		outputJson.put("Name",service_name);
		outputJson.put("ID",service_name);
		outputJson.put("Port",Integer.parseInt(service_port));
		outputJson.put("Address",service_address);
		outputJson.put("Tags", tags);

		if(checks.length() == 1){
			if(checks.getJSONObject(0).optString("endpoint", "").isEmpty() || checks.getJSONObject(0).optString("interval", "").isEmpty())
				return new JSONObject("{\"responseCode\":\"400\",\"responseMsg\":\"Required fields : [endpoint, interval] in checks\"}");
			
			checkObject.put("HTTP", checks.getJSONObject(0).optString("endpoint", ""));
			checkObject.put("Interval", checks.getJSONObject(0).optString("interval", ""));			
			if(!checks.getJSONObject(0).optString("description", "").isEmpty())
				checkObject.put("Notes", checks.getJSONObject(0).optString("description", ""));
			checkObject.put("ServiceID", service_name);
			
			outputJson.put("Check", checkObject);
		}
		else{
			JSONArray checks_new = new JSONArray();
			for (int i = 0; i < checks.length(); i++) {
				JSONObject o = checks.getJSONObject(i);
				checkObject = new JSONObject();
				if(o.optString("endpoint", "").isEmpty() || o.optString("interval", "").isEmpty())
					return new JSONObject("{\"responseCode\":\"400\",\"responseMsg\":\"Required fields : [endpoint, interval] in checks\"}");
				
				checkObject.put("HTTP", o.optString("endpoint", ""));
				checkObject.put("Interval", o.optString("interval", ""));
				if(!o.optString("description", "").isEmpty())
					checkObject.put("Notes", o.optString("description", ""));
				checkObject.put("ServiceID", service_name);
				checks_new.put(checkObject);
			}
			outputJson.put("Checks", checks_new);
		}
	
		JSONObject result = ConsulClient.getInstance().doPUT(url, outputJson);
		return result;
		
	}

	/**
	 *  De register the service from consul
	 * @param inputStream
	 * @param authString
	 * @param userid
	 * @return
	 */
	@POST
	@Path("/deregister/{serviceID}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deregisterService(@PathParam("serviceID") String serviceId,
			@HeaderParam("authorization") String authString,
			@HeaderParam("userid") String userid) {

		String url = "";
		logger.info("Deregister request for a service");
		// validate the user and the application for POST request

		if (!APIHConfig.getInstance().validateUser(authString, userid, "POST")) {
			return Response.status(401).entity("Unauthorized").build();
		}

		url = "/agent/service/deregister/" + serviceId;
		JSONObject result = ConsulClient.getInstance().doPUT(url, null);
		logger.info("Handled service de register Request");
		return handleResponse(result);

	}
	
	private Response handleResponse(JSONObject result) {

		if(result == null){
			return Response.status(500).entity("Internal Server Error � We had a problem with our server. Try again later.").build();
		}
		int responseCode = result.optInt("responseCode");
		String responseMsg = result.optString("responseMsg", "");
		if(responseCode >= 300)
		{
			logger.info("Response code is: " + responseCode + " and Error msg is :" + responseMsg);
		}
		return Response.status(responseCode).entity(responseMsg).build();
	}
}