blob: 78193fb5b5a3c2742a5701535135fdc2cb4167ac [file] [log] [blame]
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.d.config;
17
18import static org.junit.Assert.*;
19
20import java.util.concurrent.CompletableFuture;
21import java.util.concurrent.CountDownLatch;
22import java.util.concurrent.TimeUnit;
23
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.config.DynamicConfigEvent;
27import org.onosproject.config.DynamicConfigEvent.Type;
28import org.onosproject.config.DynamicConfigListener;
29import org.onosproject.config.DynamicConfigServiceAdapter;
30import org.onosproject.config.Filter;
31import org.onosproject.net.DeviceId;
32import org.onosproject.yang.model.DataNode;
33import org.onosproject.yang.model.ResourceId;
34import org.onosproject.yang.model.RpcInput;
35import org.onosproject.yang.model.RpcOutput;
36
37public class DynamicDeviceConfigServiceViewTest {
38
Yuta HIGUCHI5527e992017-08-08 18:11:17 -070039 static DeviceId did = DeviceId.deviceId("test:device");
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070040 /**
41 * Absolute ResourceId to {@code DID}.
42 */
Yuta HIGUCHI5527e992017-08-08 18:11:17 -070043 static ResourceId rid = DeviceResourceIds.toResourceId(did);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070044
45
46 /**
47 * Device relative ResourceId pointing to intf node.
48 */
Yuta HIGUCHI5527e992017-08-08 18:11:17 -070049 static ResourceId relIntf = ResourceId.builder()
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070050 .addBranchPointSchema("intf", "test")
51 .build();
52
53 DataNode node = null;
54
55 TestDynamicConfigService service;
56 DynamicDeviceConfigServiceView view;
57
58 ResourceId realPath;
59 ResourceId realId;
60 Filter realFilter;
61
62 DynamicConfigEvent viewRelevantEvent;
63 DynamicConfigEvent viewEvent;
64
65 @Before
66 public void setUp() throws Exception {
67 realPath = null;
68 realId = null;
69 realFilter = null;
70
71 viewRelevantEvent = null;
72 viewEvent = null;
73
74 service = new TestDynamicConfigService();
Yuta HIGUCHI5527e992017-08-08 18:11:17 -070075 view = DynamicDeviceConfigServiceView.deviceView(service, did);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070076 }
77
78 // FIXME add test scenario where irrelevant event get discarded.
79
80 @Test
81 public void testListener() throws CloneNotSupportedException, InterruptedException {
82 ResourceId realIntf = ResourceId.builder()
Yuta HIGUCHI5527e992017-08-08 18:11:17 -070083 .append(rid)
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -070084 .addBranchPointSchema("intf", "test")
85 .build();
86
87 final CountDownLatch recieved = new CountDownLatch(1);
88
89 DynamicConfigListener lsnr = new DynamicConfigListener() {
90
91 @Override
92 public boolean isRelevant(DynamicConfigEvent event) {
93 viewRelevantEvent = event;
94 return true;
95 }
96
97 @Override
98 public void event(DynamicConfigEvent event) {
99 viewEvent = event;
100 recieved.countDown();
101 }
102 };
103 view.addListener(lsnr);
104
105 service.post(new DynamicConfigEvent(Type.NODE_ADDED, realIntf));
106
107 assertTrue(recieved.await(5, TimeUnit.SECONDS));
108
109 assertFalse("Expect relative path but was" + viewRelevantEvent.subject(),
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700110 ResourceIds.isPrefix(rid, viewRelevantEvent.subject()));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700111 assertFalse("Expect relative path but was" + viewEvent.subject(),
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700112 ResourceIds.isPrefix(rid, viewEvent.subject()));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700113
114 view.removeListener(lsnr);
115 }
116
117 @Test
118 public void testCreateNode() {
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700119 view.createNode(relIntf, node);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700120
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700121 assertTrue(ResourceIds.isPrefix(rid, realPath));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700122 }
123
124 @Test
125 public void testReadNode() {
126 Filter filter = null;
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700127 DataNode returned = view.readNode(relIntf, filter);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700128
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700129 assertTrue(ResourceIds.isPrefix(rid, realPath));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700130
131 // FIXME test realFilter
132
133 // TODO do we expect something to happen on returned?
134 }
135
136 @Test
137 public void testNodeExist() {
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700138 view.nodeExist(relIntf);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700139
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700140 assertTrue(ResourceIds.isPrefix(rid, realPath));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700141 }
142
143 @Test
144 public void testUpdateNode() {
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700145 view.updateNode(relIntf, node);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700146
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700147 assertTrue(ResourceIds.isPrefix(rid, realPath));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700148 }
149
150 @Test
151 public void testReplaceNode() {
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700152 view.replaceNode(relIntf, node);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700153
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700154 assertTrue(ResourceIds.isPrefix(rid, realPath));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700155 }
156
157 @Test
158 public void testDeleteNode() {
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700159 view.deleteNode(relIntf);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700160
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700161 assertTrue(ResourceIds.isPrefix(rid, realPath));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700162 }
163
164 @Test
165 public void testInvokeRpc() {
166 RpcInput input = null;
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700167 view.invokeRpc(relIntf, input);
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700168
Yuta HIGUCHI5527e992017-08-08 18:11:17 -0700169 assertTrue(ResourceIds.isPrefix(rid, realId));
Yuta HIGUCHId6a576d2017-08-03 18:35:55 -0700170 }
171
172 private final class TestDynamicConfigService
173 extends DynamicConfigServiceAdapter {
174 @Override
175 public void createNode(ResourceId path, DataNode node) {
176 realPath = path;
177 }
178
179 @Override
180 public DataNode readNode(ResourceId path, Filter filter) {
181 realPath = path;
182 realFilter = filter;
183 return super.readNode(path, filter);
184 }
185
186 @Override
187 public Boolean nodeExist(ResourceId path) {
188 realPath = path;
189 return super.nodeExist(path);
190 }
191
192 @Override
193 public void updateNode(ResourceId path, DataNode node) {
194 realPath = path;
195 }
196
197 @Override
198 public void replaceNode(ResourceId path, DataNode node) {
199 realPath = path;
200 }
201
202 @Override
203 public void deleteNode(ResourceId path) {
204 realPath = path;
205 }
206
207 @Override
208 public CompletableFuture<RpcOutput> invokeRpc(ResourceId id,
209 RpcInput input) {
210 realId = id;
211 return super.invokeRpc(id, input);
212 }
213
214 public void post(DynamicConfigEvent event) {
215 listenerRegistry.process(event);
216 }
217 }
218
219}