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 | |
| 39 | static DeviceId DID = DeviceId.deviceId("test:device"); |
| 40 | /** |
| 41 | * Absolute ResourceId to {@code DID}. |
| 42 | */ |
| 43 | static ResourceId RID = DeviceResourceIds.toResourceId(DID); |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Device relative ResourceId pointing to intf node. |
| 48 | */ |
| 49 | static ResourceId REL_INTF = ResourceId.builder() |
| 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(); |
| 75 | view = DynamicDeviceConfigServiceView.deviceView(service, DID); |
| 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() |
| 83 | .append(RID) |
| 84 | .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(), |
| 110 | ResourceIds.isPrefix(RID, viewRelevantEvent.subject())); |
| 111 | assertFalse("Expect relative path but was" + viewEvent.subject(), |
| 112 | ResourceIds.isPrefix(RID, viewEvent.subject())); |
| 113 | |
| 114 | view.removeListener(lsnr); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testCreateNode() { |
| 119 | view.createNode(REL_INTF, node); |
| 120 | |
| 121 | assertTrue(ResourceIds.isPrefix(RID, realPath)); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testReadNode() { |
| 126 | Filter filter = null; |
| 127 | DataNode returned = view.readNode(REL_INTF, filter); |
| 128 | |
| 129 | assertTrue(ResourceIds.isPrefix(RID, realPath)); |
| 130 | |
| 131 | // FIXME test realFilter |
| 132 | |
| 133 | // TODO do we expect something to happen on returned? |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testNodeExist() { |
| 138 | view.nodeExist(REL_INTF); |
| 139 | |
| 140 | assertTrue(ResourceIds.isPrefix(RID, realPath)); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testUpdateNode() { |
| 145 | view.updateNode(REL_INTF, node); |
| 146 | |
| 147 | assertTrue(ResourceIds.isPrefix(RID, realPath)); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testReplaceNode() { |
| 152 | view.replaceNode(REL_INTF, node); |
| 153 | |
| 154 | assertTrue(ResourceIds.isPrefix(RID, realPath)); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testDeleteNode() { |
| 159 | view.deleteNode(REL_INTF); |
| 160 | |
| 161 | assertTrue(ResourceIds.isPrefix(RID, realPath)); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testInvokeRpc() { |
| 166 | RpcInput input = null; |
| 167 | view.invokeRpc(REL_INTF, input); |
| 168 | |
| 169 | assertTrue(ResourceIds.isPrefix(RID, realId)); |
| 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 |
| 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 | } |