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
|
package pl.edu.mimuw.cloudatlas.client;
import com.google.gson.Gson;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import pl.edu.mimuw.cloudatlas.api.Api;
import pl.edu.mimuw.cloudatlas.model.*;
import pl.edu.mimuw.cloudatlas.querysigner.QueryData;
import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi;
import java.net.InetAddress;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
/**
* APIs
* /
* /query - displays query submission form
* /installQuery - posts query installation data
* /uninstallQuery - posts query uninstallation data
* /contacts - GET - displays contacts submission form
* /contacts - POST - posts contacts installation data
* /attribs - GET - displays attribute submission form
* /attribs - POST - posts attribute submission data
* /values - GET - displays attributes values
* /zones - GET - displays zone change data
* /zones - POST - posts zone change data
* /attribNumValues - REST API to get numerical attribute values
* /attribAllValues - REST API to get all attribute values
*/
@Controller
public class ClientController {
private Api agentApi;
private QuerySignerApi querySignerApi;
private Map<ValueTime, AttributesMap> attributes;
private String currentZoneName;
private static final int MAX_ENTRIES = 10;
ClientController() {
try {
String agentHostname = System.getProperty("agent_hostname");
Registry registry = LocateRegistry.getRegistry(agentHostname);
this.agentApi = (Api) registry.lookup("Api");
String querySignerHostname = System.getProperty("querysigner_hostname");
Registry querySignerRegistry = LocateRegistry.getRegistry(querySignerHostname);
this.querySignerApi = (QuerySignerApi) querySignerRegistry.lookup("QuerySignerApi");
} catch (Exception e) {
System.err.println("Client exception:");
e.printStackTrace();
}
this.attributes = new LinkedHashMap<ValueTime, AttributesMap>() {
protected boolean removeEldestEntry(Map.Entry<ValueTime, AttributesMap> eldest) {
return size() > MAX_ENTRIES;
}
};
this.currentZoneName = System.getProperty("zone_path");
fetchAttributeData(); // fetch attribute data as early as possible
}
@GetMapping("/")
public String homePage(Model model) {
model.addAttribute("homeMessage", "Welcome to CloudaAtlas client interface");
return "home";
}
@GetMapping("/query")
public String queryPage(Model model) {
model.addAttribute("queryObject", new Query());
return "queryForm";
}
@PostMapping("/installQuery")
public String installQuery(@ModelAttribute Query queryObject, Model model) {
boolean success = true;
try {
QueryData query = this.querySignerApi.signInstallQuery(queryObject.getName(), queryObject.getValue());
this.agentApi.installQuery(queryObject.getName(), query);
} catch (Exception e) {
success = false;
System.err.println("Client exception:");
e.printStackTrace();
}
if (success) {
model.addAttribute(
"homeMessage",
"Query installed successfully");
} else {
model.addAttribute(
"homeMessage",
"Query submission failed with a remote exception");
}
return "home";
}
@PostMapping("/uninstallQuery")
public String uninstallQuery(@ModelAttribute Query queryObject, Model model) {
boolean success = true;
try {
QueryData query = querySignerApi.signUninstallQuery(queryObject.getName());
this.agentApi.uninstallQuery(queryObject.getName(), query);
} catch (Exception e) {
success = false;
System.err.println("Client exception:");
e.printStackTrace();
}
if (success) {
model.addAttribute(
"homeMessage",
"Query uninstalled successfully");
} else {
model.addAttribute(
"homeMessage",
"Query submission failed with a remote exception");
}
return "home";
}
@GetMapping("/contacts")
public String contactPage(Model model) {
model.addAttribute("contactsObject" , new DataStringInput());
return "contactsForm";
}
private Set<ValueContact> parseContactsString(DataStringInput contactsInput) throws Exception {
Gson gson = new Gson();
Map<String, ArrayList> contactStrings = gson.fromJson(contactsInput.getString(), Map.class);
Set<ValueContact> contactObjects = new HashSet<ValueContact>();
ArrayList<Double> cAddr;
byte[] inetArray = new byte[4];
for (Map.Entry<String, ArrayList> cursor : contactStrings.entrySet()) {
cAddr = cursor.getValue(); // gson always reads numerical values as doubles
for (int i = 0; i < 4; i++) {
inetArray[i] = (byte) cAddr.get(i).doubleValue();
}
contactObjects.add(new ValueContact(
new PathName(cursor.getKey()),
InetAddress.getByAddress(inetArray))
);
}
return contactObjects;
}
@PostMapping("/contacts")
public String contactPage(@ModelAttribute DataStringInput contactsObject, Model model) {
boolean success = true;
Set<ValueContact> contactObjects;
try {
contactObjects = parseContactsString(contactsObject);
this.agentApi.setFallbackContacts(contactObjects);
} catch (Exception e) {
success = false;
System.err.println("Client exception:");
e.printStackTrace();
}
if (success) {
model.addAttribute("homeMessage", "Contact list submitted successfully");
} else {
model.addAttribute("homeMessage", "Contact list submission failed");
}
return "home";
}
@GetMapping("/attribs")
public String attribPage(Model model) {
model.addAttribute("attributeObject", new AttributeInput());
return "attribForm";
}
private List<Value> parseCollectionAttributeValue(List values, ArrayList<String> types) throws Exception {
List<Value> resultValue = new ArrayList<Value>();
String currentTypeString = types.get(1);
AttributeInput attributeInput = new AttributeInput();
ArrayList<String> newTypes = new ArrayList<>(types.subList(1, types.size()));
Gson gson = new Gson();
for (int i = 0; i < values.size(); i++) {
if (currentTypeString.equals("List")) {
resultValue.add(parseListAttributeValue((List) values.get(i), newTypes));
} else if (currentTypeString.equals("Set")) {
resultValue.add(parseSetAttributeValue((List) values.get(i), newTypes));
} else {
attributeInput.setAttributeType(currentTypeString);
attributeInput.setValueString(gson.toJson(values.get(i)));
resultValue.add(parseAttributeValue(attributeInput));
}
}
return resultValue;
}
private Value parseListAttributeValue(List values, ArrayList<String> types) throws Exception {
List<Value> listResultValue = parseCollectionAttributeValue(values, types);
ArrayList<Value> resultValue = new ArrayList<>(listResultValue);
return new ValueList(resultValue, resultValue.iterator().next().getType());
}
private Value parseSetAttributeValue(List values, ArrayList<String> types) throws Exception {
List<Value> listResultValue = parseCollectionAttributeValue(values, types);
HashSet<Value> resultValue = new HashSet<>(listResultValue);
return new ValueSet(resultValue, resultValue.iterator().next().getType());
}
private Long parseIntegerAsLong(String value) {
Double doubleVal;
try {
doubleVal = Double.parseDouble(value);
return doubleVal.longValue();
} catch (NumberFormatException e) {
return Long.parseLong(value);
}
}
private Value parseAttributeValue(AttributeInput attributeObject) throws Exception {
Gson gson = new Gson();
Value attributeValue = null;
switch (attributeObject.getAttributeType()) {
case "Boolean":
if (attributeObject.getValueString().toLowerCase().equals("true")) {
attributeValue = new ValueBoolean(true);
} else if (attributeObject.getValueString().toLowerCase().equals("false")) {
attributeValue = new ValueBoolean(false);
} else {
String errMsg = "Incorrect boolean value: " + attributeObject.getValueString();
throw new UnsupportedOperationException(errMsg);
}
break;
case "Double":
attributeValue = new ValueDouble(Double.parseDouble(attributeObject.getValueString()));
break;
case "Int":
attributeValue = new ValueInt(parseIntegerAsLong(attributeObject.getValueString()));
break;
case "String":
attributeValue = new ValueString(attributeObject.getValueString());
break;
case "Time":
attributeValue = new ValueTime(parseIntegerAsLong(attributeObject.getValueString()));
break;
case "Duration":
attributeValue = new ValueDuration(parseIntegerAsLong(attributeObject.getValueString()));
break;
case "Contact":
DataStringInput contactsString = new DataStringInput();
contactsString.setString(attributeObject.getValueString());
attributeValue = parseContactsString(contactsString).iterator().next();
break;
case "List":
List parsedListValue = gson.fromJson(attributeObject.getValueString(), List.class);
ArrayList<String> parsedListTypes = new ArrayList<>(Arrays.asList(
attributeObject.getAttributeComplexType().replaceAll("\\s","").split(",")));
attributeValue = parseListAttributeValue(parsedListValue, parsedListTypes);
break;
case "Set":
List parsedSetValue = gson.fromJson(attributeObject.getValueString(), List.class);
ArrayList<String> parsedSetTypes = new ArrayList<>(Arrays.asList(
attributeObject.getAttributeComplexType(). replaceAll("\\s","").split(",")));
attributeValue = parseSetAttributeValue(parsedSetValue, parsedSetTypes);
break;
default:
String errMsg = "Value type not supported: " + attributeObject.getAttributeType();
throw new UnsupportedOperationException(errMsg);
}
return attributeValue;
}
@PostMapping("/attribs")
public String attribPage(@ModelAttribute AttributeInput attributeObject, Model model) {
boolean success = true;
Value attributeValue;
try {
attributeValue = parseAttributeValue(attributeObject);
agentApi.setAttributeValue(
attributeObject.getZoneName(),
attributeObject.getAttributeName(),
attributeValue);
} catch (Exception e) {
success = false;
System.err.println("Client exception:");
e.printStackTrace();
}
if (success) {
model.addAttribute("homeMessage", "Attribute submitted successfully");
} else {
model.addAttribute("homeMessage", "Attribute submission failed");
}
return "home";
}
private String getAvailableZonesString() {
boolean success = true;
Set<String> availableZones;
String availableZonesString = "";
try {
availableZones = agentApi.getZoneSet();
availableZonesString = availableZones.toString().substring(1, availableZones.toString().length() - 1);
} catch (Exception e) {
success = false;
System.err.println("Client exception:");
e.printStackTrace();
}
if (success) {
return "Available zones are: " + availableZonesString;
} else {
return "No zones available, error occured during fetch";
}
}
@GetMapping("/values")
public String valuesPage(Model model) {
return "attribChart";
}
@Scheduled(fixedRate = 5000)
private void fetchAttributeData() {
AttributesMap attribData;
ValueTime currentTime;
try {
if (!this.currentZoneName.isEmpty()) {
attribData = agentApi.getZoneAttributeValues(this.currentZoneName);
currentTime = new ValueTime(System.currentTimeMillis());
this.attributes.put(currentTime, attribData);
}
} catch (Exception e) {
System.err.println("Client exception:");
e.printStackTrace();
}
}
private ArrayList getAllAttributeValues(AttributesMap attribs, Boolean justNumerical) {
ArrayList valuesList = new ArrayList<>();
Value val;
for (Map.Entry<Attribute, Value> entry : attribs) {
val = entry.getValue();
// casting to ValueDouble and ValueInt caused some errors
// and gson turns all numerical values into doubles anyway
if (justNumerical && isValueNumerical(val)) {
valuesList.add(Double.parseDouble(val.toString()));
} else if (!justNumerical) {
valuesList.add(val.toString());
}
}
return valuesList;
}
private boolean isValueNumerical(Value val) {
Type valType = val.getType();
if (TypePrimitive.DOUBLE.isCompatible(valType) || TypePrimitive.INTEGER.isCompatible(valType)) {
return true;
} else {
return false;
}
}
private AttributesMap getLastAttributesMap() {
ArrayList<Map.Entry<ValueTime, AttributesMap>> attribsMap = new ArrayList<>(this.attributes.entrySet());
return attribsMap.get(attribsMap.size() - 1).getValue();
}
private ArrayList<String> getAttributesColumnNames(Boolean justNumerical) {
ArrayList<String> chartValueNames = new ArrayList<>();
AttributesMap lastAttribMap = getLastAttributesMap();
for (Map.Entry<Attribute, Value> e : lastAttribMap) {
if (!justNumerical || isValueNumerical(e.getValue())) {
chartValueNames.add(e.getKey().getName());
}
}
chartValueNames.add(0, "Timestamp");
return chartValueNames;
}
// data format compatible with Google Charts Table and Google Line Chart input
// but it's a generic 2d array table representation
// https://developers.google.com/chart/interactive/docs/gallery/table
private ArrayList<ArrayList> getValuesTable(Boolean justNumerical) {
ArrayList valueRow;
ArrayList<ArrayList> allValues = new ArrayList<>();
ArrayList<String> valueNames = getAttributesColumnNames(justNumerical);
for (Map.Entry<ValueTime, AttributesMap> attribMapEntry : this.attributes.entrySet()) {
valueRow = getAllAttributeValues(attribMapEntry.getValue(), justNumerical);
while (valueRow.size() < valueNames.size() - 1) {
valueRow.add(null);
}
valueRow.add(0, attribMapEntry.getKey().toString().substring(11, 19));
allValues.add(valueRow);
}
// optional trimming of table length
// if (allValues.size() > 10) {
// allValues = new ArrayList<ArrayList>(allValues.subList(allValues.size() - 11, allValues.size() - 1));
// }
allValues.add(0, valueNames);
return allValues;
}
private String processAttribValues(ArrayList<ArrayList> valuesTable) {
String jsonAttributes = "";
Gson gson = new Gson();
jsonAttributes = gson.toJson(valuesTable);
// System.out.println(jsonAttributes);
return jsonAttributes;
}
@GetMapping("/attribNumValues")
@ResponseBody
public String attribNumValuesApi() {
return processAttribValues(getValuesTable(true));
}
@GetMapping("/attribAllValues")
@ResponseBody
public String attribAllValuesApi() {
return processAttribValues(getValuesTable(false));
}
@GetMapping("/zones")
public String zonesGetPage(Model model) {
model.addAttribute("availableZones", getAvailableZonesString());
model.addAttribute("currentZone", "Current zone: " + this.currentZoneName);
model.addAttribute("zoneName", new DataStringInput());
return "zoneForm";
}
@PostMapping("/zones")
public String zonesPostPage(@ModelAttribute DataStringInput zoneName, Model model) {
this.currentZoneName = zoneName.getString();
this.attributes.clear();
model.addAttribute("currentZone", "Current zone: " + this.currentZoneName);
model.addAttribute("availableZones", getAvailableZonesString());
model.addAttribute("zoneName", new DataStringInput());
return "zoneForm";
}
}
|