Skip to content

Commit

Permalink
Create upversioning utility to upgrade schema and test messages versi…
Browse files Browse the repository at this point in the history
…on and change these to 1.4.0 (faucetsdn#477)
  • Loading branch information
noursaidi authored Oct 27, 2022
1 parent 2a30cff commit 7755958
Show file tree
Hide file tree
Showing 155 changed files with 471 additions and 93 deletions.
6 changes: 3 additions & 3 deletions .gencode_hash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ d8a80ab3180d33bfa17564c969018e1d4350a47dbc70c4ae8a5abbfb25cfedc9 gencode/java/u
04112dd47b0f761131c276c67d3cd8b789d25e6716b5732be9fef14fc6831f1d gencode/java/udmi/schema/DiscoveryModel.java
9962b0eb7d5adf52af6160e9f3131f8eeb52ae9e518954dbb6aead1bcad0245e gencode/java/udmi/schema/DiscoveryState.java
b90ab40a281c5e383881629bd8a791ba3d30491e06ebc623a476e2735a3f6c3a gencode/java/udmi/schema/EndpointConfiguration.java
885859ea9de29217186c34f3a90b112411d1beddaf58a88b85e7934cd3c92b6f gencode/java/udmi/schema/Entry.java
6fea7710d4eb64bfc568820f7063a89ce7808853ef7a56763fa90f323e09362f gencode/java/udmi/schema/Entry.java
017f8e237efa959b81d72f3dd2e78b915856198ceef02c041c657b30df93b7c2 gencode/java/udmi/schema/Envelope.java
e9f5c77be81486b6b8c6d88f70f2d50583d8c3fafa2ac09ead80f44b8d5e751e gencode/java/udmi/schema/Event.java
69c3c12ce057b6ab8e27ab8cab6fb009bfd1c997652214b49e6f2a26a58f302c gencode/java/udmi/schema/ExecutionConfiguration.java
Expand Down Expand Up @@ -98,11 +98,11 @@ d3968b92497e83a63f18cc0e74484a9807f1bb92db0c92d556ec2caaa143d645 gencode/java/u
ac6f8fd87c8986cce01e872460c15ff6fe71e3816f9bde610acfe25f7d38c8d4 gencode/java/udmi/schema/ValidationEvent.java
f7d117dc8b9764acf0c95a13a2bfdfbdf31d1a8ec83a707448aa4d7391ef07e2 gencode/java/udmi/schema/ValidationState.java
e007ddd1ceeae3603c85110c33e1bb4a418ff9c7a791ca0df25b7ea3caeafd36 gencode/java/udmi/schema/ValidationSummary.java
67256cd379f8b456a2d8c3b5e64c9bde740569338f8e32be626ecb65ad6fd23c gencode/python/udmi/schema/__init__.py
fb9618d9177765ef7c4494b6ecfce8e69c9df193b7dc8c2124425b290fa4e359 gencode/python/udmi/schema/__init__.py
4b25dd95f863059b761269f93adcae7049507924a1c6e74d6856849203c179db gencode/python/udmi/schema/ancillary_properties.py
5ecd6c542f33450cb4ce75d940a6dff4d3bd67d4b9de4aff5ee88abcc301dbff gencode/python/udmi/schema/building_config.py
dab4f5fca272ec48c2881bca2b6bc43786ada47fa1f6dd935c35f7ce0eb6b0f6 gencode/python/udmi/schema/building_translation.py
c33fab16eb86b93692ddbb243a2d6cfe202ca1a0fd76b351a18727a35940d958 gencode/python/udmi/schema/category.py
470b688984b89b25fcdfa8e08bd95b0c5d8c551d53a6ab5512503ee39419e6fa gencode/python/udmi/schema/category.py
6578d68f65b87b781086e72566de910db4bef365599fe3188862d4d8a81e84fb gencode/python/udmi/schema/command_discovery.py
1254c34d973c9099ae99dcea4534e234e9019f49255e2e27d2afa1bc074fd596 gencode/python/udmi/schema/command_mapping.py
c5a62f92328e2ede167fc1f53bece6e48696a0ab0e37e3a41f65cd98494ba0d7 gencode/python/udmi/schema/common.py
Expand Down
155 changes: 155 additions & 0 deletions bin/upversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/bin/bash -e
#
# Usage:
# bin/upversion NEW_VERSION
#
# Changes the version where it is hardcoded in UDMI files, performing checks
# that the new version is available, and that the files can be safely updated.
# Modifications:
# - schema/*.json (all files)
# - etc/categories.json
# - tests/**/*.json and validator/traces/**/*.out -
# all files must be added to etc/upversion.txt only files preceeded
# with a 'y' are updated. Only works with "pretty"/non-minified JSON
# - documentation in-line exammples
# - specific java files (Pubber.java, ConfigUtil.java, LocalDevice.java)

UPVERSION_LIST=etc/upversion.txt
TESTS_REGEX="^(\s{0,4}\"version\"\s*:\s*)([0-9.\"]*)(,?)"
SCHEMA_VERSION_IDENTIFIER=\$udmi_version # must start with $
ERROR_MARKER=.upversion_error

# Updates existing $udmi_version in a JSON file.
# Usage: update_existing_schema_version FILE_TO_UPDATE NEW_VERSION
function update_existing_schema_version(){
file=$1
version=$2
sed -i -E "s/\"\\$SCHEMA_VERSION_IDENTIFIER\"\s*:\s*\"[0-9.]*\"/\"$SCHEMA_VERSION_IDENTIFIER\": \"$version\"/" $file
}

# Updates an existing $udmi_version, or adds if missing
# Usage: update_schema_version FILE_TO_UPDATE NEW_VERSION
function update_schema_version(){
file=$1
version=$2
if [[ $(jq -r ".[\"$SCHEMA_VERSION_IDENTIFIER\"]" $file) == "null" ]]; then
sed -i "s/^{/{\n \"$SCHEMA_VERSION_IDENTIFIER\": \"$version\",/" $file
else
update_existing_schema_version $file $version
fi
}

# Updates UDMI_VERSION variable in a java file
# Usage: update_java_file FILE_TO_UPDATE NEW_VERSION
function update_java_file(){
file=$1
version=$2
[[ $(grep -c "UDMI_VERSION\s*=\s*\"" $file) != 1 ]] && return 1
sed -i -e "s/UDMI_VERSION\s*=\s*\"[0-9.]*\"/UDMI_VERSION = \"$version\"/" $file
}

if [[ "$#" != 1 ]]; then
echo Usage: $0 NEW_VERSION
exit 1
fi
NEW_VERSION=$1
shift 1

rm .upversion_error

if ! [[ $NEW_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo ERROR Invalid version: $NEW_VERSION
exit 1
fi

bin/check_version
if [[ -n $(git tag -l $NEW_VERSION) ]]; then
echo ERROR New version $NEW_VERSION already exists in upstream repo.
exit 1
fi

if [[ -n $(git status --untracked-files=no --porcelain) ]]; then
echo ERROR Git not clean .. commit/stash all changes first
exit 1
fi

# Check test files exist in the upversion list
for test_file in tests/*.tests/*.json; do
if [[ $(grep -P -c "^[^#] $test_file" $UPVERSION_LIST) != 1 ]]; then
echo $test_file
err_upversion_list=1
fi
done
if [[ -n $err_upversion_list ]]; then
echo ERROR .. above files not found in upversion list or listed more than once
touch $ERROR_MARKER
fi

for schema_file in schema/*.json; do
if (( $(grep -c "\"\$udmi_version\"" $schema_file) > 1 )); then
echo $schema_file
err_schema_version=1
fi
done
if [[ -n $err_schema_version ]]; then
echo ERROR .. above files have more than one \$udmi_version
touch $ERROR_MARKER
fi

[[ -f $ERROR_MARKER ]] && exit 1

# Check files in upversion list still exist
while read action file comment; do
if [[ $action != "#" ]] && ! [[ -f $file ]]; then
err_stale_file=1
echo $file
fi
done <$UPVERSION_LIST
if [[ -n $err_stale_file ]]; then
echo ERROR .. above files etc/upversion.txt no longer exist
fi

# Check files have exactly 1 matched version fields to avoid unexpected
# updates. Uses indents to try and match the top level field
while read action file comment; do
if ! [[ -f $file && $action == "y" ]]; then
continue
fi

if [[ $(grep -E -c $TESTS_REGEX $file) != 1 ]]; then
echo $file
err_one_version=1
fi
done <$UPVERSION_LIST
if [[ -n $err_one_version ]]; then
echo ERROR above files do not have exactly one version field
fi

echo -n Updating files ..

# NOTE using sed because jq reformats text
for schema_file in schema/*.json; do
update_schema_version $schema_file $NEW_VERSION
done

# Update tests using sed because jq does not handle files with comments
cat $UPVERSION_LIST | grep "^y" | awk '{print $2}' \
| xargs sed -i -E "s/$TESTS_REGEX/\1\"$NEW_VERSION\"\3/g"

update_existing_schema_version etc/category.json $NEW_VERSION

bin/gencode_docs_examples

# TODO a proper way of updating/managing these files

# Version in Pubber payloads
update_java_file pubber/src/main/java/daq/pubber/Pubber.java $NEW_VERSION
# Version in validator messages messages
update_java_file validator/src/main/java/com/google/daq/mqtt/util/ConfigUtil.java $NEW_VERSION
# Version in for generated_configs which are NOT upgraded
update_java_file validator/src/main/java/com/google/daq/mqtt/registrar/LocalDevice.java $NEW_VERSION

# NOTE messages & metadata are upgraded when there are schema changes,
# So they do not necesarily end up at the latest version when upgraded

echo Done!
23 changes: 23 additions & 0 deletions docs/guides/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ template `etc/schema_readme_template.md`, and must match (case sensitive) the
value of `$section`, otherwise the schema entry is inserted under the `Other`
section

## Releases

The `bin/upversion` tool updates :
* the `$udmi_version` field in schema files,
* the version in `version` of of manually curated payloads in `tests/*.tests`
* documentation inline message examples.
* the `UDMI_VERSION` constant in specific JAVA files.

File in the `tests` directory must be listed in `etc/upversion.txt`. Only files
which are preceded by a `y` in `upversion_yay.txt` will have their version upgraded. Comments are supported after the file path, e.g.
`y tests/state.tests/makemodel_upgrade.json tests message upgrade from v1`

`bin/upversions` carries out several checks on files which must be cleared before an update.

`bin/upversion` does not update any generated files (e.g. for CI testing).

The following files need to be update:
* After `bin/test_trace simple`, contents of `sites/udmi_site_model/sites/out/devices`
into `validator/traces/simple.out/devices`
* After `bin/test_validator`, `/tmp/validator.out` into `/etc/validator.out` (reset any changes to sites/udmi_site_model before running)
* After `bin/test_registrar && bin/test_sites`, the `out` directory for each device in `tests/downgrade.site/devices/` into the `expected` subdirectory (note these files are ignored by git, but must still be comitted)


## Configuring Cloud CI Tests

To enable the CI tests, there first needs to be a dedicated GCP Project with an IoT Core
Expand Down
6 changes: 3 additions & 3 deletions docs/specs/sequences/endpoint_reconfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Config message to initiate Reconfiguration (sequence #1 in diagrams above)
<!--example:config/endpoint_reconfiguration.json-->
```json
{
"version": 1,
"version": "1.4.0",
"blobset": {
"blobs": {
"_iot_endpoint_config": {
Expand Down Expand Up @@ -107,7 +107,7 @@ a successful reconfiguration
<!--example:state/endpoint_reconfiguration.json-->
```json
{
"version": 1,
"version": "1.4.0",
"timestamp": "2022-07-13T12:00:10.000Z",
"system": {
"hardware": {
Expand Down Expand Up @@ -135,7 +135,7 @@ This is an example of the state message sent to the original endpoint after a fa
<!--example:state/endpoint_reconfiguration_failed.json-->
```json
{
"version": 1,
"version": "1.4.0",
"timestamp": "2022-07-13T12:00:11.000Z",
"system": {
"hardware": {
Expand Down
2 changes: 2 additions & 0 deletions etc/category.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"$udmi_version": "1.4.0",
"title": "Category",
"$comment": "Auto-generated category mappings from bin/gencode_categories.",
"oneOf": [
@@ gencode stuff goes here
Expand Down
101 changes: 101 additions & 0 deletions etc/upversion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Manually maintained list of all files in tests/*.tests/*.json
# consumed by bin/upversion when upgrading versions
#
# Syntax: ACTION FILE [COMMENT]
#
# ACTIONS:
# y - update the top level "version" in the files
# n - do not update the file
#
n tests/event_discovery.tests/empty.json
n tests/event_system.tests/empty.json
n tests/event_pointset.tests/empty.json
n tests/state.tests/empty.json
n tests/config.tests/empty.json
n tests/config.tests/delta_x1_gateway.json
n tests/config.tests/delta_x1_target.json
n tests/metadata.tests/empty.json
n tests/state.tests/delta_x1_gateway.json
n tests/state.tests/delta_x1_target.json
n tests/configuration_endpoint.tests/simple.json
n tests/configuration_endpoint.tests/delta.json
n tests/model_pointset.tests/example.json
n tests/envelope.tests/lgtw.json
n tests/envelope.tests/empty.json
n tests/envelope.tests/errors2.json
n tests/envelope.tests/example.json
n tests/envelope.tests/gateway.json
n tests/envelope.tests/example2.json
n tests/envelope.tests/errors1.json
n tests/state.tests/makemodel_upgrade.json # tests message upgrade from v 1
n tests/config.tests/errors.json
n tests/state.tests/errors.json # test complete message upgrade pathway
y tests/state_validation.tests/report.json
y tests/event_discovery.tests/from_bacnet.json
y tests/event_discovery.tests/scan_error.json
y tests/event_discovery.tests/point_error.json
y tests/event_discovery.tests/errors.json
y tests/event_discovery.tests/enumeration.json
y tests/event_discovery.tests/continuous.json
y tests/event_discovery.tests/discovery.json
y tests/event_discovery.tests/implicit.json
y tests/event_system.tests/metrics.json
y tests/event_system.tests/errors.json
y tests/event_system.tests/example.json
y tests/event_system.tests/fcu.json
y tests/event_validation.tests/simple_ok.json
y tests/event_validation.tests/simple_error.json
y tests/config_mapping.tests/mapping.json
y tests/event_pointset.tests/errors.json
y tests/event_pointset.tests/writeback.json
y tests/event_pointset.tests/example.json
y tests/event_pointset.tests/fcu.json
y tests/event_pointset.tests/smartprimus.json
y tests/event_pointset.tests/partial.json
y tests/config_pointset.tests/example.json
y tests/state.tests/scan_stop.json
y tests/state.tests/scan_error.json
y tests/state.tests/enumeration.json
y tests/state.tests/writeback.json
y tests/state.tests/continuous.json
y tests/state.tests/periodic.json
y tests/state.tests/scan_bad.json
y tests/state.tests/discovery.json
y tests/state.tests/example.json
y tests/state.tests/endpoint_reconfiguration_failed.json
y tests/state.tests/endpoint_reconfiguration.json
y tests/state.tests/makemodel_error.json # current version but with makemodel, validation error
y tests/state.tests/blobset_updating.json
y tests/state.tests/blobset_received.json
y tests/state.tests/fcu.json
y tests/state.tests/gateway.json
y tests/state.tests/restart.json
y tests/state_pointset.tests/example.json
y tests/config.tests/blobset_final_incomplete_url.json
y tests/config.tests/enumeration.json
y tests/config.tests/writeback.json
y tests/config.tests/proxy.json
y tests/config.tests/continuous.json
y tests/config.tests/delta_endpoint.json
y tests/config.tests/periodic.json
y tests/config.tests/discovery.json
y tests/config.tests/example.json
y tests/config.tests/endpoint_reconfiguration.json
y tests/config.tests/fcu.json
y tests/config.tests/gateway.json
y tests/config.tests/smartprimus.json
y tests/config.tests/restart.json
y tests/config.tests/blobset_final_incomplete_payload.json
y tests/config.tests/implicit.json
y tests/state_mapping.tests/mapping.json
y tests/command_discovery.tests/provision.json
y tests/event_mapping.tests/mapping.json
y tests/event_mapping.tests/prediction.json
y tests/metadata.tests/toomany.json
y tests/metadata.tests/proxy.json
y tests/metadata.tests/example.json
y tests/metadata.tests/gateway.json
y tests/command_mapping.tests/mapping.json
y tests/metadata.tests/errors.json
y tests/config.tests/blobset_final.json
n tests/configuration_execution.tests/cloud_iot_config.json
2 changes: 1 addition & 1 deletion etc/validator.out
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sites/udmi_site_model/out/devices/AHU-1/event_pointset.out
::::::::::::::
{
"timestamp" : "1999-10-20T01:02:03Z",
"version" : "1.3.14",
"version" : "1.4.0",
"sub_folder" : "pointset",
"sub_type" : "event",
"status" : {
Expand Down
2 changes: 2 additions & 0 deletions gencode/java/udmi/schema/Entry.java

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

2 changes: 1 addition & 1 deletion gencode/python/udmi/schema/__init__.py

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

6 changes: 3 additions & 3 deletions gencode/python/udmi/schema/category.py

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

Loading

0 comments on commit 7755958

Please sign in to comment.