Skip to content

Commit

Permalink
Refactor common SiteModel class out of Pubber (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu committed Aug 9, 2022
1 parent f5d2786 commit c402885
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 121 deletions.
6 changes: 3 additions & 3 deletions .gencode_hash.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
f5cf191b0151d2142fa634d1deafb93e35041d5b9ae89b26beab32fa25fb9521 gencode/docs/config.html
e2944b13db5ff06be9caea51d03bca48f2cb093a8bb583dca14051255d34ea6b gencode/docs/envelope.html
90679d3d866579501e7aa00b515af05d42fc9fe399eafacaacf297d1e4a22884 gencode/docs/envelope.html
368f12b9b695b31d97e65509c461f56c12762ec50b9baff89fd7fd85713e8fce gencode/docs/event_discovery.html
8133e380e40f27c56accbffc665b2eeb56ec84a4da3b52ba7aa5e439c9c40572 gencode/docs/event_pointset.html
076eb51e75281ed065a9f0236a1e19b6bf3c277b51ceca84f6aa3f76d5bb6022 gencode/docs/event_system.html
Expand All @@ -25,7 +25,7 @@ d8a80ab3180d33bfa17564c969018e1d4350a47dbc70c4ae8a5abbfb25cfedc9 gencode/java/u
04112dd47b0f761131c276c67d3cd8b789d25e6716b5732be9fef14fc6831f1d gencode/java/udmi/schema/DiscoveryModel.java
9962b0eb7d5adf52af6160e9f3131f8eeb52ae9e518954dbb6aead1bcad0245e gencode/java/udmi/schema/DiscoveryState.java
090bbaf1508aa6ca8380af936af990673f300eb5a940c9e8ab94deb64efa2b7b gencode/java/udmi/schema/Entry.java
8f71ecd4c32044eccd74225006887d58827976f699ea03efaa5b7ed1b69849d0 gencode/java/udmi/schema/Envelope.java
cd362f94454eba8fd5ce3fce5d5e2b5f046d0dd9c35b01de69ef4d2e38413cc5 gencode/java/udmi/schema/Envelope.java
e9f5c77be81486b6b8c6d88f70f2d50583d8c3fafa2ac09ead80f44b8d5e751e gencode/java/udmi/schema/Event.java
aa0885ca43ab38c7597eacc38b7c512940a1a9fa061abd47d02c28e66b6fd93e gencode/java/udmi/schema/FamilyDiscoveryConfig.java
ae4a645f199c8e24b3303463d428ca17af7603ae9ae9238397a6a82e752ab454 gencode/java/udmi/schema/FamilyDiscoveryEvent.java
Expand Down Expand Up @@ -83,7 +83,7 @@ b461bdc24310ef972faf579b5be577b5af67fb0977d6afb4c42955211b26e3d5 gencode/python
9eab64849e04b25203d5da47856c3f8dda2b96903e4dc43ab932ee35014700bd gencode/python/udmi/schema/config_pointset.py
607c5047df878a1333df3ce88dcce34668959b0b315f6954bf1a4963dcf7839e gencode/python/udmi/schema/config_pointset_point.py
68685118163ea55f23938aafd10308faad1e2bf86c4f2fa9341cb53e19e2a260 gencode/python/udmi/schema/config_system.py
631371489cb1275517bebcc4040cbc655d18ca147ab540701b3fd9cedba138c5 gencode/python/udmi/schema/envelope.py
998ce105f88686f27b85f3630a396ed04b106f830c133a684ea5c505ca95b1c3 gencode/python/udmi/schema/envelope.py
1eb9019b9d0b4ff7de2df8beb625a4f89292d636ece0c02f160495c537bd6c2c gencode/python/udmi/schema/event.py
82182e3f569ad80dc0751027959c7db9135d10072fbe79f896d63a4cd2f4771f gencode/python/udmi/schema/event_discovery.py
ad33b91a7fabb4eed7e49c30a983a2106c96330facbe0f376f94d06e2263d6d0 gencode/python/udmi/schema/event_discovery_family.py
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.google.daq.mqtt.util;
package com.google.udmi.util;

/**
* Configuration parameters for a cloud connection.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package daq.pubber;
package com.google.udmi.util;

/**
* Configuration for the connection endpoint.
Expand Down
132 changes: 132 additions & 0 deletions common/src/main/java/com/google/udmi/util/SiteModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.google.udmi.util;

import static java.util.stream.Collectors.toMap;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.google.common.base.Preconditions;
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import udmi.schema.CloudModel.Auth_type;
import udmi.schema.Envelope;
import udmi.schema.GatewayModel;
import udmi.schema.Metadata;

public class SiteModel {

private static final String KEY_SITE_PATH_FORMAT = "%s/devices/%s/%s_private.pkcs8";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.setDateFormat(new ISO8601DateFormat())
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

final String sitePath;
private Map<String, Metadata> allMetadata;
private EndpointConfiguration endpointConfig;

public SiteModel(String sitePath) {
this.sitePath = sitePath;
}

public static EndpointConfiguration extractEndpointConfig(CloudIotConfig cloudIotConfig) {
EndpointConfiguration endpoint = new EndpointConfiguration();
endpoint.registryId = cloudIotConfig.registry_id;
endpoint.cloudRegion = cloudIotConfig.cloud_region;
return endpoint;
}

private static CloudIotConfig makeCloudIotConfig(Envelope attributes) {
CloudIotConfig cloudIotConfig = new CloudIotConfig();
cloudIotConfig.registry_id = Preconditions.checkNotNull(attributes.deviceRegistryId,
"deviceRegistryId");
cloudIotConfig.cloud_region = Preconditions.checkNotNull(attributes.deviceRegistryLocation,
"deviceRegistryLocation");
return cloudIotConfig;
}

public static EndpointConfiguration makeEndpointConfig(Envelope attributes) {
CloudIotConfig cloudIotConfig = makeCloudIotConfig(attributes);
return extractEndpointConfig(cloudIotConfig);
}

private Set<String> getAllDevices() {
Preconditions.checkState(sitePath != null, "sitePath not defined");
File devicesFile = new File(new File(sitePath), "devices");
File[] files = Preconditions.checkNotNull(devicesFile.listFiles(), "no files in site devices/");
return Arrays.stream(files).map(File::getName).collect(Collectors.toSet());
}

private void loadAllDeviceMetadata() {
allMetadata = getAllDevices().stream().collect(toMap(key -> key, this::loadDeviceMetadata));
}

private Metadata loadDeviceMetadata(String deviceId) {
Preconditions.checkState(sitePath != null, "sitePath not defined");
File devicesFile = new File(new File(sitePath), "devices");
File deviceDir = new File(devicesFile, deviceId);
File deviceMetadataFile = new File(deviceDir, "metadata.json");
try {
return OBJECT_MAPPER.readValue(deviceMetadataFile, Metadata.class);
} catch (Exception e) {
throw new RuntimeException(
"While reading metadata file " + deviceMetadataFile.getAbsolutePath(), e);
}
}

public Metadata getMetadata(String deviceId) {
return allMetadata.get(deviceId);
}

public void forEachDevice(BiConsumer<String, Metadata> consumer) {
allMetadata.forEach(consumer);
}

private void loadEndpointConfig(String projectId) {
Preconditions.checkState(sitePath != null,
"sitePath not defined in configuration");
File cloudConfig = new File(new File(sitePath), "cloud_iot_config.json");
try {
endpointConfig = extractEndpointConfig(
OBJECT_MAPPER.readValue(cloudConfig, CloudIotConfig.class));
endpointConfig.projectId = projectId;
} catch (Exception e) {
throw new RuntimeException("While reading config file " + cloudConfig.getAbsolutePath(), e);
}
}

public EndpointConfiguration getEndpointConfig() {
return endpointConfig;
}

public void initialize(String projectId) {
loadEndpointConfig(projectId);
loadAllDeviceMetadata();
}

public Auth_type getAuthType(String deviceId) {
return allMetadata.get(deviceId).cloud.auth_type;
}

public String getDeviceKeyFile(String deviceId) {
String gatewayId = findGateway(deviceId);
String keyDevice = gatewayId != null ? gatewayId : deviceId;
return String.format(KEY_SITE_PATH_FORMAT, sitePath,
keyDevice, getDeviceKeyPrefix(keyDevice));
}

private String findGateway(String deviceId) {
GatewayModel gateway = getMetadata(deviceId).gateway;
return gateway != null ? gateway.gateway_id : null;
}

private String getDeviceKeyPrefix(String targetId) {
Auth_type auth_type = getMetadata(targetId).cloud.auth_type;
return auth_type.value().startsWith("RS") ? "rsa" : "ec";
}
}
35 changes: 34 additions & 1 deletion gencode/docs/envelope.html

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

7 changes: 6 additions & 1 deletion gencode/java/udmi/schema/Envelope.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/envelope.py

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

1 change: 1 addition & 0 deletions pubber/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sourceSets {
main {
java {
srcDirs '../gencode/java'
srcDirs '../common/src/main/java'
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pubber/pubber.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<output url="file://$MODULE_DIR$/build/classes/java/main" />
<output-test url="file://$MODULE_DIR$/build/classes/java/test" />
<exclude-output />
<content url="file://$MODULE_DIR$/../common">
<sourceFolder url="file://$MODULE_DIR$/../common/src/main/java" isTestSource="false" />
</content>
<content url="file://$MODULE_DIR$/../gencode">
<sourceFolder url="file://$MODULE_DIR$/../gencode/java" isTestSource="false" />
</content>
Expand Down
2 changes: 2 additions & 0 deletions pubber/src/main/java/daq/pubber/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package daq.pubber;


import com.google.udmi.util.EndpointConfiguration;

/**
* General bucket of pubber configuration information.
*/
Expand Down
Loading

0 comments on commit c402885

Please sign in to comment.