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 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 22 | import com.google.common.testing.EqualsTester; |
| 23 | import org.apache.commons.io.IOUtils; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
| 28 | import org.onlab.packet.IpAddress; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 29 | import org.onlab.packet.MacAddress; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 34 | import org.onosproject.openstacknetworking.impl.DefaultInstancePort; |
| 35 | import org.onosproject.openstacknetworking.web.OpenstackFloatingIpWebResourceTest; |
| 36 | import org.onosproject.openstacknetworking.web.OpenstackNetworkWebResourceTest; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 37 | import org.onosproject.openstacknode.api.NodeState; |
| 38 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | 5a38ab6 | 2018-07-02 22:34:11 +0900 | [diff] [blame] | 39 | import org.onosproject.openstacknode.api.OpenstackNodeTest; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 40 | import org.openstack4j.model.network.NetFloatingIP; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 41 | import org.openstack4j.model.network.Port; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 42 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 43 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 44 | |
| 45 | import java.io.IOException; |
| 46 | import java.io.InputStream; |
| 47 | import java.nio.charset.StandardCharsets; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 48 | import java.util.Map; |
| 49 | import java.util.Set; |
| 50 | |
| 51 | import static org.junit.Assert.assertEquals; |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 52 | import static org.junit.Assert.assertNull; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 53 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByComputeDevId; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 54 | |
| 55 | public final class OpenstackNetworkingUtilTest { |
| 56 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 57 | private NetFloatingIP floatingIp1; |
| 58 | private NetFloatingIP floatingIp2; |
| 59 | private NetFloatingIP floatingIp3; |
| 60 | private Port openstackPort; |
| 61 | private Port openstackSriovPort1; |
| 62 | private Port openstackSriovPort2; |
| 63 | private Port openstackSriovPort3; |
| 64 | private InstancePort instancePort1; |
| 65 | private InstancePort instancePort2; |
| 66 | private InstancePort instancePort3; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 67 | |
| 68 | @Before |
| 69 | public void setUp() { |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 70 | |
| 71 | instancePort1 = DefaultInstancePort.builder() |
| 72 | .networkId("net-id-1") |
| 73 | .portId("ce705c24-c1ef-408a-bda3-7bbd946164ab") |
| 74 | .deviceId(DeviceId.deviceId("of:000000000000000a")) |
| 75 | .portNumber(PortNumber.portNumber(1, "tap-1")) |
| 76 | .ipAddress(IpAddress.valueOf("10.0.0.3")) |
| 77 | .macAddress(MacAddress.valueOf("11:22:33:44:55:66")) |
| 78 | .state(InstancePort.State.valueOf("ACTIVE")) |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 79 | .build(); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 80 | |
| 81 | instancePort2 = DefaultInstancePort.builder() |
| 82 | .networkId("net-id-2") |
| 83 | .portId("port-id-2") |
| 84 | .deviceId(DeviceId.deviceId("of:000000000000000b")) |
| 85 | .portNumber(PortNumber.portNumber(2, "tap-2")) |
| 86 | .ipAddress(IpAddress.valueOf("10.10.10.2")) |
| 87 | .macAddress(MacAddress.valueOf("22:33:44:55:66:11")) |
| 88 | .state(InstancePort.State.valueOf("ACTIVE")) |
| 89 | .build(); |
| 90 | |
| 91 | instancePort3 = DefaultInstancePort.builder() |
| 92 | .networkId("net-id-3") |
| 93 | .portId("port-id-3") |
| 94 | .deviceId(DeviceId.deviceId("of:000000000000000c")) |
| 95 | .oldDeviceId(DeviceId.deviceId("of:000000000000000d")) |
| 96 | .oldPortNumber(PortNumber.portNumber(4, "tap-4")) |
| 97 | .portNumber(PortNumber.portNumber(3, "tap-3")) |
| 98 | .ipAddress(IpAddress.valueOf("10.10.10.3")) |
| 99 | .macAddress(MacAddress.valueOf("33:44:55:66:11:22")) |
| 100 | .state(InstancePort.State.valueOf("ACTIVE")) |
| 101 | .build(); |
| 102 | |
| 103 | InputStream floatingIpjsonStream1 = OpenstackFloatingIpWebResourceTest.class |
| 104 | .getResourceAsStream("openstack-floatingip1.json"); |
| 105 | InputStream floatingIpjsonStream2 = OpenstackFloatingIpWebResourceTest.class |
| 106 | .getResourceAsStream("openstack-floatingip2.json"); |
| 107 | InputStream floatingIpjsonStream3 = OpenstackFloatingIpWebResourceTest.class |
| 108 | .getResourceAsStream("openstack-floatingip3.json"); |
| 109 | |
| 110 | floatingIp1 = (NetFloatingIP) |
| 111 | OpenstackNetworkingUtil.jsonToModelEntity(floatingIpjsonStream1, NeutronFloatingIP.class); |
| 112 | floatingIp2 = (NetFloatingIP) |
| 113 | OpenstackNetworkingUtil.jsonToModelEntity(floatingIpjsonStream2, NeutronFloatingIP.class); |
| 114 | floatingIp3 = (NetFloatingIP) |
| 115 | OpenstackNetworkingUtil.jsonToModelEntity(floatingIpjsonStream3, NeutronFloatingIP.class); |
| 116 | |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @After |
| 120 | public void tearDown() { |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Tests the floatingIp translation. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testFloatingIp() throws IOException { |
| 128 | ObjectNode floatingIpNode = |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 129 | OpenstackNetworkingUtil.modelEntityToJson(floatingIp1, NeutronFloatingIP.class); |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 130 | InputStream is = IOUtils.toInputStream(floatingIpNode.toString(), StandardCharsets.UTF_8.name()); |
| 131 | NetFloatingIP floatingIp2 = (NetFloatingIP) |
| 132 | OpenstackNetworkingUtil.jsonToModelEntity(is, NeutronFloatingIP.class); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 133 | new EqualsTester().addEqualityGroup(floatingIp1, floatingIp2).testEquals(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tests the associatedFloatingIp method. |
| 138 | * @throws NullPointerException |
| 139 | */ |
| 140 | @Test |
| 141 | public void testAsscoatedFloatingIp() throws NullPointerException { |
| 142 | Set<NetFloatingIP> testSet = Sets.newHashSet(); |
| 143 | testSet.add(floatingIp1); |
| 144 | testSet.add(floatingIp2); |
| 145 | testSet.add(floatingIp3); |
| 146 | |
| 147 | NetFloatingIP floatingIp1 = OpenstackNetworkingUtil.associatedFloatingIp(instancePort1, testSet); |
| 148 | NetFloatingIP floatingIp2 = OpenstackNetworkingUtil.associatedFloatingIp(instancePort2, testSet); |
| 149 | |
| 150 | assertEquals(floatingIp1, this.floatingIp1); |
| 151 | assertEquals(floatingIp2, null); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Tests the getGwByInstancePort method. |
| 156 | */ |
| 157 | @Test |
| 158 | public void testGetGwByInstancePort() { |
| 159 | |
| 160 | Set<OpenstackNode> gws = Sets.newConcurrentHashSet(); |
| 161 | gws.add(genGateway(1)); |
| 162 | gws.add(genGateway(2)); |
| 163 | gws.add(genGateway(3)); |
| 164 | |
| 165 | int expectedGwIndex = 2; |
| 166 | |
| 167 | OpenstackNode gw = OpenstackNetworkingUtil.getGwByInstancePort(gws, instancePort1); |
| 168 | |
| 169 | assertEquals(genGateway(expectedGwIndex), gw); |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 170 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Tests the getGwByComputeDevId method. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testGetGwByComputeDevId() { |
| 177 | Set<OpenstackNode> gws = Sets.newConcurrentHashSet(); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 178 | |
| 179 | OpenstackNode nullGw = getGwByComputeDevId(gws, genDeviceId(1)); |
| 180 | assertNull(nullGw); |
| 181 | |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 182 | gws.add(genGateway(1)); |
| 183 | gws.add(genGateway(2)); |
| 184 | gws.add(genGateway(3)); |
| 185 | |
| 186 | Set<OpenstackNode> cloneOfGws = ImmutableSet.copyOf(gws); |
| 187 | |
| 188 | Map<String, Integer> gwCountMap = Maps.newConcurrentMap(); |
| 189 | int numOfDev = 99; |
| 190 | |
| 191 | for (int i = 1; i < 1 + numOfDev; i++) { |
| 192 | OpenstackNode gw = getGwByComputeDevId(gws, genDeviceId(i)); |
| 193 | |
| 194 | if (gwCountMap.get(gw.hostname()) == null) { |
| 195 | gwCountMap.put(gw.hostname(), 1); |
| 196 | } else { |
| 197 | gwCountMap.compute(gw.hostname(), (k, v) -> v + 1); |
| 198 | } |
| 199 | |
| 200 | new EqualsTester().addEqualityGroup( |
| 201 | getGwByComputeDevId(gws, genDeviceId(i)), |
| 202 | getGwByComputeDevId(cloneOfGws, genDeviceId(i))) |
| 203 | .testEquals(); |
| 204 | } |
| 205 | |
| 206 | int sum = gwCountMap.values().stream().mapToInt(Integer::intValue).sum(); |
| 207 | assertEquals(numOfDev, sum); |
| 208 | } |
| 209 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame^] | 210 | /** |
| 211 | * Tests getIntfNameFromPciAddress method. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testGetIntfNameFromPciAddress() { |
| 215 | |
| 216 | InputStream portJsonStream = OpenstackNetworkWebResourceTest.class |
| 217 | .getResourceAsStream("openstack-port.json"); |
| 218 | |
| 219 | InputStream sriovPortJsonStream1 = OpenstackNetworkWebResourceTest.class |
| 220 | .getResourceAsStream("openstack-port-sriov1.json"); |
| 221 | InputStream sriovPortJsonStream2 = OpenstackNetworkWebResourceTest.class |
| 222 | .getResourceAsStream("openstack-port-sriov2.json"); |
| 223 | InputStream sriovPortJsonStream3 = OpenstackNetworkWebResourceTest.class |
| 224 | .getResourceAsStream("openstack-port-sriov3.json"); |
| 225 | |
| 226 | openstackPort = (Port) |
| 227 | OpenstackNetworkingUtil.jsonToModelEntity(portJsonStream, NeutronPort.class); |
| 228 | |
| 229 | openstackSriovPort1 = (Port) |
| 230 | OpenstackNetworkingUtil.jsonToModelEntity(sriovPortJsonStream1, NeutronPort.class); |
| 231 | openstackSriovPort2 = (Port) |
| 232 | OpenstackNetworkingUtil.jsonToModelEntity(sriovPortJsonStream2, NeutronPort.class); |
| 233 | openstackSriovPort3 = (Port) |
| 234 | OpenstackNetworkingUtil.jsonToModelEntity(sriovPortJsonStream3, NeutronPort.class); |
| 235 | |
| 236 | String expectedIntfName1 = "enp5s8"; |
| 237 | String expectedIntfName2 = "enp5s8f3"; |
| 238 | |
| 239 | assertNull(OpenstackNetworkingUtil.getIntfNameFromPciAddress(openstackPort)); |
| 240 | assertEquals(expectedIntfName1, OpenstackNetworkingUtil.getIntfNameFromPciAddress(openstackSriovPort1)); |
| 241 | assertEquals(expectedIntfName2, OpenstackNetworkingUtil.getIntfNameFromPciAddress(openstackSriovPort2)); |
| 242 | assertNull(OpenstackNetworkingUtil.getIntfNameFromPciAddress(openstackSriovPort3)); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * tests swapStaleLocation method. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testSwapStaleLocation() { |
| 250 | InstancePort swappedInstancePort = OpenstackNetworkingUtil.swapStaleLocation(instancePort3); |
| 251 | |
| 252 | assertEquals(instancePort3.oldDeviceId(), swappedInstancePort.deviceId()); |
| 253 | assertEquals(instancePort3.oldPortNumber(), swappedInstancePort.portNumber()); |
| 254 | |
| 255 | } |
| 256 | |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 257 | private OpenstackNode genGateway(int index) { |
| 258 | |
| 259 | Device intgBrg = InternalOpenstackNodeTest.createDevice(index); |
| 260 | |
| 261 | String hostname = "gateway-" + index; |
| 262 | IpAddress ip = Ip4Address.valueOf("10.10.10." + index); |
| 263 | NodeState state = NodeState.COMPLETE; |
| 264 | String uplinkPort = "eth0"; |
| 265 | return InternalOpenstackNodeTest.createNode(hostname, |
| 266 | OpenstackNode.NodeType.GATEWAY, intgBrg, ip, uplinkPort, state); |
| 267 | |
| 268 | } |
| 269 | |
| 270 | private DeviceId genDeviceId(int index) { |
| 271 | return DeviceId.deviceId("of:compute-" + index); |
| 272 | } |
| 273 | |
| 274 | protected class InternalOpenstackNodeTest extends OpenstackNodeTest { |
| 275 | } |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 276 | } |