Skip to content

Commit

Permalink
Move registration data extraction from link in a dedicated class.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 14, 2023
1 parent b7e8a2d commit 332d487
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.eclipse.leshan.server.queue.ClientAwakeTimeProvider;
import org.eclipse.leshan.server.queue.PresenceListener;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.registration.RegistrationDataExtractor;
import org.eclipse.leshan.server.registration.RegistrationIdProvider;
import org.eclipse.leshan.server.registration.RegistrationListener;
import org.eclipse.leshan.server.registration.RegistrationStore;
Expand All @@ -78,11 +79,12 @@ public class LeshanTestServer extends LeshanServer {
public LeshanTestServer(LwM2mServerEndpointsProvider endpointsProvider, RegistrationStore registrationStore,
SecurityStore securityStore, Authorizer authorizer, LwM2mModelProvider modelProvider, LwM2mEncoder encoder,
LwM2mDecoder decoder, boolean noQueueMode, ClientAwakeTimeProvider awakeTimeProvider,
RegistrationIdProvider registrationIdProvider, LwM2mLinkParser linkParser,
ServerSecurityInfo serverSecurityInfo, boolean updateRegistrationOnNotification) {
RegistrationIdProvider registrationIdProvider, RegistrationDataExtractor registrationDataExtractor,
LwM2mLinkParser linkParser, ServerSecurityInfo serverSecurityInfo,
boolean updateRegistrationOnNotification) {
super(endpointsProvider, registrationStore, securityStore, authorizer, modelProvider, encoder, decoder,
noQueueMode, awakeTimeProvider, registrationIdProvider, updateRegistrationOnNotification, linkParser,
serverSecurityInfo);
noQueueMode, awakeTimeProvider, registrationIdProvider, registrationDataExtractor,
updateRegistrationOnNotification, linkParser, serverSecurityInfo);

if (securityStore != null && !(securityStore instanceof EditableSecurityStore)) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.leshan.server.model.VersionedModelProvider;
import org.eclipse.leshan.server.queue.ClientAwakeTimeProvider;
import org.eclipse.leshan.server.queue.StaticClientAwakeTimeProvider;
import org.eclipse.leshan.server.registration.RegistrationDataExtractor;
import org.eclipse.leshan.server.registration.RegistrationIdProvider;
import org.eclipse.leshan.server.registration.RegistrationStore;
import org.eclipse.leshan.server.security.Authorizer;
Expand Down Expand Up @@ -79,8 +80,8 @@ protected LeshanTestServer createServer(LwM2mServerEndpointsProvider endpointsPr
RegistrationStore registrationStore, SecurityStore securityStore, Authorizer authorizer,
LwM2mModelProvider modelProvider, LwM2mEncoder encoder, LwM2mDecoder decoder, boolean noQueueMode,
ClientAwakeTimeProvider awakeTimeProvider, RegistrationIdProvider registrationIdProvider,
LwM2mLinkParser linkParser, ServerSecurityInfo serverSecurityInfo,
boolean updateRegistrationOnNotification) {
RegistrationDataExtractor registrationDataExtractor, LwM2mLinkParser linkParser,
ServerSecurityInfo serverSecurityInfo, boolean updateRegistrationOnNotification) {

// create endpoint provider.
if (endpointsProvider == null) {
Expand All @@ -103,8 +104,8 @@ protected LeshanTestServer createServer(LwM2mServerEndpointsProvider endpointsPr
}
}
return new LeshanTestServer(endpointsProvider, registrationStore, securityStore, authorizer, modelProvider,
encoder, decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, linkParser,
serverSecurityInfo, updateRegistrationOnNotification);
encoder, decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, registrationDataExtractor,
linkParser, serverSecurityInfo, updateRegistrationOnNotification);
}

public static LeshanTestServerBuilder givenServerUsing(Protocol protocolToUse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
import org.eclipse.leshan.core.californium.identity.DefaultCoapIdentityHandler;
import org.eclipse.leshan.core.californium.identity.IdentityHandler;
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
Expand Down Expand Up @@ -55,8 +56,10 @@
import org.eclipse.leshan.core.tlv.Tlv;
import org.eclipse.leshan.core.tlv.Tlv.TlvType;
import org.eclipse.leshan.core.tlv.TlvDecoder;
import org.eclipse.leshan.server.registration.DefaultRegistrationDataExtractor;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.registration.Registration.Builder;
import org.eclipse.leshan.server.registration.RegistrationDataExtractor.RegistrationData;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -84,9 +87,17 @@ private Registration newRegistration(String rootpath) throws UnknownHostExceptio
Builder b = new Registration.Builder("regid", "endpoint",
Identity.unsecure(Inet4Address.getLoopbackAddress(), 12354),
EndpointUriUtil.createUri("coap://localhost:5683"));
b.extractDataFromObjectLink(true);

if (rootpath != null) {
b.objectLinks(new Link[] { new Link(rootpath, new ResourceTypeAttribute("oma.lwm2m")) });
Link[] links = new Link[] { new Link(rootpath, new ResourceTypeAttribute("oma.lwm2m")) };
b.objectLinks(links);

RegistrationData dataFromObjectLinks = new DefaultRegistrationDataExtractor()
.extractDataFromObjectLinks(links, LwM2mVersion.V1_0);
b.rootPath(dataFromObjectLinks.getAlternatePath());
b.supportedContentFormats(dataFromObjectLinks.getSupportedContentFormats());
b.supportedObjects(dataFromObjectLinks.getSupportedObjects());
b.availableInstances(dataFromObjectLinks.getAvailableInstances());
}
return b.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.eclipse.leshan.server.queue.PresenceStateListener;
import org.eclipse.leshan.server.queue.QueueModeLwM2mRequestSender;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.registration.RegistrationDataExtractor;
import org.eclipse.leshan.server.registration.RegistrationHandler;
import org.eclipse.leshan.server.registration.RegistrationIdProvider;
import org.eclipse.leshan.server.registration.RegistrationListener;
Expand Down Expand Up @@ -118,6 +119,7 @@ public class LeshanServer {
* @param awakeTimeProvider to set the client awake time if queue mode is used.
* @param registrationIdProvider to provide registrationId using for location-path option values on response of
* Register operation.
* @param registrationDataExtractor to extract registration data from object links
* @param updateRegistrationOnNotification will activate registration update on observe notification.
* @param linkParser a parser {@link LwM2mLinkParser} used to parse a CoRE Link.
* @param serverSecurityInfo credentials of the Server
Expand All @@ -126,8 +128,9 @@ public class LeshanServer {
public LeshanServer(LwM2mServerEndpointsProvider endpointsProvider, RegistrationStore registrationStore,
SecurityStore securityStore, Authorizer authorizer, LwM2mModelProvider modelProvider, LwM2mEncoder encoder,
LwM2mDecoder decoder, boolean noQueueMode, ClientAwakeTimeProvider awakeTimeProvider,
RegistrationIdProvider registrationIdProvider, boolean updateRegistrationOnNotification,
LwM2mLinkParser linkParser, ServerSecurityInfo serverSecurityInfo) {
RegistrationIdProvider registrationIdProvider, RegistrationDataExtractor registrationDataExtractor,
boolean updateRegistrationOnNotification, LwM2mLinkParser linkParser,
ServerSecurityInfo serverSecurityInfo) {

Validate.notNull(endpointsProvider, "endpointsProvider cannot be null");
Validate.notNull(registrationStore, "registration store cannot be null");
Expand Down Expand Up @@ -157,7 +160,7 @@ public LeshanServer(LwM2mServerEndpointsProvider endpointsProvider, Registration
ServerEndpointToolbox toolbox = new ServerEndpointToolbox(decoder, encoder, linkParser,
new DefaultClientProfileProvider(registrationStore, modelProvider));
RegistrationHandler registrationHandler = new RegistrationHandler(registrationService, authorizer,
registrationIdProvider);
registrationIdProvider, registrationDataExtractor);
DefaultUplinkRequestReceiver requestReceiver = new DefaultUplinkRequestReceiver(registrationHandler,
sendService);
endpointsProvider.createEndpoints(requestReceiver, observationService, toolbox, serverSecurityInfo, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
import org.eclipse.leshan.server.model.StandardModelProvider;
import org.eclipse.leshan.server.queue.ClientAwakeTimeProvider;
import org.eclipse.leshan.server.queue.StaticClientAwakeTimeProvider;
import org.eclipse.leshan.server.registration.DefaultRegistrationDataExtractor;
import org.eclipse.leshan.server.registration.InMemoryRegistrationStore;
import org.eclipse.leshan.server.registration.RandomStringRegistrationIdProvider;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.registration.RegistrationDataExtractor;
import org.eclipse.leshan.server.registration.RegistrationIdProvider;
import org.eclipse.leshan.server.registration.RegistrationStore;
import org.eclipse.leshan.server.security.Authorizer;
Expand All @@ -65,6 +67,7 @@ public class LeshanServerBuilder {
private Authorizer authorizer;
private ClientAwakeTimeProvider awakeTimeProvider;
private RegistrationIdProvider registrationIdProvider;
private RegistrationDataExtractor registrationDataExtractor;

private LwM2mEncoder encoder;
private LwM2mDecoder decoder;
Expand Down Expand Up @@ -234,6 +237,18 @@ public void setRegistrationIdProvider(RegistrationIdProvider registrationIdProvi
this.registrationIdProvider = registrationIdProvider;
}

/**
* Sets {@link RegistrationDataExtractor} responsible to extract Registration Data from object links generally
* received from Register or Update requests.
* <p>
* By default, {@link DefaultRegistrationDataExtractor} is used.
*
* @param registrationDataExtractor the {@link RegistrationDataExtractor} to set.
*/
public void setRegistrationDataExtractor(RegistrationDataExtractor registrationDataExtractor) {
this.registrationDataExtractor = registrationDataExtractor;
}

/**
* Update Registration on notification.
* <p>
Expand Down Expand Up @@ -293,12 +308,16 @@ public LeshanServer build() {
if (registrationIdProvider == null)
registrationIdProvider = new RandomStringRegistrationIdProvider();

if (registrationDataExtractor == null) {
registrationDataExtractor = new DefaultRegistrationDataExtractor();
}

ServerSecurityInfo serverSecurityInfo = new ServerSecurityInfo(privateKey, publicKey, certificateChain,
trustedCertificates);

return createServer(endpointProvider, registrationStore, securityStore, authorizer, modelProvider, encoder,
decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, linkParser, serverSecurityInfo,
updateRegistrationOnNotification);
decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, registrationDataExtractor, linkParser,
serverSecurityInfo, updateRegistrationOnNotification);
}

/**
Expand All @@ -309,16 +328,16 @@ public LeshanServer build() {
*
* @see LeshanServer#LeshanServer(LwM2mServerEndpointsProvider, RegistrationStore, SecurityStore, Authorizer,
* LwM2mModelProvider, LwM2mEncoder, LwM2mDecoder, boolean, ClientAwakeTimeProvider, RegistrationIdProvider,
* boolean, LwM2mLinkParser, ServerSecurityInfo)
* RegistrationDataExtractor, boolean, LwM2mLinkParser, ServerSecurityInfo)
*/
protected LeshanServer createServer(LwM2mServerEndpointsProvider endpointsProvider,
RegistrationStore registrationStore, SecurityStore securityStore, Authorizer authorizer,
LwM2mModelProvider modelProvider, LwM2mEncoder encoder, LwM2mDecoder decoder, boolean noQueueMode,
ClientAwakeTimeProvider awakeTimeProvider, RegistrationIdProvider registrationIdProvider,
LwM2mLinkParser linkParser, ServerSecurityInfo serverSecurityInfo,
boolean updateRegistrationOnNotification) {
RegistrationDataExtractor registrationDataExtractor, LwM2mLinkParser linkParser,
ServerSecurityInfo serverSecurityInfo, boolean updateRegistrationOnNotification) {
return new LeshanServer(endpointsProvider, registrationStore, securityStore, authorizer, modelProvider, encoder,
decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, updateRegistrationOnNotification,
linkParser, serverSecurityInfo);
decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, registrationDataExtractor,
updateRegistrationOnNotification, linkParser, serverSecurityInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void updateRegistrationOnRegistration(Observation observation, ClientPro
if (updateRegistrationOnNotification) {
Identity obsIdentity = profile.getIdentity();
RegistrationUpdate regUpdate = new RegistrationUpdate(observation.getRegistrationId(), obsIdentity, null,
null, null, null, null, null);
null, null, null, null, null, null, null, null, null);
UpdatedRegistration updatedRegistration = registrationStore.updateRegistration(regUpdate);
if (updatedRegistration == null || updatedRegistration.getUpdatedRegistration() == null) {
LOG.error("Unexpected error: There is no registration with id {} for this observation {}",
Expand Down
Loading

0 comments on commit 332d487

Please sign in to comment.