/
EndpointStateServiceImpl.java
430 lines (368 loc) · 19.1 KB
/
EndpointStateServiceImpl.java
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
/**
*
*/
package org.easysoa.registry.integration;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import org.easysoa.registry.rest.integration.EndpointStateService;
import org.easysoa.registry.rest.integration.ServiceLevelHealth;
import org.easysoa.registry.rest.integration.SlaOrOlaIndicator;
import org.easysoa.registry.rest.integration.SlaOrOlaIndicators;
import org.easysoa.registry.types.Endpoint;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.directory.DirectoryException;
import org.nuxeo.ecm.directory.Session;
import org.nuxeo.ecm.directory.api.DirectoryService;
import org.nuxeo.ecm.directory.sql.filter.SQLBetweenFilter;
import org.nuxeo.ecm.platform.query.nxql.NXQLQueryBuilder;
import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
import org.nuxeo.runtime.api.Framework;
/**
* Endpoint state service implementation
*
* @author jguillemotte
*
*/
@Path("easysoa/endpointStateService")
public class EndpointStateServiceImpl implements EndpointStateService {
// Servlet context
@Context
HttpServletRequest request;
/**
* @see org.easysoa.registry.rest.integration.EndpointStateService#updateSlaOlaIndicators(SlaOrOlaIndicator[])
*/
@Override
public void createSlaOlaIndicators(SlaOrOlaIndicators slaOrOlaIndicators) throws Exception {
DirectoryService directoryService = Framework.getService(DirectoryService.class);
if(slaOrOlaIndicators != null){
// Open a session on slaOrOlaIndicator directory
Session session = directoryService.open("slaOrOlaIndicator");
if(session == null){
throw new Exception("Unable to open a new session on directory 'slaOrOlaIndicator'");
}
try{
// Update each indicator
for(SlaOrOlaIndicator indicator : slaOrOlaIndicators.getSlaOrOlaIndicatorList()){
// Create new indicator
createIndicator(session, indicator);
}
}
catch(Exception ex){
// Return the exception and cancel the transaction
throw new Exception("Failed to update SLA or OLA indicators ", ex);
} finally {
// NB. container manages transaction, so no need to commit or rollback
// (see doc of deprecated session.commit())
session.close();
}
}
}
private void createIndicator(Session session, SlaOrOlaIndicator indicator)
throws DirectoryException, ClientException, Exception {
checkNotNull(indicator.getEndpointId(), "SlaOrOlaIndicator.endpointId");
checkNotNull(indicator.getSlaOrOlaName(), "SlaOrOlaIndicator.slaOrOlaName");
// TODO LATER check that both exist
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("endpointId", indicator.getEndpointId());
properties.put("slaOrOlaName", indicator.getSlaOrOlaName());
properties.put("serviceLeveHealth", indicator.getServiceLevelHealth().toString());
properties.put("serviceLevelViolation", indicator.isServiceLevelViolation());
if(indicator.getTimestamp() != null){
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(indicator.getTimestamp());
properties.put("timestamp", calendar);
}
session.createEntry(properties);
}
private void checkNotNull(String value, String displayName) throws Exception {
if (value == null || value.trim().isEmpty()) {
throw new Exception(displayName + " must not be empty !");
}
}
/**
* @see org.easysoa.registry.rest.integration.EndpointStateService#updateSlaOlaIndicators(SlaOrOlaIndicator[])
*/
@Override
public void updateSlaOlaIndicators(SlaOrOlaIndicators slaOrOlaIndicators) throws Exception {
DirectoryService directoryService = Framework.getService(DirectoryService.class);
if(slaOrOlaIndicators != null){
// Open a session on slaOrOlaIndicator directory
Session session = directoryService.open("slaOrOlaIndicator");
if (session == null){
throw new Exception("Unable to open a new session on directory 'slaOrOlaIndicator'");
}
try {
// Update each indicator
for(SlaOrOlaIndicator indicator : slaOrOlaIndicators.getSlaOrOlaIndicatorList()){
// get the indicator if exists
Map<String, Serializable> parameters = new HashMap<String, Serializable>();
parameters.put("slaOrOlaName", indicator.getSlaOrOlaName());
parameters.put("endpointId", indicator.getEndpointId());
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(indicator.getTimestamp());
parameters.put("timestamp", calendar);
DocumentModelList documentModelList = session.query(parameters);
DocumentModel indicatorModel;
if(documentModelList != null && documentModelList.size() > 0){
// Update existing indicator
indicatorModel = documentModelList.get(0);
indicatorModel.setPropertyValue("serviceLeveHealth", indicator.getServiceLevelHealth().toString());
indicatorModel.setPropertyValue("serviceLevelViolation", String.valueOf(indicator.isServiceLevelViolation()));
session.updateEntry(indicatorModel);
} else {
// Create new indicator
createIndicator(session, indicator);
}
}
} catch (Exception ex) {
// Return the exception and cancel the transaction
throw new Exception("Failed to update SLA or OLA indicators ", ex);
} finally {
// NB. container manages transaction, so no need to commit or rollback
// (see doc of deprecated session.commit())
session.close();
}
}
}
/**
* @see org.easysoa.registry.rest.integration.EndpointStateService#getSlaOrOlaIndicatorsByEnv(String, String, Date,
* Date, int, int)
*/
@Override
public SlaOrOlaIndicators getSlaOrOlaIndicatorsByEnv(String environment, String projectId,
String periodStart, String periodEnd, int pageSize, int pageStart) throws Exception {
// TODO NXQL query returning all endpoints where environment & projectId
// TODO put them in call to getSlaOrOlaIndicators(String endpointIds...) and return it
CoreSession documentManager = SessionFactory.getSession(request);
if(environment != null && !"".equals(environment) && projectId != null && !"".equals(projectId)){
throw new IllegalArgumentException("Environment or projectid parameter must not be null or empty");
}
// Fetch SoaNode list
ArrayList<String> parameters = new ArrayList<String>();
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM Endpoint WHERE ");
// Search parameters
if(environment != null && !"".equals(environment)){
query.append(Endpoint.XPATH_ENDP_ENVIRONMENT + " like '?' ");
parameters.add(environment);
}
if(projectId != null && !"".equals(projectId)){
if(environment != null && !"".equals(environment)){
query.append(" AND ");
}
query.append("endp:projectid" + " like '?' ");
parameters.add(projectId);
}
// Execute query
String nxqlQuery = NXQLQueryBuilder.getQuery(query.toString(), parameters.toArray(), false, true);
DocumentModelList soaNodeModelList = documentManager.query(nxqlQuery);
// Get endpoints list
List<String> endpointsList = new ArrayList<String>();
for(DocumentModel documentModel : soaNodeModelList){
endpointsList.add((String)documentModel.getPropertyValue(Endpoint.XPATH_UUID));
}
// Get endpoints indicators
return this.getSlaOrOlaIndicators(endpointsList, periodStart, periodEnd, pageSize, pageStart);
}
/**
*
* not REST, used by above
* TODO LATER ask Nuxeo to support OR in SQL Directory queries
*
* @param endpointIds
* @param periodStart
* @param periodEnd
* @param pageSize
* @param pageStart
* @return
* @throws Exception
*/
protected SlaOrOlaIndicators getSlaOrOlaIndicators(List<String> endpointIds,
String periodStart, String periodEnd, int pageSize, int pageStart) throws Exception {
// For each endpoint, get the corresponding indicators and returns the indicator list
SlaOrOlaIndicators slaOrOlaIndicators = new SlaOrOlaIndicators();
for(String endpointId : endpointIds){
slaOrOlaIndicators.getSlaOrOlaIndicatorList().addAll(getSlaOrOlaIndicators(endpointId, "", periodStart, periodEnd, pageSize, pageStart).getSlaOrOlaIndicatorList());
}
return slaOrOlaIndicators;
}
public int getTotalNumberOfSlaOrOlaindicators(List<String> endpointIds,
String periodStart, String periodEnd) throws Exception {
DirectoryService directoryService = Framework.getService(DirectoryService.class);
Session session = directoryService.open(org.easysoa.registry.types.SlaOrOlaIndicator.DOCTYPE);
if (session == null){
throw new Exception("Unable to open a new session on directory 'slaOrOlaIndicator'");
}
try {
int totalIndicatorNumber = 0;
for(String endpointId : endpointIds){
int indicatorNumber = getTotalNumberOfSlaOrOlaindicators(session, endpointId, "", periodStart, periodEnd);
totalIndicatorNumber = totalIndicatorNumber + indicatorNumber;
}
return totalIndicatorNumber;
} catch (Exception ex) {
// Return the exception and cancel the transaction
throw new Exception("Failed to getTotalNumberOfSlaOrOlaindicators ", ex);
} finally {
// NB. container manages transaction, so no need to commit or rollback
// (see doc of deprecated session.commit())
session.close();
}
}
public int getTotalNumberOfSlaOrOlaindicators(Session session, String endpointId,
String slaOrOlaName, String periodStart, String periodEnd) throws Exception {
return session.query(buildParameters(endpointId,
slaOrOlaName, periodStart, periodEnd), buildFullTextSearchParams()).size();
}
/**
* @see org.easysoa.registry.rest.integration.EndpointStateService#getSlaOrOlaIndicators(String, String, Date,
* Date, int, int)
*/
@Override
public SlaOrOlaIndicators getSlaOrOlaIndicators(String endpointId,
String slaOrOlaName, String periodStart, String periodEnd, int pageSize, int pageStart) throws Exception {
SlaOrOlaIndicators slaOrOlaIndicators = new SlaOrOlaIndicators();
DirectoryService directoryService = Framework.getService(DirectoryService.class);
Session session = directoryService.open(org.easysoa.registry.types.SlaOrOlaIndicator.DOCTYPE);
if (session == null) {
throw new Exception("Unable to open a new session on directory '" + org.easysoa.registry.types.SlaOrOlaIndicator.DOCTYPE + "'");
}
try {
/*
* Returns level indicators, in the given period (default : daily)
* OPT paginated navigation
* @param periodStart : if null day start, if both null returns all in the current day
* @param periodEnd : if null now, if both null returns all in the current day
* @param pageSize OPT pagination : number of indicators per page, if not specified, all results are returned
* @param pageStart OPT pagination : index of the first indicator to return (starts with 0)
* @return SlaOrOlaIndicators array of SlaOrOlaIndicator
*/
Map<String, Serializable> parameters = new HashMap<String, Serializable>();
if(endpointId != null && !"".equals(endpointId)){
parameters.put("endpointId", endpointId);
}
if(slaOrOlaName != null && !"".equals(slaOrOlaName)){
parameters.put("slaOrOlaName", slaOrOlaName);
}
// Execute query
// Use this method to have request on date range and pagination
Map<String, String> orderByParams = new HashMap<String, String>();
Set<String> fullTextSearchParams = new HashSet<String>();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Calendar calendarFrom = new GregorianCalendar();
Calendar calendarTo = new GregorianCalendar();
Calendar currentDate = new GregorianCalendar();
// Add date Range param
// If periodEnd is null and period start is null, set to current day end
if(periodEnd == null && periodStart == null){
calendarTo.clear();
calendarTo.set(currentDate.get(Calendar.YEAR) , currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
}
// If period End is null and periodStart not null, set to now
else if(periodEnd == null && periodStart != null) {
calendarTo.setTime(currentDate.getTime());
}
// else set with the periodEnd param
else {
calendarTo.setTime(dateFormater.parse(periodEnd));
}
// Set period start
if(periodStart != null){
calendarFrom.setTime(dateFormater.parse(periodStart));
}
// If periodStart is null, set to current day start
else {
calendarFrom.clear();
// NB. to ease testing, temporarily replace by : currentDate.get(Calendar.YEAR) - 1
calendarFrom.set(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
}
SQLBetweenFilter dateRangeFilter = new SQLBetweenFilter(calendarFrom, calendarTo);
parameters.put("timestamp", dateRangeFilter);
// Execute the query
DocumentModelList soaNodeModelList = session.query(parameters, fullTextSearchParams, orderByParams, false, pageSize, pageStart * pageSize);
SlaOrOlaIndicator indicator;
for(DocumentModel model : soaNodeModelList){
indicator = new SlaOrOlaIndicator();
indicator.setEndpointId((String)model.getPropertyValue(org.easysoa.registry.types.SlaOrOlaIndicator.XPATH_ENDPOINT_ID));
indicator.setType(model.getType());
indicator.setServiceLevelHealth(ServiceLevelHealth.valueOf((String)model.getPropertyValue(org.easysoa.registry.types.SlaOrOlaIndicator.XPATH_SERVICE_LEVEL_HEALTH)));
indicator.setServiceLevelViolation((Boolean)model.getPropertyValue(org.easysoa.registry.types.SlaOrOlaIndicator.XPATH_SERVICE_LEVEL_VIOLATION));
indicator.setSlaOrOlaName((String)model.getPropertyValue(org.easysoa.registry.types.SlaOrOlaIndicator.XPATH_SLA_OR_OLA_NAME));
GregorianCalendar calendar = (GregorianCalendar)model.getPropertyValue(org.easysoa.registry.types.SlaOrOlaIndicator.XPATH_TIMESTAMP);
indicator.setTimestamp(calendar.getTime());
slaOrOlaIndicators.getSlaOrOlaIndicatorList().add(indicator);
}
} catch (ClientException ex) {
// Return the exception and cancel the transaction
throw new Exception("Failed to getSlaOrOlaIndicators on paginated range ", ex);
} finally {
// NB. container manages transaction, so no need to commit or rollback
// (see doc of deprecated session.commit())
session.close();
}
return slaOrOlaIndicators;
}
private Map<String, Serializable> buildParameters(String endpointId,
String slaOrOlaName, String periodStart, String periodEnd) throws Exception {
Map<String, Serializable> parameters = new HashMap<String, Serializable>();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Calendar calendarFrom = new GregorianCalendar();
Calendar calendarTo = new GregorianCalendar();
Calendar currentDate = new GregorianCalendar();
if(endpointId != null && !"".equals(endpointId)){
parameters.put("endpointId", endpointId);
}
if(slaOrOlaName != null && !"".equals(slaOrOlaName)){
parameters.put("slaOrOlaName", slaOrOlaName);
}
// Add date Range param
// If periodEnd is null and period start is null, set to current day end
if(periodEnd == null && periodStart == null){
calendarTo.clear();
calendarTo.set(currentDate.get(Calendar.YEAR) , currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
}
// If period End is null and periodStart not null, set to now
else if(periodEnd == null && periodStart != null) {
calendarTo.setTime(currentDate.getTime());
}
// else set with the periodEnd param
else {
calendarTo.setTime(dateFormater.parse(periodEnd));
}
// Set period start
if(periodStart != null){
calendarFrom.setTime(dateFormater.parse(periodStart));
}
// If periodStart is null, set to current day start
else {
calendarFrom.clear();
// TODO : previous setting was to fix the from date to the current day
// But cause some problems to test the UI when the model is not reloaded daily
// Remove the "-1" when finished
calendarFrom.set(currentDate.get(Calendar.YEAR) - 1, currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
}
SQLBetweenFilter dateRangeFilter = new SQLBetweenFilter(calendarFrom, calendarTo);
parameters.put("timestamp", dateRangeFilter);
return parameters;
}
private Set<String> buildFullTextSearchParams() {
Set<String> fullTextSearchParams = new HashSet<String>();
// TODO : complete this method
return fullTextSearchParams;
}
}