alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [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 | */ |
| 16 | package org.onosproject.incubator.net.mcast.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 26 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 27 | import org.onosproject.core.CoreServiceAdapter; |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 28 | import org.onosproject.core.DefaultApplicationId; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 29 | import org.onosproject.incubator.store.mcast.impl.DistributedMcastStore; |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.mcast.McastEvent; |
| 33 | import org.onosproject.net.mcast.McastListener; |
| 34 | import org.onosproject.net.mcast.McastRoute; |
| 35 | import org.onosproject.store.service.TestStorageService; |
| 36 | |
| 37 | import java.util.List; |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 38 | |
| 39 | import static junit.framework.Assert.fail; |
| 40 | import static junit.framework.TestCase.assertEquals; |
| 41 | import static org.onosproject.net.NetTestTools.did; |
| 42 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 43 | |
| 44 | /** |
| 45 | * Tests for the multicast RIB. |
| 46 | */ |
| 47 | public class MulticastRouteManagerTest { |
| 48 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 49 | McastRoute r1 = new McastRoute(IpAddress.valueOf("1.1.1.1"), |
| 50 | IpAddress.valueOf("1.1.1.2"), |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 51 | McastRoute.Type.IGMP); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 52 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 53 | McastRoute r11 = new McastRoute(IpAddress.valueOf("1.1.1.1"), |
| 54 | IpAddress.valueOf("1.1.1.2"), |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 55 | McastRoute.Type.STATIC); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 56 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 57 | McastRoute r2 = new McastRoute(IpAddress.valueOf("2.2.2.1"), |
| 58 | IpAddress.valueOf("2.2.2.2"), |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 59 | McastRoute.Type.PIM); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 60 | |
| 61 | ConnectPoint cp1 = new ConnectPoint(did("1"), PortNumber.portNumber(1)); |
| 62 | |
| 63 | ConnectPoint cp2 = new ConnectPoint(did("2"), PortNumber.portNumber(2)); |
| 64 | |
| 65 | private TestMulticastListener listener = new TestMulticastListener(); |
| 66 | |
| 67 | private MulticastRouteManager manager; |
| 68 | |
| 69 | private List<McastEvent> events; |
| 70 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 71 | private DistributedMcastStore mcastStore; |
| 72 | |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 73 | @Before |
| 74 | public void setUp() throws Exception { |
| 75 | manager = new MulticastRouteManager(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 76 | mcastStore = new DistributedMcastStore(); |
| 77 | TestUtils.setField(mcastStore, "storageService", new TestStorageService()); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 78 | injectEventDispatcher(manager, new TestEventDispatcher()); |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 79 | events = Lists.newArrayList(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 80 | manager.store = mcastStore; |
| 81 | mcastStore.activate(); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 82 | manager.activate(); |
| 83 | manager.addListener(listener); |
| 84 | } |
| 85 | |
| 86 | @After |
| 87 | public void tearDown() { |
| 88 | manager.removeListener(listener); |
| 89 | manager.deactivate(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 90 | mcastStore.deactivate(); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testAdd() { |
| 95 | manager.add(r1); |
| 96 | |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 97 | validateEvents(McastEvent.Type.ROUTE_ADDED); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testRemove() { |
| 102 | manager.add(r1); |
| 103 | |
| 104 | manager.remove(r1); |
| 105 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 106 | |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 107 | validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.ROUTE_REMOVED); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testAddSource() { |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 112 | manager.addSource(r1, cp1); |
| 113 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 114 | validateEvents(McastEvent.Type.SOURCE_ADDED); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 115 | assertEquals("Route is not equal", cp1, manager.fetchSource(r1)); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testAddSink() { |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 120 | manager.addSink(r1, cp1); |
| 121 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 122 | validateEvents(McastEvent.Type.SINK_ADDED); |
| 123 | assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1)); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testRemoveSink() { |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 128 | |
| 129 | manager.addSource(r1, cp1); |
| 130 | manager.addSink(r1, cp1); |
| 131 | manager.addSink(r1, cp2); |
| 132 | manager.removeSink(r1, cp2); |
| 133 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 134 | validateEvents(McastEvent.Type.SOURCE_ADDED, |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 135 | McastEvent.Type.SINK_ADDED, |
| 136 | McastEvent.Type.SINK_ADDED, |
| 137 | McastEvent.Type.SINK_REMOVED); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 138 | assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1)); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private void validateEvents(McastEvent.Type... evs) { |
| 142 | if (events.size() != evs.length) { |
| 143 | fail(String.format("Mismatch number of events# obtained -> %s : expected %s", |
| 144 | events, evs)); |
| 145 | } |
| 146 | |
| 147 | for (int i = 0; i < evs.length; i++) { |
| 148 | if (evs[i] != events.get(i).type()) { |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 149 | fail(String.format("Mismatched events# obtained -> %s : expected %s", |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 150 | events, evs)); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | class TestMulticastListener implements McastListener { |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 156 | @Override |
| 157 | public void event(McastEvent event) { |
| 158 | events.add(event); |
| 159 | } |
| 160 | } |
| 161 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 162 | private class TestCoreService extends CoreServiceAdapter { |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 163 | @Override |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 164 | public ApplicationId registerApplication(String name) { |
| 165 | return new DefaultApplicationId(0, name); |
alshabib | ed0951f | 2015-10-02 21:39:27 +0200 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |