Skip to content

Commit

Permalink
Make LwM2mIdentitySerDes / LwM2mPeerSerDes customizable in Redis store
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslawLegierski authored and sbernard31 committed Jul 3, 2023
1 parent ec90257 commit 2066961
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
import org.eclipse.leshan.core.observation.ObservationIdentifier;
import org.eclipse.leshan.core.observation.SingleObservation;
import org.eclipse.leshan.core.peer.LwM2mIdentity;
import org.eclipse.leshan.core.peer.LwM2mPeer;
import org.eclipse.leshan.core.util.NamedThreadFactory;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.redis.serialization.LwM2mIdentitySerDes;
import org.eclipse.leshan.server.redis.serialization.LwM2mPeerSerDes;
import org.eclipse.leshan.server.redis.serialization.ObservationSerDes;
import org.eclipse.leshan.server.redis.serialization.RegistrationSerDes;
import org.eclipse.leshan.server.registration.Deregistration;
Expand Down Expand Up @@ -98,7 +100,7 @@ public class RedisRegistrationStore implements RegistrationStore, Startable, Sto
private final JedisLock lock;
private final RegistrationSerDes registrationSerDes;
private final ObservationSerDes observationSerDes;
private final LwM2mIdentitySerDes identitySerDes = new LwM2mIdentitySerDes(); // TODO add it to builder ?
private final LwM2mIdentitySerDes identitySerDes;

public RedisRegistrationStore(Pool<Jedis> p) {
this(new Builder(p).generateDefaultValue());
Expand All @@ -121,6 +123,7 @@ public RedisRegistrationStore(Builder builder) {
this.lock = builder.lock;
this.registrationSerDes = builder.registrationSerDes;
this.observationSerDes = builder.observationSerDes;
this.identitySerDes = builder.identitySerDes;
}

/* *************** Redis Key utility function **************** */
Expand Down Expand Up @@ -766,6 +769,7 @@ public void setExpirationListener(ExpirationListener listener) {
public static class Builder {

private final Pool<Jedis> pool;

private String prefix;
private String registrationByEndpointPrefix;
private String endpointByRegistrationIdPrefix;
Expand All @@ -786,6 +790,8 @@ public static class Builder {
private JedisLock lock;
private RegistrationSerDes registrationSerDes;
private ObservationSerDes observationSerDes;
private LwM2mIdentitySerDes identitySerDes;
private LwM2mPeerSerDes peerSerDes;

/**
* Set the prefix for all keys and prefixes.
Expand Down Expand Up @@ -942,6 +948,23 @@ public Builder setRegistrationSerDes(RegistrationSerDes registrationSerDes) {
return this;
}

/**
* Set {@link LwM2mIdentitySerDes} instance used to serialize/de-serialize {@link LwM2mIdentity} to/from this
* store.
*/
public Builder setIdentitySerDes(LwM2mIdentitySerDes identitySerDes) {
this.identitySerDes = identitySerDes;
return this;
}

/**
* Set {@link LwM2mPeerSerDes} instance used to serialize/de-serialize {@link LwM2mPeer} to/from this store.
*/
public Builder setPeerSerDes(LwM2mPeerSerDes peerSerDes) {
this.peerSerDes = peerSerDes;
return this;
}

/**
* Set {@link ObservationSerDes} instance used to serialize/de-serialize {@link Observation} to/from this store.
*/
Expand Down Expand Up @@ -977,7 +1000,14 @@ protected Builder generateDefaultValue() {
}

if (this.registrationSerDes == null) {
this.registrationSerDes = new RegistrationSerDes();
if (peerSerDes == null) {
this.peerSerDes = new LwM2mPeerSerDes();
}
this.registrationSerDes = new RegistrationSerDes(peerSerDes);
}

if (this.identitySerDes == null) {
this.identitySerDes = new LwM2mIdentitySerDes();
}

if (this.observationSerDes == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@
public class RegistrationSerDes {

private final AttributeParser attributeParser;
private final LwM2mPeerSerDes peerSerDes = new LwM2mPeerSerDes();
private final LwM2mPeerSerDes peerSerDes;

public RegistrationSerDes() {
public RegistrationSerDes(LwM2mPeerSerDes peerSerDes) {
// Define all supported Attributes
Collection<AttributeModel<?>> suppportedAttributes = new ArrayList<AttributeModel<?>>();
suppportedAttributes.addAll(Attributes.ALL);
suppportedAttributes.addAll(LwM2mAttributes.ALL);

// Create default link Parser
this.attributeParser = new DefaultAttributeParser(suppportedAttributes);
this.peerSerDes = peerSerDes;
}

public RegistrationSerDes() {
this.attributeParser = new DefaultAttributeParser();
this.peerSerDes = new LwM2mPeerSerDes();
}

public RegistrationSerDes(AttributeParser attributeParser) {
public RegistrationSerDes(AttributeParser attributeParser, LwM2mPeerSerDes peerSerDes) {
this.attributeParser = attributeParser;
this.peerSerDes = peerSerDes;
}

public JsonNode jSerialize(Registration r) {
Expand Down

0 comments on commit 2066961

Please sign in to comment.