|
|
@ -31,6 +31,7 @@ import org.eclipse.leshan.server.bootstrap.BootstrapUtil; |
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress; |
|
|
import java.net.InetSocketAddress; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
import static org.eclipse.leshan.server.bootstrap.BootstrapUtil.toWriteRequest; |
|
|
import static org.eclipse.leshan.server.bootstrap.BootstrapUtil.toWriteRequest; |
|
|
|
|
|
|
|
|
@ -46,6 +47,8 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi |
|
|
*/ |
|
|
*/ |
|
|
protected Map<Integer, Integer> securityInstances; |
|
|
protected Map<Integer, Integer> securityInstances; |
|
|
protected Map<Integer, Integer> serverInstances; |
|
|
protected Map<Integer, Integer> serverInstances; |
|
|
|
|
|
protected Integer bootstrapServerIdOld; |
|
|
|
|
|
protected Integer bootstrapServerIdNew; |
|
|
|
|
|
|
|
|
public LwM2MBootstrapConfigStoreTaskProvider(BootstrapConfigStore store) { |
|
|
public LwM2MBootstrapConfigStoreTaskProvider(BootstrapConfigStore store) { |
|
|
this.store = store; |
|
|
this.store = store; |
|
|
@ -99,8 +102,7 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi |
|
|
findServerInstanceId(readResponse); |
|
|
findServerInstanceId(readResponse); |
|
|
// create requests from config
|
|
|
// create requests from config
|
|
|
tasks.requestsToSend = this.toRequests(config, |
|
|
tasks.requestsToSend = this.toRequests(config, |
|
|
config.contentFormat != null ? config.contentFormat : session.getContentFormat(), |
|
|
config.contentFormat != null ? config.contentFormat : session.getContentFormat()); |
|
|
this.securityInstances.get(0)); |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
// create requests from config
|
|
|
// create requests from config
|
|
|
tasks.requestsToSend = BootstrapUtil.toRequests(config, |
|
|
tasks.requestsToSend = BootstrapUtil.toRequests(config, |
|
|
@ -116,6 +118,7 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void findSecurityInstanceId(Link[] objectLinks) { |
|
|
protected void findSecurityInstanceId(Link[] objectLinks) { |
|
|
|
|
|
log.info("Object after discover: [{}]", objectLinks); |
|
|
this.securityInstances = new HashMap<>(); |
|
|
this.securityInstances = new HashMap<>(); |
|
|
for (Link link : objectLinks) { |
|
|
for (Link link : objectLinks) { |
|
|
if (link.getUrl().startsWith("/0/")) { |
|
|
if (link.getUrl().startsWith("/0/")) { |
|
|
@ -147,10 +150,20 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void findServerInstanceId(BootstrapReadResponse readResponse) { |
|
|
protected void findServerInstanceId(BootstrapReadResponse readResponse) { |
|
|
serverInstances = new HashMap<>(); |
|
|
this.serverInstances = new HashMap<>(); |
|
|
((LwM2mObject) readResponse.getContent()).getInstances().values().forEach(instance -> { |
|
|
((LwM2mObject) readResponse.getContent()).getInstances().values().forEach(instance -> { |
|
|
serverInstances.put(((Long) instance.getResource(0).getValue()).intValue(), instance.getId()); |
|
|
serverInstances.put(((Long) instance.getResource(0).getValue()).intValue(), instance.getId()); |
|
|
}); |
|
|
}); |
|
|
|
|
|
if (this.securityInstances != null && this.securityInstances.size() > 0 && this.serverInstances != null && this.serverInstances.size() > 0) { |
|
|
|
|
|
this.findBootstrapServerId(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void findBootstrapServerId() { |
|
|
|
|
|
Map<Integer, Integer> filteredMap = this.serverInstances.entrySet() |
|
|
|
|
|
.stream().filter(x -> !this.securityInstances.containsKey(x.getKey())) |
|
|
|
|
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
|
|
|
|
|
this.bootstrapServerIdOld = filteredMap.keySet().stream().findFirst().get(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public BootstrapConfigStore getStore() { |
|
|
public BootstrapConfigStore getStore() { |
|
|
@ -172,8 +185,10 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(BootstrapConfig bootstrapConfig, |
|
|
public List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(BootstrapConfig bootstrapConfig, |
|
|
ContentFormat contentFormat, int bootstrapSecurityInstanceId) { |
|
|
ContentFormat contentFormat) { |
|
|
List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests = new ArrayList<>(); |
|
|
List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests = new ArrayList<>(); |
|
|
|
|
|
List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requestsDelete = new ArrayList<>(); |
|
|
|
|
|
List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requestsWrite = new ArrayList<>(); |
|
|
boolean isBsServer = false; |
|
|
boolean isBsServer = false; |
|
|
boolean isLwServer = false; |
|
|
boolean isLwServer = false; |
|
|
/** Map<serverId, InstanceId> */ |
|
|
/** Map<serverId, InstanceId> */ |
|
|
@ -182,57 +197,55 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi |
|
|
int id = 0; |
|
|
int id = 0; |
|
|
for (BootstrapConfig.ServerSecurity security : new TreeMap<>(bootstrapConfig.security).values()) { |
|
|
for (BootstrapConfig.ServerSecurity security : new TreeMap<>(bootstrapConfig.security).values()) { |
|
|
if (security.bootstrapServer) { |
|
|
if (security.bootstrapServer) { |
|
|
requests.add(toWriteRequest(bootstrapSecurityInstanceId, security, contentFormat)); |
|
|
requestsWrite.add(toWriteRequest(this.securityInstances.get(0), security, contentFormat)); |
|
|
isBsServer = true; |
|
|
isBsServer = true; |
|
|
instances.put(security.serverId, bootstrapSecurityInstanceId); |
|
|
this.bootstrapServerIdNew = security.serverId; |
|
|
|
|
|
instances.put(security.serverId, this.securityInstances.get(0)); |
|
|
} else { |
|
|
} else { |
|
|
if (id == bootstrapSecurityInstanceId) { |
|
|
if (id == this.securityInstances.get(0)) { |
|
|
id++; |
|
|
id++; |
|
|
} |
|
|
} |
|
|
requests.add(toWriteRequest(id, security, contentFormat)); |
|
|
requestsWrite.add(toWriteRequest(id, security, contentFormat)); |
|
|
instances.put(security.serverId, id); |
|
|
instances.put(security.serverId, id); |
|
|
|
|
|
isLwServer = true; |
|
|
|
|
|
if (!isBsServer && this.securityInstances.containsKey(security.serverId) && id != this.securityInstances.get(security.serverId)) { |
|
|
|
|
|
requestsDelete.add(new BootstrapDeleteRequest("/0/" + this.securityInstances.get(security.serverId))); |
|
|
|
|
|
} |
|
|
id++; |
|
|
id++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// handle server
|
|
|
// handle server
|
|
|
for (Map.Entry<Integer, BootstrapConfig.ServerConfig> server : bootstrapConfig.servers.entrySet()) { |
|
|
for (Map.Entry<Integer, BootstrapConfig.ServerConfig> server : bootstrapConfig.servers.entrySet()) { |
|
|
int securityInstanceId = instances.get(server.getValue().shortId); |
|
|
int securityInstanceId = instances.get(server.getValue().shortId); |
|
|
requests.add(toWriteRequest(securityInstanceId, server.getValue(), contentFormat)); |
|
|
requestsWrite.add(toWriteRequest(securityInstanceId, server.getValue(), contentFormat)); |
|
|
if (!isBsServer && this.serverInstances.containsKey(server.getValue().shortId) && securityInstanceId != this.serverInstances.get(server.getValue().shortId)){ |
|
|
if (!isLwServer && this.bootstrapServerIdNew != null && server.getValue().shortId == this.bootstrapServerIdNew && |
|
|
requests.add(new BootstrapDeleteRequest("/0/" + securityInstanceId)); |
|
|
(this.bootstrapServerIdNew != this.bootstrapServerIdOld || securityInstanceId != this.serverInstances.get(this.bootstrapServerIdOld))) { |
|
|
requests.add(new BootstrapDeleteRequest("/1/" + this.serverInstances.get(server.getValue().shortId))); |
|
|
requestsDelete.add(new BootstrapDeleteRequest("/1/" + this.serverInstances.get(this.bootstrapServerIdOld))); |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
if (!isBsServer && this.serverInstances.containsKey(server.getValue().shortId) && securityInstanceId != this.serverInstances.get(server.getValue().shortId)) { |
|
|
|
|
|
requestsDelete.add(new BootstrapDeleteRequest("/1/" + this.serverInstances.get(server.getValue().shortId))); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
isLwServer = true; |
|
|
|
|
|
} |
|
|
} |
|
|
// handle acl
|
|
|
// handle acl
|
|
|
for (Map.Entry<Integer, BootstrapConfig.ACLConfig> acl : bootstrapConfig.acls.entrySet()) { |
|
|
for (Map.Entry<Integer, BootstrapConfig.ACLConfig> acl : bootstrapConfig.acls.entrySet()) { |
|
|
requests.add(toWriteRequest(acl.getKey(), acl.getValue(), contentFormat)); |
|
|
requestsWrite.add(toWriteRequest(acl.getKey(), acl.getValue(), contentFormat)); |
|
|
} |
|
|
} |
|
|
// handle delete
|
|
|
// handle delete
|
|
|
if (isBsServer & isLwServer) { |
|
|
if (isBsServer & isLwServer) { |
|
|
requests.add(new BootstrapDeleteRequest("/0")); |
|
|
requests.add(new BootstrapDeleteRequest("/0")); |
|
|
requests.add(new BootstrapDeleteRequest("/1")); |
|
|
requests.add(new BootstrapDeleteRequest("/1")); |
|
|
|
|
|
} else if (requestsDelete.size() > 0) { |
|
|
|
|
|
requests.addAll(requestsDelete); |
|
|
|
|
|
} |
|
|
|
|
|
// handle write
|
|
|
|
|
|
if (requestsWrite.size() > 0) { |
|
|
|
|
|
requests.addAll(requestsWrite); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return (requests); |
|
|
return (requests); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private InetSocketAddress getSocketAddress(String uri) { |
|
|
|
|
|
// String uri1 = "coap://localhost:5687";
|
|
|
|
|
|
if (uri.contains("coap://")) { |
|
|
|
|
|
uri = uri.replace("coap://", ""); |
|
|
|
|
|
} else return null; |
|
|
|
|
|
String[] uris = uri.split(":"); |
|
|
|
|
|
if (uris.length == 2) { |
|
|
|
|
|
try { |
|
|
|
|
|
return new InetSocketAddress(uris[0], Integer.parseInt(uris[1])); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initSupportedObjectsDefault() { |
|
|
private void initSupportedObjectsDefault() { |
|
|
this.supportedObjects = new HashMap<>(); |
|
|
this.supportedObjects = new HashMap<>(); |
|
|
this.supportedObjects.put(0, "1.1"); |
|
|
this.supportedObjects.put(0, "1.1"); |
|
|
|