blob: d482d60aa01f367f567634bcd603f6e65fd2a332 [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;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella09943842020-03-27 12:53:46 +010033import org.onosproject.net.config.basics.BasicDeviceConfig;
Charles Chancf789822019-03-22 10:04:27 -070034import org.onosproject.net.config.basics.InterfaceConfig;
35import org.onosproject.net.host.InterfaceIpAddress;
36import org.onosproject.net.intf.Interface;
37import org.onosproject.net.intf.InterfaceService;
38import org.onosproject.net.neighbour.NeighbourResolutionService;
pierventre2b102f92020-09-08 16:45:36 +020039import org.onosproject.segmentrouting.DeviceConfiguration;
Charles Chancf789822019-03-22 10:04:27 -070040import org.onosproject.segmentrouting.SegmentRoutingManager;
41
42import java.io.InputStream;
43import java.util.List;
44import java.util.Set;
45
46import static org.easymock.EasyMock.anyObject;
47import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.eq;
49import static org.easymock.EasyMock.expect;
50import static org.easymock.EasyMock.expectLastCall;
51import static org.easymock.EasyMock.replay;
52import static org.junit.Assert.*;
53
54public class DeviceConfigurationTest {
55 private static final DeviceId DEV1 = DeviceId.deviceId("of:1");
56 private static final String CONFIG_KEY = "segmentrouting";
57 private static final PortNumber PORT1 = PortNumber.portNumber(1);
58 private static final PortNumber PORT2 = PortNumber.portNumber(2);
59 private static final ConnectPoint CP1 = new ConnectPoint(DEV1, PORT1);
60 private static final ConnectPoint CP2 = new ConnectPoint(DEV1, PORT2);
61 private static final MacAddress MAC1 = MacAddress.valueOf("00:11:22:33:44:55");
62 private static final VlanId VLAN1 = VlanId.vlanId((short) 10);
63 private static final VlanId VLAN2 = VlanId.vlanId((short) 20);
64 private static final IpPrefix PREFIX1 = IpPrefix.valueOf("10.0.1.254/24");
65 private static final IpPrefix PREFIX2 = IpPrefix.valueOf("10.0.2.254/24");
66 private static final IpPrefix ROUTE1 = IpPrefix.valueOf("20.0.1.254/24");
67 private static final InterfaceIpAddress INTF1_IP = new InterfaceIpAddress(PREFIX1.address(), PREFIX1);
68 private static final InterfaceIpAddress INTF2_IP = new InterfaceIpAddress(PREFIX2.address(), PREFIX2);
69 private static final List<InterfaceIpAddress> IP_LIST1 = Lists.newArrayList(INTF1_IP);
70 private static final List<InterfaceIpAddress> IP_LIST2 = Lists.newArrayList(INTF2_IP);
71 private static final Interface INTF1 = new Interface("mock-intf1", CP1, IP_LIST1, MAC1,
72 null, VLAN1, null, null);
73 private static final Interface INTF2 = new Interface("mock-intf2", CP2, IP_LIST2, MAC1,
74 null, VLAN2, null, null);
75 private static final Set<Interface> INTERFACES = Sets.newHashSet(INTF1, INTF2);
76
77 private DeviceConfiguration devConfig;
78
Andrea Campanella09943842020-03-27 12:53:46 +010079 private NetworkConfigRegistry networkConfigService;
80
Charles Chancf789822019-03-22 10:04:27 -070081 @Before
82 public void setUp() throws Exception {
83 InterfaceService interfaceService;
Andrea Campanella09943842020-03-27 12:53:46 +010084 networkConfigService = null;
Charles Chancf789822019-03-22 10:04:27 -070085 NeighbourResolutionService neighbourResolutionService;
86 SegmentRoutingManager srManager;
87
88 // Mock device netcfg
89 InputStream jsonStream = SegmentRoutingDeviceConfigTest.class.getResourceAsStream("/device.json");
90 ObjectMapper mapper = new ObjectMapper();
91 JsonNode jsonNode = mapper.readTree(jsonStream);
92 SegmentRoutingDeviceConfig srDevConfig = new SegmentRoutingDeviceConfig();
93 srDevConfig.init(DEV1, CONFIG_KEY, jsonNode, mapper, config -> { });
Andrea Campanella09943842020-03-27 12:53:46 +010094 BasicDeviceConfig basicDeviceConfig = new BasicDeviceConfig();
95 basicDeviceConfig.init(DEV1, DEV1.toString(), JsonNodeFactory.instance.objectNode(), mapper, config -> { });
96 BasicDeviceConfig purgeOnDisconnectConfig = basicDeviceConfig.purgeOnDisconnection(true);
97
Charles Chancf789822019-03-22 10:04:27 -070098
99 // Mock interface netcfg
100 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface1.json");
101 jsonNode = mapper.readTree(jsonStream);
102 InterfaceConfig interfaceConfig1 = new InterfaceConfig();
103 interfaceConfig1.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
104 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface2.json");
105 jsonNode = mapper.readTree(jsonStream);
106 InterfaceConfig interfaceConfig2 = new InterfaceConfig();
107 interfaceConfig2.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
108
109 networkConfigService = createMock(NetworkConfigRegistry.class);
110 expect(networkConfigService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class))
111 .andReturn(Sets.newHashSet(DEV1)).anyTimes();
112 expect(networkConfigService.getConfig(DEV1, SegmentRoutingDeviceConfig.class))
113 .andReturn(srDevConfig).anyTimes();
Andrea Campanella09943842020-03-27 12:53:46 +0100114 expect(networkConfigService.addConfig(DEV1, BasicDeviceConfig.class))
115 .andReturn(basicDeviceConfig).anyTimes();
116 expect(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class))
117 .andReturn(basicDeviceConfig).anyTimes();
118 expect(networkConfigService.applyConfig(DEV1, BasicDeviceConfig.class, purgeOnDisconnectConfig.node()))
119 .andReturn(purgeOnDisconnectConfig).anyTimes();
Charles Chancf789822019-03-22 10:04:27 -0700120 expect(networkConfigService.getSubjects(ConnectPoint.class, InterfaceConfig.class))
121 .andReturn(Sets.newHashSet(CP1, CP2)).anyTimes();
122 expect(networkConfigService.getConfig(CP1, InterfaceConfig.class)).andReturn(interfaceConfig1).anyTimes();
123 expect(networkConfigService.getConfig(CP2, InterfaceConfig.class)).andReturn(interfaceConfig2).anyTimes();
124 expect(networkConfigService.applyConfig(eq(CP1), eq(InterfaceConfig.class), anyObject()))
125 .andReturn(interfaceConfig1).anyTimes();
126 expect(networkConfigService.applyConfig(eq(CP2), eq(InterfaceConfig.class), anyObject()))
127 .andReturn(interfaceConfig2).anyTimes();
128 expect(networkConfigService.getConfig(null, SegmentRoutingAppConfig.class)).andReturn(null).anyTimes();
129 replay(networkConfigService);
130
131 interfaceService = createMock(InterfaceService.class);
132 expect(interfaceService.getInterfaces()).andReturn(INTERFACES).anyTimes();
133 expect(interfaceService.getInterfacesByPort(CP1)).andReturn(Sets.newHashSet(INTF1)).anyTimes();
134 expect(interfaceService.getInterfacesByPort(CP2)).andReturn(Sets.newHashSet(INTF2)).anyTimes();
135 replay(interfaceService);
136
137 neighbourResolutionService = createMock(NeighbourResolutionService.class);
138 neighbourResolutionService.registerNeighbourHandler(anyObject(ConnectPoint.class), anyObject(), anyObject());
139 expectLastCall().anyTimes();
140 replay(neighbourResolutionService);
141
142 srManager = new SegmentRoutingManager();
143 srManager.interfaceService = interfaceService;
144 srManager.cfgService = networkConfigService;
145 srManager.neighbourResolutionService = neighbourResolutionService;
146
147 devConfig = new DeviceConfiguration(srManager);
148 devConfig.addSubnet(CP2, ROUTE1);
149 }
150
151 @Test
152 public void getConfiguredSubnets() {
153 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2);
154 assertEquals(expect, devConfig.getConfiguredSubnets(DEV1));
155 }
156
157 @Test
158 public void getSubnets() {
159 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2, ROUTE1);
160 assertEquals(expect, devConfig.getSubnets(DEV1));
161 }
162
163 @Test
164 public void getPortSubnets() {
165 assertEquals(Sets.newHashSet(PREFIX1), devConfig.getPortSubnets(DEV1, PORT1));
166 assertEquals(Sets.newHashSet(PREFIX2), devConfig.getPortSubnets(DEV1, PORT2));
167 }
168
169 @Test
170 public void inSameSubnet() {
171 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX1.address()));
172 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX2.address()));
173 assertFalse(devConfig.inSameSubnet(DEV1, ROUTE1.address()));
174 }
Andrea Campanella09943842020-03-27 12:53:46 +0100175
176 @Test
177 public void getPurgeOnDisconnect() {
178 assertNotNull(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class));
179 assertTrue(networkConfigService.getConfig(DEV1, BasicDeviceConfig.class).purgeOnDisconnection());
180 }
Charles Chancf789822019-03-22 10:04:27 -0700181}