Skip to content

Commit 8792d14

Browse files
authored
Updates to registrar tool (#1075)
1 parent 0c8962b commit 8792d14

File tree

43 files changed

+336
-183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+336
-183
lines changed

.gencode_hash.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fa94295bc0c97518634c7232e7e55e1c923d7eb42bfa3004651ed183e1ccbb40 gencode/java/u
4141
2e7c0912c6f43b8dae9e9149b7361d3640bde4f01de6e13d8e487fe01be2c2c0 gencode/java/udmi/schema/Bucket.java
4242
79b678b524b3edce833fe56691dcd8c47b53a12f7491e761121408e6e7ceb5e3 gencode/java/udmi/schema/BuildingTranslation.java
4343
27ca4f61bd43beca0871b465f3906785a1030a4526605f1fa9b10e3adda8df05 gencode/java/udmi/schema/CapabilityValidationState.java
44-
ec164962f2f00924ecb41ae07c2e01d6bf027951ccc605e0210988370b987973 gencode/java/udmi/schema/Category.java
44+
224c6be2cb6d90a47d181fa30ba1418cf6cc5385ec1c010fe112728382a70216 gencode/java/udmi/schema/Category.java
4545
65a892c39e687a00e66e02f7ace2e17f07d3633a1b20b3dfd1e4ac3ec72a8788 gencode/java/udmi/schema/CloudConfigModel.java
4646
5fcf687fa0167889a63a1fbaafc8c3f2d9ac16609c8e804fdce8171903c37a1f gencode/java/udmi/schema/CloudModel.java
4747
051b4a7c85a2f1a310787f88f336be2fc9b87c99a94ac2f970955d2fe1daa931 gencode/java/udmi/schema/CloudQuery.java

bin/pull_mqtt

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ sudo mosquitto_sub $SERVER_OPTS -R -F "%j" -t $topic_filter |
5555
out_path=$OUT_BASE/$registryId/devices/$deviceId/${timepath}/${usetime}_${subFolder}_${subType}
5656
out_dir=$(dirname $out_path)
5757
mkdir -p $out_dir
58-
echo $(realpath --relative-to $OUT_BASE $out_path).json $topic
58+
echo $(realpath --relative-to $UDMI_ROOT $out_path).json
5959
echo $json > $out_path.json
6060
echo {} | jq ".deviceRegistryId=\"$registryId\" | \
6161
.subFolder=\"$subFolder\" |
6262
.subType=\"$subType\" |
6363
.deviceId=\"$deviceId\"" > $out_path.attr
64+
echo $topic > $out_path.mqtt
6465
done
6566

6667
echo Finished mqtt message capture at $(date -u -Is)

bin/test_regclean

+36-20
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@ pubber_config=/tmp/pubber_config.json
3434
pubber_log=out/pubber.log
3535
serial_no=clean-$RANDOM
3636
device_id=AHU-1
37+
gateway_id=GAT-123
3738
fake_id=ACME-2301
3839
fake_device=$site_path/devices/$fake_id
3940
fake_metadata=$fake_device/metadata.json
4041
device_dir=$site_path/devices/$device_id
4142

4243
function check_csv_file {
43-
expected=$1
44+
the_device=$1
45+
expected=$2
4446
more $csv_file | cat
4547

46-
fake_state=`fgrep $fake_id $csv_file | cut -d, -f3`
47-
[[ ${fake_state# } == $expected ]] || fail Device $fake_id not $expected
48-
[[ `fgrep BLOCK $csv_file | wc -l` -le 1 ]] || fail More than one blocked device
48+
state=`fgrep $the_device $csv_file | cut -d, -f3`
49+
[[ ${state# } == $expected ]] || fail Device $the_device not $expected
50+
[[ `fgrep BLOCK $csv_file | wc -l` -le 1 ]] || fail More than one matching blocked device
4951

5052
device_num_id=`fgrep AHU-22 $csv_file | cut -d, -f2`
51-
[[ $(echo $device_num_id) == $expected_num_id ]] || fail Device num_id $device_num_id does not match expected $expected_num_id
53+
[[ $(echo $device_num_id) == $expected_num_id ]] || fail Device AHU-22 num_id $device_num_id does not match expected $expected_num_id
5254

5355
sns_line=`fgrep -n SNS-4 $csv_file | cut -d: -f1`
5456
[[ $sns_line == 6 ]] || fail Device SNS-4 not found on expected output csv line, was $sns_line expected 6
@@ -168,45 +170,55 @@ jq '.iot_provider = "dynamic"' $iot_config | sponge $iot_config
168170
jq '.project_id = "this-is-not-right"' $iot_config | sponge $iot_config
169171
jq '.udmi_namespace = "bunny"' $iot_config | sponge $iot_config
170172

171-
echo Create one device to test that it works...
172-
echo bin/registrar $site_args $device_id
173-
bin/registrar $site_args $device_id
174-
175-
echo Check $device_id to make sure that it works...
176-
echo bin/pubber $pubber_config
177-
result=0
178-
timeout 30s bin/pubber $pubber_config 2> $pubber_log || result=$?
179-
[[ $result == 0 ]] || fail Improper pubber failure code $result
180-
181173
echo Clean out the registry to make sure devices get removed...
182174
echo bin/registrar $site_args -d -x
183175
bin/registrar $site_args -d -x
184176

185177
echo Checking reported cloud version info
186178
jq .cloud_version.udmi_ref $output_file
187179

188-
echo Pubber config from $pubber_config:
189-
cat $pubber_config
180+
echo Cleaning message capture output out/registries/
181+
rm -rf out/registries/
190182

191183
echo Check for failure in running pubber...
192184
echo bin/pubber $pubber_config
193185
result=0
194186
timeout 30s bin/pubber $pubber_config 2> $pubber_log || result=$?
195187
[[ $result == 255 ]] || fail Improper pubber failure code $result
196188

197-
echo Now recreate the registry from scratch!
189+
echo Pubber config from $pubber_config:
190+
cat $pubber_config
191+
192+
echo Create one device to test that it works...
193+
echo bin/registrar $site_args $device_id
194+
bin/registrar $site_args $device_id
195+
196+
echo Check $device_id to make sure that it works...
197+
echo bin/pubber $pubber_config
198+
result=0
199+
timeout 30s bin/pubber $pubber_config 2> $pubber_log || result=$?
200+
[[ $result == 0 ]] || fail Improper pubber failure code $result
201+
202+
echo Now recreate the complete registry from scratch!
198203
echo bin/registrar $site_args
199204
bin/registrar $site_args
200205

201-
check_csv_file ERROR
206+
check_csv_file $fake_id ERROR
202207

203208
echo Remove fake device from site model, so it will be blocked
204209
rm -rf $fake_device
205210
echo $UDMI_ROOT/bin/registrar $full_args -b
206211
# Test from other-directory for proper path handling
207212
(cd /tmp; $UDMI_ROOT/bin/registrar $full_args -b)
208213

209-
check_csv_file BLOCK
214+
check_csv_file $fake_id BLOCK
215+
check_csv_file $gateway_id READ
216+
217+
echo Delete the gateway to make sure stuff happens...
218+
echo bin/registrar $site_args -d $gateway_id
219+
bin/registrar $site_args -d $gateway_id
220+
221+
check_csv_file $gateway_id CLEAN
210222

211223
echo Check $device_id again, but this time with success...
212224
echo bin/pubber $pubber_config
@@ -216,6 +228,10 @@ timeout 30s bin/pubber $pubber_config 2> $pubber_log || result=$?
216228

217229
echo Successful pubber check passed on clean registry.
218230

231+
echo Registering everything to end up with everything in place
232+
echo bin/registrar $site_args
233+
bin/registrar $site_args
234+
219235
if [[ -n $solo_arg ]]; then
220236
echo Deleting singular entry $solo_arg...
221237
echo bin/registrar $site_args -d $solo_arg

bin/test_registrar

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
#!/bin/bash -e
22

3-
ROOT_DIR=$(dirname $0)/..
4-
cd ${ROOT_DIR}
3+
UDMI_ROOT=$(dirname $0)/..
4+
cd ${UDMI_ROOT}
5+
source etc/shell_common.sh
56

67
bin/clone_model
78

89
TEST_SITE=sites/udmi_site_model
910
find $TEST_SITE -name out | xargs rm -rf
1011
(cd $TEST_SITE; git checkout .)
1112

12-
exit_status=clean
13+
exit_status=
1314
echo '###################################' starting test section processing
1415

1516
bin/registrar ${TEST_SITE}
1617

1718
output_file=${TEST_SITE}/out/registration_summary.json
1819

19-
clean_devices=$(jq -r .Clean[] $output_file | wc -l)
20+
clean_devices=$(jq -r .status $output_file | fgrep CLEAN | wc -l)
2021
EXPECTED_CLEAN=3 # AHU-1 has a bad site location, so does not match as clean.
2122

2223
echo Found ${clean_devices} clean devices, expecting $EXPECTED_CLEAN.
23-
[ "${clean_devices}" == $EXPECTED_CLEAN ] || exit_status=expected_clean
24+
[ "${clean_devices}" == $EXPECTED_CLEAN ] || exit_status+=" expected_clean"
2425
echo '###################################' end test section expected_clean: $exit_status
2526

2627
device_files=$(ls -d ${TEST_SITE}/devices/* | fgrep -v XXX)
@@ -33,7 +34,7 @@ generated_config=sites/udmi_site_model/devices/AHU-1/out/generated_config.json
3334
[[ "Degrees-Celsius" == $(jq -r .pointset.points.filter_differential_pressure_sensor.units $generated_config) ]] || (echo Units mismatch; touch $error_file)
3435
if [[ -f $error_file ]]; then
3536
echo Errors found testing generated config.
36-
exit_status=generated_config
37+
exit_status+=" generated_config"
3738
fi
3839
echo '###################################' end test section generated_config: $exit_status
3940

@@ -62,7 +63,7 @@ function check_norm {
6263
sm_devices=$(check_norm)
6364
echo Devices with correct site_defaults: ${sm_devices}
6465

65-
[ "${sm_devices}" == "${device_files}" ] || exit_status=site_defaults
66+
[ "${sm_devices}" == "${device_files}" ] || exit_status+=" site_defaults"
6667
echo '###################################' end test section site_defaults: $exit_status
6768

6869
mv ${TEST_SITE}/site_defaults.json ${TEST_SITE}/_site_defaults.json
@@ -73,19 +74,19 @@ base_devices=sites/udmi_site_model/devices/AHU-1
7374
sm_devices=$(check_norm)
7475
echo Devices with correct no-defaults site_defaults: ${sm_devices}
7576
echo Expecting: $base_devices
76-
[ "${sm_devices}" == "${base_devices}" ] || exit_status=no_defaults
77+
[ "${sm_devices}" == "${base_devices}" ] || exit_status+=" no_defaults"
7778
echo '###################################' end test section no_defaults: $exit_status
7879

7980

8081
config_points=`jq '.pointset.points | length' $TEST_SITE/devices/AHU-22/out/generated_config.json`
81-
[[ $config_points == 3 ]] || exit_status=missing_points
82+
[[ $config_points == 3 ]] || exit_status+=" missing_points"
8283
echo '###################################' end test section missing_points: $exit_status
8384

8485
config_points=`jq .pointset.points $TEST_SITE/devices/SNS-4/out/generated_config.json`
85-
[[ $config_points == null ]] || exit_status=configured_points
86+
[[ $config_points == null ]] || exit_status+=" configured_points"
8687
echo '###################################' end test section configured_points: $exit_status
8788

8889
mv ${TEST_SITE}/_site_defaults.json ${TEST_SITE}/site_defaults.json
8990

90-
echo Done with registrar tests, results are \'$exit_status\' \(expected to be \'clean\'\)
91-
[[ $exit_status == clean ]]
91+
[[ -z $exit_status ]] || fail Failed registrar tests: $exit_status
92+
echo Registrar standalone tests passed.

bin/test_runlocal

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ cat out/message_capture.log
1212

1313
echo Captured $(wc -l out/message_capture.log) messages.
1414

15-
systems=$(find out/registries/ -name \*update_model.json | xargs jq .system | fgrep -v null | wc -l)
16-
[[ $systems == 196 ]] || fail Expected 196 system operations, found $systems
15+
systems=$(find out/registries/ -name \*update_model.json | xargs jq .system | fgrep -v null | wc -l) || true
16+
[[ $systems == 269 ]] || fail Expected 269 system operations, found $systems
1717

18-
deletes=$(find out/registries/ -name \*update_model.json | xargs jq .cloud.operation | fgrep DELETE | wc -l)
18+
deletes=$(find out/registries/ -name \*update_model.json | xargs jq .cloud.operation | fgrep DELETE | wc -l) || true
1919
[[ $deletes == 1 ]] || fail Expected 1 delete operations, found $deletes
2020

21-
binds=$(find out/registries/ -name \*update_model.json | xargs jq .cloud.operation | fgrep BIND | wc -l)
22-
[[ $binds == 2 ]] || fail Expected 2 bind operations, found $binds
21+
binds=$(find out/registries/ -name \*update_model.json | xargs jq .cloud.operation | fgrep BIND | wc -l) || true
22+
[[ $binds == 3 ]] || fail Expected 3 bind operations, found $binds
23+
24+
blocks=$(find out/registries/ -name \*update_model.json | xargs jq .cloud.operation | fgrep BLOCK | wc -l) || true
25+
[[ $blocks == 1 ]] || fail Expected 1 block operations, found $blocks
2326

2427
echo Done with successful runlocal validation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.google.daq.mqtt.util;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Simple class for a keyed map of exceptions.
8+
*/
9+
public class ErrorMap extends HashMap<String, Exception> {
10+
11+
private final String description;
12+
13+
public ErrorMap(String description) {
14+
super();
15+
this.description = description;
16+
}
17+
18+
/**
19+
* Throw an exception if there's something in the map.
20+
*/
21+
public void throwIfNotEmpty() {
22+
if (!isEmpty()) {
23+
throw asException();
24+
}
25+
}
26+
27+
public ErrorMapException asException() {
28+
return new ErrorMapException();
29+
}
30+
31+
public class ErrorMapException extends RuntimeException {
32+
33+
ErrorMapException() {
34+
super(description);
35+
}
36+
37+
public Map<String, Exception> getMap() {
38+
return ErrorMap.this;
39+
}
40+
}
41+
}

common/src/main/java/com/google/daq/mqtt/util/ExceptionMap.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.google.udmi.util.GeneralUtils.multiTrim;
44

5+
import com.google.daq.mqtt.util.ErrorMap.ErrorMapException;
56
import java.io.ByteArrayOutputStream;
67
import java.io.IOException;
78
import java.io.OutputStream;
@@ -25,12 +26,18 @@ public class ExceptionMap extends RuntimeException {
2526
private static final byte[] SEPARATOR_BYTES = ": ".getBytes();
2627
private static final String ERROR_FORMAT_INDENT = " ";
2728

28-
final Map<String, Exception> exceptions = new TreeMap<>();
29+
final Map<ExceptionCategory, Exception> exceptions = new TreeMap<>();
2930

3031
public ExceptionMap(String description) {
3132
super(description);
3233
}
3334

35+
public enum ExceptionCategory {
36+
missing, extra, out, validation, loading, writing, site_metadata, initializing, sample,
37+
registering, envelope, credentials, samples, files, binding, creating, updating, schema,
38+
configuring, status
39+
}
40+
3441
/**
3542
* Format the given exception with indicated level.
3643
*
@@ -46,12 +53,15 @@ private static ErrorTree format(Throwable e, final String prefix, final String i
4653
errorTree.prefix = prefix;
4754
errorTree.message = multiTrim(e.getMessage());
4855
final String newPrefix = prefix + indent;
49-
if (e instanceof ExceptionMap) {
56+
if (e instanceof ExceptionMap exceptionMap) {
5057
if (e.getCause() != null) {
5158
errorTree.child = format(e.getCause(), newPrefix, indent);
5259
}
53-
((ExceptionMap) e)
54-
.forEach((key, sub) -> errorTree.children.put(key, format(sub, newPrefix, indent)));
60+
exceptionMap.forEach((key, sub) ->
61+
errorTree.children.put(key.toString(), format(sub, newPrefix, indent)));
62+
} else if (e instanceof ErrorMapException errorMap) {
63+
errorMap.getMap().forEach((key, sub) ->
64+
errorTree.children.put(key, format(sub, newPrefix, indent)));
5565
} else if (e instanceof ValidationException) {
5666
((ValidationException) e)
5767
.getCausingExceptions()
@@ -69,7 +79,7 @@ private static ErrorTree format(Throwable e, final String prefix, final String i
6979
return errorTree;
7080
}
7181

72-
private void forEach(BiConsumer<String, Exception> consumer) {
82+
private void forEach(BiConsumer<ExceptionCategory, Exception> consumer) {
7383
exceptions.forEach(consumer);
7484
}
7585

@@ -96,13 +106,13 @@ public void throwIfNotEmpty() {
96106
* @param key entry key
97107
* @param exception exception to add
98108
*/
99-
public void put(String key, Exception exception) {
109+
public void put(ExceptionCategory key, Exception exception) {
100110
if (exceptions.put(key, exception) != null) {
101111
throw new IllegalArgumentException("Exception key already defined: " + key);
102112
}
103113
}
104114

105-
public Stream<Map.Entry<String, Exception>> stream() {
115+
public Stream<Map.Entry<ExceptionCategory, Exception>> stream() {
106116
return exceptions.entrySet().stream();
107117
}
108118

@@ -118,7 +128,7 @@ public int size() {
118128
/**
119129
* Execute the action and capture into the map if it throws an exception.
120130
*/
121-
public void capture(String category, Runnable action) {
131+
public void capture(ExceptionCategory category, Runnable action) {
122132
try {
123133
action.run();
124134
} catch (Exception e) {

0 commit comments

Comments
 (0)