blob: 469def0bfed82aace00300c6aec6357badcf2790 [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;
21import com.google.common.collect.Lists;
22import com.google.common.collect.Sets;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.config.NetworkConfigRegistry;
32import org.onosproject.net.config.basics.InterfaceConfig;
33import org.onosproject.net.host.InterfaceIpAddress;
34import org.onosproject.net.intf.Interface;
35import org.onosproject.net.intf.InterfaceService;
36import org.onosproject.net.neighbour.NeighbourResolutionService;
37import org.onosproject.segmentrouting.SegmentRoutingManager;
38
39import java.io.InputStream;
40import java.util.List;
41import java.util.Set;
42
43import static org.easymock.EasyMock.anyObject;
44import static org.easymock.EasyMock.createMock;
45import static org.easymock.EasyMock.eq;
46import static org.easymock.EasyMock.expect;
47import static org.easymock.EasyMock.expectLastCall;
48import static org.easymock.EasyMock.replay;
49import static org.junit.Assert.*;
50
51public class DeviceConfigurationTest {
52 private static final DeviceId DEV1 = DeviceId.deviceId("of:1");
53 private static final String CONFIG_KEY = "segmentrouting";
54 private static final PortNumber PORT1 = PortNumber.portNumber(1);
55 private static final PortNumber PORT2 = PortNumber.portNumber(2);
56 private static final ConnectPoint CP1 = new ConnectPoint(DEV1, PORT1);
57 private static final ConnectPoint CP2 = new ConnectPoint(DEV1, PORT2);
58 private static final MacAddress MAC1 = MacAddress.valueOf("00:11:22:33:44:55");
59 private static final VlanId VLAN1 = VlanId.vlanId((short) 10);
60 private static final VlanId VLAN2 = VlanId.vlanId((short) 20);
61 private static final IpPrefix PREFIX1 = IpPrefix.valueOf("10.0.1.254/24");
62 private static final IpPrefix PREFIX2 = IpPrefix.valueOf("10.0.2.254/24");
63 private static final IpPrefix ROUTE1 = IpPrefix.valueOf("20.0.1.254/24");
64 private static final InterfaceIpAddress INTF1_IP = new InterfaceIpAddress(PREFIX1.address(), PREFIX1);
65 private static final InterfaceIpAddress INTF2_IP = new InterfaceIpAddress(PREFIX2.address(), PREFIX2);
66 private static final List<InterfaceIpAddress> IP_LIST1 = Lists.newArrayList(INTF1_IP);
67 private static final List<InterfaceIpAddress> IP_LIST2 = Lists.newArrayList(INTF2_IP);
68 private static final Interface INTF1 = new Interface("mock-intf1", CP1, IP_LIST1, MAC1,
69 null, VLAN1, null, null);
70 private static final Interface INTF2 = new Interface("mock-intf2", CP2, IP_LIST2, MAC1,
71 null, VLAN2, null, null);
72 private static final Set<Interface> INTERFACES = Sets.newHashSet(INTF1, INTF2);
73
74 private DeviceConfiguration devConfig;
75
76 @Before
77 public void setUp() throws Exception {
78 InterfaceService interfaceService;
79 NetworkConfigRegistry networkConfigService;
80 NeighbourResolutionService neighbourResolutionService;
81 SegmentRoutingManager srManager;
82
83 // Mock device netcfg
84 InputStream jsonStream = SegmentRoutingDeviceConfigTest.class.getResourceAsStream("/device.json");
85 ObjectMapper mapper = new ObjectMapper();
86 JsonNode jsonNode = mapper.readTree(jsonStream);
87 SegmentRoutingDeviceConfig srDevConfig = new SegmentRoutingDeviceConfig();
88 srDevConfig.init(DEV1, CONFIG_KEY, jsonNode, mapper, config -> { });
89
90 // Mock interface netcfg
91 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface1.json");
92 jsonNode = mapper.readTree(jsonStream);
93 InterfaceConfig interfaceConfig1 = new InterfaceConfig();
94 interfaceConfig1.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
95 jsonStream = InterfaceConfig.class.getResourceAsStream("/interface2.json");
96 jsonNode = mapper.readTree(jsonStream);
97 InterfaceConfig interfaceConfig2 = new InterfaceConfig();
98 interfaceConfig2.init(CP1, CONFIG_KEY, jsonNode, mapper, config -> { });
99
100 networkConfigService = createMock(NetworkConfigRegistry.class);
101 expect(networkConfigService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class))
102 .andReturn(Sets.newHashSet(DEV1)).anyTimes();
103 expect(networkConfigService.getConfig(DEV1, SegmentRoutingDeviceConfig.class))
104 .andReturn(srDevConfig).anyTimes();
105 expect(networkConfigService.getSubjects(ConnectPoint.class, InterfaceConfig.class))
106 .andReturn(Sets.newHashSet(CP1, CP2)).anyTimes();
107 expect(networkConfigService.getConfig(CP1, InterfaceConfig.class)).andReturn(interfaceConfig1).anyTimes();
108 expect(networkConfigService.getConfig(CP2, InterfaceConfig.class)).andReturn(interfaceConfig2).anyTimes();
109 expect(networkConfigService.applyConfig(eq(CP1), eq(InterfaceConfig.class), anyObject()))
110 .andReturn(interfaceConfig1).anyTimes();
111 expect(networkConfigService.applyConfig(eq(CP2), eq(InterfaceConfig.class), anyObject()))
112 .andReturn(interfaceConfig2).anyTimes();
113 expect(networkConfigService.getConfig(null, SegmentRoutingAppConfig.class)).andReturn(null).anyTimes();
114 replay(networkConfigService);
115
116 interfaceService = createMock(InterfaceService.class);
117 expect(interfaceService.getInterfaces()).andReturn(INTERFACES).anyTimes();
118 expect(interfaceService.getInterfacesByPort(CP1)).andReturn(Sets.newHashSet(INTF1)).anyTimes();
119 expect(interfaceService.getInterfacesByPort(CP2)).andReturn(Sets.newHashSet(INTF2)).anyTimes();
120 replay(interfaceService);
121
122 neighbourResolutionService = createMock(NeighbourResolutionService.class);
123 neighbourResolutionService.registerNeighbourHandler(anyObject(ConnectPoint.class), anyObject(), anyObject());
124 expectLastCall().anyTimes();
125 replay(neighbourResolutionService);
126
127 srManager = new SegmentRoutingManager();
128 srManager.interfaceService = interfaceService;
129 srManager.cfgService = networkConfigService;
130 srManager.neighbourResolutionService = neighbourResolutionService;
131
132 devConfig = new DeviceConfiguration(srManager);
133 devConfig.addSubnet(CP2, ROUTE1);
134 }
135
136 @Test
137 public void getConfiguredSubnets() {
138 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2);
139 assertEquals(expect, devConfig.getConfiguredSubnets(DEV1));
140 }
141
142 @Test
143 public void getSubnets() {
144 Set<IpPrefix> expect = Sets.newHashSet(PREFIX1, PREFIX2, ROUTE1);
145 assertEquals(expect, devConfig.getSubnets(DEV1));
146 }
147
148 @Test
149 public void getPortSubnets() {
150 assertEquals(Sets.newHashSet(PREFIX1), devConfig.getPortSubnets(DEV1, PORT1));
151 assertEquals(Sets.newHashSet(PREFIX2), devConfig.getPortSubnets(DEV1, PORT2));
152 }
153
154 @Test
155 public void inSameSubnet() {
156 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX1.address()));
157 assertTrue(devConfig.inSameSubnet(DEV1, PREFIX2.address()));
158 assertFalse(devConfig.inSameSubnet(DEV1, ROUTE1.address()));
159 }
160}