Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.link.impl; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Iterables; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 19 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.AfterClass; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.BeforeClass; |
| 24 | import org.junit.Ignore; |
| 25 | import org.junit.Test; |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.ControllerNode; |
| 28 | import org.onosproject.cluster.DefaultControllerNode; |
| 29 | import org.onosproject.cluster.NodeId; |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 30 | import org.onosproject.mastership.MastershipServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Link; |
| 35 | import org.onosproject.net.Link.Type; |
| 36 | import org.onosproject.net.LinkKey; |
| 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.SparseAnnotations; |
| 39 | import org.onosproject.net.device.DeviceClockService; |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceClockServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.link.DefaultLinkDescription; |
| 42 | import org.onosproject.net.link.LinkDescription; |
| 43 | import org.onosproject.net.link.LinkEvent; |
| 44 | import org.onosproject.net.link.LinkStore; |
| 45 | import org.onosproject.net.link.LinkStoreDelegate; |
| 46 | import org.onosproject.net.provider.ProviderId; |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 47 | import org.onosproject.store.Timestamp; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.store.cluster.StaticClusterService; |
| 49 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 51 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 52 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 53 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 54 | import java.util.HashMap; |
| 55 | import java.util.Map; |
| 56 | import java.util.Set; |
| 57 | import java.util.concurrent.CountDownLatch; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 59 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 60 | import java.util.concurrent.atomic.AtomicLong; |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 61 | import static org.easymock.EasyMock.*; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 62 | import static org.junit.Assert.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import static org.onosproject.cluster.ControllerNode.State.ACTIVE; |
| 64 | import static org.onosproject.net.DeviceId.deviceId; |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 65 | import static org.onosproject.net.Link.Type.DIRECT; |
| 66 | import static org.onosproject.net.Link.Type.EDGE; |
| 67 | import static org.onosproject.net.Link.Type.INDIRECT; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.NetTestTools.assertAnnotationsEquals; |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 69 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 70 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 71 | import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Test of the GossipLinkStoreTest implementation. |
| 75 | */ |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 76 | @Ignore |
| 77 | public class ECLinkStoreTest { |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 78 | |
| 79 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 80 | private static final ProviderId PIDA = new ProviderId("of", "bar", true); |
| 81 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 82 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 83 | |
| 84 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 85 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 86 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 87 | |
| 88 | private static final SparseAnnotations A1 = DefaultAnnotations.builder() |
| 89 | .set("A1", "a1") |
| 90 | .set("B1", "b1") |
| 91 | .build(); |
| 92 | private static final SparseAnnotations A1_2 = DefaultAnnotations.builder() |
| 93 | .remove("A1") |
| 94 | .set("B3", "b3") |
| 95 | .build(); |
| 96 | private static final SparseAnnotations A2 = DefaultAnnotations.builder() |
| 97 | .set("A2", "a2") |
| 98 | .set("B2", "b2") |
| 99 | .build(); |
| 100 | private static final SparseAnnotations A2_2 = DefaultAnnotations.builder() |
| 101 | .remove("A2") |
| 102 | .set("B4", "b4") |
| 103 | .build(); |
| 104 | |
| 105 | // local node |
| 106 | private static final NodeId NID1 = new NodeId("local"); |
| 107 | private static final ControllerNode ONOS1 = |
| 108 | new DefaultControllerNode(NID1, IpAddress.valueOf("127.0.0.1")); |
| 109 | |
| 110 | // remote node |
| 111 | private static final NodeId NID2 = new NodeId("remote"); |
| 112 | private static final ControllerNode ONOS2 = |
| 113 | new DefaultControllerNode(NID2, IpAddress.valueOf("127.0.0.2")); |
| 114 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 115 | private ECLinkStore linkStoreImpl; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 116 | private LinkStore linkStore; |
| 117 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 118 | private DeviceClockService deviceClockService; |
| 119 | private ClusterCommunicationService clusterCommunicator; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 120 | |
| 121 | @BeforeClass |
| 122 | public static void setUpBeforeClass() throws Exception { |
| 123 | } |
| 124 | |
| 125 | @AfterClass |
| 126 | public static void tearDownAfterClass() throws Exception { |
| 127 | } |
| 128 | |
| 129 | @Before |
| 130 | public void setUp() throws Exception { |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 131 | // TODO mock clusterCommunicator |
| 132 | clusterCommunicator = createNiceMock(ClusterCommunicationService.class); |
| 133 | clusterCommunicator.addSubscriber(anyObject(MessageSubject.class), |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 134 | anyObject(ClusterMessageHandler.class), |
| 135 | anyObject(ExecutorService.class)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 136 | expectLastCall().anyTimes(); |
| 137 | replay(clusterCommunicator); |
| 138 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 139 | linkStoreImpl = new ECLinkStore(); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 140 | linkStoreImpl.deviceClockService = deviceClockService; |
| 141 | linkStoreImpl.clusterCommunicator = clusterCommunicator; |
| 142 | linkStoreImpl.clusterService = new TestClusterService(); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 143 | linkStoreImpl.deviceClockService = new TestDeviceClockService(); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 144 | linkStoreImpl.mastershipService = new TestMastershipService(); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 145 | linkStoreImpl.activate(); |
| 146 | linkStore = linkStoreImpl; |
| 147 | |
| 148 | verify(clusterCommunicator); |
| 149 | reset(clusterCommunicator); |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 150 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @After |
| 154 | public void tearDown() throws Exception { |
| 155 | linkStoreImpl.deactivate(); |
| 156 | } |
| 157 | |
| 158 | private void putLink(DeviceId srcId, PortNumber srcNum, |
| 159 | DeviceId dstId, PortNumber dstNum, Type type, |
| 160 | SparseAnnotations... annotations) { |
| 161 | ConnectPoint src = new ConnectPoint(srcId, srcNum); |
| 162 | ConnectPoint dst = new ConnectPoint(dstId, dstNum); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 163 | linkStore.createOrUpdateLink(PID, new DefaultLinkDescription(src, dst, type, annotations)); |
| 164 | verify(clusterCommunicator); |
| 165 | } |
| 166 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 167 | private void putLink(LinkKey key, Type type, SparseAnnotations... annotations) { |
| 168 | putLink(key.src().deviceId(), key.src().port(), |
| 169 | key.dst().deviceId(), key.dst().port(), |
| 170 | type, annotations); |
| 171 | } |
| 172 | |
| 173 | private static void assertLink(DeviceId srcId, PortNumber srcNum, |
| 174 | DeviceId dstId, PortNumber dstNum, Type type, |
| 175 | Link link) { |
| 176 | assertEquals(srcId, link.src().deviceId()); |
| 177 | assertEquals(srcNum, link.src().port()); |
| 178 | assertEquals(dstId, link.dst().deviceId()); |
| 179 | assertEquals(dstNum, link.dst().port()); |
| 180 | assertEquals(type, link.type()); |
| 181 | } |
| 182 | |
| 183 | private static void assertLink(LinkKey key, Type type, Link link) { |
| 184 | assertLink(key.src().deviceId(), key.src().port(), |
| 185 | key.dst().deviceId(), key.dst().port(), |
| 186 | type, link); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public final void testGetLinkCount() { |
| 191 | assertEquals("initialy empty", 0, linkStore.getLinkCount()); |
| 192 | |
| 193 | putLink(DID1, P1, DID2, P2, DIRECT); |
| 194 | putLink(DID2, P2, DID1, P1, DIRECT); |
| 195 | putLink(DID1, P1, DID2, P2, DIRECT); |
| 196 | |
| 197 | assertEquals("expecting 2 unique link", 2, linkStore.getLinkCount()); |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public final void testGetLinks() { |
| 202 | assertEquals("initialy empty", 0, |
| 203 | Iterables.size(linkStore.getLinks())); |
| 204 | |
| 205 | LinkKey linkId1 = LinkKey.linkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2)); |
| 206 | LinkKey linkId2 = LinkKey.linkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1)); |
| 207 | |
| 208 | putLink(linkId1, DIRECT); |
| 209 | putLink(linkId2, DIRECT); |
| 210 | putLink(linkId1, DIRECT); |
| 211 | |
| 212 | assertEquals("expecting 2 unique link", 2, |
| 213 | Iterables.size(linkStore.getLinks())); |
| 214 | |
| 215 | Map<LinkKey, Link> links = new HashMap<>(); |
| 216 | for (Link link : linkStore.getLinks()) { |
| 217 | links.put(LinkKey.linkKey(link), link); |
| 218 | } |
| 219 | |
| 220 | assertLink(linkId1, DIRECT, links.get(linkId1)); |
| 221 | assertLink(linkId2, DIRECT, links.get(linkId2)); |
| 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public final void testGetDeviceEgressLinks() { |
| 226 | LinkKey linkId1 = LinkKey.linkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2)); |
| 227 | LinkKey linkId2 = LinkKey.linkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1)); |
| 228 | LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3)); |
| 229 | |
| 230 | putLink(linkId1, DIRECT); |
| 231 | putLink(linkId2, DIRECT); |
| 232 | putLink(linkId3, DIRECT); |
| 233 | |
| 234 | // DID1,P1 => DID2,P2 |
| 235 | // DID2,P2 => DID1,P1 |
| 236 | // DID1,P2 => DID2,P3 |
| 237 | |
| 238 | Set<Link> links1 = linkStore.getDeviceEgressLinks(DID1); |
| 239 | assertEquals(2, links1.size()); |
| 240 | // check |
| 241 | |
| 242 | Set<Link> links2 = linkStore.getDeviceEgressLinks(DID2); |
| 243 | assertEquals(1, links2.size()); |
| 244 | assertLink(linkId2, DIRECT, links2.iterator().next()); |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | public final void testGetDeviceIngressLinks() { |
| 249 | LinkKey linkId1 = LinkKey.linkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2)); |
| 250 | LinkKey linkId2 = LinkKey.linkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1)); |
| 251 | LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3)); |
| 252 | |
| 253 | putLink(linkId1, DIRECT); |
| 254 | putLink(linkId2, DIRECT); |
| 255 | putLink(linkId3, DIRECT); |
| 256 | |
| 257 | // DID1,P1 => DID2,P2 |
| 258 | // DID2,P2 => DID1,P1 |
| 259 | // DID1,P2 => DID2,P3 |
| 260 | |
| 261 | Set<Link> links1 = linkStore.getDeviceIngressLinks(DID2); |
| 262 | assertEquals(2, links1.size()); |
| 263 | // check |
| 264 | |
| 265 | Set<Link> links2 = linkStore.getDeviceIngressLinks(DID1); |
| 266 | assertEquals(1, links2.size()); |
| 267 | assertLink(linkId2, DIRECT, links2.iterator().next()); |
| 268 | } |
| 269 | |
| 270 | @Test |
| 271 | public final void testGetLink() { |
| 272 | ConnectPoint src = new ConnectPoint(DID1, P1); |
| 273 | ConnectPoint dst = new ConnectPoint(DID2, P2); |
| 274 | LinkKey linkId1 = LinkKey.linkKey(src, dst); |
| 275 | |
| 276 | putLink(linkId1, DIRECT); |
| 277 | |
| 278 | Link link = linkStore.getLink(src, dst); |
| 279 | assertLink(linkId1, DIRECT, link); |
| 280 | |
| 281 | assertNull("There shouldn't be reverese link", |
| 282 | linkStore.getLink(dst, src)); |
| 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public final void testGetEgressLinks() { |
| 287 | final ConnectPoint d1P1 = new ConnectPoint(DID1, P1); |
| 288 | final ConnectPoint d2P2 = new ConnectPoint(DID2, P2); |
| 289 | LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2); |
| 290 | LinkKey linkId2 = LinkKey.linkKey(d2P2, d1P1); |
| 291 | LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3)); |
| 292 | |
| 293 | putLink(linkId1, DIRECT); |
| 294 | putLink(linkId2, DIRECT); |
| 295 | putLink(linkId3, DIRECT); |
| 296 | |
| 297 | // DID1,P1 => DID2,P2 |
| 298 | // DID2,P2 => DID1,P1 |
| 299 | // DID1,P2 => DID2,P3 |
| 300 | |
| 301 | Set<Link> links1 = linkStore.getEgressLinks(d1P1); |
| 302 | assertEquals(1, links1.size()); |
| 303 | assertLink(linkId1, DIRECT, links1.iterator().next()); |
| 304 | |
| 305 | Set<Link> links2 = linkStore.getEgressLinks(d2P2); |
| 306 | assertEquals(1, links2.size()); |
| 307 | assertLink(linkId2, DIRECT, links2.iterator().next()); |
| 308 | } |
| 309 | |
| 310 | @Test |
| 311 | public final void testGetIngressLinks() { |
| 312 | final ConnectPoint d1P1 = new ConnectPoint(DID1, P1); |
| 313 | final ConnectPoint d2P2 = new ConnectPoint(DID2, P2); |
| 314 | LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2); |
| 315 | LinkKey linkId2 = LinkKey.linkKey(d2P2, d1P1); |
| 316 | LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3)); |
| 317 | |
| 318 | putLink(linkId1, DIRECT); |
| 319 | putLink(linkId2, DIRECT); |
| 320 | putLink(linkId3, DIRECT); |
| 321 | |
| 322 | // DID1,P1 => DID2,P2 |
| 323 | // DID2,P2 => DID1,P1 |
| 324 | // DID1,P2 => DID2,P3 |
| 325 | |
| 326 | Set<Link> links1 = linkStore.getIngressLinks(d2P2); |
| 327 | assertEquals(1, links1.size()); |
| 328 | assertLink(linkId1, DIRECT, links1.iterator().next()); |
| 329 | |
| 330 | Set<Link> links2 = linkStore.getIngressLinks(d1P1); |
| 331 | assertEquals(1, links2.size()); |
| 332 | assertLink(linkId2, DIRECT, links2.iterator().next()); |
| 333 | } |
| 334 | |
| 335 | @Test |
| 336 | public final void testCreateOrUpdateLink() { |
| 337 | ConnectPoint src = new ConnectPoint(DID1, P1); |
| 338 | ConnectPoint dst = new ConnectPoint(DID2, P2); |
| 339 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 340 | final DefaultLinkDescription linkDescription = new DefaultLinkDescription(src, dst, INDIRECT); |
| 341 | LinkEvent event = linkStore.createOrUpdateLink(PID, |
| 342 | linkDescription); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 343 | |
| 344 | assertLink(DID1, P1, DID2, P2, INDIRECT, event.subject()); |
| 345 | assertEquals(LINK_ADDED, event.type()); |
| 346 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 347 | LinkEvent event2 = linkStore.createOrUpdateLink(PID, |
| 348 | new DefaultLinkDescription(src, dst, DIRECT)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 349 | |
| 350 | assertLink(DID1, P1, DID2, P2, DIRECT, event2.subject()); |
| 351 | assertEquals(LINK_UPDATED, event2.type()); |
| 352 | |
| 353 | // no change |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 354 | LinkEvent event3 = linkStore.createOrUpdateLink(PID, |
| 355 | new DefaultLinkDescription(src, dst, DIRECT)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 356 | |
| 357 | assertNull("No change event expected", event3); |
| 358 | } |
| 359 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 360 | private static void assertLinkDescriptionEquals(ConnectPoint src, |
| 361 | ConnectPoint dst, |
| 362 | Type type, |
| 363 | LinkDescription actual) { |
| 364 | assertEquals(src, actual.src()); |
| 365 | assertEquals(dst, actual.dst()); |
| 366 | assertEquals(type, actual.type()); |
| 367 | // TODO check annotations |
| 368 | } |
| 369 | |
| 370 | @Test |
| 371 | public final void testCreateOrUpdateLinkAncillary() { |
| 372 | ConnectPoint src = new ConnectPoint(DID1, P1); |
| 373 | ConnectPoint dst = new ConnectPoint(DID2, P2); |
| 374 | |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 375 | // add Ancillary link |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 376 | LinkEvent event = linkStore.createOrUpdateLink(PIDA, |
| 377 | new DefaultLinkDescription(src, dst, INDIRECT, A1)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 378 | |
| 379 | assertNotNull("Ancillary only link is ignored", event); |
| 380 | |
| 381 | // add Primary link |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 382 | LinkEvent event2 = linkStore.createOrUpdateLink(PID, |
| 383 | new DefaultLinkDescription(src, dst, INDIRECT, A2)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 384 | |
| 385 | assertLink(DID1, P1, DID2, P2, INDIRECT, event2.subject()); |
| 386 | assertAnnotationsEquals(event2.subject().annotations(), A2, A1); |
| 387 | assertEquals(LINK_UPDATED, event2.type()); |
| 388 | |
| 389 | // update link type |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 390 | LinkEvent event3 = linkStore.createOrUpdateLink(PID, |
| 391 | new DefaultLinkDescription(src, dst, DIRECT, A2)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 392 | |
| 393 | assertLink(DID1, P1, DID2, P2, DIRECT, event3.subject()); |
| 394 | assertAnnotationsEquals(event3.subject().annotations(), A2, A1); |
| 395 | assertEquals(LINK_UPDATED, event3.type()); |
| 396 | |
| 397 | |
| 398 | // no change |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 399 | LinkEvent event4 = linkStore.createOrUpdateLink(PID, |
| 400 | new DefaultLinkDescription(src, dst, DIRECT)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 401 | |
| 402 | assertNull("No change event expected", event4); |
| 403 | |
| 404 | // update link annotation (Primary) |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 405 | LinkEvent event5 = linkStore.createOrUpdateLink(PID, |
| 406 | new DefaultLinkDescription(src, dst, DIRECT, A2_2)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 407 | |
| 408 | assertLink(DID1, P1, DID2, P2, DIRECT, event5.subject()); |
| 409 | assertAnnotationsEquals(event5.subject().annotations(), A2, A2_2, A1); |
| 410 | assertEquals(LINK_UPDATED, event5.type()); |
| 411 | |
| 412 | // update link annotation (Ancillary) |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 413 | LinkEvent event6 = linkStore.createOrUpdateLink(PIDA, |
| 414 | new DefaultLinkDescription(src, dst, DIRECT, A1_2)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 415 | |
| 416 | assertLink(DID1, P1, DID2, P2, DIRECT, event6.subject()); |
| 417 | assertAnnotationsEquals(event6.subject().annotations(), A2, A2_2, A1, A1_2); |
| 418 | assertEquals(LINK_UPDATED, event6.type()); |
| 419 | |
| 420 | // update link type (Ancillary) : ignored |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 421 | LinkEvent event7 = linkStore.createOrUpdateLink(PIDA, |
| 422 | new DefaultLinkDescription(src, dst, EDGE)); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 423 | assertNull("Ancillary change other than annotation is ignored", event7); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | @Test |
| 428 | public final void testRemoveLink() { |
| 429 | final ConnectPoint d1P1 = new ConnectPoint(DID1, P1); |
| 430 | final ConnectPoint d2P2 = new ConnectPoint(DID2, P2); |
| 431 | LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2); |
| 432 | LinkKey linkId2 = LinkKey.linkKey(d2P2, d1P1); |
| 433 | |
| 434 | putLink(linkId1, DIRECT, A1); |
| 435 | putLink(linkId2, DIRECT, A2); |
| 436 | |
| 437 | // DID1,P1 => DID2,P2 |
| 438 | // DID2,P2 => DID1,P1 |
| 439 | // DID1,P2 => DID2,P3 |
| 440 | |
| 441 | LinkEvent event = linkStore.removeLink(d1P1, d2P2); |
| 442 | assertEquals(LINK_REMOVED, event.type()); |
| 443 | assertAnnotationsEquals(event.subject().annotations(), A1); |
| 444 | LinkEvent event2 = linkStore.removeLink(d1P1, d2P2); |
| 445 | assertNull(event2); |
| 446 | |
| 447 | assertLink(linkId2, DIRECT, linkStore.getLink(d2P2, d1P1)); |
| 448 | assertAnnotationsEquals(linkStore.getLink(d2P2, d1P1).annotations(), A2); |
| 449 | |
| 450 | // annotations, etc. should not survive remove |
| 451 | putLink(linkId1, DIRECT); |
| 452 | assertLink(linkId1, DIRECT, linkStore.getLink(d1P1, d2P2)); |
| 453 | assertAnnotationsEquals(linkStore.getLink(d1P1, d2P2).annotations()); |
| 454 | } |
| 455 | |
| 456 | @Test |
| 457 | public final void testAncillaryVisible() { |
| 458 | ConnectPoint src = new ConnectPoint(DID1, P1); |
| 459 | ConnectPoint dst = new ConnectPoint(DID2, P2); |
| 460 | |
| 461 | // add Ancillary link |
| 462 | linkStore.createOrUpdateLink(PIDA, |
| 463 | new DefaultLinkDescription(src, dst, INDIRECT, A1)); |
| 464 | |
| 465 | // Ancillary only link should not be visible |
| 466 | assertEquals(1, linkStore.getLinkCount()); |
| 467 | assertNotNull(linkStore.getLink(src, dst)); |
| 468 | } |
| 469 | |
| 470 | // If Delegates should be called only on remote events, |
| 471 | // then Simple* should never call them, thus not test required. |
| 472 | @Ignore("Ignore until Delegate spec. is clear.") |
| 473 | @Test |
| 474 | public final void testEvents() throws InterruptedException { |
| 475 | |
| 476 | final ConnectPoint d1P1 = new ConnectPoint(DID1, P1); |
| 477 | final ConnectPoint d2P2 = new ConnectPoint(DID2, P2); |
| 478 | final LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2); |
| 479 | |
| 480 | final CountDownLatch addLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 481 | LinkStoreDelegate checkAdd = event -> { |
| 482 | assertEquals(LINK_ADDED, event.type()); |
| 483 | assertLink(linkId1, INDIRECT, event.subject()); |
| 484 | addLatch.countDown(); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 485 | }; |
| 486 | final CountDownLatch updateLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 487 | LinkStoreDelegate checkUpdate = event -> { |
| 488 | assertEquals(LINK_UPDATED, event.type()); |
| 489 | assertLink(linkId1, DIRECT, event.subject()); |
| 490 | updateLatch.countDown(); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 491 | }; |
| 492 | final CountDownLatch removeLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 493 | LinkStoreDelegate checkRemove = event -> { |
| 494 | assertEquals(LINK_REMOVED, event.type()); |
| 495 | assertLink(linkId1, DIRECT, event.subject()); |
| 496 | removeLatch.countDown(); |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | linkStore.setDelegate(checkAdd); |
| 500 | putLink(linkId1, INDIRECT); |
| 501 | assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS)); |
| 502 | |
| 503 | linkStore.unsetDelegate(checkAdd); |
| 504 | linkStore.setDelegate(checkUpdate); |
| 505 | putLink(linkId1, DIRECT); |
| 506 | assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS)); |
| 507 | |
| 508 | linkStore.unsetDelegate(checkUpdate); |
| 509 | linkStore.setDelegate(checkRemove); |
| 510 | linkStore.removeLink(d1P1, d2P2); |
| 511 | assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS)); |
| 512 | } |
| 513 | |
| 514 | private static final class TestClusterService extends StaticClusterService { |
| 515 | |
| 516 | public TestClusterService() { |
| 517 | localNode = ONOS1; |
| 518 | nodes.put(NID1, ONOS1); |
| 519 | nodeStates.put(NID1, ACTIVE); |
| 520 | |
| 521 | nodes.put(NID2, ONOS2); |
| 522 | nodeStates.put(NID2, ACTIVE); |
| 523 | } |
| 524 | } |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 525 | |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 526 | private final class TestDeviceClockService extends DeviceClockServiceAdapter { |
| 527 | |
| 528 | private final AtomicLong ticker = new AtomicLong(); |
| 529 | |
| 530 | @Override |
| 531 | public Timestamp getTimestamp(DeviceId deviceId) { |
| 532 | if (DID1.equals(deviceId)) { |
| 533 | return new MastershipBasedTimestamp(1, ticker.getAndIncrement()); |
| 534 | } else if (DID2.equals(deviceId)) { |
| 535 | return new MastershipBasedTimestamp(2, ticker.getAndIncrement()); |
| 536 | } else { |
| 537 | throw new IllegalStateException(); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public boolean isTimestampAvailable(DeviceId deviceId) { |
| 543 | return DID1.equals(deviceId) || DID2.equals(deviceId); |
| 544 | } |
| 545 | } |
| 546 | |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 547 | private final class TestMastershipService extends MastershipServiceAdapter { |
| 548 | @Override |
| 549 | public NodeId getMasterFor(DeviceId deviceId) { |
| 550 | return NID1; |
| 551 | } |
| 552 | } |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 553 | } |