DynamicDeviceConfigServiceView
DynamicConfigService interface to provide a viewport under specified Device's tree.
Change-Id: I6134b526ec24d6c5ca1c0329cf2a1b885eafe35e
diff --git a/apps/config/src/main/java/org/onosproject/d/config/DynamicDeviceConfigServiceView.java b/apps/config/src/main/java/org/onosproject/d/config/DynamicDeviceConfigServiceView.java
new file mode 100644
index 0000000..1ec6929
--- /dev/null
+++ b/apps/config/src/main/java/org/onosproject/d/config/DynamicDeviceConfigServiceView.java
@@ -0,0 +1,170 @@
+/*
+ * 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 com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+
+import org.onosproject.config.DynamicConfigEvent;
+import org.onosproject.config.DynamicConfigListener;
+import org.onosproject.config.DynamicConfigService;
+import org.onosproject.config.Filter;
+import org.onosproject.config.ForwardingDynamicConfigService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.RpcInput;
+import org.onosproject.yang.model.RpcOutput;
+import org.slf4j.Logger;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * DynamicConfigService interface to provide a viewport
+ * under specified Device's tree.
+ */
+@Beta
+public class DynamicDeviceConfigServiceView
+ extends ForwardingDynamicConfigService
+ implements DynamicConfigService {
+
+ private static final Logger log = getLogger(DynamicDeviceConfigServiceView.class);
+
+ private final DeviceId deviceId;
+
+ private ResourceId deviceRoot;
+
+ /**
+ * original to wrapped listener.
+ */
+ private final Map<DynamicConfigListener, DynamicConfigListener> wrapped =
+ Collections.synchronizedMap(new IdentityHashMap<>());
+
+ protected DynamicDeviceConfigServiceView(DynamicConfigService delegate, DeviceId deviceId) {
+ super(delegate);
+ this.deviceId = checkNotNull(deviceId);
+ this.deviceRoot = DeviceResourceIds.toResourceId(this.deviceId);
+ }
+
+ /**
+ * Create a DynamicDeviceConfigServiceView for specified {@code deviceId}.
+ *
+ * @param service reference to actual DynamicConfigService.
+ * @param deviceId of the Device to provide view port about.
+ * @return DynamicDeviceConfigServiceView
+ */
+ public static DynamicDeviceConfigServiceView deviceView(DynamicConfigService service,
+ DeviceId deviceId) {
+ return new DynamicDeviceConfigServiceView(service, deviceId);
+ }
+
+ @Override
+ public void createNode(ResourceId path, DataNode node) {
+ super.createNode(toAbsoluteId(path), node);
+ }
+
+ @Override
+ public DataNode readNode(ResourceId path, Filter filter) {
+ // FIXME transform filter? is it possible?
+ return super.readNode(toAbsoluteId(path), filter);
+ }
+
+ @Override
+ public Boolean nodeExist(ResourceId path) {
+ return super.nodeExist(toAbsoluteId(path));
+ }
+
+ @Override
+ public void updateNode(ResourceId path, DataNode node) {
+ super.updateNode(toAbsoluteId(path), node);
+ }
+
+ @Override
+ public void replaceNode(ResourceId path, DataNode node) {
+ super.replaceNode(toAbsoluteId(path), node);
+ }
+
+ @Override
+ public void deleteNode(ResourceId path) {
+ super.deleteNode(toAbsoluteId(path));
+ }
+
+ @Override
+ public CompletableFuture<RpcOutput> invokeRpc(ResourceId id,
+ RpcInput input) {
+ return super.invokeRpc(toAbsoluteId(id), input);
+ }
+
+ @Override
+ public void addListener(DynamicConfigListener listener) {
+ super.addListener(wrapped.computeIfAbsent(listener,
+ DynamicDeviceConfigListener::new));
+ }
+
+ @Override
+ public void removeListener(DynamicConfigListener listener) {
+ DynamicConfigListener w = wrapped.remove(listener);
+ if (w != null) {
+ super.removeListener(w);
+ }
+ }
+
+ private ResourceId toAbsoluteId(ResourceId path) {
+ checkArgument(!path.nodeKeys().contains(DeviceResourceIds.ROOT_NODE),
+ "%s was already absolute path", path);
+
+ return ResourceIds.concat(deviceRoot, path);
+ }
+
+ private ResourceId toDeviceRelativeId(ResourceId path) {
+ checkArgument(path.nodeKeys().contains(DeviceResourceIds.ROOT_NODE),
+ "%s was not absolute path", path);
+
+ return ResourceIds.relativize(deviceRoot, path);
+ }
+
+ class DynamicDeviceConfigListener implements DynamicConfigListener {
+
+ private final DynamicConfigListener lsnr;
+
+ DynamicDeviceConfigListener(DynamicConfigListener lsnr) {
+ this.lsnr = checkNotNull(lsnr);
+ }
+
+ private DynamicConfigEvent deviceEvent(DynamicConfigEvent event) {
+ return new DynamicConfigEvent(event.type(),
+ toDeviceRelativeId(event.subject()));
+ }
+
+ @Override
+ public boolean isRelevant(DynamicConfigEvent event) {
+ // FIXME ignore event irrelevant to current device
+ return lsnr.isRelevant(deviceEvent(event));
+ }
+
+ @Override
+ public void event(DynamicConfigEvent event) {
+ lsnr.event(deviceEvent(event));
+ }
+
+ }
+}
diff --git a/apps/config/src/test/java/org/onosproject/d/config/DynamicDeviceConfigServiceViewTest.java b/apps/config/src/test/java/org/onosproject/d/config/DynamicDeviceConfigServiceViewTest.java
new file mode 100644
index 0000000..797ab1c
--- /dev/null
+++ b/apps/config/src/test/java/org/onosproject/d/config/DynamicDeviceConfigServiceViewTest.java
@@ -0,0 +1,219 @@
+/*
+ * 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 org.junit.Assert.*;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.config.DynamicConfigEvent;
+import org.onosproject.config.DynamicConfigEvent.Type;
+import org.onosproject.config.DynamicConfigListener;
+import org.onosproject.config.DynamicConfigServiceAdapter;
+import org.onosproject.config.Filter;
+import org.onosproject.net.DeviceId;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.RpcInput;
+import org.onosproject.yang.model.RpcOutput;
+
+public class DynamicDeviceConfigServiceViewTest {
+
+ static DeviceId DID = DeviceId.deviceId("test:device");
+ /**
+ * Absolute ResourceId to {@code DID}.
+ */
+ static ResourceId RID = DeviceResourceIds.toResourceId(DID);
+
+
+ /**
+ * Device relative ResourceId pointing to intf node.
+ */
+ static ResourceId REL_INTF = ResourceId.builder()
+ .addBranchPointSchema("intf", "test")
+ .build();
+
+ DataNode node = null;
+
+ TestDynamicConfigService service;
+ DynamicDeviceConfigServiceView view;
+
+ ResourceId realPath;
+ ResourceId realId;
+ Filter realFilter;
+
+ DynamicConfigEvent viewRelevantEvent;
+ DynamicConfigEvent viewEvent;
+
+ @Before
+ public void setUp() throws Exception {
+ realPath = null;
+ realId = null;
+ realFilter = null;
+
+ viewRelevantEvent = null;
+ viewEvent = null;
+
+ service = new TestDynamicConfigService();
+ view = DynamicDeviceConfigServiceView.deviceView(service, DID);
+ }
+
+ // FIXME add test scenario where irrelevant event get discarded.
+
+ @Test
+ public void testListener() throws CloneNotSupportedException, InterruptedException {
+ ResourceId realIntf = ResourceId.builder()
+ .append(RID)
+ .addBranchPointSchema("intf", "test")
+ .build();
+
+ final CountDownLatch recieved = new CountDownLatch(1);
+
+ DynamicConfigListener lsnr = new DynamicConfigListener() {
+
+ @Override
+ public boolean isRelevant(DynamicConfigEvent event) {
+ viewRelevantEvent = event;
+ return true;
+ }
+
+ @Override
+ public void event(DynamicConfigEvent event) {
+ viewEvent = event;
+ recieved.countDown();
+ }
+ };
+ view.addListener(lsnr);
+
+ service.post(new DynamicConfigEvent(Type.NODE_ADDED, realIntf));
+
+ assertTrue(recieved.await(5, TimeUnit.SECONDS));
+
+ assertFalse("Expect relative path but was" + viewRelevantEvent.subject(),
+ ResourceIds.isPrefix(RID, viewRelevantEvent.subject()));
+ assertFalse("Expect relative path but was" + viewEvent.subject(),
+ ResourceIds.isPrefix(RID, viewEvent.subject()));
+
+ view.removeListener(lsnr);
+ }
+
+ @Test
+ public void testCreateNode() {
+ view.createNode(REL_INTF, node);
+
+ assertTrue(ResourceIds.isPrefix(RID, realPath));
+ }
+
+ @Test
+ public void testReadNode() {
+ Filter filter = null;
+ DataNode returned = view.readNode(REL_INTF, filter);
+
+ assertTrue(ResourceIds.isPrefix(RID, realPath));
+
+ // FIXME test realFilter
+
+ // TODO do we expect something to happen on returned?
+ }
+
+ @Test
+ public void testNodeExist() {
+ view.nodeExist(REL_INTF);
+
+ assertTrue(ResourceIds.isPrefix(RID, realPath));
+ }
+
+ @Test
+ public void testUpdateNode() {
+ view.updateNode(REL_INTF, node);
+
+ assertTrue(ResourceIds.isPrefix(RID, realPath));
+ }
+
+ @Test
+ public void testReplaceNode() {
+ view.replaceNode(REL_INTF, node);
+
+ assertTrue(ResourceIds.isPrefix(RID, realPath));
+ }
+
+ @Test
+ public void testDeleteNode() {
+ view.deleteNode(REL_INTF);
+
+ assertTrue(ResourceIds.isPrefix(RID, realPath));
+ }
+
+ @Test
+ public void testInvokeRpc() {
+ RpcInput input = null;
+ view.invokeRpc(REL_INTF, input);
+
+ assertTrue(ResourceIds.isPrefix(RID, realId));
+ }
+
+ private final class TestDynamicConfigService
+ extends DynamicConfigServiceAdapter {
+ @Override
+ public void createNode(ResourceId path, DataNode node) {
+ realPath = path;
+ }
+
+ @Override
+ public DataNode readNode(ResourceId path, Filter filter) {
+ realPath = path;
+ realFilter = filter;
+ return super.readNode(path, filter);
+ }
+
+ @Override
+ public Boolean nodeExist(ResourceId path) {
+ realPath = path;
+ return super.nodeExist(path);
+ }
+
+ @Override
+ public void updateNode(ResourceId path, DataNode node) {
+ realPath = path;
+ }
+
+ @Override
+ public void replaceNode(ResourceId path, DataNode node) {
+ realPath = path;
+ }
+
+ @Override
+ public void deleteNode(ResourceId path) {
+ realPath = path;
+ }
+
+ @Override
+ public CompletableFuture<RpcOutput> invokeRpc(ResourceId id,
+ RpcInput input) {
+ realId = id;
+ return super.invokeRpc(id, input);
+ }
+
+ public void post(DynamicConfigEvent event) {
+ listenerRegistry.process(event);
+ }
+ }
+
+}