blob: 5bb40f31eed8b2745206c0fd3af5e76c3e44e0bd [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 -> { });
100 BasicDeviceConfig purgeOnDisconnectConfig = basicDeviceConfig.purgeOnDisconnection(true);
101
Charles Chancf789822019-03-22 10:04:27 -0700102
103 // Mock interface netcfg
104 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface1.json");
105 jsonNode = mapper.readTree(jsonStream);
106 InterfaceConfig interfaceConfig1 = new InterfaceConfig();
107 interfaceConfig1.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
Charles Chan44e914e2021-04-01 16:18:30 -0700108
Charles Chancf789822019-03-22 10:04:27 -0700109 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface2.json");
110 jsonNode = mapper.readTree(jsonStream);
111 InterfaceConfig interfaceConfig2 = new InterfaceConfig();
112 interfaceConfig2.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
113
Charles Chan44e914e2021-04-01 16:18:30 -0700114 jsonStream = BasicHostConfig.class.getResourceAsStream("/host1.json");
115 jsonNode = mapper.readTree(jsonStream);
116 BasicHostConfig hostConfig1 = new BasicHostConfig();
117 hostConfig1.init(ROUTER_HOST, "basic", jsonNode, mapper, config -> { });
118
Charles Chancf789822019-03-22 10:04:27 -0700119 networkConfigService = createMock(NetworkConfigRegistry.class);
120 expect(networkConfigService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class))
121 .andReturn(Sets.newHashSet(DEV1)).anyTimes();
122 expect(networkConfigService.getConfig(DEV1, SegmentRoutingDeviceConfig.class))
123 .andReturn(srDevConfig).anyTimes();
Andrea Campanella09943842020-03-27 12:53:46 +0100124 expect(networkConfigService.addConfig(DEV1, BasicDeviceConfig.class))
125 .andReturn(basicDeviceConfig).anyTimes();
126 expect(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class))
127 .andReturn(basicDeviceConfig).anyTimes();
128 expect(networkConfigService.applyConfig(DEV1, BasicDeviceConfig.class, purgeOnDisconnectConfig.node()))
129 .andReturn(purgeOnDisconnectConfig).anyTimes();
Charles Chancf789822019-03-22 10:04:27 -0700130 expect(networkConfigService.getSubjects(ConnectPoint.class, InterfaceConfig.class))
131 .andReturn(Sets.newHashSet(CP1, CP2)).anyTimes();
132 expect(networkConfigService.getConfig(CP1, InterfaceConfig.class)).andReturn(interfaceConfig1).anyTimes();
133 expect(networkConfigService.getConfig(CP2, InterfaceConfig.class)).andReturn(interfaceConfig2).anyTimes();
134 expect(networkConfigService.applyConfig(eq(CP1), eq(InterfaceConfig.class), anyObject()))
135 .andReturn(interfaceConfig1).anyTimes();
136 expect(networkConfigService.applyConfig(eq(CP2), eq(InterfaceConfig.class), anyObject()))
137 .andReturn(interfaceConfig2).anyTimes();
138 expect(networkConfigService.getConfig(null, SegmentRoutingAppConfig.class)).andReturn(null).anyTimes();
Charles Chan44e914e2021-04-01 16:18:30 -0700139 expect(networkConfigService.getConfig(ROUTER_HOST, BasicHostConfig.class)).andReturn(hostConfig1).anyTimes();
140 expect(networkConfigService.applyConfig(eq(ROUTER_HOST), eq(BasicHostConfig.class), anyObject()))
141 .andReturn(hostConfig1).anyTimes();
Charles Chancf789822019-03-22 10:04:27 -0700142 replay(networkConfigService);
143
144 interfaceService = createMock(InterfaceService.class);
145 expect(interfaceService.getInterfaces()).andReturn(INTERFACES).anyTimes();
146 expect(interfaceService.getInterfacesByPort(CP1)).andReturn(Sets.newHashSet(INTF1)).anyTimes();
147 expect(interfaceService.getInterfacesByPort(CP2)).andReturn(Sets.newHashSet(INTF2)).anyTimes();
148 replay(interfaceService);
149
150 neighbourResolutionService = createMock(NeighbourResolutionService.class);
151 neighbourResolutionService.registerNeighbourHandler(anyObject(ConnectPoint.class), anyObject(), anyObject());
152 expectLastCall().anyTimes();
153 replay(neighbourResolutionService);
154
155 srManager = new SegmentRoutingManager();
156 srManager.interfaceService = interfaceService;
157 srManager.cfgService = networkConfigService;
158 srManager.neighbourResolutionService = neighbourResolutionService;
159
160 devConfig = new DeviceConfiguration(srManager);
161 devConfig.addSubnet(CP2, ROUTE1);
162 }
163
164 @Test
165 public void getConfiguredSubnets() {
166 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2);
167 assertEquals(expect, devConfig.getConfiguredSubnets(DEV1));
168 }
169
170 @Test
171 public void getSubnets() {
172 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2, ROUTE1);
173 assertEquals(expect, devConfig.getSubnets(DEV1));
174 }
175
176 @Test
177 public void getPortSubnets() {
178 assertEquals(Sets.newHashSet(PREFIX1), devConfig.getPortSubnets(DEV1, PORT1));
179 assertEquals(Sets.newHashSet(PREFIX2), devConfig.getPortSubnets(DEV1, PORT2));
180 }
181
182 @Test
183 public void inSameSubnet() {
184 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX1.address()));
185 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX2.address()));
186 assertFalse(devConfig.inSameSubnet(DEV1, ROUTE1.address()));
187 }
Andrea Campanella09943842020-03-27 12:53:46 +0100188
189 @Test
190 public void getPurgeOnDisconnect() {
191 assertNotNull(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class));
192 assertTrue(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class).purgeOnDisconnection());
193 }
Charles Chancf789822019-03-22 10:04:27 -0700194}