Skip to content

Commit

Permalink
chore: finish cache impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 committed Jun 28, 2024
1 parent 631cd15 commit c686e1f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
import com.aws.greengrass.util.Coerce;
import lombok.NonNull;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -46,6 +51,16 @@
* | |---- certificateId:
* | |---- "s": status
* | |---- "l": lastUpdated
* | |---- "clientDeviceThingAssociations":
* | |---- "v1":
* | |---- associations: [...]
* | |---- "l": lastUpdated
* | |---- "clientDeviceThingDescription":
* | |---- "v1":
* | |---- thingName:
* | |---- "attributes":
* | |---- k:v
* | |---- "l": lastUpdated
* | |---- "hostAddresses":
* |---- <source>:
* |---- [...]
Expand All @@ -64,6 +79,14 @@ public final class RuntimeConfiguration {
static final String CERTS_STATUS_UPDATED_KEY = "l";
private static final String HOST_ADDRESSES_KEY = "hostAddresses";

private static final String ASSOCIATIONS_KEY = "clientDeviceThingAssociations";
private static final String ASSOCIATIONS_V1 = "v1";
private static final String DESCRIPTION_KEY = "clientDeviceThingDescription";
private static final String DESCRIPTION_V1 = "v1";
private static final String LAST_UPDATED_KEY = "l";
private static final String ASSOCIATIONS_PROP_KEY = "associations";
private static final String ATTRIBUTES_PROP_KEY = "attributes";

private final Topics config;


Expand Down Expand Up @@ -206,20 +229,59 @@ public void removeCertificateV1(String certificateId) {
}
}

public void putThingAssociationV1(ThingAssociationV1DTO thingAssociationV1DTO) {
// TODO
public void putThingAssociationV1(ThingAssociationV1DTO dto) {
Topics t = getOrRepairTopics(config, ASSOCIATIONS_KEY, ASSOCIATIONS_V1);
t.lookup(ASSOCIATIONS_PROP_KEY).withValue(new ArrayList<>(dto.getAssociatedThingNames()));
t.lookup(LAST_UPDATED_KEY).withValue(dto.getLastUpdated().toEpochSecond(ZoneOffset.UTC));
}

public ThingAssociationV1DTO getThingAssociationV1() {
return null; // TODO
public Optional<ThingAssociationV1DTO> getThingAssociationV1() {
Topics t = config.findTopics(ASSOCIATIONS_KEY, ASSOCIATIONS_V1);
if (t == null) {
return Optional.empty();
}

Set<String> thingNames = new HashSet<>();
Topic associationsTopic = t.find(ASSOCIATIONS_PROP_KEY);
if (associationsTopic != null) {
thingNames.addAll(Coerce.toStringList(associationsTopic));
}

LocalDateTime lastFetched = null;
Topic lastFetchedTopic = t.find(LAST_UPDATED_KEY);
if (lastFetchedTopic != null) {
lastFetched = LocalDateTime.ofInstant(Instant.ofEpochMilli(Coerce.toLong(lastFetchedTopic)),
ZoneId.of("UTC"));
}
return Optional.of(new ThingAssociationV1DTO(thingNames, lastFetched));
}

public void putThingDescriptionV1(ThingDescriptionV1DTO thingDescriptionV1DTO) {
// TODO
public void putThingDescriptionV1(ThingDescriptionV1DTO dto) {
Topics t = getOrRepairTopics(config, DESCRIPTION_KEY, DESCRIPTION_V1, dto.getThingName());
t.lookup(LAST_UPDATED_KEY).withValue(dto.getLastUpdated().toEpochSecond(ZoneOffset.UTC));
Map<String, Object> attrs = new HashMap<>(dto.getAttributes());
getOrRepairTopics(t, ATTRIBUTES_PROP_KEY).replaceAndWait(attrs);
}

public ThingDescriptionV1DTO getThingDescriptionV1(String thingName) {
return null; // TODO
public Optional<ThingDescriptionV1DTO> getThingDescriptionV1(String thingName) {
Topics t = config.findTopics(DESCRIPTION_KEY, DESCRIPTION_V1, thingName);
if (t == null) {
return Optional.empty();
}

Map<String, String> attrs = new HashMap<>();
Topics attributesTopic = t.findTopics(ATTRIBUTES_PROP_KEY);
if (attributesTopic != null && !attributesTopic.isEmpty()) {
attributesTopic.forEach(node -> attrs.put(node.getName(), Coerce.toString(node)));
}

LocalDateTime lastFetched = null;
Topic lastFetchedTopic = t.find(LAST_UPDATED_KEY);
if (lastFetchedTopic != null) {
lastFetched = LocalDateTime.ofInstant(Instant.ofEpochMilli(Coerce.toLong(lastFetchedTopic)),
ZoneId.of("UTC"));
}
return Optional.of(new ThingDescriptionV1DTO(thingName, attrs, lastFetched));
}

private Topics getOrRepairTopics(Topics root, String... path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ private void refresh() {

private Optional<Set<String>> getAssociatedThingNames() {
// use cached value, provided it's not stale
ThingAssociationV1DTO dto = runtimeConfiguration.getThingAssociationV1();
Optional<ThingAssociationV1DTO> dto = runtimeConfiguration.getThingAssociationV1();
// TODO pull from configuration
if (dto != null && dto.getLastFetched().plusSeconds(DEFAULT_THING_ASSOCIATION_TRUST_DURATION_SECONDS)
if (dto.isPresent() && dto.get().getLastUpdated().plusSeconds(DEFAULT_THING_ASSOCIATION_TRUST_DURATION_SECONDS)
.isBefore(LocalDateTime.now())) {
logger.atTrace().log("Using locally cached thing associations");
return Optional.ofNullable(dto.getAssociatedThingNames());
return dto.map(ThingAssociationV1DTO::getAssociatedThingNames);
}

if (networkStateProvider.getConnectionState() == NetworkStateProvider.ConnectionState.NETWORK_DOWN) {
Expand Down Expand Up @@ -192,12 +192,12 @@ private Optional<Set<String>> fetchAssociatedThingNames() {

private Optional<Map<String, String>> getThingAttributes(String thingName) {
// use cached value, provided it's not stale
ThingDescriptionV1DTO dto = runtimeConfiguration.getThingDescriptionV1(thingName);
Optional<ThingDescriptionV1DTO> dto = runtimeConfiguration.getThingDescriptionV1(thingName);
// TODO pull from configuration
if (dto != null && dto.getLastFetched().plusSeconds(DEFAULT_THING_DESCRIPTION_TRUST_DURATION_SECONDS)
if (dto.isPresent() && dto.get().getLastUpdated().plusSeconds(DEFAULT_THING_DESCRIPTION_TRUST_DURATION_SECONDS)
.isBefore(LocalDateTime.now())) {
logger.atTrace().log("Using locally cached thing description");
return Optional.ofNullable(dto.getAttributes());
return dto.map(ThingDescriptionV1DTO::getAttributes);
}

if (networkStateProvider.getConnectionState() == NetworkStateProvider.ConnectionState.NETWORK_DOWN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
@AllArgsConstructor
public class ThingAssociationV1DTO {
Set<String> associatedThingNames;
LocalDateTime lastFetched;
LocalDateTime lastUpdated;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
public class ThingDescriptionV1DTO {
String thingName;
Map<String, String> attributes;
LocalDateTime lastFetched;
LocalDateTime lastUpdated;
}

0 comments on commit c686e1f

Please sign in to comment.