-
Notifications
You must be signed in to change notification settings - Fork 48
/
Registrar.java
720 lines (659 loc) · 26.6 KB
/
Registrar.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package com.google.daq.mqtt.registrar;
import static com.google.daq.mqtt.validator.Validator.NO_SITE;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration;
import com.github.fge.jsonschema.core.load.download.URIDownloader;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.google.api.services.cloudiot.v1.model.Device;
import com.google.api.services.cloudiot.v1.model.DeviceCredential;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.daq.mqtt.util.CloudDeviceSettings;
import com.google.daq.mqtt.util.CloudIotManager;
import com.google.daq.mqtt.util.ConfigUtil;
import com.google.daq.mqtt.util.ExceptionMap;
import com.google.daq.mqtt.util.ExceptionMap.ErrorTree;
import com.google.daq.mqtt.util.PubSubPusher;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import udmi.schema.Config;
import udmi.schema.Envelope.SubFolder;
import udmi.schema.Metadata;
public class Registrar {
public static final String SCHEMA_BASE_PATH = "schema";
static final String METADATA_JSON = "metadata.json";
static final String ENVELOPE_JSON = "envelope.json";
static final String NORMALIZED_JSON = "metadata_norm.json";
static final String DEVICE_ERRORS_JSON = "errors.json";
static final String GENERATED_CONFIG_JSON = "generated_config.json";
private static final String DEVICES_DIR = "devices";
private static final String ERROR_FORMAT_INDENT = " ";
private static final ObjectMapper OBJECT_MAPPER =
new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setDateFormat(new ISO8601DateFormat())
.setSerializationInclusion(Include.NON_NULL);
private static final String UDMI_VERSION_KEY = "UDMI_VERSION";
private static final String VERSION_KEY = "Version";
private static final String VERSION_MAIN_KEY = "main";
private static final String SCHEMA_SUFFIX = ".json";
private static final String REGISTRATION_SUMMARY_JSON = "registration_summary.json";
private static final String SCHEMA_NAME = "UDMI";
private static final String SITE_METADATA_JSON = "site_metadata.json";
private static final String SWARM_SUBFOLDER = "swarm";
private static final long PROCESSING_TIMEOUT_MIN = 60;
private static final int RUNNER_THREADS = 25;
private static final String CONFIG_SUB_TYPE = "config";
private static final String MODEL_SUB_TYPE = "model";
private final Map<String, JsonSchema> schemas = new HashMap<>();
private final String generation = getGenerationString();
private CloudIotManager cloudIotManager;
private File siteDir;
private File schemaBase;
private PubSubPusher updatePusher;
private PubSubPusher feedPusher;
private Map<String, LocalDevice> localDevices;
private File summaryFile;
private ExceptionMap blockErrors;
private String projectId;
private boolean updateCloudIoT;
private Duration idleLimit;
private Set<String> cloudDevices;
private Metadata siteMetadata;
public static void main(String[] args) {
ArrayList<String> argList = new ArrayList<>(List.of(args));
Registrar registrar = new Registrar();
try {
boolean processAllDevices = processArgs(argList, registrar);
if (registrar.schemaBase == null) {
registrar.setToolRoot(null);
}
registrar.loadSiteMetadata();
if (processAllDevices) {
registrar.processDevices();
} else {
registrar.processDevices(argList);
}
registrar.writeErrors();
registrar.shutdown();
} catch (ExceptionMap em) {
ExceptionMap.format(em, ERROR_FORMAT_INDENT).write(System.err);
throw new RuntimeException("mapped exceptions", em);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("main exception", ex);
}
}
private static boolean processArgs(List<String> argList, Registrar registrar) {
while (argList.size() > 0) {
String option = argList.remove(0);
switch (option) {
case "-r":
registrar.setToolRoot(argList.remove(0));
break;
case "-p":
registrar.setProjectId(argList.remove(0));
break;
case "-s":
registrar.setSitePath(argList.remove(0));
break;
case "-f":
registrar.setFeedTopic(argList.remove(0));
break;
case "-u":
registrar.setUpdateFlag(true);
break;
case "-l":
registrar.setIdleLimit(argList.remove(0));
break;
case "--":
return false;
default:
if (option.startsWith("-")) {
throw new RuntimeException("Unknown cmdline option " + option);
}
argList.add(0, option);
return false;
}
}
return true;
}
private void setIdleLimit(String option) {
idleLimit = Duration.parse("PT" + option);
System.err.println("Limiting devices to duration " + idleLimit.toSeconds() + "s");
}
private void setUpdateFlag(boolean update) {
updateCloudIoT = update;
}
private void setFeedTopic(String feedTopic) {
System.err.println("Sending device feed to topic " + feedTopic);
feedPusher = new PubSubPusher(projectId, feedTopic);
System.err.println("Checking subscription for existing messages...");
if (!feedPusher.isEmpty()) {
throw new RuntimeException("Feed is not empty, do you have enough pullers?");
}
}
private void writeErrors() throws Exception {
Map<String, Map<String, String>> errorSummary = new TreeMap<>();
DeviceExceptionManager dem = new DeviceExceptionManager(siteDir);
localDevices
.values()
.forEach(device -> device.writeErrors(dem.forDevice(device.getDeviceId())));
localDevices
.values()
.forEach(
device -> {
Set<Entry<String, ErrorTree>> entries =
device.getTreeChildren(dem.forDevice(device.getDeviceId()));
entries.stream()
.forEach(
error ->
errorSummary
.computeIfAbsent(error.getKey(), cat -> new TreeMap<>())
.put(device.getDeviceId(), error.getValue().message));
if (entries.isEmpty()) {
errorSummary
.computeIfAbsent("Clean", cat -> new TreeMap<>())
.put(device.getDeviceId(), device.getNormalizedTimestamp());
}
});
if (blockErrors != null && !blockErrors.isEmpty()) {
errorSummary.put(
"Block",
blockErrors.stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString())));
}
System.err.println("\nSummary:");
errorSummary.forEach(
(key, value) -> System.err.println(" Device " + key + ": " + value.size()));
System.err.println("Out of " + localDevices.size() + " total.");
String version = Optional.ofNullable(System.getenv(UDMI_VERSION_KEY)).orElse("unknown");
errorSummary.put(VERSION_KEY, Map.of(VERSION_MAIN_KEY, version));
OBJECT_MAPPER.writeValue(summaryFile, errorSummary);
}
private void setSitePath(String sitePath) {
Preconditions.checkNotNull(SCHEMA_NAME, "schemaName not set yet");
siteDir = new File(sitePath);
summaryFile = new File(siteDir, REGISTRATION_SUMMARY_JSON);
summaryFile.delete();
}
private void initializeCloudProject() {
File cloudIotConfig = new File(siteDir, ConfigUtil.CLOUD_IOT_CONFIG_JSON);
System.err.println("Reading Cloud IoT config from " + cloudIotConfig.getAbsolutePath());
cloudIotManager = new CloudIotManager(projectId, cloudIotConfig, SCHEMA_NAME);
System.err.println(
String.format(
"Working with project %s registry %s/%s",
cloudIotManager.getProjectId(),
cloudIotManager.getCloudRegion(),
cloudIotManager.getRegistryId()));
if (cloudIotManager.getUpdateTopic() != null) {
updatePusher = new PubSubPusher(projectId, cloudIotManager.getUpdateTopic());
}
}
private String getGenerationString() {
try {
Date generationDate = new Date();
String quotedString = OBJECT_MAPPER.writeValueAsString(generationDate);
return quotedString.substring(1, quotedString.length() - 1);
} catch (JsonProcessingException e) {
throw new RuntimeException("While forming generation timestamp", e);
}
}
private void processDevices() {
processDevices(null);
}
private void processDevices(List<String> devices) {
Set<String> deviceSet = calculateDevices(devices);
AtomicInteger updatedCount = new AtomicInteger();
AtomicInteger processedCount = new AtomicInteger();
try {
localDevices = loadLocalDevices(deviceSet);
cloudDevices = fetchCloudDevices();
if (deviceSet != null) {
Set<String> unknowns = Sets.difference(deviceSet, localDevices.keySet());
if (!unknowns.isEmpty()) {
throw new RuntimeException(
"Unknown specified devices: " + Joiner.on(", ").join(unknowns));
}
}
processLocalDevices(updatedCount, processedCount);
System.err.printf("Finished processing %d device records...%n", processedCount.get());
if (updateCloudIoT) {
bindGatewayDevices(localDevices, deviceSet);
Set<String> extraDevices = cloudDevices == null ? ImmutableSet.of()
: Sets.difference(cloudDevices, localDevices.keySet());
blockErrors = blockExtraDevices(extraDevices);
}
System.err.printf("Updated %d devices (out of %d)%n", updatedCount.get(),
localDevices.size());
if (cloudDevices != null) {
Set<String> unknownDevices = Sets.difference(localDevices.keySet(), cloudDevices);
System.err.printf("Skipped %d not-in-cloud devices.%n", unknownDevices.size());
}
} catch (Exception e) {
throw new RuntimeException("While processing devices", e);
}
}
private void processLocalDevices(AtomicInteger updatedCount, AtomicInteger processedCount)
throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(RUNNER_THREADS);
for (String localName : localDevices.keySet()) {
executor.execute(() -> {
int count = processedCount.incrementAndGet();
if (count % 500 == 0) {
System.err.printf("Processed %d device records...%n", count);
}
processLocalDevice(localName, updatedCount);
});
}
executor.shutdown();
executor.awaitTermination(PROCESSING_TIMEOUT_MIN, TimeUnit.MINUTES);
}
private void processLocalDevice(String localName, AtomicInteger processedDeviceCount) {
LocalDevice localDevice = localDevices.get(localName);
if (!localDevice.isValid()) {
System.err.println("Skipping (invalid) " + localName);
return;
}
if (shouldLimitDevice(localDevice)) {
// System.err.println("Skipping active device " + localDevice.getDeviceId());
return;
}
processedDeviceCount.incrementAndGet();
try {
localDevice.writeConfigFile();
if (cloudDevices != null) {
updateCloudIoT(localName, localDevice);
sendUpdateMessages(localDevice);
if (cloudDevices.contains(localName)) {
sendFeedMessage(localDevice);
}
}
} catch (Exception e) {
System.err.println("Deferring exception: " + e);
localDevice.captureError(LocalDevice.EXCEPTION_REGISTERING, e);
}
}
private boolean shouldLimitDevice(LocalDevice localDevice) {
try {
if (idleLimit == null) {
return false;
}
Device device = cloudIotManager.fetchDevice(localDevice.getDeviceId());
if (device == null || device.getLastEventTime() == null || idleLimit == null) {
return false;
}
return Instant.now().minus(idleLimit).isBefore(Instant.parse(device.getLastEventTime()));
} catch (Exception e) {
throw new RuntimeException("While checking device limit " + localDevice.getDeviceId(), e);
}
}
private boolean sendFeedMessage(LocalDevice localDevice) {
if (feedPusher == null) {
return false;
}
if (!localDevice.isDirectConnect()) {
System.err.println("Skipping feed message for proxy device " + localDevice.getDeviceId());
return false;
}
System.err.println("Sending feed message for " + localDevice.getDeviceId());
try {
Map<String, String> attributes = new HashMap<>();
attributes.put("subFolder", SWARM_SUBFOLDER);
attributes.put("deviceId", localDevice.getDeviceId());
attributes.put("deviceRegistryId", cloudIotManager.cloudIotConfig.registry_id);
attributes.put("deviceRegistryLocation", cloudIotManager.cloudIotConfig.cloud_region);
SwarmMessage swarmMessage = new SwarmMessage();
swarmMessage.key_base64 = Base64.getEncoder().encodeToString(localDevice.getKeyBytes());
swarmMessage.device_metadata = localDevice.getMetadata();
String swarmString = OBJECT_MAPPER.writeValueAsString(swarmMessage);
feedPusher.sendMessage(attributes, swarmString);
} catch (Exception e) {
throw new RuntimeException("While sending swarm feed message", e);
}
return true;
}
private void updateCloudIoT(String localName, LocalDevice localDevice) {
if (!updateCloudIoT) {
return;
}
updateCloudIoT(localDevice);
Device device =
Preconditions.checkNotNull(fetchDevice(localName), "missing device " + localName);
BigInteger numId =
Preconditions.checkNotNull(
device.getNumId(), "missing deviceNumId for " + localName);
localDevice.setDeviceNumId(numId.toString());
}
private void updateCloudIoT(LocalDevice localDevice) {
String localName = localDevice.getDeviceId();
fetchDevice(localName);
CloudDeviceSettings localDeviceSettings = localDevice.getSettings();
if (cloudIotManager.registerDevice(localName, localDeviceSettings)) {
System.err.println("Created new device entry " + localName);
} else {
System.err.println("Updated device entry " + localName);
}
}
private Set<String> calculateDevices(List<String> devices) {
if (devices == null) {
return null;
}
return devices.stream().map(this::deviceNameFromPath).collect(Collectors.toSet());
}
private String deviceNameFromPath(String device) {
while (device.endsWith("/")) {
device = device.substring(0, device.length() - 1);
}
int slashPos = device.lastIndexOf('/');
if (slashPos >= 0) {
device = device.substring(slashPos + 1);
}
return device;
}
private ExceptionMap blockExtraDevices(Set<String> extraDevices) {
ExceptionMap exceptionMap = new ExceptionMap("Block devices errors");
if (!cloudIotManager.cloudIotConfig.block_unknown) {
return exceptionMap;
}
for (String extraName : extraDevices) {
try {
System.err.println("Blocking extra device " + extraName);
cloudIotManager.blockDevice(extraName, true);
} catch (Exception e) {
exceptionMap.put(extraName, e);
}
}
return exceptionMap;
}
private Device fetchDevice(String localName) {
try {
return cloudIotManager.fetchDevice(localName);
} catch (Exception e) {
throw new RuntimeException("Fetching device " + localName, e);
}
}
private void sendUpdateMessages(LocalDevice localDevice) {
if (updatePusher != null) {
System.err.println("Sending model/config update for " + localDevice.getDeviceId());
sendUpdateMessage(localDevice, SubFolder.SYSTEM);
sendUpdateMessage(localDevice, SubFolder.POINTSET);
sendUpdateMessage(localDevice, SubFolder.GATEWAY);
sendUpdateMessage(localDevice, SubFolder.LOCALNET);
}
}
private void sendUpdateMessage(LocalDevice localDevice, SubFolder subFolder) {
sendUpdateMessage(localDevice, MODEL_SUB_TYPE, subFolder, localDevice.getMetadata());
sendUpdateMessage(localDevice, CONFIG_SUB_TYPE, subFolder, localDevice.deviceConfigObject());
}
private void sendUpdateMessage(LocalDevice localDevice, String subType, SubFolder subfolder, Object target) {
String fieldName = subfolder.toString().toLowerCase();
try {
Field declaredField = target.getClass().getDeclaredField(fieldName);
sendSubMessage(localDevice, subType, subfolder, declaredField.get(target));
} catch (Exception e) {
throw new RuntimeException(String.format("Getting field %s from target %s", fieldName, target.getClass().getSimpleName()));
}
}
private void sendSubMessage(LocalDevice localDevice, String subType, SubFolder subFolder,
Object subConfig) {
try {
Map<String, String> attributes = new HashMap<>();
attributes.put("deviceId", localDevice.getDeviceId());
attributes.put("deviceNumId", localDevice.getDeviceNumId());
attributes.put("deviceRegistryId", cloudIotManager.getRegistryId());
attributes.put("projectId", cloudIotManager.getProjectId());
attributes.put("subType", subType);
attributes.put("subFolder", subFolder.value());
String messageString = OBJECT_MAPPER.writeValueAsString(subConfig);
updatePusher.sendMessage(attributes, messageString);
} catch (Exception e) {
throw new RuntimeException(
String.format("Sending %s/%s messages for %s%n", subType, subFolder,
localDevice.getDeviceId()), e);
}
}
private void bindGatewayDevices(Map<String, LocalDevice> localDevices, Set<String> deviceSet) {
localDevices.values().stream()
.filter(localDevice -> localDevice.getSettings().proxyDevices != null)
.forEach(
localDevice ->
localDevice.getSettings().proxyDevices.stream()
.filter(proxyDevice -> deviceSet == null || deviceSet.contains(proxyDevice))
.forEach(
proxyDeviceId -> {
try {
String gatewayId = localDevice.getDeviceId();
System.err.println(
"Binding " + proxyDeviceId + " to gateway " + gatewayId);
cloudIotManager.bindDevice(proxyDeviceId, gatewayId);
} catch (Exception e) {
throw new RuntimeException("While binding device " + proxyDeviceId, e);
}
}));
}
private void shutdown() {
if (updatePusher != null) {
updatePusher.shutdown();
}
if (feedPusher != null) {
feedPusher.shutdown();
}
}
private Set<String> fetchCloudDevices() {
boolean requiresCloud = updateCloudIoT || (idleLimit != null);
if (requiresCloud) {
Set<String> devices = cloudIotManager.fetchDeviceList();
System.err.printf("Fetched %d devices from cloud registry %s%n",
devices.size(), cloudIotManager.getRegistryPath());
return devices;
} else {
System.err.println("Skipping remote registry fetch");
return null;
}
}
private Map<String, LocalDevice> loadLocalDevices(Set<String> specifiedDevices) {
Preconditions.checkNotNull(siteDir, "site directory");
File devicesDir = new File(siteDir, DEVICES_DIR);
if (!devicesDir.isDirectory()) {
throw new RuntimeException("Not a valid directory: " + devicesDir.getAbsolutePath());
}
final String[] devices;
if (specifiedDevices == null) {
devices = devicesDir.list();
} else {
devices = devicesDir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return specifiedDevices.contains(name);
}
});
}
Preconditions.checkNotNull(devices, "No devices found in " + devicesDir.getAbsolutePath());
Map<String, LocalDevice> localDevices = loadDevices(siteDir, devicesDir, devices);
writeNormalized(localDevices);
validateKeys(localDevices);
validateExpected(localDevices);
validateSamples(localDevices);
return localDevices;
}
private void validateSamples(Map<String, LocalDevice> localDevices) {
for (LocalDevice device : localDevices.values()) {
try {
device.validateSamples();
} catch (Exception e) {
device.captureError(LocalDevice.EXCEPTION_SAMPLES, e);
}
}
}
private void validateExpected(Map<String, LocalDevice> localDevices) {
for (LocalDevice device : localDevices.values()) {
try {
device.validateExpected();
} catch (Exception e) {
device.captureError(LocalDevice.EXCEPTION_FILES, e);
}
}
}
private void writeNormalized(Map<String, LocalDevice> localDevices) {
for (String deviceName : localDevices.keySet()) {
try {
localDevices.get(deviceName).writeNormalized();
} catch (Exception e) {
throw new RuntimeException("While writing normalized " + deviceName, e);
}
}
}
private void validateKeys(Map<String, LocalDevice> localDevices) {
Map<DeviceCredential, String> usedCredentials = new HashMap<>();
localDevices.values().stream()
.filter(LocalDevice::isDirectConnect)
.forEach(
localDevice -> {
CloudDeviceSettings settings = localDevice.getSettings();
String deviceName = localDevice.getDeviceId();
for (DeviceCredential credential : settings.credentials) {
String previous = usedCredentials.put(credential, deviceName);
if (previous != null) {
RuntimeException exception =
new RuntimeException(
String.format(
"Duplicate credentials found for %s & %s", previous, deviceName));
localDevice.captureError(LocalDevice.EXCEPTION_CREDENTIALS, exception);
}
}
});
}
private Map<String, LocalDevice> loadDevices(File siteDir, File devicesDir,
String[] devices) {
HashMap<String, LocalDevice> localDevices = new HashMap<>();
AtomicInteger loaded = new AtomicInteger(0);
for (String deviceName : devices) {
int count = loaded.incrementAndGet();
if (LocalDevice.deviceExists(devicesDir, deviceName)) {
if (devices.length < 100) {
System.err.println("Loading local device " + deviceName);
} else if (count % 500 == 0) {
System.err.printf("Loaded %d devices...%n", count);
}
LocalDevice localDevice =
localDevices.computeIfAbsent(
deviceName,
keyName -> new LocalDevice(siteDir, devicesDir, deviceName, schemas, generation,
siteMetadata));
try {
localDevice.loadCredentials();
} catch (Exception e) {
localDevice.captureError(LocalDevice.EXCEPTION_CREDENTIALS, e);
}
if (cloudIotManager != null) {
try {
localDevice.validateEnvelope(
cloudIotManager.getRegistryId(), cloudIotManager.getSiteName());
} catch (Exception e) {
localDevice.captureError(LocalDevice.EXCEPTION_ENVELOPE,
new RuntimeException("While validating envelope", e));
}
}
}
}
System.err.printf("Finished loading %d local devices.%n", loaded.get());
return localDevices;
}
private void setProjectId(String projectId) {
if (NO_SITE.equals(projectId) || projectId == null) {
return;
}
this.projectId = projectId;
initializeCloudProject();
}
private void setToolRoot(String toolRoot) {
schemaBase = new File(toolRoot, SCHEMA_BASE_PATH);
File[] schemaFiles = schemaBase.listFiles(file -> file.getName().endsWith(SCHEMA_SUFFIX));
for (File schemaFile : Objects.requireNonNull(schemaFiles)) {
loadSchema(schemaFile.getName());
}
if (schemas.isEmpty()) {
throw new RuntimeException(
"No schemas successfully loaded from " + schemaBase.getAbsolutePath());
}
}
private void loadSchema(String key) {
File schemaFile = new File(schemaBase, key);
try (InputStream schemaStream = new FileInputStream(schemaFile)) {
JsonSchema schema =
JsonSchemaFactory.newBuilder()
.setLoadingConfiguration(
LoadingConfiguration.newBuilder()
.addScheme("file", new RelativeDownloader())
.freeze())
.freeze()
.getJsonSchema(OBJECT_MAPPER.readTree(schemaStream));
schemas.put(key, schema);
} catch (Exception e) {
throw new RuntimeException("While loading schema " + schemaFile.getAbsolutePath(), e);
}
}
private void loadSiteMetadata() {
this.siteMetadata = null;
if (!schemas.containsKey(METADATA_JSON)) {
return;
}
File siteMetadataFile = new File(siteDir, SITE_METADATA_JSON);
try (InputStream targetStream = new FileInputStream(siteMetadataFile)) {
schemas.get(METADATA_JSON).validate(OBJECT_MAPPER.readTree(targetStream));
} catch (FileNotFoundException e) {
return;
} catch (Exception e) {
throw new RuntimeException("While validating " + SITE_METADATA_JSON, e);
}
try {
System.err.printf("Loading " + SITE_METADATA_JSON + "\n");
this.siteMetadata = OBJECT_MAPPER.readValue(siteMetadataFile, Metadata.class);
} catch (Exception e) {
throw new RuntimeException("While loading " + SITE_METADATA_JSON, e);
}
}
class RelativeDownloader implements URIDownloader {
@Override
public InputStream fetch(URI source) {
try {
return new FileInputStream(new File(schemaBase, source.getSchemeSpecificPart()));
} catch (Exception e) {
throw new RuntimeException("While loading sub-schema " + source, e);
}
}
}
}