Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DeviceId; |
Charles Chan | 44e914e | 2021-04-01 16:18:30 -0700 | [diff] [blame] | 31 | import org.onosproject.net.HostId; |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 34 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Charles Chan | 44e914e | 2021-04-01 16:18:30 -0700 | [diff] [blame] | 35 | import org.onosproject.net.config.basics.BasicHostConfig; |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.basics.InterfaceConfig; |
| 37 | import org.onosproject.net.host.InterfaceIpAddress; |
| 38 | import org.onosproject.net.intf.Interface; |
| 39 | import org.onosproject.net.intf.InterfaceService; |
| 40 | import org.onosproject.net.neighbour.NeighbourResolutionService; |
pierventre | 2b102f9 | 2020-09-08 16:45:36 +0200 | [diff] [blame] | 41 | import org.onosproject.segmentrouting.DeviceConfiguration; |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 42 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
| 43 | |
| 44 | import java.io.InputStream; |
| 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 47 | |
| 48 | import static org.easymock.EasyMock.anyObject; |
| 49 | import static org.easymock.EasyMock.createMock; |
| 50 | import static org.easymock.EasyMock.eq; |
| 51 | import static org.easymock.EasyMock.expect; |
| 52 | import static org.easymock.EasyMock.expectLastCall; |
| 53 | import static org.easymock.EasyMock.replay; |
| 54 | import static org.junit.Assert.*; |
| 55 | |
| 56 | public 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 Chan | 44e914e | 2021-04-01 16:18:30 -0700 | [diff] [blame] | 64 | 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 Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 66 | 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 Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 83 | private NetworkConfigRegistry networkConfigService; |
| 84 | |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 85 | @Before |
| 86 | public void setUp() throws Exception { |
| 87 | InterfaceService interfaceService; |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 88 | networkConfigService = null; |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 89 | 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 Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 98 | BasicDeviceConfig basicDeviceConfig = new BasicDeviceConfig(); |
| 99 | basicDeviceConfig.init(DEV1, DEV1.toString(), JsonNodeFactory.instance.objectNode(), mapper, config -> { }); |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 100 | |
| 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 Chan | 44e914e | 2021-04-01 16:18:30 -0700 | [diff] [blame] | 106 | |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 107 | 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 Chan | 44e914e | 2021-04-01 16:18:30 -0700 | [diff] [blame] | 112 | 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 Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 117 | 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 Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 122 | expect(networkConfigService.addConfig(DEV1, BasicDeviceConfig.class)) |
| 123 | .andReturn(basicDeviceConfig).anyTimes(); |
| 124 | expect(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class)) |
| 125 | .andReturn(basicDeviceConfig).anyTimes(); |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 126 | 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 Chan | 44e914e | 2021-04-01 16:18:30 -0700 | [diff] [blame] | 135 | 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 Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 138 | 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 Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 184 | } |