blob: f5b26c2746297010a604dd0ae57859fd0693190f [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
37import java.util.List;
alshabibed0951f2015-10-02 21:39:27 +020038
39import static junit.framework.Assert.fail;
40import static junit.framework.TestCase.assertEquals;
41import static org.onosproject.net.NetTestTools.did;
42import static org.onosproject.net.NetTestTools.injectEventDispatcher;
43
44/**
45 * Tests for the multicast RIB.
46 */
47public class MulticastRouteManagerTest {
48
alshabib79e52872015-12-07 16:01:01 -080049 McastRoute r1 = new McastRoute(IpAddress.valueOf("1.1.1.1"),
50 IpAddress.valueOf("1.1.1.2"),
Thomas Vachuskac65dd712015-11-04 17:19:10 -080051 McastRoute.Type.IGMP);
alshabibed0951f2015-10-02 21:39:27 +020052
alshabib79e52872015-12-07 16:01:01 -080053 McastRoute r11 = new McastRoute(IpAddress.valueOf("1.1.1.1"),
54 IpAddress.valueOf("1.1.1.2"),
Thomas Vachuskac65dd712015-11-04 17:19:10 -080055 McastRoute.Type.STATIC);
alshabibed0951f2015-10-02 21:39:27 +020056
alshabib79e52872015-12-07 16:01:01 -080057 McastRoute r2 = new McastRoute(IpAddress.valueOf("2.2.2.1"),
58 IpAddress.valueOf("2.2.2.2"),
Thomas Vachuskac65dd712015-11-04 17:19:10 -080059 McastRoute.Type.PIM);
alshabibed0951f2015-10-02 21:39:27 +020060
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
alshabib79e52872015-12-07 16:01:01 -080071 private DistributedMcastStore mcastStore;
72
alshabibed0951f2015-10-02 21:39:27 +020073 @Before
74 public void setUp() throws Exception {
75 manager = new MulticastRouteManager();
alshabib79e52872015-12-07 16:01:01 -080076 mcastStore = new DistributedMcastStore();
77 TestUtils.setField(mcastStore, "storageService", new TestStorageService());
alshabibed0951f2015-10-02 21:39:27 +020078 injectEventDispatcher(manager, new TestEventDispatcher());
Thomas Vachuskac65dd712015-11-04 17:19:10 -080079 events = Lists.newArrayList();
alshabib79e52872015-12-07 16:01:01 -080080 manager.store = mcastStore;
81 mcastStore.activate();
alshabibed0951f2015-10-02 21:39:27 +020082 manager.activate();
83 manager.addListener(listener);
84 }
85
86 @After
87 public void tearDown() {
88 manager.removeListener(listener);
89 manager.deactivate();
alshabib79e52872015-12-07 16:01:01 -080090 mcastStore.deactivate();
alshabibed0951f2015-10-02 21:39:27 +020091 }
92
93 @Test
94 public void testAdd() {
95 manager.add(r1);
96
alshabibed0951f2015-10-02 21:39:27 +020097 validateEvents(McastEvent.Type.ROUTE_ADDED);
98 }
99
100 @Test
101 public void testRemove() {
102 manager.add(r1);
103
104 manager.remove(r1);
105
alshabib79e52872015-12-07 16:01:01 -0800106
alshabibed0951f2015-10-02 21:39:27 +0200107 validateEvents(McastEvent.Type.ROUTE_ADDED, McastEvent.Type.ROUTE_REMOVED);
108 }
109
110 @Test
111 public void testAddSource() {
alshabibed0951f2015-10-02 21:39:27 +0200112 manager.addSource(r1, cp1);
113
alshabib79e52872015-12-07 16:01:01 -0800114 validateEvents(McastEvent.Type.SOURCE_ADDED);
alshabibed0951f2015-10-02 21:39:27 +0200115 assertEquals("Route is not equal", cp1, manager.fetchSource(r1));
116 }
117
118 @Test
119 public void testAddSink() {
alshabibed0951f2015-10-02 21:39:27 +0200120 manager.addSink(r1, cp1);
121
alshabib79e52872015-12-07 16:01:01 -0800122 validateEvents(McastEvent.Type.SINK_ADDED);
123 assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1));
alshabibed0951f2015-10-02 21:39:27 +0200124 }
125
126 @Test
127 public void testRemoveSink() {
alshabibed0951f2015-10-02 21:39:27 +0200128
129 manager.addSource(r1, cp1);
130 manager.addSink(r1, cp1);
131 manager.addSink(r1, cp2);
132 manager.removeSink(r1, cp2);
133
alshabib79e52872015-12-07 16:01:01 -0800134 validateEvents(McastEvent.Type.SOURCE_ADDED,
alshabibed0951f2015-10-02 21:39:27 +0200135 McastEvent.Type.SINK_ADDED,
136 McastEvent.Type.SINK_ADDED,
137 McastEvent.Type.SINK_REMOVED);
alshabib79e52872015-12-07 16:01:01 -0800138 assertEquals("Route is not equal", Sets.newHashSet(cp1), manager.fetchSinks(r1));
alshabibed0951f2015-10-02 21:39:27 +0200139 }
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 Vachuskac65dd712015-11-04 17:19:10 -0800149 fail(String.format("Mismatched events# obtained -> %s : expected %s",
alshabibed0951f2015-10-02 21:39:27 +0200150 events, evs));
151 }
152 }
153 }
154
155 class TestMulticastListener implements McastListener {
alshabibed0951f2015-10-02 21:39:27 +0200156 @Override
157 public void event(McastEvent event) {
158 events.add(event);
159 }
160 }
161
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800162 private class TestCoreService extends CoreServiceAdapter {
alshabibed0951f2015-10-02 21:39:27 +0200163 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800164 public ApplicationId registerApplication(String name) {
165 return new DefaultApplicationId(0, name);
alshabibed0951f2015-10-02 21:39:27 +0200166 }
167 }
168}