[ONOS-6884] Device config related utilities
- misc fixes around dynamic config
Change-Id: I3a7b6130e8c698594fa7deac8a04219c9c8a4af2
diff --git a/apps/config/src/main/java/org/onosproject/config/DynamicConfigEvent.java b/apps/config/src/main/java/org/onosproject/config/DynamicConfigEvent.java
index 859e14d..a2e513e 100755
--- a/apps/config/src/main/java/org/onosproject/config/DynamicConfigEvent.java
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigEvent.java
@@ -53,7 +53,7 @@
NODE_DELETED,
/**
- * Signifies an unknown and hence invalid store opeartion.
+ * Signifies an unknown and hence invalid store operation.
*/
UNKNOWN_OPRN
}
diff --git a/apps/config/src/main/java/org/onosproject/config/DynamicConfigServiceAdapter.java b/apps/config/src/main/java/org/onosproject/config/DynamicConfigServiceAdapter.java
new file mode 100644
index 0000000..4dcd671
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigServiceAdapter.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.config;
+
+import static org.onosproject.yang.model.RpcOutput.Status.RPC_NODATA;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.RpcInput;
+import org.onosproject.yang.model.RpcOutput;
+
+/**
+ * Adapter for DynamicConfigService.
+ */
+public class DynamicConfigServiceAdapter
+ implements DynamicConfigService {
+
+ protected final ListenerRegistry<DynamicConfigEvent, DynamicConfigListener>
+ listenerRegistry = new ListenerRegistry<>();
+
+
+
+ @Override
+ public void createNode(ResourceId path, DataNode node) {
+ }
+
+ @Override
+ public DataNode readNode(ResourceId path, Filter filter) {
+ return null;
+ }
+
+ @Override
+ public Boolean nodeExist(ResourceId path) {
+ return true;
+ }
+
+ @Override
+ public void updateNode(ResourceId path, DataNode node) {
+ }
+
+ @Override
+ public void replaceNode(ResourceId path, DataNode node) {
+ }
+
+ @Override
+ public void deleteNode(ResourceId path) {
+ }
+
+ @Override
+ public CompletableFuture<RpcOutput> invokeRpc(ResourceId id,
+ RpcInput input) {
+ return CompletableFuture.completedFuture(new RpcOutput(RPC_NODATA, null));
+ }
+
+ @Override
+ public void addListener(DynamicConfigListener listener) {
+ listenerRegistry.addListener(listener);
+ }
+
+ @Override
+ public void removeListener(DynamicConfigListener listener) {
+ listenerRegistry.removeListener(listener);
+ }
+
+}
diff --git a/apps/config/src/main/java/org/onosproject/config/ForwardingDynamicConfigService.java b/apps/config/src/main/java/org/onosproject/config/ForwardingDynamicConfigService.java
new file mode 100644
index 0000000..b8e9167
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/config/ForwardingDynamicConfigService.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.config;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.RpcInput;
+import org.onosproject.yang.model.RpcOutput;
+
+/**
+ * A DynamicConfigService which forwards all its method calls
+ * to another DynamicConfigService.
+ */
+public class ForwardingDynamicConfigService implements DynamicConfigService {
+
+ private final DynamicConfigService delegate;
+
+ protected ForwardingDynamicConfigService(DynamicConfigService delegate) {
+ this.delegate = checkNotNull(delegate);
+ }
+
+ protected DynamicConfigService delegate() {
+ return delegate;
+ }
+
+ @Override
+ public void addListener(DynamicConfigListener listener) {
+ delegate.addListener(listener);
+ }
+
+ @Override
+ public void removeListener(DynamicConfigListener listener) {
+ delegate.removeListener(listener);
+ }
+
+ @Override
+ public void createNode(ResourceId path, DataNode node) {
+ delegate.createNode(path, node);
+ }
+
+ @Override
+ public DataNode readNode(ResourceId path, Filter filter) {
+ return delegate.readNode(path, filter);
+ }
+
+ @Override
+ public Boolean nodeExist(ResourceId path) {
+ return delegate.nodeExist(path);
+ }
+
+ @Override
+ public void updateNode(ResourceId path, DataNode node) {
+ delegate.updateNode(path, node);
+ }
+
+ @Override
+ public void replaceNode(ResourceId path, DataNode node) {
+ delegate.replaceNode(path, node);
+ }
+
+ @Override
+ public void deleteNode(ResourceId path) {
+ delegate.deleteNode(path);
+ }
+
+ @Override
+ public CompletableFuture<RpcOutput> invokeRpc(ResourceId id,
+ RpcInput input) {
+ return delegate.invokeRpc(id, input);
+ }
+}
diff --git a/apps/config/src/main/java/org/onosproject/d/config/DataNodes.java b/apps/config/src/main/java/org/onosproject/d/config/DataNodes.java
new file mode 100644
index 0000000..5998206
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/d/config/DataNodes.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.d.config;
+
+import java.util.Collection;
+import java.util.Optional;
+
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.NodeKey;
+import org.onosproject.yang.model.SchemaId;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Utility related to DataNode.
+ */
+@Beta
+public abstract class DataNodes {
+
+ // TODO it might make sense to turn these into DataNode methods
+
+ // should avoid using this. use other child methods
+ @Deprecated
+ public static Optional<DataNode> childOnlyByName(DataNode node, String name) {
+ if (node instanceof InnerNode) {
+ InnerNode inode = (InnerNode) node;
+ return inode.childNodes().values().stream()
+ .filter(dn -> dn.key().schemaId().name().equals(name))
+ .findFirst();
+ } else {
+ return Optional.empty();
+ }
+ }
+
+ public static Optional<DataNode> child(DataNode node, String name, String namespace) {
+ if (node instanceof InnerNode) {
+ InnerNode inode = (InnerNode) node;
+ NodeKey<?> key = NodeKey.builder().schemaId(name, namespace).build();
+ return Optional.ofNullable(inode.childNodes().get(key));
+ } else {
+ return Optional.empty();
+ }
+ }
+
+ public static Optional<DataNode> child(DataNode node, SchemaId child) {
+ if (node instanceof InnerNode) {
+ InnerNode inode = (InnerNode) node;
+ NodeKey<?> key = NodeKey.builder().schemaId(child).build();
+ return Optional.ofNullable(inode.childNodes().get(key));
+ } else {
+ return Optional.empty();
+ }
+ }
+
+ public static Optional<DataNode> child(DataNode node, NodeKey<?> child) {
+ if (node instanceof InnerNode) {
+ InnerNode inode = (InnerNode) node;
+ return Optional.ofNullable(inode.childNodes().get(child));
+ } else {
+ return Optional.empty();
+ }
+ }
+
+ public static Collection<DataNode> children(DataNode node) {
+ if (node instanceof InnerNode) {
+ InnerNode inode = (InnerNode) node;
+ return inode.childNodes().values();
+ } else {
+ return ImmutableList.of();
+ }
+ }
+
+}
diff --git a/apps/config/src/main/java/org/onosproject/d/config/DeviceResourceIds.java b/apps/config/src/main/java/org/onosproject/d/config/DeviceResourceIds.java
new file mode 100644
index 0000000..a49c6fc
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/d/config/DeviceResourceIds.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.d.config;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import java.util.Optional;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.yang.model.KeyLeaf;
+import org.onosproject.yang.model.ListKey;
+import org.onosproject.yang.model.NodeKey;
+import org.onosproject.yang.model.ResourceId;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Utility related to device ResourceId.
+ */
+@Beta
+public abstract class DeviceResourceIds {
+
+ // assuming following device tree structure
+ // - "/"
+ // +- devices
+ // +- device (=device root node:ListKey)
+
+
+ // FIXME randomly defined namespace, replace with something appropriate
+ /**
+ * SchemaId namespace for DCS defined nodes.
+ */
+ public static final String DCS_NAMESPACE = "org.onosproject.dcs";
+
+ /**
+ * SchemaId name for root node.
+ */
+ public static final String ROOT_NAME = "/";
+ /**
+ * SchemaId name for devices node.
+ */
+ public static final String DEVICES_NAME = "devices";
+ /**
+ * SchemaId name for device node.
+ */
+ public static final String DEVICE_NAME = "device";
+ /**
+ * KeyLeaf {@code name}, which holds DeviceId information on device node.
+ */
+ public static final String DEVICE_ID_KL_NAME = "device-id";
+
+ /**
+ * ResourceId pointing at root node.
+ */
+ public static final ResourceId ROOT_ID = ResourceId.builder()
+ .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
+ .build();
+
+ static final NodeKey<?> ROOT_NODE =
+ NodeKey.builder().schemaId(ROOT_NAME, DCS_NAMESPACE).build();
+
+ /**
+ * nodeKeys index for root node.
+ */
+ static final int ROOT_INDEX = 0;
+ /**
+ * nodeKeys index for devices node.
+ */
+ static final int DEVICES_INDEX = 1;
+ /**
+ * nodeKeys index for device node.
+ */
+ static final int DEVICE_INDEX = 2;
+
+ /**
+ * Tests if specified path points to root node of a Device.
+ *
+ * @param path to test.
+ * @return true if path points to root node of a Device.
+ */
+ public static boolean isDeviceRootNode(ResourceId path) {
+ return path.nodeKeys().size() == 3 &&
+ isUnderDeviceRootNode(path);
+ }
+
+ /**
+ * Tests if specified path points to root node of a Device.
+ *
+ * @param path to test.
+ * @return true if path points to root node of a Device.
+ */
+ public static boolean isUnderDeviceRootNode(ResourceId path) {
+ return path.nodeKeys().size() >= 3 &&
+ // TODO Would be better to test whole schemeId
+ DEVICE_NAME.equals(path.nodeKeys().get(DEVICE_INDEX).schemaId().name()) &&
+ (path.nodeKeys().get(DEVICE_INDEX) instanceof ListKey) &&
+ // TODO Would be better to test whole schemeId
+ DEVICES_NAME.equals(path.nodeKeys().get(DEVICES_INDEX).schemaId().name()) &&
+ ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
+ }
+
+ /**
+ * Tests if specified path points to root or devices node.
+ *
+ * @param path to test.
+ * @return true if path points to root node of a Device.
+ */
+ public static boolean isRootOrDevicesNode(ResourceId path) {
+ return isDevicesNode(path) ||
+ isRootNode(path);
+ }
+
+ public static boolean isDevicesNode(ResourceId path) {
+ return path.nodeKeys().size() == 2 &&
+ // TODO Would be better to test whole schemeId
+ DEVICES_NAME.equals(path.nodeKeys().get(DEVICES_INDEX).schemaId().name()) &&
+ ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
+ }
+
+ public static boolean isRootNode(ResourceId path) {
+ return path.nodeKeys().size() == 1 &&
+ ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
+ }
+
+ /**
+ * Transforms device resource path to DeviceId.
+ *
+ * @param path pointing to somewhere in the subtree of a device
+ * @return DeviceId
+ * @throws IllegalArgumentException if the path was not part of devices tree
+ */
+ public static DeviceId toDeviceId(ResourceId path) {
+ checkArgument(isUnderDeviceRootNode(path), path);
+ // FIXME if we decide to drop any of intermediate nodes
+ // "/" - "devices" - "device"
+ return toDeviceId(path.nodeKeys().get(DEVICE_INDEX));
+ }
+
+ /**
+ * Transforms root node of a device to corresponding DeviceId.
+ *
+ * @param deviceRoot NodeKey of a device root node
+ * @return DeviceId
+ * @throws IllegalArgumentException if not a device node
+ */
+ public static DeviceId toDeviceId(NodeKey<?> deviceRoot) {
+ // TODO Would be better to test whole schemeId
+ if (!DEVICE_NAME.equals(deviceRoot.schemaId().name())) {
+ throw new IllegalArgumentException(deviceRoot + " is not a device node");
+ }
+
+ if (deviceRoot instanceof ListKey) {
+ ListKey device = (ListKey) deviceRoot;
+ Optional<DeviceId> did = device.keyLeafs().stream()
+ // TODO If we decide to define ONOS schema for device ID,
+ // use whole schemaId to filter, not only by name
+ .filter(kl -> DEVICE_ID_KL_NAME.equals(kl.leafSchema().name()))
+ .map(KeyLeaf::leafValAsString)
+ .map(DeviceId::deviceId)
+ .findFirst();
+
+ if (did.isPresent()) {
+ return did.get();
+ }
+ throw new IllegalArgumentException("device-id not found in " + deviceRoot);
+ }
+ throw new IllegalArgumentException("Unexpected type " + deviceRoot.getClass());
+ }
+
+ /**
+ * Transforms DeviceId into a ResourceId pointing to device root node.
+ *
+ * @param deviceId to transform
+ * @return ResourceId
+ */
+ public static ResourceId toResourceId(DeviceId deviceId) {
+ return ResourceId.builder()
+ .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
+ .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
+ .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE)
+ .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, deviceId.toString())
+ .build();
+ }
+
+}
diff --git a/apps/config/src/main/java/org/onosproject/d/config/ResourceIds.java b/apps/config/src/main/java/org/onosproject/d/config/ResourceIds.java
new file mode 100644
index 0000000..4340d27
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/d/config/ResourceIds.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.d.config;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.KeyLeaf;
+import org.onosproject.yang.model.LeafListKey;
+import org.onosproject.yang.model.ListKey;
+import org.onosproject.yang.model.NodeKey;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.ResourceId.Builder;
+import org.onosproject.yang.model.SchemaId;
+import org.slf4j.Logger;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Utility related to ResourceId.
+ */
+@Beta
+public abstract class ResourceIds {
+
+ private static final Logger log = getLogger(ResourceIds.class);
+
+ /**
+ * Builds the ResourceId of specified {@code node}.
+ *
+ * @param parent ResourceId of {@code node} parent
+ * @param node to create ResourceId.
+ * @return ResourceId of {@code node}
+ */
+ public static ResourceId resourceId(ResourceId parent, DataNode node) {
+ final ResourceId.Builder builder;
+ if (parent == null) {
+ builder = ResourceId.builder();
+ } else {
+ try {
+ builder = parent.copyBuilder();
+ } catch (CloneNotSupportedException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ SchemaId sid = node.key().schemaId();
+ switch (node.type()) {
+ case MULTI_INSTANCE_LEAF_VALUE_NODE:
+ builder.addLeafListBranchPoint(sid.name(), sid.namespace(),
+ ((LeafListKey) node.key()).asString());
+ break;
+
+ case MULTI_INSTANCE_NODE:
+ builder.addBranchPointSchema(sid.name(), sid.namespace());
+ for (KeyLeaf keyLeaf : ((ListKey) node.key()).keyLeafs()) {
+ builder.addKeyLeaf(keyLeaf.leafSchema().name(),
+ keyLeaf.leafSchema().namespace(),
+ keyLeaf.leafValAsString());
+ }
+ break;
+
+ case SINGLE_INSTANCE_LEAF_VALUE_NODE:
+ case SINGLE_INSTANCE_NODE:
+ builder.addBranchPointSchema(sid.name(), sid.namespace());
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unknown type " + node);
+
+ }
+
+ return builder.build();
+ }
+
+ /**
+ * Concats {@code path} after {@code prefix}.
+ *
+ * @param prefix path
+ * @param path to append after {@code path}
+ * @return concatenated ResouceId
+ */
+ public static ResourceId concat(ResourceId prefix, ResourceId path) {
+ checkArgument(!path.nodeKeys().contains(DeviceResourceIds.ROOT_NODE),
+ "%s was already absolute path", path);
+ try {
+ return prefix.copyBuilder().append(path).build();
+ } catch (CloneNotSupportedException e) {
+ log.error("Could not copy {}", path, e);
+ throw new IllegalArgumentException("Could not copy " + path, e);
+ }
+ }
+
+
+ /**
+ * Returns {@code child} ad relative ResourceId against {@code base}.
+ *
+ * @param base ResourceId
+ * @param child ResourceId to relativize
+ * @return relative ResourceId
+ */
+ public static ResourceId relativize(ResourceId base, ResourceId child) {
+ checkArgument(child.nodeKeys().size() >= base.nodeKeys().size(),
+ "%s path must be deeper than base prefix %s", child, base);
+ checkArgument(base.nodeKeys().equals(child.nodeKeys().subList(0, base.nodeKeys().size())),
+ "%s is not a prefix of %s", child, base);
+
+ // FIXME waiting for Yang tools 2.2.0-b4 or later
+// return ResourceId.builder().append(child.nodeKeys().subList(base.nodeKeys().size(),
+// child.nodeKeys().size())).build();
+
+ Builder builder = ResourceId.builder();
+ for (NodeKey nodeKey : child.nodeKeys().subList(base.nodeKeys().size(),
+ child.nodeKeys().size())) {
+ if (nodeKey instanceof ListKey) {
+ ListKey listKey = (ListKey) nodeKey;
+ for (KeyLeaf keyLeaf : listKey.keyLeafs()) {
+ builder.addKeyLeaf(keyLeaf.leafSchema().name(),
+ keyLeaf.leafSchema().namespace(),
+ keyLeaf.leafValAsString());
+ }
+ } else if (nodeKey instanceof LeafListKey) {
+ LeafListKey llKey = (LeafListKey) nodeKey;
+ builder.addLeafListBranchPoint(llKey.schemaId().name(),
+ llKey.schemaId().namespace(),
+ llKey.value());
+
+ } else {
+ builder.addBranchPointSchema(nodeKey.schemaId().name(),
+ nodeKey.schemaId().namespace());
+ }
+ }
+ return builder.build();
+ }
+
+ /**
+ * Tests if {@code child} starts with {@code prefix}.
+ *
+ * @param prefix expected
+ * @param child to test
+ * @return true if {@code child} starts with {@code prefix}
+ */
+ public static boolean isPrefix(ResourceId prefix, ResourceId child) {
+
+ return child.nodeKeys().size() >= prefix.nodeKeys().size() &&
+ prefix.nodeKeys().equals(child.nodeKeys().subList(0, prefix.nodeKeys().size()));
+ }
+
+}
diff --git a/apps/config/src/main/java/org/onosproject/d/config/package-info.java b/apps/config/src/main/java/org/onosproject/d/config/package-info.java
new file mode 100644
index 0000000..b833e16
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/d/config/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * Dynamic device config supporting classes and interfaces.
+ */
+package org.onosproject.d.config;