Skip to content

Commit 5535dcf

Browse files
authored
More UDMIS and Registrar fixes (#721)
1 parent b994949 commit 5535dcf

File tree

25 files changed

+262
-174
lines changed

25 files changed

+262
-174
lines changed

.github/workflows/testing.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ jobs:
4949
- name: Setup udmis container build
5050
if: ${{ github.event_name == 'push' }}
5151
run: |
52+
set -x
5253
revhash=$(git rev-parse $GITHUB_REF)
5354
IMAGE_TAG=g${revhash:0:9}
5455
PUSH_REPO=$PUSH_REGISTRY/${{ github.repository }}
5556
PUSH_TAG=$PUSH_REPO:$IMAGE_TAG
5657
echo PUSH_TAG=$PUSH_TAG >> $GITHUB_ENV
57-
udmis/bin/container prep --no-check $PUSH_REPO
58+
udmis/bin/container prep --no-check ${PUSH_REPO%/*}
5859
echo Pushing built container as $PUSH_TAG | tee -a $GITHUB_STEP_SUMMARY
5960
- name: Build and push docker image
6061
if: ${{ github.event_name == 'push' }}
@@ -295,7 +296,6 @@ jobs:
295296
cd sites/udmi_site_model/out
296297
find . -type f | sort | xargs more | cat
297298
- name: itemized sequencer tests
298-
if: ${{ always() }}
299299
run: |
300300
bin/test_itemized $GCP_TARGET_PROJECT
301301
- name: itemized output logs

common/src/main/java/com/google/udmi/util/GeneralUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public class GeneralUtils {
4949
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
5050
.enable(SerializationFeature.INDENT_OUTPUT)
5151
.setDateFormat(new ISO8601DateFormat())
52+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
5253
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
53-
private static final ObjectMapper OBJECT_MAPPER_RAW =
54+
public static final ObjectMapper OBJECT_MAPPER_RAW =
5455
OBJECT_MAPPER.copy()
5556
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
5657
.enable(Feature.ALLOW_TRAILING_COMMA)
@@ -59,7 +60,7 @@ public class GeneralUtils {
5960
.setSerializationInclusion(Include.NON_NULL);
6061
public static final ObjectMapper OBJECT_MAPPER_STRICT =
6162
OBJECT_MAPPER_RAW.copy()
62-
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
63+
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
6364
.disable(SerializationFeature.INDENT_OUTPUT);
6465

6566
private static final String SEPARATOR = "\n ";

udmis/.idea/runConfigurations/UdmiServicePod_configured.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

udmis/bin/container

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DROOT=.
66

77
function usage {
88
echo Error: $*
9-
echo Usage: $0 { prep, build, shell, run, push, deploy, update, status, logs, stop } [--no-check] [--selfie] [repo]
9+
echo Usage: $0 { prep, build, shell, run, deploy, update, status, logs, stop } [--no-check] [--selfie] [repo]
1010
echo Project: $GCP_PROJECT
1111
echo Try starting with: $0 build
1212
exit 1
@@ -18,8 +18,6 @@ shift || usage missing command
1818
GCP_PROJECT=$(gcloud config get project)
1919
REPOSITORY=gcr.io/$GCP_PROJECT
2020

21-
mkdir -p tmp
22-
2321
IMAGE=udmis
2422

2523
if [[ $1 == "--no-check" ]]; then
@@ -60,15 +58,16 @@ current_user=$USER@$HOSTNAME
6058

6159
revparse=`git rev-parse HEAD`
6260

63-
udmi_ref=$REPOSITORY:g${revparse:0:9}
61+
udmi_ver=g${revparse:0:9}
62+
udmi_ref=$REPOSITORY/udmi:$udmi_ver
6463

6564
version=`git describe || echo $udmi_ref`
6665

6766
RUNARGS="--rm -ti -v $PWD/var:/udmi -v $HOME/.config:/root/.config --tmpfs /tmp"
6867
TEMPLATES=$(cd etc; ls k8s_*.yaml)
6968

7069
if [[ $cmd == prep || $cmd == build ]]; then
71-
rm -rf var tmp && mkdir -p var
70+
rm -rf var tmp && mkdir -p var tmp
7271
bin/build
7372
build_time=`date --utc -Imin -r $LIBFILE`
7473
cp etc/prod_pod.json var/
@@ -84,29 +83,32 @@ EOF
8483
echo Next try: $0 build
8584
fi
8685

87-
if [[ $cmd == prep ]]; then
88-
true
89-
elif [[ $cmd == build ]]; then
86+
if [[ $cmd == build ]]; then
9087
echo Building Dockerfile.$IMAGE
9188
docker build -f Dockerfile.$IMAGE -t $IMAGE $DROOT
92-
echo Next try: $0 push
93-
elif [[ $cmd == run ]]; then
94-
docker run $RUNARGS $IMAGE
95-
elif [[ $cmd == shell ]]; then
96-
docker run $RUNARGS $IMAGE bash
97-
elif [[ $cmd == push ]]; then
98-
IMAGENAME=$REPOSITORY/$IMAGE
99-
docker tag $IMAGE $IMAGENAME
100-
docker push $IMAGENAME
101-
hash=$(docker images --digests $IMAGENAME | fgrep latest | awk '{print $3}')
102-
ihash=$IMAGENAME@$hash
89+
fi
90+
91+
if [[ $cmd == build && $REPOSITORY != local ]]; then
92+
docker tag $IMAGE $udmi_ref
93+
docker push $udmi_ref
94+
hash=$(docker images --digests ${udmi_ref%:*} | fgrep $udmi_ver | awk '{print $3}')
95+
ihash=$udmi_ref@$hash
10396
for file in $TEMPLATES; do
10497
if fgrep -q @IMAGE-$IMAGE@ etc/$file; then
10598
sed < etc/$file > tmp/$file -e "s^@IMAGE-$IMAGE@^$ihash^"
10699
echo Updated tmp/$file with image $ihash
107100
fi
108101
done
109-
echo Next try: $0 deploy
102+
fi
103+
104+
if [[ $cmd == prep ]]; then
105+
echo Next try: $0 build
106+
elif [[ $cmd == build ]]; then
107+
echo Next try: $0 '{run, deploy, update}'
108+
elif [[ $cmd == run ]]; then
109+
docker run $RUNARGS $IMAGE
110+
elif [[ $cmd == shell ]]; then
111+
docker run $RUNARGS $IMAGE bash
110112
elif [[ $cmd == deploy ]]; then
111113
kubectl apply -f tmp/k8s_pod.yaml
112114
echo Next try: $0 status

udmis/bin/deploy

Lines changed: 0 additions & 20 deletions
This file was deleted.

udmis/bin/deploy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
update

udmis/bin/update

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ fi
88
project_id=$1
99
shift
1010

11+
CMD=$(basename $0)
1112
ROOT=$(dirname $0)/..
1213
cd $ROOT
1314

1415
bin/setup $project_id
1516

1617
bin/container build
17-
bin/container push
18-
bin/container update
18+
bin/container $CMD
1919
sleep 10
2020
kubectl get pods

udmis/etc/prod_pod.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
},
2525
"clearblade-iot-core": {
2626
"provider": "clearblade",
27-
"project_id": "${CLEARBLADE_PROJECT}"
27+
"project_id": "${CLEARBLADE_PROJECT}",
28+
"options": "distributor=stately"
2829
},
2930
"gcp-iot-core": {
3031
"provider": "gcp",
3132
"project_id": "${GCP_PROJECT}",
32-
"options": "${GCP_IOT_OPTIONS}"
33+
"options": "${GCP_IOT_OPTIONS},distributor=stately"
3334
}
3435
},
3536
"distributors": {

udmis/src/main/java/com/google/bos/udmi/service/access/ClearBladeIotAccessProvider.java

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import static com.google.udmi.util.GeneralUtils.friendlyStackTrace;
88
import static com.google.udmi.util.GeneralUtils.ifNotNullGet;
99
import static com.google.udmi.util.GeneralUtils.ifNotNullThen;
10+
import static com.google.udmi.util.GeneralUtils.ifNotTrueThen;
1011
import static com.google.udmi.util.GeneralUtils.ifTrueThen;
1112
import static com.google.udmi.util.GeneralUtils.isTrue;
1213
import static com.google.udmi.util.JsonUtil.getDate;
13-
import static com.google.udmi.util.JsonUtil.getTimestamp;
1414
import static java.lang.String.format;
1515
import static java.util.Objects.requireNonNull;
1616
import static java.util.Optional.ofNullable;
@@ -53,10 +53,7 @@
5353
import com.google.common.collect.BiMap;
5454
import com.google.common.collect.ImmutableBiMap;
5555
import com.google.common.collect.ImmutableList;
56-
import com.google.common.collect.ImmutableSet;
5756
import com.google.udmi.util.GeneralUtils;
58-
import java.time.Duration;
59-
import java.time.Instant;
6057
import java.util.AbstractMap.SimpleEntry;
6158
import java.util.Base64;
6259
import java.util.Date;
@@ -66,7 +63,6 @@
6663
import java.util.Map.Entry;
6764
import java.util.Objects;
6865
import java.util.Set;
69-
import java.util.concurrent.CompletableFuture;
7066
import java.util.stream.Collectors;
7167
import org.jetbrains.annotations.NotNull;
7268
import org.jetbrains.annotations.Nullable;
@@ -103,8 +99,11 @@ public class ClearBladeIotAccessProvider extends IotAccessBase {
10399
* Create a new instance for interfacing with GCP IoT Core.
104100
*/
105101
public ClearBladeIotAccessProvider(IotAccess iotAccess) {
102+
super(iotAccess);
106103
projectId = getProjectId(iotAccess);
107104
ifTrueThen(isEnabled(), this::fetchRegistryRegions);
105+
ifNotTrueThen(isEnabled(),
106+
() -> warn("Clearblade access provided disabled because project id is null or empty"));
108107
}
109108

110109
private static Credential convertIot(DeviceCredential device) {
@@ -147,6 +146,27 @@ protected DeviceManagerClient getDeviceManagerClient() {
147146
return new DeviceManagerClient();
148147
}
149148

149+
@NotNull
150+
protected Set<String> getRegistriesForRegion(String region) {
151+
try {
152+
DeviceManagerClient deviceManagerClient = getDeviceManagerClient();
153+
ListDeviceRegistriesRequest request = ListDeviceRegistriesRequest.Builder.newBuilder()
154+
.setParent(LocationName.of(projectId, region).getLocationFullName())
155+
.build();
156+
ListDeviceRegistriesResponse response = deviceManagerClient.listDeviceRegistries(request);
157+
requireNonNull(response, "get registries response is null");
158+
List<DeviceRegistry> deviceRegistries = response.getDeviceRegistriesList();
159+
Set<String> registries =
160+
ofNullable(deviceRegistries).orElseGet(ImmutableList::of).stream()
161+
.map(registry -> registry.toBuilder().getId())
162+
.collect(Collectors.toSet());
163+
debug("Fetched " + registries.size() + " registries for region " + region);
164+
return registries;
165+
} catch (Exception e) {
166+
throw new RuntimeException("While fetching registries for region " + region, e);
167+
}
168+
}
169+
150170
@Override
151171
protected boolean isEnabled() {
152172
return !isNullOrEmpty(projectId);
@@ -276,6 +296,32 @@ private CloudModel deleteDevice(String registryId, Device device) {
276296
}
277297
}
278298

299+
@NotNull
300+
private HashMap<String, CloudModel> fetchDevices(String deviceRegistryId, String gatewayId) {
301+
String location = getRegistryLocation(deviceRegistryId);
302+
DeviceManagerClient deviceManagerClient = getDeviceManagerClient();
303+
GatewayListOptions gatewayListOptions = ifNotNullGet(gatewayId, this::getGatewayListOptions);
304+
String registryFullName =
305+
RegistryName.of(projectId, location, deviceRegistryId).getRegistryFullName();
306+
String pageToken = null;
307+
HashMap<String, CloudModel> collect = new HashMap<>();
308+
do {
309+
DevicesListRequest request = DevicesListRequest.Builder.newBuilder().setParent(
310+
registryFullName)
311+
.setGatewayListOptions(gatewayListOptions)
312+
.setPageToken(pageToken)
313+
.build();
314+
DevicesListResponse response = deviceManagerClient.listDevices(request);
315+
requireNonNull(response, "DeviceRegistriesList fetch failed");
316+
Map<String, CloudModel> responseMap =
317+
response.getDevicesList().stream().map(ClearBladeIotAccessProvider::convertToEntry)
318+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
319+
collect.putAll(responseMap);
320+
pageToken = response.getNextPageToken();
321+
} while (pageToken != null);
322+
return collect;
323+
}
324+
279325
private String getDeviceName(String registryId, String deviceId) {
280326
return DeviceName.of(projectId, getRegistryLocation(registryId), registryId, deviceId)
281327
.toString();
@@ -298,27 +344,6 @@ private String getProjectId(IotAccess iotAccess) {
298344
}
299345
}
300346

301-
@NotNull
302-
protected Set<String> getRegistriesForRegion(String region) {
303-
try {
304-
DeviceManagerClient deviceManagerClient = getDeviceManagerClient();
305-
ListDeviceRegistriesRequest request = ListDeviceRegistriesRequest.Builder.newBuilder()
306-
.setParent(LocationName.of(projectId, region).getLocationFullName())
307-
.build();
308-
ListDeviceRegistriesResponse response = deviceManagerClient.listDeviceRegistries(request);
309-
requireNonNull(response, "get registries response is null");
310-
List<DeviceRegistry> deviceRegistries = response.getDeviceRegistriesList();
311-
Set<String> registries =
312-
ofNullable(deviceRegistries).orElseGet(ImmutableList::of).stream()
313-
.map(registry -> registry.toBuilder().getId())
314-
.collect(Collectors.toSet());
315-
debug("Fetched " + registries.size() + " registries for region " + region);
316-
return registries;
317-
} catch (Exception e) {
318-
throw new RuntimeException("While fetching registries for region " + region, e);
319-
}
320-
}
321-
322347
private String getRegistryLocation(String registry) {
323348
return getRegistryRegion(registry);
324349
}
@@ -362,32 +387,6 @@ private CloudModel listRegistryDevices(String deviceRegistryId, String gatewayId
362387
}
363388
}
364389

365-
@NotNull
366-
private HashMap<String, CloudModel> fetchDevices(String deviceRegistryId, String gatewayId) {
367-
String location = getRegistryLocation(deviceRegistryId);
368-
DeviceManagerClient deviceManagerClient = getDeviceManagerClient();
369-
GatewayListOptions gatewayListOptions = ifNotNullGet(gatewayId, this::getGatewayListOptions);
370-
String registryFullName =
371-
RegistryName.of(projectId, location, deviceRegistryId).getRegistryFullName();
372-
String pageToken = null;
373-
HashMap<String, CloudModel> collect = new HashMap<>();
374-
do {
375-
DevicesListRequest request = DevicesListRequest.Builder.newBuilder().setParent(
376-
registryFullName)
377-
.setGatewayListOptions(gatewayListOptions)
378-
.setPageToken(pageToken)
379-
.build();
380-
DevicesListResponse response = deviceManagerClient.listDevices(request);
381-
requireNonNull(response, "DeviceRegistriesList fetch failed");
382-
Map<String, CloudModel> responseMap =
383-
response.getDevicesList().stream().map(ClearBladeIotAccessProvider::convertToEntry)
384-
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
385-
collect.putAll(responseMap);
386-
pageToken = response.getNextPageToken();
387-
} while (pageToken != null);
388-
return collect;
389-
}
390-
391390
private void unbindDevice(String registryId, String gatewayId, String proxyId) {
392391
try {
393392
debug(format("Unbind %s: %s from %s", registryId, proxyId, gatewayId));

udmis/src/main/java/com/google/bos/udmi/service/access/DynamicIotAccessProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class DynamicIotAccessProvider extends IotAccessBase {
3838
* Create a new instance for interfacing with multiple providers.
3939
*/
4040
public DynamicIotAccessProvider(IotAccess iotAccess) {
41+
super(iotAccess);
4142
providerList = Arrays.asList(iotAccess.project_id.split(","));
4243
}
4344

@@ -135,6 +136,11 @@ public void sendCommandBase(String registryId, String deviceId, SubFolder folder
135136
getProviderFor(registryId).sendCommandBase(registryId, deviceId, folder, message);
136137
}
137138

139+
@Override
140+
public void updateRegistryRegions(Map<String, String> regions) {
141+
providers.values().forEach(provider -> provider.updateRegistryRegions(regions));
142+
}
143+
138144
@Override
139145
public void setProviderAffinity(String registryId, String deviceId, String providerId) {
140146
if (providerId != null) {

0 commit comments

Comments
 (0)