Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.d.config; |
| 17 | |
| 18 | import static org.junit.Assert.*; |
| 19 | |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | import java.util.concurrent.CountDownLatch; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.config.DynamicConfigEvent; |
| 27 | import org.onosproject.config.DynamicConfigEvent.Type; |
| 28 | import org.onosproject.config.DynamicConfigListener; |
| 29 | import org.onosproject.config.DynamicConfigServiceAdapter; |
| 30 | import org.onosproject.config.Filter; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.yang.model.DataNode; |
| 33 | import org.onosproject.yang.model.ResourceId; |
| 34 | import org.onosproject.yang.model.RpcInput; |
| 35 | import org.onosproject.yang.model.RpcOutput; |
| 36 | |
| 37 | public class DynamicDeviceConfigServiceViewTest { |
| 38 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 39 | static DeviceId did = DeviceId.deviceId("test:device"); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Absolute ResourceId to {@code DID}. |
| 42 | */ |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 43 | static ResourceId rid = DeviceResourceIds.toResourceId(did); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | /** |
| 47 | * Device relative ResourceId pointing to intf node. |
| 48 | */ |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 49 | static ResourceId relIntf = ResourceId.builder() |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 50 | .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 HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 75 | view = DynamicDeviceConfigServiceView.deviceView(service, did); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 76 | } |
| 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 HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 83 | .append(rid) |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 84 | .addBranchPointSchema("intf", "test") |
| 85 | .build(); |
| 86 | |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 87 | final CountDownLatch received = new CountDownLatch(1); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 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; |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 100 | received.countDown(); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | view.addListener(lsnr); |
| 104 | |
| 105 | service.post(new DynamicConfigEvent(Type.NODE_ADDED, realIntf)); |
| 106 | |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 107 | assertTrue(received.await(5, TimeUnit.SECONDS)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 108 | |
| 109 | assertFalse("Expect relative path but was" + viewRelevantEvent.subject(), |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 110 | ResourceIds.isPrefix(rid, viewRelevantEvent.subject())); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 111 | assertFalse("Expect relative path but was" + viewEvent.subject(), |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 112 | ResourceIds.isPrefix(rid, viewEvent.subject())); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 113 | |
| 114 | view.removeListener(lsnr); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testCreateNode() { |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 119 | view.createNode(relIntf, node); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 120 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 121 | assertTrue(ResourceIds.isPrefix(rid, realPath)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testReadNode() { |
| 126 | Filter filter = null; |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 127 | DataNode returned = view.readNode(relIntf, filter); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 128 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 129 | assertTrue(ResourceIds.isPrefix(rid, realPath)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 130 | |
| 131 | // FIXME test realFilter |
| 132 | |
| 133 | // TODO do we expect something to happen on returned? |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testNodeExist() { |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 138 | view.nodeExist(relIntf); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 139 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 140 | assertTrue(ResourceIds.isPrefix(rid, realPath)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testUpdateNode() { |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 145 | view.updateNode(relIntf, node); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 146 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 147 | assertTrue(ResourceIds.isPrefix(rid, realPath)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testReplaceNode() { |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 152 | view.replaceNode(relIntf, node); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 153 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 154 | assertTrue(ResourceIds.isPrefix(rid, realPath)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testDeleteNode() { |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 159 | view.deleteNode(relIntf); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 160 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 161 | assertTrue(ResourceIds.isPrefix(rid, realPath)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testInvokeRpc() { |
Ray Milkey | 765b644 | 2018-09-14 12:08:02 -0700 | [diff] [blame] | 166 | RpcInput input = new RpcInput(relIntf, null); |
| 167 | view.invokeRpc(input); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 168 | |
Yuta HIGUCHI | 5527e99 | 2017-08-08 18:11:17 -0700 | [diff] [blame] | 169 | assertTrue(ResourceIds.isPrefix(rid, realId)); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 170 | } |
| 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 |
Ray Milkey | 765b644 | 2018-09-14 12:08:02 -0700 | [diff] [blame] | 208 | public CompletableFuture<RpcOutput> invokeRpc(RpcInput input) { |
| 209 | realId = input.id(); |
| 210 | return super.invokeRpc(input); |
Yuta HIGUCHI | d6a576d | 2017-08-03 18:35:55 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | public void post(DynamicConfigEvent event) { |
| 214 | listenerRegistry.process(event); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | } |