Skip to content

Commit 21d7f20

Browse files
grafnukhyatimahendru
authored andcommitted
Tweaking bind code
1 parent d4aafc6 commit 21d7f20

File tree

8 files changed

+40
-23
lines changed

8 files changed

+40
-23
lines changed

udmis/src/main/java/com/google/bos/udmi/service/access/ClearBladeIotAccessProvider.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected DeviceManagerInterface getDeviceManager(int monitorSec) {
246246
}
247247

248248
private CloudModel bindDevicesToGateway(String registryId, String gatewayId,
249-
CloudModel cloudModel, Consumer<Integer> progress) {
249+
CloudModel cloudModel, Consumer<String> progress) {
250250
Set<String> deviceIds = ReflectProcessor.isLegacyRequest(cloudModel)
251251
? cloudModel.device_ids.keySet() : getDeviceIds(cloudModel.gateway);
252252
boolean toBind = cloudModel.operation == BIND;
@@ -529,10 +529,11 @@ private Map<String, CloudModel> augmentGatewayModels(String registryId,
529529
HashMap<String, String> proxyDeviceGateways = new HashMap<>();
530530
Set<Entry<String, CloudModel>> gateways =
531531
boundDevices.entrySet().stream().filter(this::isGateway).collect(Collectors.toSet());
532-
AtomicInteger count = new AtomicInteger(-gateways.size());
532+
AtomicInteger count = new AtomicInteger();
533533
gateways.forEach(entry -> {
534534
augmentGatewayModel(registryId, entry, proxyDeviceGateways);
535-
progress.accept(format("Processed %d gateways...", count.incrementAndGet()));
535+
progress.accept(format("Augmented gateway %s (%d/%d)",
536+
entry.getKey(), count.incrementAndGet(), gateways.size()));
536537
});
537538
boundDevices.entrySet()
538539
.forEach(entry -> augmentProxiedModel(entry, proxyDeviceGateways));
@@ -566,7 +567,7 @@ private GatewayModel makeGatewayModel(HashMap<String, CloudModel> boundDevices)
566567

567568
@Override
568569
public CloudModel modelDevice(String registryId, String deviceId, CloudModel cloudModel,
569-
Consumer<Integer> progress) {
570+
Consumer<String> progress) {
570571
String devicePath = getDeviceName(registryId, deviceId);
571572
Operation operation = cloudModel.operation;
572573
Resource_type type = ofNullable(cloudModel.resource_type).orElse(Resource_type.DEVICE);
@@ -640,7 +641,7 @@ private StateNotificationConfig stateNotificationConfig() {
640641
}
641642

642643
private CloudModel unbindAndDelete(String registryId, Device device, CloudModel request,
643-
Consumer<Integer> progress) {
644+
Consumer<String> progress) {
644645
try {
645646
final Set<String> unbindIds = ReflectProcessor.isLegacyRequest(request)
646647
? legacyFindGateways(registryId, device)
@@ -685,7 +686,7 @@ private Set<String> legacyFindGateways(String registryId, Device device) {
685686
}
686687

687688
private CloudModel unbindAndDeleteCore(String registryId, Device device, Set<String> unbindIds,
688-
Consumer<Integer> progress) {
689+
Consumer<String> progress) {
689690
String deviceId = requireNonNull(device.toBuilder().getId(), "unspecified device id");
690691
try {
691692
ifNotNullThen(unbindIds, ids -> unbindGatewayDevices(registryId, device, ids, progress));
@@ -704,20 +705,25 @@ private CloudModel unbindAndDeleteCore(String registryId, Device device, Set<Str
704705
}
705706

706707
private void bindDevicesGateways(String registryId, Set<String> gatewayIds,
707-
Set<String> deviceIds, boolean toBind, Consumer<Integer> progress) {
708+
Set<String> deviceIds, boolean toBind, Consumer<String> progress) {
708709
String opCode = toBind ? "Binding" : "Unbinding";
709710
info("%s %s: gateways %s devices %s", opCode, registryId, gatewayIds, deviceIds);
710711
AtomicInteger opCount = new AtomicInteger(0);
712+
ifNotNullThen(progress,
713+
p -> p.accept(format("%s %d devices on %d gateways", opCode, deviceIds.size(),
714+
gatewayIds.size())));
711715
gatewayIds.forEach(gatewayId -> {
712716
deviceIds.forEach(deviceId -> {
713717
ifTrueThen(opCount.incrementAndGet() % OP_PROGRESS_BATCH_SIZE == 0,
714-
() -> ifNotNullThen(progress, p -> p.accept(opCount.get())));
718+
() -> ifNotNullThen(progress,
719+
p -> p.accept(format("%s %d devices...", opCode, opCount.get()))));
715720
ifTrueThen(toBind,
716721
() -> bindDevice(registryId, gatewayId, deviceId),
717722
() -> unbindDevice(registryId, gatewayId, deviceId));
718723
});
719724
});
720-
ifTrueThen(opCount.get() > 0, () -> ifNotNullThen(progress, p -> p.accept(opCount.get())));
725+
ifTrueThen(opCount.get() > 0, () -> ifNotNullThen(progress, p -> p.accept(
726+
format("Completed binding %d devices.", opCount.get()))));
721727
}
722728

723729
private void unbindDevice(String registryId, String gatewayId, String proxyId) {
@@ -734,7 +740,7 @@ private void unbindDevice(String registryId, String gatewayId, String proxyId) {
734740
}
735741

736742
private boolean unbindGatewayDevices(String registryId, Device gatewayDevice,
737-
Set<String> unbindIds, Consumer<Integer> progress) {
743+
Set<String> unbindIds, Consumer<String> progress) {
738744
try {
739745
ImmutableSet<String> gatewayIds = ImmutableSet.of(gatewayDevice.toBuilder().getId());
740746
bindDevicesGateways(registryId, gatewayIds, unbindIds, false, progress);

udmis/src/main/java/com/google/bos/udmi/service/access/DynamicIotAccessProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public CloudModel listDevices(String registryId, Consumer<String> progress) {
152152

153153
@Override
154154
public CloudModel modelDevice(String registryId, String deviceId, CloudModel cloudModel,
155-
Consumer<Integer> progress) {
155+
Consumer<String> progress) {
156156
debug("%s iot device %s/%s, %s %s", cloudModel.operation, registryId, deviceId,
157157
cloudModel.blocked, cloudModel.num_id);
158158
return getProviderFor(registryId, deviceId).modelDevice(registryId, deviceId, cloudModel,

udmis/src/main/java/com/google/bos/udmi/service/access/ImplicitIotAccessProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ private Map<String, CloudModel> listBoundDevices(String registryId, String gatew
326326

327327
@Override
328328
public CloudModel modelDevice(String registryId, String deviceId, CloudModel cloudModel,
329-
Consumer<Integer> progress) {
329+
Consumer<String> progress) {
330330
Operation operation = cloudModel.operation;
331331
Resource_type type = ofNullable(cloudModel.resource_type).orElse(Resource_type.DEVICE);
332332
checkState(type == DEVICE || type == GATEWAY, "unexpected resource type " + type);

udmis/src/main/java/com/google/bos/udmi/service/access/IotAccessProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static IotAccessProvider from(IotAccess iotAccess) {
6262
CloudModel listDevices(String registryId, Consumer<String> progress);
6363

6464
CloudModel modelDevice(String registryId, String deviceId,
65-
CloudModel cloudModel, Consumer<Integer> progress);
65+
CloudModel cloudModel, Consumer<String> progress);
6666

6767
CloudModel modelRegistry(String registryId, String deviceId, CloudModel cloudModel);
6868

udmis/src/main/java/com/google/bos/udmi/service/access/LocalIotAccessProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public CloudModel listDevices(String registryId, Consumer<String> progress) {
8989

9090
@Override
9191
public CloudModel modelDevice(String registryId, String deviceId, CloudModel cloudModel,
92-
Consumer<Integer> progress) {
92+
Consumer<String> progress) {
9393
throw new RuntimeException("Not yet implemented");
9494
}
9595

udmis/src/main/java/com/google/bos/udmi/service/access/PubSubIotAccessProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public CloudModel listDevices(String registryId, Consumer<String> progress) {
144144

145145
@Override
146146
public CloudModel modelDevice(String registryId, String deviceId, CloudModel cloudModel,
147-
Consumer<Integer> progress) {
147+
Consumer<String> progress) {
148148
throw new RuntimeException("modelDevice not implemented for PubSub");
149149
}
150150

udmis/src/main/java/com/google/bos/udmi/service/core/ReflectProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private CloudModel queryCloudDevice(Envelope attributes) {
260260

261261
private CloudModel queryCloudRegistry(Envelope attributes) {
262262
CloudModel cloudModel = iotAccess.listDevices(attributes.deviceRegistryId,
263-
progress -> reflectUdmiLog(attributes, format("Fetched %d devices...", progress)));
263+
progress -> reflectUdmiLog(attributes, progress));
264264
cloudModel.operation = READ;
265265
return cloudModel;
266266
}
@@ -303,7 +303,7 @@ private CloudModel updateModel(Envelope attributes, CloudModel request) {
303303
return iotAccess.modelRegistry(deviceRegistryId, deviceId, request);
304304
} else {
305305
return iotAccess.modelDevice(deviceRegistryId, deviceId, request, progress ->
306-
reflectUdmiLog(attributes, format("Processed %d entries for %s...", progress, deviceId)));
306+
reflectUdmiLog(attributes, progress));
307307
}
308308
}
309309

validator/src/main/java/com/google/daq/mqtt/registrar/Registrar.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public class Registrar {
173173
private SiteModel siteModel;
174174
private boolean queryOnly;
175175
private boolean strictWarnings;
176+
private boolean doNotUpdate;
176177

177178
/**
178179
* Main entry point for registrar.
@@ -277,21 +278,26 @@ private void setRunnerThreads(String argValue) {
277278
@CommandLineOption(short_form = "-d", description = "Delete (known) devices")
278279
private void setDeleteDevices() {
279280
checkNotNull(projectId, "delete devices specified with no target project");
280-
this.deleteDevices = true;
281-
this.updateCloudIoT = true;
281+
deleteDevices = true;
282+
updateCloudIoT = true;
282283
}
283284

284285
@CommandLineOption(short_form = "-i", description = "Instantiate extra (unknown) devices")
285286
private void setInstantiateExtras() {
286-
this.instantiateExtras = true;
287-
this.updateCloudIoT = true;
287+
instantiateExtras = true;
288+
updateCloudIoT = true;
288289
}
289290

290291
@CommandLineOption(short_form = "-x", description = "Expunge (unknown) devices")
291292
private void setExpungeDevices() {
292293
checkNotNull(projectId, "expunge devices specified with no target project");
293-
this.expungeDevices = true;
294-
this.updateCloudIoT = true;
294+
expungeDevices = true;
295+
updateCloudIoT = true;
296+
}
297+
298+
@CommandLineOption(short_form = "-z", description = "Do not update existing devices")
299+
private void setDoNotUpdate() {
300+
doNotUpdate = true;
295301
}
296302

297303
@CommandLineOption(short_form = "-e", arg_name = "suffix", description = "Set registry suffix")
@@ -787,6 +793,11 @@ private boolean processLocalDevice(String localName, AtomicInteger processedDevi
787793
System.err.println("Skipping active device " + localDevice.getDeviceId());
788794
return false;
789795
}
796+
797+
if (doNotUpdate && cloudModels.containsKey(localName)) {
798+
return false;
799+
}
800+
790801
Instant start = Instant.now();
791802
int count = processedDeviceCount.incrementAndGet();
792803
boolean created = false;

0 commit comments

Comments
 (0)