From cfa710e92fd6f0833e5d9e10d66c3434d990bca9 Mon Sep 17 00:00:00 2001 From: Trevor Pering Date: Fri, 20 Sep 2024 09:48:16 -0700 Subject: [PATCH] Adding metadata extraction --- common/src/main/java/com/google/udmi/util/GeneralUtils.java | 5 ++--- common/src/main/java/com/google/udmi/util/SiteModel.java | 6 ++---- .../bos/udmi/service/access/ImplicitIotAccessProvider.java | 4 ++++ .../main/java/com/google/daq/mqtt/registrar/Registrar.java | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/google/udmi/util/GeneralUtils.java b/common/src/main/java/com/google/udmi/util/GeneralUtils.java index 67982a5b01..e64152ed33 100644 --- a/common/src/main/java/com/google/udmi/util/GeneralUtils.java +++ b/common/src/main/java/com/google/udmi/util/GeneralUtils.java @@ -61,14 +61,13 @@ public class GeneralUtils { .enable(SerializationFeature.INDENT_OUTPUT) .setDateFormat(new ISO8601DateFormat()) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) - .setSerializationInclusion(JsonInclude.Include.NON_NULL); + .setSerializationInclusion(Include.NON_NULL); public static final ObjectMapper OBJECT_MAPPER_RAW = OBJECT_MAPPER.copy() .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS) .enable(Feature.ALLOW_TRAILING_COMMA) .enable(Feature.STRICT_DUPLICATE_DETECTION) - .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .setSerializationInclusion(Include.NON_NULL); + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); public static final ObjectMapper OBJECT_MAPPER_STRICT = OBJECT_MAPPER_RAW.copy() .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) diff --git a/common/src/main/java/com/google/udmi/util/SiteModel.java b/common/src/main/java/com/google/udmi/util/SiteModel.java index 87f4743fea..cef15ed570 100644 --- a/common/src/main/java/com/google/udmi/util/SiteModel.java +++ b/common/src/main/java/com/google/udmi/util/SiteModel.java @@ -33,7 +33,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.util.ISO8601DateFormat; import com.google.common.collect.ImmutableList; - import com.google.daq.mqtt.util.ExceptionMap; import java.io.File; import java.util.Arrays; @@ -71,8 +70,8 @@ public class SiteModel { public static final String DEVICES_DIR = "devices"; public static final String REFLECTOR_DIR = "reflector"; public static final String METADATA_JSON = "metadata.json"; - public static final String EXTRA_DEVICES_BASE = "extras"; - public static final String EXTRA_DEVICES_FORMAT = EXTRA_DEVICES_BASE + "/%s"; + public static final String EXTRAS_DIR = "extras"; + public static final String EXTRA_DEVICES_FORMAT = EXTRAS_DIR + "/%s"; public static final String NORMALIZED_JSON = "metadata_norm.json"; public static final String SITE_DEFAULTS_FILE = "site_defaults.json"; public static final String REGISTRATION_SUMMARY_BASE = "out/registration_summary"; @@ -89,7 +88,6 @@ public class SiteModel { private static final Pattern IOT_CORE_PATTERN = Pattern.compile( "projects/(.*)/locations/(.*)/registries/(.*)/devices/(.*)"); private static final Pattern MQTT_PATTERN = Pattern.compile("/r/(.*)/d/(.*)"); - private static final String EXTRAS_DIR = "extras"; private static final String CLOUD_IOT_CONFIG_JSON = "cloud_iot_config.json"; private static final Pattern SPEC_PATTERN = Pattern.compile( "(//([a-z]+)/)?([a-z-]+)(/([a-z0-9]+))?"); diff --git a/udmis/src/main/java/com/google/bos/udmi/service/access/ImplicitIotAccessProvider.java b/udmis/src/main/java/com/google/bos/udmi/service/access/ImplicitIotAccessProvider.java index 81c10bc681..7eea7051ae 100644 --- a/udmis/src/main/java/com/google/bos/udmi/service/access/ImplicitIotAccessProvider.java +++ b/udmis/src/main/java/com/google/bos/udmi/service/access/ImplicitIotAccessProvider.java @@ -11,9 +11,11 @@ import static com.google.udmi.util.GeneralUtils.ifNullThen; import static com.google.udmi.util.GeneralUtils.ifTrueThen; import static com.google.udmi.util.GeneralUtils.isNullOrNotEmpty; +import static com.google.udmi.util.GeneralUtils.requireNull; import static com.google.udmi.util.JsonUtil.asMap; import static com.google.udmi.util.JsonUtil.isoConvert; import static com.google.udmi.util.JsonUtil.stringify; +import static com.google.udmi.util.JsonUtil.stringifyTerse; import static java.lang.String.format; import static java.util.Objects.requireNonNull; import static java.util.Optional.ofNullable; @@ -201,6 +203,8 @@ private Map toDeviceMap(CloudModel cloudModel, String createdAt) ifNotNullThen(createdAt, x -> properties.put(CREATED_AT_PROPERTY, createdAt)); properties.put(RESOURCE_TYPE_PROPERTY, ofNullable(cloudModel.resource_type).orElse(DEVICE).toString()); + requireNull(cloudModel.metadata_str, "unexpected metadata_str content"); + properties.put(METADATA_STR_KEY, stringifyTerse(cloudModel.metadata)); properties.put(BLOCKED_PROPERTY, booleanString(cloudModel.blocked)); ifNotNullThen(cloudModel.num_id, id -> properties.put(NUM_ID_PROPERTY, id)); ifTrueThen(!cloudModel.credentials.isEmpty(), () -> { diff --git a/validator/src/main/java/com/google/daq/mqtt/registrar/Registrar.java b/validator/src/main/java/com/google/daq/mqtt/registrar/Registrar.java index f7aee21537..559f91564d 100644 --- a/validator/src/main/java/com/google/daq/mqtt/registrar/Registrar.java +++ b/validator/src/main/java/com/google/daq/mqtt/registrar/Registrar.java @@ -817,7 +817,7 @@ private CloudModel processExtra(String extraName, AtomicInteger alreadyBlocked) } private void reapExtraDevices(Set current) { - File extrasDir = new File(siteDir, SiteModel.EXTRA_DEVICES_BASE); + File extrasDir = new File(siteDir, SiteModel.EXTRAS_DIR); String[] existing = ofNullable(extrasDir.list()).orElse(new String[0]); Set previous = Arrays.stream(existing).collect(Collectors.toSet()); difference(previous, current).forEach(expired -> {