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