2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.incubator.net.mcast.impl;
18 import com.google.common.collect.Lists;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.onlab.junit.TestUtils;
23 import org.onlab.packet.IpPrefix;
24 import org.onosproject.common.event.impl.TestEventDispatcher;
25 import org.onosproject.core.ApplicationId;
26 import org.onosproject.core.CoreService;
27 import org.onosproject.core.DefaultApplicationId;
28 import org.onosproject.core.IdGenerator;
29 import org.onosproject.core.Version;
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;
37 import java.util.List;
40 import static junit.framework.Assert.fail;
41 import static junit.framework.TestCase.assertEquals;
42 import static org.onosproject.net.NetTestTools.did;
43 import static org.onosproject.net.NetTestTools.injectEventDispatcher;
46 * Tests for the multicast RIB.
48 public class MulticastRouteManagerTest {
50 McastRoute r1 = new McastRoute(IpPrefix.valueOf("1.1.1.1/8"),
51 IpPrefix.valueOf("1.1.1.2/8"),
52 McastRoute.Type.IGMP);
54 McastRoute r11 = new McastRoute(IpPrefix.valueOf("1.1.1.1/8"),
55 IpPrefix.valueOf("1.1.1.2/8"),
56 McastRoute.Type.STATIC);
58 McastRoute r2 = new McastRoute(IpPrefix.valueOf("2.2.2.1/8"),
59 IpPrefix.valueOf("2.2.2.2/8"),
62 ConnectPoint cp1 = new ConnectPoint(did("1"), PortNumber.portNumber(1));
64 ConnectPoint cp2 = new ConnectPoint(did("2"), PortNumber.portNumber(2));
66 private TestMulticastListener listener = new TestMulticastListener();
68 private MulticastRouteManager manager;
70 private List<McastEvent> events;
73 public void setUp() throws Exception {
74 manager = new MulticastRouteManager();
75 injectEventDispatcher(manager, new TestEventDispatcher());
76 TestUtils.setField(manager, "storageService", new TestStorageService());
77 TestUtils.setField(manager, "coreService", new TestCoreService());
78 events = Lists.newArrayList();
80 manager.addListener(listener);
84 public void tearDown() {
85 manager.removeListener(listener);
90 public void testAdd() {
93 assertEquals("Add failed", manager.mcastRoutes.size(), 1);
94 validateEvents(McastEvent.Type.ROUTE_ADDED);
98 public void testRemove() {
103 assertEquals("Remove failed", manager.mcastRoutes.size(), 0);
104 validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.ROUTE_REMOVED);
108 public void testAddSource() {
111 manager.addSource(r1, cp1);
113 validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.SOURCE_ADDED);
114 assertEquals("Route is not equal", cp1, manager.fetchSource(r1));
118 public void testAddSink() {
121 manager.addSource(r1, cp1);
122 manager.addSink(r1, cp1);
124 validateEvents(McastEvent.Type.ROUTE_ADDED,
125 McastEvent.Type.SOURCE_ADDED,
126 McastEvent.Type.SINK_ADDED);
127 assertEquals("Route is not equal", Lists.newArrayList(cp1), manager.fetchSinks(r1));
131 public void testRemoveSink() {
134 manager.addSource(r1, cp1);
135 manager.addSink(r1, cp1);
136 manager.addSink(r1, cp2);
137 manager.removeSink(r1, cp2);
139 validateEvents(McastEvent.Type.ROUTE_ADDED,
140 McastEvent.Type.SOURCE_ADDED,
141 McastEvent.Type.SINK_ADDED,
142 McastEvent.Type.SINK_ADDED,
143 McastEvent.Type.SINK_REMOVED);
144 assertEquals("Route is not equal", Lists.newArrayList(cp1), manager.fetchSinks(r1));
147 private void validateEvents(McastEvent.Type... evs) {
148 if (events.size() != evs.length) {
149 fail(String.format("Mismatch number of events# obtained -> %s : expected %s",
153 for (int i = 0; i < evs.length; i++) {
154 if (evs[i] != events.get(i).type()) {
155 fail(String.format("Mismtached events# obtained -> %s : expected %s",
161 class TestMulticastListener implements McastListener {
164 public void event(McastEvent event) {
169 private class TestCoreService implements CoreService {
171 public Version version() {
176 public Set<ApplicationId> getAppIds() {
181 public ApplicationId getAppId(Short id) {
186 public ApplicationId getAppId(String name) {
191 public ApplicationId registerApplication(String identifier) {
192 return new DefaultApplicationId(0, identifier);
196 public IdGenerator getIdGenerator(String topic) {