Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | package org.onosproject.openstacknetworking.util; |
| 17 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 24 | import com.google.common.testing.EqualsTester; |
| 25 | import org.apache.commons.io.IOUtils; |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.IpAddress; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 31 | import org.onlab.packet.MacAddress; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.DefaultPort; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceService; |
| 38 | import org.onosproject.net.device.DeviceServiceAdapter; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame^] | 39 | import org.onosproject.openstacknetworking.api.Constants; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 40 | import org.onosproject.openstacknetworking.api.InstancePort; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 41 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 42 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 43 | import org.onosproject.openstacknetworking.impl.DefaultInstancePort; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 44 | import org.onosproject.openstacknetworking.impl.OpenstackNetworkServiceAdapter; |
| 45 | import org.onosproject.openstacknetworking.impl.OpenstackRouterServiceAdapter; |
| 46 | import org.onosproject.openstacknetworking.impl.TestRouterInterface; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 47 | import org.onosproject.openstacknetworking.web.OpenstackFloatingIpWebResourceTest; |
| 48 | import org.onosproject.openstacknetworking.web.OpenstackNetworkWebResourceTest; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 49 | import org.onosproject.openstacknode.api.DefaultOpenstackAuth; |
| 50 | import org.onosproject.openstacknode.api.DefaultOpenstackNode; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 51 | import org.onosproject.openstacknode.api.NodeState; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 52 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 53 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | 5a38ab6 | 2018-07-02 22:34:11 +0900 | [diff] [blame] | 54 | import org.onosproject.openstacknode.api.OpenstackNodeTest; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 55 | import org.openstack4j.model.network.NetFloatingIP; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 56 | import org.openstack4j.model.network.Network; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 57 | import org.openstack4j.model.network.Port; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 58 | import org.openstack4j.model.network.RouterInterface; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 59 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 60 | import org.openstack4j.openstack.networking.domain.NeutronNetwork; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 61 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 62 | |
| 63 | import java.io.IOException; |
| 64 | import java.io.InputStream; |
| 65 | import java.nio.charset.StandardCharsets; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 66 | import java.util.List; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 67 | import java.util.Map; |
| 68 | import java.util.Set; |
| 69 | |
| 70 | import static org.junit.Assert.assertEquals; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 71 | import static org.junit.Assert.assertFalse; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 72 | import static org.junit.Assert.assertNull; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 73 | import static org.junit.Assert.assertTrue; |
| 74 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 75 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface; |
| 76 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.associatedFloatingIp; |
| 77 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode; |
| 78 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getConnectedClient; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 79 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByComputeDevId; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 80 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByInstancePort; |
| 81 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getIntfNameFromPciAddress; |
| 82 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.isAssociatedWithVM; |
| 83 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
| 84 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson; |
| 85 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson; |
| 86 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.routerInterfacesEquals; |
| 87 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.swapStaleLocation; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame^] | 88 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.vnicType; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 89 | |
| 90 | public final class OpenstackNetworkingUtilTest { |
| 91 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 92 | private NetFloatingIP floatingIp1; |
| 93 | private NetFloatingIP floatingIp2; |
| 94 | private NetFloatingIP floatingIp3; |
| 95 | private Port openstackPort; |
| 96 | private Port openstackSriovPort1; |
| 97 | private Port openstackSriovPort2; |
| 98 | private Port openstackSriovPort3; |
| 99 | private InstancePort instancePort1; |
| 100 | private InstancePort instancePort2; |
| 101 | private InstancePort instancePort3; |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 102 | private Map<String, RouterInterface> routerInterfaceMap = Maps.newHashMap(); |
| 103 | private OpenstackNode openstackControlNodeV2; |
| 104 | private OpenstackNode openstackControlNodeV3; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 105 | |
| 106 | @Before |
| 107 | public void setUp() { |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 108 | |
| 109 | instancePort1 = DefaultInstancePort.builder() |
| 110 | .networkId("net-id-1") |
| 111 | .portId("ce705c24-c1ef-408a-bda3-7bbd946164ab") |
| 112 | .deviceId(DeviceId.deviceId("of:000000000000000a")) |
| 113 | .portNumber(PortNumber.portNumber(1, "tap-1")) |
| 114 | .ipAddress(IpAddress.valueOf("10.0.0.3")) |
| 115 | .macAddress(MacAddress.valueOf("11:22:33:44:55:66")) |
| 116 | .state(InstancePort.State.valueOf("ACTIVE")) |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 117 | .build(); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 118 | |
| 119 | instancePort2 = DefaultInstancePort.builder() |
| 120 | .networkId("net-id-2") |
| 121 | .portId("port-id-2") |
| 122 | .deviceId(DeviceId.deviceId("of:000000000000000b")) |
| 123 | .portNumber(PortNumber.portNumber(2, "tap-2")) |
| 124 | .ipAddress(IpAddress.valueOf("10.10.10.2")) |
| 125 | .macAddress(MacAddress.valueOf("22:33:44:55:66:11")) |
| 126 | .state(InstancePort.State.valueOf("ACTIVE")) |
| 127 | .build(); |
| 128 | |
| 129 | instancePort3 = DefaultInstancePort.builder() |
| 130 | .networkId("net-id-3") |
| 131 | .portId("port-id-3") |
| 132 | .deviceId(DeviceId.deviceId("of:000000000000000c")) |
| 133 | .oldDeviceId(DeviceId.deviceId("of:000000000000000d")) |
| 134 | .oldPortNumber(PortNumber.portNumber(4, "tap-4")) |
| 135 | .portNumber(PortNumber.portNumber(3, "tap-3")) |
| 136 | .ipAddress(IpAddress.valueOf("10.10.10.3")) |
| 137 | .macAddress(MacAddress.valueOf("33:44:55:66:11:22")) |
| 138 | .state(InstancePort.State.valueOf("ACTIVE")) |
| 139 | .build(); |
| 140 | |
| 141 | InputStream floatingIpjsonStream1 = OpenstackFloatingIpWebResourceTest.class |
| 142 | .getResourceAsStream("openstack-floatingip1.json"); |
| 143 | InputStream floatingIpjsonStream2 = OpenstackFloatingIpWebResourceTest.class |
| 144 | .getResourceAsStream("openstack-floatingip2.json"); |
| 145 | InputStream floatingIpjsonStream3 = OpenstackFloatingIpWebResourceTest.class |
| 146 | .getResourceAsStream("openstack-floatingip3.json"); |
| 147 | |
| 148 | floatingIp1 = (NetFloatingIP) |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 149 | jsonToModelEntity(floatingIpjsonStream1, NeutronFloatingIP.class); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 150 | floatingIp2 = (NetFloatingIP) |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 151 | jsonToModelEntity(floatingIpjsonStream2, NeutronFloatingIP.class); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 152 | floatingIp3 = (NetFloatingIP) |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 153 | jsonToModelEntity(floatingIpjsonStream3, NeutronFloatingIP.class); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 154 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 155 | InputStream portJsonStream = OpenstackNetworkWebResourceTest.class |
| 156 | .getResourceAsStream("openstack-port.json"); |
| 157 | |
| 158 | InputStream sriovPortJsonStream1 = OpenstackNetworkWebResourceTest.class |
| 159 | .getResourceAsStream("openstack-port-sriov1.json"); |
| 160 | InputStream sriovPortJsonStream2 = OpenstackNetworkWebResourceTest.class |
| 161 | .getResourceAsStream("openstack-port-sriov2.json"); |
| 162 | InputStream sriovPortJsonStream3 = OpenstackNetworkWebResourceTest.class |
| 163 | .getResourceAsStream("openstack-port-sriov3.json"); |
| 164 | |
| 165 | openstackPort = (Port) |
| 166 | jsonToModelEntity(portJsonStream, NeutronPort.class); |
| 167 | |
| 168 | openstackSriovPort1 = (Port) |
| 169 | jsonToModelEntity(sriovPortJsonStream1, NeutronPort.class); |
| 170 | openstackSriovPort2 = (Port) |
| 171 | jsonToModelEntity(sriovPortJsonStream2, NeutronPort.class); |
| 172 | openstackSriovPort3 = (Port) |
| 173 | jsonToModelEntity(sriovPortJsonStream3, NeutronPort.class); |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @After |
| 177 | public void tearDown() { |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests the floatingIp translation. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testFloatingIp() throws IOException { |
| 185 | ObjectNode floatingIpNode = |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 186 | modelEntityToJson(floatingIp1, NeutronFloatingIP.class); |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 187 | InputStream is = IOUtils.toInputStream(floatingIpNode.toString(), StandardCharsets.UTF_8.name()); |
| 188 | NetFloatingIP floatingIp2 = (NetFloatingIP) |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 189 | jsonToModelEntity(is, NeutronFloatingIP.class); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 190 | new EqualsTester().addEqualityGroup(floatingIp1, floatingIp2).testEquals(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Tests the associatedFloatingIp method. |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 195 | */ |
| 196 | @Test |
| 197 | public void testAsscoatedFloatingIp() throws NullPointerException { |
| 198 | Set<NetFloatingIP> testSet = Sets.newHashSet(); |
| 199 | testSet.add(floatingIp1); |
| 200 | testSet.add(floatingIp2); |
| 201 | testSet.add(floatingIp3); |
| 202 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 203 | NetFloatingIP floatingIp1 = associatedFloatingIp(instancePort1, testSet); |
| 204 | NetFloatingIP floatingIp2 = associatedFloatingIp(instancePort2, testSet); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 205 | |
| 206 | assertEquals(floatingIp1, this.floatingIp1); |
| 207 | assertEquals(floatingIp2, null); |
| 208 | } |
| 209 | |
| 210 | /** |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 211 | * Tests the isAssociatedWithVM method. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testIsAssociatedWithVM() { |
| 215 | OpenstackNetworkService service = new TestOpenstackNetworkService(); |
| 216 | NetFloatingIP floatingIp4 = new NeutronFloatingIP().toBuilder().portId("portId4").build(); |
| 217 | |
| 218 | assertFalse(isAssociatedWithVM(service, floatingIp4)); |
| 219 | assertFalse(isAssociatedWithVM(service, floatingIp3)); |
| 220 | assertTrue(isAssociatedWithVM(service, floatingIp1)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests the isAssociatedWithVM method in case IllegalStateException is occurred. |
| 225 | */ |
| 226 | @Test(expected = IllegalStateException.class) |
| 227 | public void testIsAssociatedWithVMexceptionCase() { |
| 228 | OpenstackNetworkService service = new TestOpenstackNetworkService(); |
| 229 | isAssociatedWithVM(service, floatingIp2); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 234 | * Tests the getGwByInstancePort method. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testGetGwByInstancePort() { |
| 238 | |
| 239 | Set<OpenstackNode> gws = Sets.newConcurrentHashSet(); |
| 240 | gws.add(genGateway(1)); |
| 241 | gws.add(genGateway(2)); |
| 242 | gws.add(genGateway(3)); |
| 243 | |
| 244 | int expectedGwIndex = 2; |
| 245 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 246 | OpenstackNode gw = getGwByInstancePort(gws, instancePort1); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 247 | |
| 248 | assertEquals(genGateway(expectedGwIndex), gw); |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 249 | |
| 250 | assertNull(getGwByInstancePort(gws, null)); |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 251 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Tests the getGwByComputeDevId method. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testGetGwByComputeDevId() { |
| 258 | Set<OpenstackNode> gws = Sets.newConcurrentHashSet(); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 259 | |
| 260 | OpenstackNode nullGw = getGwByComputeDevId(gws, genDeviceId(1)); |
| 261 | assertNull(nullGw); |
| 262 | |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 263 | gws.add(genGateway(1)); |
| 264 | gws.add(genGateway(2)); |
| 265 | gws.add(genGateway(3)); |
| 266 | |
| 267 | Set<OpenstackNode> cloneOfGws = ImmutableSet.copyOf(gws); |
| 268 | |
| 269 | Map<String, Integer> gwCountMap = Maps.newConcurrentMap(); |
| 270 | int numOfDev = 99; |
| 271 | |
| 272 | for (int i = 1; i < 1 + numOfDev; i++) { |
| 273 | OpenstackNode gw = getGwByComputeDevId(gws, genDeviceId(i)); |
| 274 | |
| 275 | if (gwCountMap.get(gw.hostname()) == null) { |
| 276 | gwCountMap.put(gw.hostname(), 1); |
| 277 | } else { |
| 278 | gwCountMap.compute(gw.hostname(), (k, v) -> v + 1); |
| 279 | } |
| 280 | |
| 281 | new EqualsTester().addEqualityGroup( |
| 282 | getGwByComputeDevId(gws, genDeviceId(i)), |
| 283 | getGwByComputeDevId(cloneOfGws, genDeviceId(i))) |
| 284 | .testEquals(); |
| 285 | } |
| 286 | |
| 287 | int sum = gwCountMap.values().stream().mapToInt(Integer::intValue).sum(); |
| 288 | assertEquals(numOfDev, sum); |
| 289 | } |
| 290 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 291 | /** |
| 292 | * Tests getIntfNameFromPciAddress method. |
| 293 | */ |
| 294 | @Test |
| 295 | public void testGetIntfNameFromPciAddress() { |
| 296 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 297 | String expectedIntfName1 = "enp5s8"; |
| 298 | String expectedIntfName2 = "enp5s8f3"; |
| 299 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 300 | assertNull(getIntfNameFromPciAddress(openstackPort)); |
| 301 | assertEquals(expectedIntfName1, getIntfNameFromPciAddress(openstackSriovPort1)); |
| 302 | assertEquals(expectedIntfName2, getIntfNameFromPciAddress(openstackSriovPort2)); |
| 303 | assertNull(getIntfNameFromPciAddress(openstackSriovPort3)); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 307 | * Tests swapStaleLocation method. |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 308 | */ |
| 309 | @Test |
| 310 | public void testSwapStaleLocation() { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 311 | InstancePort swappedInstancePort = swapStaleLocation(instancePort3); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 312 | |
| 313 | assertEquals(instancePort3.oldDeviceId(), swappedInstancePort.deviceId()); |
| 314 | assertEquals(instancePort3.oldPortNumber(), swappedInstancePort.portNumber()); |
| 315 | |
| 316 | } |
| 317 | |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 318 | /** |
| 319 | * Tests hasIntfAleadyInDevice method. |
| 320 | */ |
| 321 | @Test |
| 322 | public void testHasIntfAleadyInDevice() { |
| 323 | DeviceService deviceService = new TestDeviceService(); |
| 324 | assertTrue(OpenstackNetworkingUtil.hasIntfAleadyInDevice(DeviceId.deviceId("deviceId"), |
| 325 | "port1", deviceService)); |
| 326 | assertTrue(OpenstackNetworkingUtil.hasIntfAleadyInDevice(DeviceId.deviceId("deviceId"), |
| 327 | "port2", deviceService)); |
| 328 | assertTrue(OpenstackNetworkingUtil.hasIntfAleadyInDevice(DeviceId.deviceId("deviceId"), |
| 329 | "port3", deviceService)); |
| 330 | assertFalse(OpenstackNetworkingUtil.hasIntfAleadyInDevice(DeviceId.deviceId("deviceId"), |
| 331 | "port4", deviceService)); |
| 332 | } |
| 333 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 334 | /** |
| 335 | * Tests addRouterIface method. |
| 336 | */ |
| 337 | @Test |
| 338 | public void testAddRouterIface() { |
| 339 | OpenstackRouterAdminService service = new TestOpenstackRouterAdminService(); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 340 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 341 | addRouterIface(openstackPort, service); |
| 342 | RouterInterface initialRouterInterface = new TestRouterInterface(openstackPort.getDeviceId(), |
| 343 | openstackPort.getFixedIps().stream().findAny().get().getSubnetId(), |
| 344 | openstackPort.getId(), |
| 345 | openstackPort.getTenantId()); |
| 346 | |
| 347 | assertTrue(routerInterfacesEquals(initialRouterInterface, service.routerInterface(openstackPort.getId()))); |
| 348 | |
| 349 | addRouterIface(openstackSriovPort1, service); |
| 350 | RouterInterface updatedInitialRouterInterface = new TestRouterInterface(openstackSriovPort1.getDeviceId(), |
| 351 | openstackSriovPort1.getFixedIps().stream().findAny().get().getSubnetId(), |
| 352 | openstackSriovPort1.getId(), |
| 353 | openstackSriovPort1.getTenantId()); |
| 354 | |
| 355 | assertTrue(routerInterfacesEquals( |
| 356 | updatedInitialRouterInterface, service.routerInterface(openstackSriovPort1.getId()))); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Util for generating dummy gateway node. |
| 361 | * |
| 362 | * @param index dummy gateway number |
| 363 | * @return dummy gateway node |
| 364 | */ |
| 365 | public OpenstackNode genGateway(int index) { |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 366 | Device intgBrg = InternalOpenstackNodeTest.createDevice(index); |
| 367 | |
| 368 | String hostname = "gateway-" + index; |
| 369 | IpAddress ip = Ip4Address.valueOf("10.10.10." + index); |
| 370 | NodeState state = NodeState.COMPLETE; |
| 371 | String uplinkPort = "eth0"; |
| 372 | return InternalOpenstackNodeTest.createNode(hostname, |
| 373 | OpenstackNode.NodeType.GATEWAY, intgBrg, ip, uplinkPort, state); |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Tests the testPrettyJson method. |
| 378 | */ |
| 379 | @Test |
| 380 | public void testPrettyJson() { |
| 381 | String string = prettyJson(new ObjectMapper(), "{\"json\":\"json\"}"); |
| 382 | String prettyJsonString = "{\n \"json\" : \"json\"\n}"; |
| 383 | assertEquals(string, prettyJsonString); |
| 384 | |
| 385 | assertNull(prettyJson(new ObjectMapper(), "{\"json\":\"json\"")); |
| 386 | assertNull(prettyJson(new ObjectMapper(), "{\"json\"\"json\"}")); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Tests the checkArpMode method. |
| 391 | */ |
| 392 | @Test |
| 393 | public void testCheckArpMode() { |
| 394 | assertFalse(checkArpMode(null)); |
| 395 | assertTrue(checkArpMode("proxy")); |
| 396 | assertTrue(checkArpMode("broadcast")); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Tests the getConnectedClient method. |
| 401 | */ |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 402 | @Test |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 403 | public void testGetConnectedClient() { |
| 404 | OpenstackNode.Builder osNodeBuilderV2 = DefaultOpenstackNode.builder(); |
| 405 | OpenstackAuth.Builder osNodeAuthBuilderV2 = DefaultOpenstackAuth.builder() |
| 406 | .version("v2.0") |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 407 | .protocol(OpenstackAuth.Protocol.HTTP) |
| 408 | .project("admin") |
| 409 | .username("admin") |
| 410 | .password("password") |
| 411 | .perspective(OpenstackAuth.Perspective.PUBLIC); |
| 412 | |
| 413 | openstackControlNodeV2 = osNodeBuilderV2.hostname("controllerv2") |
| 414 | .type(OpenstackNode.NodeType.CONTROLLER) |
| 415 | .managementIp(IpAddress.valueOf("1.1.1.1")) |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 416 | .endpoint("1.1.1.1") |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 417 | .authentication(osNodeAuthBuilderV2.build()) |
| 418 | .state(NodeState.COMPLETE) |
| 419 | .build(); |
| 420 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 421 | OpenstackNode.Builder osNodeBuilderV3 = DefaultOpenstackNode.builder(); |
| 422 | OpenstackAuth.Builder osNodeAuthBuilderV3 = DefaultOpenstackAuth.builder() |
| 423 | .version("v2") |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 424 | .protocol(OpenstackAuth.Protocol.HTTP) |
| 425 | .project("admin") |
| 426 | .username("admin") |
| 427 | .password("password") |
| 428 | .perspective(OpenstackAuth.Perspective.PUBLIC); |
| 429 | |
| 430 | openstackControlNodeV3 = osNodeBuilderV3.hostname("controllerv3") |
| 431 | .type(OpenstackNode.NodeType.CONTROLLER) |
| 432 | .managementIp(IpAddress.valueOf("2.2.2.2")) |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 433 | .endpoint("2.2.2.2") |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 434 | .authentication(osNodeAuthBuilderV3.build()) |
| 435 | .state(NodeState.COMPLETE) |
| 436 | .build(); |
| 437 | |
| 438 | getConnectedClient(openstackControlNodeV2); |
| 439 | getConnectedClient(openstackControlNodeV3); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 440 | |
| 441 | } |
| 442 | |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame^] | 443 | /** |
| 444 | * Tests the vnicType method. |
| 445 | */ |
| 446 | @Test |
| 447 | public void testVnicType() { |
| 448 | String portNameNormalTap = "tap123456789ab"; |
| 449 | String portNameNormalVhu = "tap123456789ab"; |
| 450 | String portNameNormalCavium = "enp1f2s3"; |
| 451 | String portNameUnsupported = "123456789ab"; |
| 452 | |
| 453 | assertEquals(vnicType(portNameNormalTap), Constants.VnicType.NORMAL); |
| 454 | assertEquals(vnicType(portNameNormalVhu), Constants.VnicType.NORMAL); |
| 455 | assertEquals(vnicType(portNameNormalCavium), Constants.VnicType.DIRECT); |
| 456 | assertEquals(vnicType(portNameUnsupported), Constants.VnicType.UNSUPPORTED); |
| 457 | } |
| 458 | |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 459 | private DeviceId genDeviceId(int index) { |
| 460 | return DeviceId.deviceId("of:compute-" + index); |
| 461 | } |
| 462 | |
| 463 | protected class InternalOpenstackNodeTest extends OpenstackNodeTest { |
| 464 | } |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 465 | |
| 466 | /** |
| 467 | * Mocks the DeviceService. |
| 468 | */ |
| 469 | private class TestDeviceService extends DeviceServiceAdapter { |
| 470 | @Override |
| 471 | public List<org.onosproject.net.Port> getPorts(DeviceId deviceId) { |
| 472 | List<org.onosproject.net.Port> ports = Lists.newArrayList(); |
| 473 | DefaultAnnotations.Builder annotations1 = DefaultAnnotations.builder() |
| 474 | .set(PORT_NAME, "port1"); |
| 475 | DefaultAnnotations.Builder annotations2 = DefaultAnnotations.builder() |
| 476 | .set(PORT_NAME, "port2"); |
| 477 | DefaultAnnotations.Builder annotations3 = DefaultAnnotations.builder() |
| 478 | .set(PORT_NAME, "port3"); |
| 479 | |
| 480 | org.onosproject.net.Port port1 = new DefaultPort(null, PortNumber.portNumber(1), |
| 481 | true, annotations1.build()); |
| 482 | org.onosproject.net.Port port2 = new DefaultPort(null, PortNumber.portNumber(2), |
| 483 | true, annotations2.build()); |
| 484 | org.onosproject.net.Port port3 = new DefaultPort(null, PortNumber.portNumber(3), |
| 485 | true, annotations3.build()); |
| 486 | |
| 487 | ports.add(port1); |
| 488 | ports.add(port2); |
| 489 | ports.add(port3); |
| 490 | |
| 491 | return ports; |
| 492 | } |
| 493 | } |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 494 | |
| 495 | private class TestOpenstackNetworkService extends OpenstackNetworkServiceAdapter { |
| 496 | @Override |
| 497 | public Network network(String networkId) { |
| 498 | if (networkId.equals(openstackSriovPort1.getNetworkId())) { |
| 499 | return new NeutronNetwork().toBuilder().name("network").build(); |
| 500 | } else { |
| 501 | return null; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public Port port(String portId) { |
| 507 | if (portId.equals(floatingIp1.getPortId())) { |
| 508 | return openstackSriovPort1; |
| 509 | } else if (portId.equals(floatingIp2.getPortId())) { |
| 510 | return openstackSriovPort2; |
| 511 | } else if (portId.equals("portId4")) { |
| 512 | return new NeutronPort().toBuilder().name("osPort4").build(); |
| 513 | } else { |
| 514 | return null; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | private class TestOpenstackRouterAdminService extends OpenstackRouterServiceAdapter { |
| 520 | @Override |
| 521 | public void addRouterInterface(RouterInterface osRouterIface) { |
| 522 | routerInterfaceMap.put(osRouterIface.getPortId(), osRouterIface); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public void updateRouterInterface(RouterInterface osRouterIface) { |
| 527 | routerInterfaceMap.put(osRouterIface.getPortId(), osRouterIface); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public RouterInterface routerInterface(String osRouterIfaceId) { |
| 532 | return routerInterfaceMap.get(osRouterIfaceId); |
| 533 | } |
| 534 | } |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 535 | } |