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; |
| 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.openstacknode.api.NodeState; |
| 32 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | 5a38ab6 | 2018-07-02 22:34:11 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknode.api.OpenstackNodeTest; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 34 | import org.openstack4j.model.network.NetFloatingIP; |
| 35 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.nio.charset.StandardCharsets; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 40 | import java.util.Map; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | import static org.junit.Assert.assertEquals; |
| 44 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByComputeDevId; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 45 | |
| 46 | public final class OpenstackNetworkingUtilTest { |
| 47 | |
| 48 | private NetFloatingIP floatingIp; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() { |
| 52 | floatingIp = NeutronFloatingIP.builder() |
| 53 | .floatingNetworkId("floatingNetworkingId") |
| 54 | .portId("portId") |
| 55 | .build(); |
| 56 | } |
| 57 | |
| 58 | @After |
| 59 | public void tearDown() { |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Tests the floatingIp translation. |
| 64 | */ |
| 65 | @Test |
| 66 | public void testFloatingIp() throws IOException { |
| 67 | ObjectNode floatingIpNode = |
| 68 | OpenstackNetworkingUtil.modelEntityToJson(floatingIp, NeutronFloatingIP.class); |
| 69 | InputStream is = IOUtils.toInputStream(floatingIpNode.toString(), StandardCharsets.UTF_8.name()); |
| 70 | NetFloatingIP floatingIp2 = (NetFloatingIP) |
| 71 | OpenstackNetworkingUtil.jsonToModelEntity(is, NeutronFloatingIP.class); |
| 72 | new EqualsTester().addEqualityGroup(floatingIp, floatingIp2).testEquals(); |
| 73 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Tests the getGwByComputeDevId method. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testGetGwByComputeDevId() { |
| 80 | Set<OpenstackNode> gws = Sets.newConcurrentHashSet(); |
| 81 | gws.add(genGateway(1)); |
| 82 | gws.add(genGateway(2)); |
| 83 | gws.add(genGateway(3)); |
| 84 | |
| 85 | Set<OpenstackNode> cloneOfGws = ImmutableSet.copyOf(gws); |
| 86 | |
| 87 | Map<String, Integer> gwCountMap = Maps.newConcurrentMap(); |
| 88 | int numOfDev = 99; |
| 89 | |
| 90 | for (int i = 1; i < 1 + numOfDev; i++) { |
| 91 | OpenstackNode gw = getGwByComputeDevId(gws, genDeviceId(i)); |
| 92 | |
| 93 | if (gwCountMap.get(gw.hostname()) == null) { |
| 94 | gwCountMap.put(gw.hostname(), 1); |
| 95 | } else { |
| 96 | gwCountMap.compute(gw.hostname(), (k, v) -> v + 1); |
| 97 | } |
| 98 | |
| 99 | new EqualsTester().addEqualityGroup( |
| 100 | getGwByComputeDevId(gws, genDeviceId(i)), |
| 101 | getGwByComputeDevId(cloneOfGws, genDeviceId(i))) |
| 102 | .testEquals(); |
| 103 | } |
| 104 | |
| 105 | int sum = gwCountMap.values().stream().mapToInt(Integer::intValue).sum(); |
| 106 | assertEquals(numOfDev, sum); |
| 107 | } |
| 108 | |
| 109 | private OpenstackNode genGateway(int index) { |
| 110 | |
| 111 | Device intgBrg = InternalOpenstackNodeTest.createDevice(index); |
| 112 | |
| 113 | String hostname = "gateway-" + index; |
| 114 | IpAddress ip = Ip4Address.valueOf("10.10.10." + index); |
| 115 | NodeState state = NodeState.COMPLETE; |
| 116 | String uplinkPort = "eth0"; |
| 117 | return InternalOpenstackNodeTest.createNode(hostname, |
| 118 | OpenstackNode.NodeType.GATEWAY, intgBrg, ip, uplinkPort, state); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | private DeviceId genDeviceId(int index) { |
| 123 | return DeviceId.deviceId("of:compute-" + index); |
| 124 | } |
| 125 | |
| 126 | protected class InternalOpenstackNodeTest extends OpenstackNodeTest { |
| 127 | } |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 128 | } |