diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/model/LwM2mModelRepository.java b/leshan-core/src/main/java/org/eclipse/leshan/core/model/LwM2mModelRepository.java new file mode 100644 index 0000000000..e9b2142bbd --- /dev/null +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/model/LwM2mModelRepository.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2019 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.model; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +import org.eclipse.leshan.util.Validate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A collection of LWM2M object definitions which could contained several definitions of the same object in different + * version. + */ +public class LwM2mModelRepository { + private static final Logger LOG = LoggerFactory.getLogger(LwM2mModelRepository.class); + + // This map contains all the object models available. Different version could be used. + // This map is indexed by a string composed of objectid and version (objectid##version) + private final Map objects; + + public LwM2mModelRepository(ObjectModel... objectModels) { + this(Arrays.asList(objectModels)); + } + + public LwM2mModelRepository(Collection objectModels) { + if (objectModels == null) { + objects = new TreeMap<>(); + } else { + Map map = new HashMap<>(); + for (ObjectModel model : objectModels) { + String key = getKey(model); + if (key == null) { + new IllegalArgumentException(String.format("Model %s is invalid : object id is missing.", model)); + } + ObjectModel old = map.put(key, model); + if (old != null) { + LOG.debug("Model already exists for object {} in version {}. Overriding it.", model.id, + model.getVersion()); + } + } + objects = Collections.unmodifiableMap(map); + } + } + + public ObjectModel getObjectModel(Integer objectId, String version) { + Validate.notNull(objectId, "objectid must not be null"); + Validate.notNull(version, "version must not be null"); + + return objects.get(getKey(objectId, version)); + } + + private String getKey(ObjectModel objectModel) { + return getKey(objectModel.id, objectModel.getVersion()); + } + + private String getKey(Integer objectId, String version) { + if (objectId == null) { + return null; + } + return objectId + "##" + version; + } +} diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/model/ObjectModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/model/ObjectModel.java index 73a27c796b..c052143fe9 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/model/ObjectModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/model/ObjectModel.java @@ -77,6 +77,17 @@ public boolean isOmaObject() { return id >= OMA_OBJECT_MIN_ID && id <= OMA_OBJECT_MAX_ID; } + /** + * @return the version and if the version is null or empty return the default value 1.0 + * @see ObjectModel#DEFAULT_VERSION + */ + public String getVersion() { + if (version == null || version.isEmpty()) { + return ObjectModel.DEFAULT_VERSION; + } + return version; + } + @Override public String toString() { StringBuilder builder = new StringBuilder();