blob: e33637ec670e615c5d1a1beeaaa4e3dab404c69f [file] [log] [blame]
Jian Lidea0fdb2018-04-02 19:02:48 +09001/*
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 */
16package org.onosproject.openstacknetworking.util;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li1064e4f2018-05-29 16:16:53 +090019import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
Jian Lidea0fdb2018-04-02 19:02:48 +090022import com.google.common.testing.EqualsTester;
23import org.apache.commons.io.IOUtils;
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
Jian Li1064e4f2018-05-29 16:16:53 +090027import org.onlab.packet.Ip4Address;
28import org.onlab.packet.IpAddress;
29import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.openstacknode.api.NodeState;
32import org.onosproject.openstacknode.api.OpenstackNode;
33import org.onosproject.openstacknode.impl.OpenstackNodeTest;
Jian Lidea0fdb2018-04-02 19:02:48 +090034import org.openstack4j.model.network.NetFloatingIP;
35import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
36
37import java.io.IOException;
38import java.io.InputStream;
39import java.nio.charset.StandardCharsets;
Jian Li1064e4f2018-05-29 16:16:53 +090040import java.util.Map;
41import java.util.Set;
42
43import static org.junit.Assert.assertEquals;
44import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getGwByComputeDevId;
Jian Lidea0fdb2018-04-02 19:02:48 +090045
46public 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 Li1064e4f2018-05-29 16:16:53 +090074
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 Lidea0fdb2018-04-02 19:02:48 +0900128}