blob: fc8c294cc258c3aac2ef5dee193dec14db92abe8 [file] [log] [blame]
Charles Chan6c624992017-08-18 17:11:34 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.segmentrouting;
18
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Host;
30import org.onosproject.net.config.NetworkConfigRegistryAdapter;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.intf.Interface;
33import org.onosproject.routeservice.ResolvedRoute;
34import org.onosproject.routeservice.Route;
35import org.onosproject.routeservice.RouteEvent;
36import org.onosproject.routeservice.RouteInfo;
37import org.onosproject.segmentrouting.config.DeviceConfiguration;
38
39import java.util.Map;
40import java.util.Set;
41
42import static org.junit.Assert.*;
43
44/**
45 * Unit test for {@link RouteHandler}.
46 */
47public class RouteHandlerTest {
48 private RouteHandler routeHandler;
49
50 // Mocked routing and bridging tables
51 private static final Map<MockBridgingTableKey, MockBridgingTableValue> BRIDGING_TABLE =
52 Maps.newConcurrentMap();
53 private static final Map<MockRoutingTableKey, MockRoutingTableValue> ROUTING_TABLE =
54 Maps.newConcurrentMap();
55 private static final Map<ConnectPoint, Set<IpPrefix>> SUBNET_TABLE = Maps.newConcurrentMap();
56 // Mocked Next Id
57 private static final Map<Integer, TrafficTreatment> NEXT_TABLE = Maps.newConcurrentMap();
58
59 private static final IpPrefix P1 = IpPrefix.valueOf("10.0.0.0/24");
60 private static final IpAddress N1 = IpAddress.valueOf("10.0.1.254");
61 private static final MacAddress M1 = MacAddress.valueOf("00:00:00:00:00:01");
62 private static final VlanId V1 = VlanId.vlanId((short) 1);
63 private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1");
64 private static final Route R1 = new Route(Route.Source.STATIC, P1, N1);
65 private static final ResolvedRoute RR1 = new ResolvedRoute(R1, M1, V1, CP1);
66
67 private static final IpAddress N2 = IpAddress.valueOf("10.0.2.254");
68 private static final MacAddress M2 = MacAddress.valueOf("00:00:00:00:00:02");
69 private static final VlanId V2 = VlanId.vlanId((short) 2);
70 private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint("of:0000000000000001/2");
71 private static final Route R2 = new Route(Route.Source.STATIC, P1, N2);
72 private static final ResolvedRoute RR2 = new ResolvedRoute(R2, M2, V2, CP2);
73
74 private static final RouteInfo RI1 = new RouteInfo(P1, RR1, Sets.newHashSet(RR1));
75
76 // A set of hosts
77 private static final Set<Host> HOSTS = Sets.newHashSet();
78 // A set of devices of which we have mastership
79 private static final Set<DeviceId> LOCAL_DEVICES = Sets.newHashSet();
80 // A set of interfaces
81 private static final Set<Interface> INTERFACES = Sets.newHashSet();
82 // A set of routes
83 private static final Set<RouteInfo> ROUTE_INFOS = Sets.newHashSet(RI1);
84
85 @Before
86 public void setUp() throws Exception {
87// TODO Initialize pairDevice and pairLocalPort config
88// ObjectMapper mapper = new ObjectMapper();
89// ConfigApplyDelegate delegate = config -> {};
90//
91// SegmentRoutingDeviceConfig dev3Config = new SegmentRoutingDeviceConfig();
92// JsonNode dev3Tree = mapper.createObjectNode();
93// dev3Config.init(DEV3, "host-handler-test", dev3Tree, mapper, delegate);
94// dev3Config.setPairDeviceId(DEV4).setPairLocalPort(P9);
95//
96// SegmentRoutingDeviceConfig dev4Config = new SegmentRoutingDeviceConfig();
97// JsonNode dev4Tree = mapper.createObjectNode();
98// dev4Config.init(DEV4, "host-handler-test", dev4Tree, mapper, delegate);
99// dev4Config.setPairDeviceId(DEV3).setPairLocalPort(P9);
100
101 MockNetworkConfigRegistry mockNetworkConfigRegistry = new MockNetworkConfigRegistry();
102// mockNetworkConfigRegistry.applyConfig(dev3Config);
103// mockNetworkConfigRegistry.applyConfig(dev4Config);
104
105 // Initialize Segment Routing Manager
106 SegmentRoutingManager srManager = new MockSegmentRoutingManager(NEXT_TABLE);
107 srManager.cfgService = new NetworkConfigRegistryAdapter();
108 srManager.deviceConfiguration = new DeviceConfiguration(srManager);
109 srManager.flowObjectiveService = new MockFlowObjectiveService(BRIDGING_TABLE, NEXT_TABLE);
110 srManager.routingRulePopulator = new MockRoutingRulePopulator(srManager, ROUTING_TABLE);
111 srManager.defaultRoutingHandler = new MockDefaultRoutingHandler(srManager, SUBNET_TABLE);
112 srManager.interfaceService = new MockInterfaceService(INTERFACES);
113 srManager.mastershipService = new MockMastershipService(LOCAL_DEVICES);
114 srManager.hostService = new MockHostService(HOSTS);
115 srManager.cfgService = mockNetworkConfigRegistry;
116 srManager.routeService = new MockRouteService(ROUTE_INFOS);
117
118 routeHandler = new RouteHandler(srManager);
119
120 ROUTING_TABLE.clear();
121 BRIDGING_TABLE.clear();
122 SUBNET_TABLE.clear();
123 }
124
125 @Test
126 public void init() throws Exception {
127 routeHandler.init(CP1.deviceId());
128
129 assertEquals(1, ROUTING_TABLE.size());
130 assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)));
131 assertEquals(1, SUBNET_TABLE.size());
132 assertTrue(SUBNET_TABLE.get(CP1).contains(P1));
133 }
134
135 @Test
136 public void processRouteAdded() throws Exception {
137 RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_ADDED, RR1);
138 routeHandler.processRouteAdded(re);
139
140 assertEquals(1, ROUTING_TABLE.size());
141 assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)));
142 assertEquals(1, SUBNET_TABLE.size());
143 assertTrue(SUBNET_TABLE.get(CP1).contains(P1));
144 }
145
146 @Test
147 public void processRouteUpdated() throws Exception {
148 processRouteAdded();
149
150 RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, RR2, RR1);
151 routeHandler.processRouteUpdated(re);
152
153 assertEquals(1, ROUTING_TABLE.size());
154 assertNotNull(ROUTING_TABLE.get(new MockRoutingTableKey(CP2.deviceId(), P1)));
155 assertEquals(1, SUBNET_TABLE.size());
156 assertTrue(SUBNET_TABLE.get(CP2).contains(P1));
157 }
158
159 @Test
160 public void processRouteRemoved() throws Exception {
161 processRouteAdded();
162
163 RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, RR1);
164 routeHandler.processRouteRemoved(re);
165
166 assertEquals(0, ROUTING_TABLE.size());
167 assertEquals(0, SUBNET_TABLE.size());
168 }
169
170}