blob: b0ab5e5f3ef8c03348d44f6c610740120cdee649 [file] [log] [blame]
Charles Chancf789822019-03-22 10:04:27 -07001/*
2 * Copyright 2019-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.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
Andrea Campanella09943842020-03-27 12:53:46 +010021import com.fasterxml.jackson.databind.node.JsonNodeFactory;
Charles Chancf789822019-03-22 10:04:27 -070022import com.google.common.collect.Lists;
23import com.google.common.collect.Sets;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
Charles Chan44e914e2021-04-01 16:18:30 -070031import org.onosproject.net.HostId;
Charles Chancf789822019-03-22 10:04:27 -070032import org.onosproject.net.PortNumber;
33import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella09943842020-03-27 12:53:46 +010034import org.onosproject.net.config.basics.BasicDeviceConfig;
Charles Chan44e914e2021-04-01 16:18:30 -070035import org.onosproject.net.config.basics.BasicHostConfig;
Charles Chancf789822019-03-22 10:04:27 -070036import org.onosproject.net.config.basics.InterfaceConfig;
37import org.onosproject.net.host.InterfaceIpAddress;
38import org.onosproject.net.intf.Interface;
39import org.onosproject.net.intf.InterfaceService;
40import org.onosproject.net.neighbour.NeighbourResolutionService;
pierventre2b102f92020-09-08 16:45:36 +020041import org.onosproject.segmentrouting.DeviceConfiguration;
Charles Chancf789822019-03-22 10:04:27 -070042import org.onosproject.segmentrouting.SegmentRoutingManager;
43
44import java.io.InputStream;
45import java.util.List;
46import java.util.Set;
47
48import static org.easymock.EasyMock.anyObject;
49import static org.easymock.EasyMock.createMock;
50import static org.easymock.EasyMock.eq;
51import static org.easymock.EasyMock.expect;
52import static org.easymock.EasyMock.expectLastCall;
53import static org.easymock.EasyMock.replay;
54import static org.junit.Assert.*;
55
56public class DeviceConfigurationTest {
57 private static final DeviceId DEV1 = DeviceId.deviceId("of:1");
58 private static final String CONFIG_KEY = "segmentrouting";
59 private static final PortNumber PORT1 = PortNumber.portNumber(1);
60 private static final PortNumber PORT2 = PortNumber.portNumber(2);
61 private static final ConnectPoint CP1 = new ConnectPoint(DEV1, PORT1);
62 private static final ConnectPoint CP2 = new ConnectPoint(DEV1, PORT2);
63 private static final MacAddress MAC1 = MacAddress.valueOf("00:11:22:33:44:55");
Charles Chan44e914e2021-04-01 16:18:30 -070064 private static final MacAddress ROUTER_MAC = MacAddress.valueOf("00:00:00:00:01:80");
65 private static final HostId ROUTER_HOST = HostId.hostId(ROUTER_MAC, VlanId.NONE);
Charles Chancf789822019-03-22 10:04:27 -070066 private static final VlanId VLAN1 = VlanId.vlanId((short) 10);
67 private static final VlanId VLAN2 = VlanId.vlanId((short) 20);
68 private static final IpPrefix PREFIX1 = IpPrefix.valueOf("10.0.1.254/24");
69 private static final IpPrefix PREFIX2 = IpPrefix.valueOf("10.0.2.254/24");
70 private static final IpPrefix ROUTE1 = IpPrefix.valueOf("20.0.1.254/24");
71 private static final InterfaceIpAddress INTF1_IP = new InterfaceIpAddress(PREFIX1.address(), PREFIX1);
72 private static final InterfaceIpAddress INTF2_IP = new InterfaceIpAddress(PREFIX2.address(), PREFIX2);
73 private static final List<InterfaceIpAddress> IP_LIST1 = Lists.newArrayList(INTF1_IP);
74 private static final List<InterfaceIpAddress> IP_LIST2 = Lists.newArrayList(INTF2_IP);
75 private static final Interface INTF1 = new Interface("mock-intf1", CP1, IP_LIST1, MAC1,
76 null, VLAN1, null, null);
77 private static final Interface INTF2 = new Interface("mock-intf2", CP2, IP_LIST2, MAC1,
78 null, VLAN2, null, null);
79 private static final Set<Interface> INTERFACES = Sets.newHashSet(INTF1, INTF2);
80
81 private DeviceConfiguration devConfig;
82
Andrea Campanella09943842020-03-27 12:53:46 +010083 private NetworkConfigRegistry networkConfigService;
84
Charles Chancf789822019-03-22 10:04:27 -070085 @Before
86 public void setUp() throws Exception {
87 InterfaceService interfaceService;
Andrea Campanella09943842020-03-27 12:53:46 +010088 networkConfigService = null;
Charles Chancf789822019-03-22 10:04:27 -070089 NeighbourResolutionService neighbourResolutionService;
90 SegmentRoutingManager srManager;
91
92 // Mock device netcfg
93 InputStream jsonStream = SegmentRoutingDeviceConfigTest.class.getResourceAsStream("/device.json");
94 ObjectMapper mapper = new ObjectMapper();
95 JsonNode jsonNode = mapper.readTree(jsonStream);
96 SegmentRoutingDeviceConfig srDevConfig = new SegmentRoutingDeviceConfig();
97 srDevConfig.init(DEV1, CONFIG_KEY, jsonNode, mapper, config -> { });
Andrea Campanella09943842020-03-27 12:53:46 +010098 BasicDeviceConfig basicDeviceConfig = new BasicDeviceConfig();
99 basicDeviceConfig.init(DEV1, DEV1.toString(), JsonNodeFactory.instance.objectNode(), mapper, config -> { });
Charles Chancf789822019-03-22 10:04:27 -0700100
101 // Mock interface netcfg
102 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface1.json");
103 jsonNode = mapper.readTree(jsonStream);
104 InterfaceConfig interfaceConfig1 = new InterfaceConfig();
105 interfaceConfig1.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
Charles Chan44e914e2021-04-01 16:18:30 -0700106
Charles Chancf789822019-03-22 10:04:27 -0700107 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface2.json");
108 jsonNode = mapper.readTree(jsonStream);
109 InterfaceConfig interfaceConfig2 = new InterfaceConfig();
110 interfaceConfig2.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
111
Charles Chan44e914e2021-04-01 16:18:30 -0700112 jsonStream = BasicHostConfig.class.getResourceAsStream("/host1.json");
113 jsonNode = mapper.readTree(jsonStream);
114 BasicHostConfig hostConfig1 = new BasicHostConfig();
115 hostConfig1.init(ROUTER_HOST, "basic", jsonNode, mapper, config -> { });
116
Charles Chancf789822019-03-22 10:04:27 -0700117 networkConfigService = createMock(NetworkConfigRegistry.class);
118 expect(networkConfigService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class))
119 .andReturn(Sets.newHashSet(DEV1)).anyTimes();
120 expect(networkConfigService.getConfig(DEV1, SegmentRoutingDeviceConfig.class))
121 .andReturn(srDevConfig).anyTimes();
Andrea Campanella09943842020-03-27 12:53:46 +0100122 expect(networkConfigService.addConfig(DEV1, BasicDeviceConfig.class))
123 .andReturn(basicDeviceConfig).anyTimes();
124 expect(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class))
125 .andReturn(basicDeviceConfig).anyTimes();
Charles Chancf789822019-03-22 10:04:27 -0700126 expect(networkConfigService.getSubjects(ConnectPoint.class, InterfaceConfig.class))
127 .andReturn(Sets.newHashSet(CP1, CP2)).anyTimes();
128 expect(networkConfigService.getConfig(CP1, InterfaceConfig.class)).andReturn(interfaceConfig1).anyTimes();
129 expect(networkConfigService.getConfig(CP2, InterfaceConfig.class)).andReturn(interfaceConfig2).anyTimes();
130 expect(networkConfigService.applyConfig(eq(CP1), eq(InterfaceConfig.class), anyObject()))
131 .andReturn(interfaceConfig1).anyTimes();
132 expect(networkConfigService.applyConfig(eq(CP2), eq(InterfaceConfig.class), anyObject()))
133 .andReturn(interfaceConfig2).anyTimes();
134 expect(networkConfigService.getConfig(null, SegmentRoutingAppConfig.class)).andReturn(null).anyTimes();
Charles Chan44e914e2021-04-01 16:18:30 -0700135 expect(networkConfigService.getConfig(ROUTER_HOST, BasicHostConfig.class)).andReturn(hostConfig1).anyTimes();
136 expect(networkConfigService.applyConfig(eq(ROUTER_HOST), eq(BasicHostConfig.class), anyObject()))
137 .andReturn(hostConfig1).anyTimes();
Charles Chancf789822019-03-22 10:04:27 -0700138 replay(networkConfigService);
139
140 interfaceService = createMock(InterfaceService.class);
141 expect(interfaceService.getInterfaces()).andReturn(INTERFACES).anyTimes();
142 expect(interfaceService.getInterfacesByPort(CP1)).andReturn(Sets.newHashSet(INTF1)).anyTimes();
143 expect(interfaceService.getInterfacesByPort(CP2)).andReturn(Sets.newHashSet(INTF2)).anyTimes();
144 replay(interfaceService);
145
146 neighbourResolutionService = createMock(NeighbourResolutionService.class);
147 neighbourResolutionService.registerNeighbourHandler(anyObject(ConnectPoint.class), anyObject(), anyObject());
148 expectLastCall().anyTimes();
149 replay(neighbourResolutionService);
150
151 srManager = new SegmentRoutingManager();
152 srManager.interfaceService = interfaceService;
153 srManager.cfgService = networkConfigService;
154 srManager.neighbourResolutionService = neighbourResolutionService;
155
156 devConfig = new DeviceConfiguration(srManager);
157 devConfig.addSubnet(CP2, ROUTE1);
158 }
159
160 @Test
161 public void getConfiguredSubnets() {
162 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2);
163 assertEquals(expect, devConfig.getConfiguredSubnets(DEV1));
164 }
165
166 @Test
167 public void getSubnets() {
168 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2, ROUTE1);
169 assertEquals(expect, devConfig.getSubnets(DEV1));
170 }
171
172 @Test
173 public void getPortSubnets() {
174 assertEquals(Sets.newHashSet(PREFIX1), devConfig.getPortSubnets(DEV1, PORT1));
175 assertEquals(Sets.newHashSet(PREFIX2), devConfig.getPortSubnets(DEV1, PORT2));
176 }
177
178 @Test
179 public void inSameSubnet() {
180 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX1.address()));
181 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX2.address()));
182 assertFalse(devConfig.inSameSubnet(DEV1, ROUTE1.address()));
183 }
Charles Chancf789822019-03-22 10:04:27 -0700184}