Skip to content

Commit

Permalink
Fix Clearblade device blocking and increase config size limit (faucet…
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu authored Jan 31, 2024
1 parent d69c55c commit a7a3b9b
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 45 deletions.
6 changes: 3 additions & 3 deletions .gencode_hash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ d0db0ed95c6e2aef32cd55749a13053db2809503e82b20ee04b98fd7f58c1e93 gencode/docs/e
f6ca85990e940f109a48e35b25102d8860e5707a7b2145412046f44d599ef635 gencode/docs/event_pointset.html
110090929fdb5af934c852768d25467361a21be7d8a693de333219ce47fa83fb gencode/docs/event_system.html
4c15e549ded3e0489e1717e24a68a94b3995e43c2032ea7ca1c89deaa4f18c21 gencode/docs/event_validation.html
337a47ea354379d76462d801b91994092717274f83086691061947f836385805 gencode/docs/metadata.html
7a19fad4fa28e83e1dd872696bd5c98fda171866ffa825730795bdead1ab071d gencode/docs/metadata.html
1135e90f2b1922e60c7f991d6a65d4150ff6e7e82d6e307649040ae644644796 gencode/docs/monitoring.html
180b32717db748e164a185b163ef9a97aa83d9d6add306283d5b9852d04af947 gencode/docs/persistent_device.html
5d039d607af9ec75ee552dfe36b16c702687ea16f5663f41fc49b4533b86e00d gencode/docs/properties.html
Expand All @@ -38,7 +38,7 @@ fcbed49f1af8b791d8c52bcbe18f65521a79d9ac3eb33ec3afd9b342ab2bfc56 gencode/java/u
0a4f6bcd5065418c1cdc6c05b900b3de31744847d25b6ab6de7aabb1e724710e gencode/java/udmi/schema/BuildingTranslation.java
c47b17d70ed7fffae5cb829623088358eca22c8fa5613edd97518bed0b522620 gencode/java/udmi/schema/CapabilityValidationState.java
24e03efa125f374a785dc90132adec6b4e91abf0bf8cb1f9b4b7de80b27f3d09 gencode/java/udmi/schema/Category.java
8f30d92884cfd9ce376fa9fb4be3c1806ed59e9cf0e8fa9c6bf8ba61ac397e4c gencode/java/udmi/schema/CloudModel.java
f6a557eb73dfa077d7c5daf897bde345c7403232808608c39e24a0cbee2755c9 gencode/java/udmi/schema/CloudModel.java
aa3fc1777951cb1e1e2a68e4c31a9bdeeacc339cb191c347d88534b185fb04a4 gencode/java/udmi/schema/Config.java
cf9762392e02b5d04c6498963222cc0c00f7be6c3cd82bde3d063a5eceba2b65 gencode/java/udmi/schema/Connections.java
8164e93ccc76d78548d456890e064df8427a09bbcd50686b37a10d3ff5ad429f gencode/java/udmi/schema/Credential.java
Expand Down Expand Up @@ -168,7 +168,7 @@ dd30f748b5321223933c272d58f5fa6dc319912737255170322f0df3244732a7 gencode/python
4361f48caba6a948a5916f67c8168215721cb3cc2384649f0336804785f01f1d gencode/python/udmi/schema/event_validation.py
633c85b0999d969310918bb03e9a40a08960f420b862340045a0290f8f5c4fe5 gencode/python/udmi/schema/event_validation_device.py
02e464aec10ae7f8cf10045f6107e069de4b852a24cd8fae6a4290a01c0d2ac8 gencode/python/udmi/schema/metadata.py
a871eab6c89b9b9bd824cd5f4041f99f918aaa4b6775ce70b34d558f974322f2 gencode/python/udmi/schema/model_cloud.py
8324124d37edc203800ec722b6eb2d4b3baef836bd4bd72c221b499b8a4bd125 gencode/python/udmi/schema/model_cloud.py
805cc8dcb29732d1965bbd533b12d2bc4966d584f05b8a478dd2cac98fd99d52 gencode/python/udmi/schema/model_discovery.py
44d057dffcac3a994e7b894f8e1ebb643ba49d7dbfbb0bfea9cc04f97af0bc47 gencode/python/udmi/schema/model_discovery_family.py
bef91798566798ec70b2fe10ae52c909090de9095b92e2567da3cbdcec29cd2c gencode/python/udmi/schema/model_features.py
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/com/google/udmi/util/GeneralUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ public static String encodeBase64(byte[] payload) {
return Base64.getEncoder().encodeToString(payload);
}

/**
* TODO: Update to use varargs, and create non-supplier version.
*/
public static <T> T firstNonNull(T obj1, T obj2, Supplier<T> supplier) {
return ofNullable(ofNullable(obj1).orElse(obj2)).orElseGet(supplier);
}

/**
* Get a "friendly" (cause messages only) stack trace string. There is no science to this, it's
* just a hacky algorithm that turns a pedantically detailed Java stack trace into something
Expand Down
42 changes: 41 additions & 1 deletion gencode/docs/metadata.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion gencode/java/udmi/schema/CloudModel.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions gencode/python/udmi/schema/model_cloud.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions schema/model_cloud.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"blocked": {
"type": "boolean"
},
"detail": {
"type": "string"
},
"credentials": {
"type": "array",
"items": {
Expand Down Expand Up @@ -72,11 +75,13 @@
},
"operation": {
"enum": [
"ERROR",
"FETCH",
"CREATE",
"UPDATE",
"DELETE",
"BIND",
"ALLOW",
"BLOCK"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class ClearBladeIotAccessProvider extends IotAccessBase {
);
private static final String EMPTY_RETURN_RECEIPT = "-1";
private static final String RESOURCE_EXISTS = "-2";
private static final String BLOCKED_FIELD_MASK = "blocked";
private static final String UPDATE_FIELD_MASK = "blocked,credentials,metadata";
private static final GatewayConfig NON_GATEWAY_CONFIG = new GatewayConfig();
private static final GatewayConfig GATEWAY_CONFIG = GatewayConfig.newBuilder()
Expand Down Expand Up @@ -149,13 +150,8 @@ private static Entry<String, CloudModel> convertToEntry(Device device) {
return new SimpleEntry<>(deviceBuilder.getId(), cloudModel);
}

/**
* Get a numerical ID for ths device. This field is a legacy from the deprecated GCP IoT Core
* system, and so this provides a stable replacement value based off a hash of the device name
* (including project and registry) for systems that require it to exist.
*/
private static String extractNumId(Device device) {
return format("%d", Math.abs(Objects.hash(device.toBuilder().getName())));
return device.toBuilder().getNumId();
}

@Nullable
Expand Down Expand Up @@ -316,7 +312,7 @@ private CloudModel createDevice(String registryId, Device device) {
.build();
requireNonNull(deviceManagerClient.createDevice(request),
"create device failed for " + parent);
cloudModel.num_id = extractNumId(device);
cloudModel.num_id = hashedDeviceId(registryId, device.toBuilder().getId());
return cloudModel;
} catch (ApplicationException applicationException) {
if (applicationException.getMessage().contains("ALREADY_EXISTS")) {
Expand Down Expand Up @@ -420,7 +416,8 @@ private CloudModel modelDevice(String registryId, String deviceId, CloudModel cl
case UPDATE -> updateDevice(registryId, device);
case DELETE -> unbindAndDelete(registryId, device);
case BIND -> bindDeviceToGateway(registryId, deviceId, cloudModel);
default -> throw new RuntimeException("Unknown operation " + operation);
case BLOCK -> blockDevice(registryId, device);
default -> throw new RuntimeException("Unknown device operation " + operation);
};
} catch (Exception e) {
throw new RuntimeException("While " + operation + "ing " + devicePath, e);
Expand Down Expand Up @@ -501,7 +498,7 @@ private CloudModel unbindAndDelete(String registryId, Device device) {
deviceManagerClient.deleteDevice(request);
CloudModel cloudModel = new CloudModel();
cloudModel.operation = DELETE;
cloudModel.num_id = extractNumId(device);
cloudModel.num_id = hashedDeviceId(registryId, deviceId);
return cloudModel;
} catch (Exception e) {
throw new RuntimeException(format("While deleting %s/%s", registryId, deviceId), e);
Expand Down Expand Up @@ -533,6 +530,25 @@ private void unbindGatewayDevices(String registryId, Device device) {
}
}

private CloudModel blockDevice(String registryId, Device device) {
DeviceManagerClient deviceManagerClient = getDeviceManagerClient();
String deviceId = device.toBuilder().getId();
String name = getDeviceName(registryId, deviceId);
Device fullDevice = device.toBuilder().setName(name).setBlocked(true).build();
try {
UpdateDeviceRequest request =
UpdateDeviceRequest.Builder.newBuilder().setDevice(fullDevice).setName(name)
.setUpdateMask(BLOCKED_FIELD_MASK).build();
requireNonNull(deviceManagerClient.updateDevice(request), "Invalid RPC response");
CloudModel cloudModel = new CloudModel();
cloudModel.operation = Operation.BLOCK;
cloudModel.num_id = hashedDeviceId(registryId, deviceId);
return cloudModel;
} catch (Exception e) {
throw new RuntimeException("While updating " + deviceId, e);
}
}

private CloudModel updateDevice(String registryId, Device device) {
DeviceManagerClient deviceManagerClient = getDeviceManagerClient();
String deviceId = device.toBuilder().getId();
Expand All @@ -545,13 +561,20 @@ private CloudModel updateDevice(String registryId, Device device) {
requireNonNull(deviceManagerClient.updateDevice(request), "Invalid RPC response");
CloudModel cloudModel = new CloudModel();
cloudModel.operation = Operation.UPDATE;
cloudModel.num_id = extractNumId(device);
cloudModel.num_id = hashedDeviceId(registryId, deviceId);
return cloudModel;
} catch (Exception e) {
throw new RuntimeException("While updating " + deviceId, e);
}
}

/**
* Create pseudo device numerical id that can be used for operation verification.
*/
private String hashedDeviceId(String registryId, String deviceId) {
return String.valueOf(Objects.hash(registryId, deviceId));
}

@Override
public void activate() {
super.activate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public CloudModel modelResource(String registryId, String deviceId, CloudModel c
case BIND:
return bindDeviceToGateway(registryId, deviceId, cloudModel);
default:
throw new RuntimeException("Unknown operation " + operation);
throw new RuntimeException("Unknown resource operation " + operation);
}
} catch (Exception e) {
throw new RuntimeException("While " + operation + "ing " + devicePath, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
public abstract class IotAccessBase extends ContainerBase {

public static final int MAX_CONFIG_LENGTH = 65535;
public static final int MAX_CONFIG_LENGTH = 262144;
public static final TemporalAmount REGION_RETRY_BACKOFF = Duration.ofSeconds(30);
protected static final String EMPTY_JSON = "{}";
private static final long REGISTRY_COMMAND_BACKOFF_SEC = 60;
Expand Down
Loading

0 comments on commit a7a3b9b

Please sign in to comment.