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