blob: 9272668a762809b83ccef189d0ab41ba21be99fa [file] [log] [blame]
alshabibed0951f2015-10-02 21:39:27 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabibed0951f2015-10-02 21:39:27 +02003 *
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 */
Ray Milkeya9ae0d42017-08-06 22:10:47 -070016package org.onosproject.net.mcast.impl;
alshabibed0951f2015-10-02 21:39:27 +020017
18import com.google.common.collect.Lists;
alshabib79e52872015-12-07 16:01:01 -080019import com.google.common.collect.Sets;
alshabibed0951f2015-10-02 21:39:27 +020020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
alshabib79e52872015-12-07 16:01:01 -080024import org.onlab.packet.IpAddress;
alshabibed0951f2015-10-02 21:39:27 +020025import org.onosproject.common.event.impl.TestEventDispatcher;
26import org.onosproject.core.ApplicationId;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080027import org.onosproject.core.CoreServiceAdapter;
alshabibed0951f2015-10-02 21:39:27 +020028import org.onosproject.core.DefaultApplicationId;
Ray Milkeya9ae0d42017-08-06 22:10:47 -070029import org.onosproject.store.mcast.impl.DistributedMcastStore;
alshabibed0951f2015-10-02 21:39:27 +020030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.mcast.McastEvent;
33import org.onosproject.net.mcast.McastListener;
34import org.onosproject.net.mcast.McastRoute;
35import org.onosproject.store.service.TestStorageService;
36
Sean Condon436c60a2021-01-01 14:23:29 +000037import java.util.Arrays;
alshabibed0951f2015-10-02 21:39:27 +020038import java.util.List;
alshabibed0951f2015-10-02 21:39:27 +020039
40import static junit.framework.Assert.fail;
41import static junit.framework.TestCase.assertEquals;
42import static org.onosproject.net.NetTestTools.did;
43import static org.onosproject.net.NetTestTools.injectEventDispatcher;
44
45/**
46 * Tests for the multicast RIB.
47 */
48public class MulticastRouteManagerTest {
49
alshabib79e52872015-12-07 16:01:01 -080050 McastRoute r1 = new McastRoute(IpAddress.valueOf("1.1.1.1"),
51 IpAddress.valueOf("1.1.1.2"),
Thomas Vachuskac65dd712015-11-04 17:19:10 -080052 McastRoute.Type.IGMP);
alshabibed0951f2015-10-02 21:39:27 +020053
alshabib79e52872015-12-07 16:01:01 -080054 McastRoute r11 = new McastRoute(IpAddress.valueOf("1.1.1.1"),
55 IpAddress.valueOf("1.1.1.2"),
Thomas Vachuskac65dd712015-11-04 17:19:10 -080056 McastRoute.Type.STATIC);
alshabibed0951f2015-10-02 21:39:27 +020057
alshabib79e52872015-12-07 16:01:01 -080058 McastRoute r2 = new McastRoute(IpAddress.valueOf("2.2.2.1"),
59 IpAddress.valueOf("2.2.2.2"),
Thomas Vachuskac65dd712015-11-04 17:19:10 -080060 McastRoute.Type.PIM);
alshabibed0951f2015-10-02 21:39:27 +020061
62 ConnectPoint cp1 = new ConnectPoint(did("1"), PortNumber.portNumber(1));
63
64 ConnectPoint cp2 = new ConnectPoint(did("2"), PortNumber.portNumber(2));
65
66 private TestMulticastListener listener = new TestMulticastListener();
67
68 private MulticastRouteManager manager;
69
70 private List<McastEvent> events;
71
alshabib79e52872015-12-07 16:01:01 -080072 private DistributedMcastStore mcastStore;
73
alshabibed0951f2015-10-02 21:39:27 +020074 @Before
75 public void setUp() throws Exception {
76 manager = new MulticastRouteManager();
alshabib79e52872015-12-07 16:01:01 -080077 mcastStore = new DistributedMcastStore();
78 TestUtils.setField(mcastStore, "storageService", new TestStorageService());
alshabibed0951f2015-10-02 21:39:27 +020079 injectEventDispatcher(manager, new TestEventDispatcher());
Thomas Vachuskac65dd712015-11-04 17:19:10 -080080 events = Lists.newArrayList();
alshabib79e52872015-12-07 16:01:01 -080081 manager.store = mcastStore;
82 mcastStore.activate();
alshabibed0951f2015-10-02 21:39:27 +020083 manager.activate();
84 manager.addListener(listener);
85 }
86
87 @After
88 public void tearDown() {
89 manager.removeListener(listener);
90 manager.deactivate();
alshabib79e52872015-12-07 16:01:01 -080091 mcastStore.deactivate();
alshabibed0951f2015-10-02 21:39:27 +020092 }
93
94 @Test
95 public void testAdd() {
96 manager.add(r1);
97
alshabibed0951f2015-10-02 21:39:27 +020098 validateEvents(McastEvent.Type.ROUTE_ADDED);
99 }
100
101 @Test
102 public void testRemove() {
103 manager.add(r1);
104
105 manager.remove(r1);
106
alshabib79e52872015-12-07 16:01:01 -0800107
alshabibed0951f2015-10-02 21:39:27 +0200108 validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.ROUTE_REMOVED);
109 }
110
111 @Test
112 public void testAddSource() {
alshabibed0951f2015-10-02 21:39:27 +0200113 manager.addSource(r1, cp1);
114
alshabib79e52872015-12-07 16:01:01 -0800115 validateEvents(McastEvent.Type.SOURCE_ADDED);
alshabibed0951f2015-10-02 21:39:27 +0200116 assertEquals("Route is not equal", cp1, manager.fetchSource(r1));
117 }
118
119 @Test
120 public void testAddSink() {
alshabibed0951f2015-10-02 21:39:27 +0200121 manager.addSink(r1, cp1);
122
alshabib79e52872015-12-07 16:01:01 -0800123 validateEvents(McastEvent.Type.SINK_ADDED);
124 assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1));
alshabibed0951f2015-10-02 21:39:27 +0200125 }
126
127 @Test
128 public void testRemoveSink() {
alshabibed0951f2015-10-02 21:39:27 +0200129
130 manager.addSource(r1, cp1);
131 manager.addSink(r1, cp1);
132 manager.addSink(r1, cp2);
133 manager.removeSink(r1, cp2);
134
alshabib79e52872015-12-07 16:01:01 -0800135 validateEvents(McastEvent.Type.SOURCE_ADDED,
alshabibed0951f2015-10-02 21:39:27 +0200136 McastEvent.Type.SINK_ADDED,
137 McastEvent.Type.SINK_ADDED,
138 McastEvent.Type.SINK_REMOVED);
alshabib79e52872015-12-07 16:01:01 -0800139 assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1));
alshabibed0951f2015-10-02 21:39:27 +0200140 }
141
142 private void validateEvents(McastEvent.Type... evs) {
143 if (events.size() != evs.length) {
144 fail(String.format("Mismatch number of events# obtained -> %s : expected %s",
Sean Condon436c60a2021-01-01 14:23:29 +0000145 events, Arrays.toString(evs)));
alshabibed0951f2015-10-02 21:39:27 +0200146 }
147
148 for (int i = 0; i < evs.length; i++) {
149 if (evs[i] != events.get(i).type()) {
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800150 fail(String.format("Mismatched events# obtained -> %s : expected %s",
Sean Condon436c60a2021-01-01 14:23:29 +0000151 events, Arrays.toString(evs)));
alshabibed0951f2015-10-02 21:39:27 +0200152 }
153 }
154 }
155
156 class TestMulticastListener implements McastListener {
alshabibed0951f2015-10-02 21:39:27 +0200157 @Override
158 public void event(McastEvent event) {
159 events.add(event);
160 }
161 }
162
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800163 private class TestCoreService extends CoreServiceAdapter {
alshabibed0951f2015-10-02 21:39:27 +0200164 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800165 public ApplicationId registerApplication(String name) {
166 return new DefaultApplicationId(0, name);
alshabibed0951f2015-10-02 21:39:27 +0200167 }
168 }
169}