77import static com .google .udmi .util .GeneralUtils .friendlyStackTrace ;
88import static com .google .udmi .util .GeneralUtils .ifNotNullGet ;
99import static com .google .udmi .util .GeneralUtils .ifNotNullThen ;
10+ import static com .google .udmi .util .GeneralUtils .ifNotTrueThen ;
1011import static com .google .udmi .util .GeneralUtils .ifTrueThen ;
1112import static com .google .udmi .util .GeneralUtils .isTrue ;
1213import static com .google .udmi .util .JsonUtil .getDate ;
13- import static com .google .udmi .util .JsonUtil .getTimestamp ;
1414import static java .lang .String .format ;
1515import static java .util .Objects .requireNonNull ;
1616import static java .util .Optional .ofNullable ;
5353import com .google .common .collect .BiMap ;
5454import com .google .common .collect .ImmutableBiMap ;
5555import com .google .common .collect .ImmutableList ;
56- import com .google .common .collect .ImmutableSet ;
5756import com .google .udmi .util .GeneralUtils ;
58- import java .time .Duration ;
59- import java .time .Instant ;
6057import java .util .AbstractMap .SimpleEntry ;
6158import java .util .Base64 ;
6259import java .util .Date ;
6663import java .util .Map .Entry ;
6764import java .util .Objects ;
6865import java .util .Set ;
69- import java .util .concurrent .CompletableFuture ;
7066import java .util .stream .Collectors ;
7167import org .jetbrains .annotations .NotNull ;
7268import 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 ));
0 commit comments