Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 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 | */ |
Ray Milkey | 5d08e1e | 2016-11-02 13:02:12 +0100 | [diff] [blame] | 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.util.Set; |
| 21 | import java.util.stream.IntStream; |
| 22 | |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.incubator.net.virtual.DefaultVirtualHost; |
| 29 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 30 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.HostId; |
| 33 | import org.onosproject.net.HostLocation; |
| 34 | import org.onosproject.net.NetTestTools; |
| 35 | |
| 36 | import com.fasterxml.jackson.databind.JsonNode; |
| 37 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 38 | import com.google.common.collect.ImmutableSet; |
| 39 | |
| 40 | import static org.hamcrest.MatcherAssert.assertThat; |
| 41 | import static org.hamcrest.Matchers.is; |
| 42 | import static org.hamcrest.Matchers.isOneOf; |
| 43 | import static org.hamcrest.Matchers.notNullValue; |
| 44 | |
| 45 | /** |
| 46 | * Tests VirtualHostCodec class. |
| 47 | */ |
| 48 | |
| 49 | public class VirtualHostCodecTest { |
| 50 | |
| 51 | private static final String TEST_IP1 = "1.1.1.1"; |
| 52 | private static final String TEST_IP2 = "2.2.2.2"; |
| 53 | private static final String TEST_HOST_ID = "12:34:56:78:90:11/1"; |
| 54 | private static final String TEST_MAC_ADDRESS = "11:11:22:22:33:33"; |
| 55 | private static final long TEST_NETWORK_ID = 44L; |
| 56 | private static final short TEST_VLAN_ID = (short) 12; |
| 57 | private static final ConnectPoint CONNECT_POINT = |
| 58 | NetTestTools.connectPoint("d1", 1); |
| 59 | |
| 60 | @Test |
| 61 | public void testEncode() { |
| 62 | MockCodecContext context = new MockCodecContext(); |
| 63 | NetworkId networkId = NetworkId.networkId(TEST_NETWORK_ID); |
| 64 | HostId id = NetTestTools.hid(TEST_HOST_ID); |
| 65 | MacAddress mac = MacAddress.valueOf(TEST_MAC_ADDRESS); |
| 66 | VlanId vlan = VlanId.vlanId(TEST_VLAN_ID); |
| 67 | HostLocation location = |
| 68 | new HostLocation(CONNECT_POINT, 0L); |
| 69 | Set<IpAddress> ips = ImmutableSet.of(IpAddress.valueOf(TEST_IP1), |
| 70 | IpAddress.valueOf(TEST_IP2)); |
| 71 | VirtualHost host = |
| 72 | new DefaultVirtualHost(networkId, id, mac, vlan, location, ips); |
| 73 | JsonCodec<VirtualHost> codec = context.codec(VirtualHost.class); |
| 74 | ObjectNode node = codec.encode(host, context); |
| 75 | |
Ray Milkey | 5d08e1e | 2016-11-02 13:02:12 +0100 | [diff] [blame] | 76 | assertThat(node.get(VirtualHostCodec.NETWORK_ID).asLong(), |
| 77 | is(TEST_NETWORK_ID)); |
| 78 | assertThat(node.get(VirtualHostCodec.HOST_ID).asText(), |
| 79 | is(TEST_HOST_ID)); |
| 80 | assertThat(node.get(VirtualHostCodec.MAC_ADDRESS).asText(), |
| 81 | is(TEST_MAC_ADDRESS)); |
| 82 | assertThat(node.get(VirtualHostCodec.VLAN).asInt(), |
| 83 | is((int) TEST_VLAN_ID)); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 84 | assertThat(node.get(VirtualHostCodec.HOST_LOCATION).get(0).get("elementId").asText(), |
Ray Milkey | 5d08e1e | 2016-11-02 13:02:12 +0100 | [diff] [blame] | 85 | is(location.deviceId().toString())); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 86 | assertThat(node.get(VirtualHostCodec.HOST_LOCATION).get(0).get("port").asLong(), |
Ray Milkey | 5d08e1e | 2016-11-02 13:02:12 +0100 | [diff] [blame] | 87 | is(location.port().toLong())); |
| 88 | |
| 89 | JsonNode jsonIps = node.get(VirtualHostCodec.IP_ADDRESSES); |
| 90 | assertThat(jsonIps, notNullValue()); |
| 91 | assertThat(jsonIps.isArray(), is(true)); |
| 92 | assertThat(jsonIps.size(), is(ips.size())); |
| 93 | |
| 94 | IntStream.of(0, 1).forEach(index -> |
| 95 | assertThat(jsonIps.get(index).asText(), |
| 96 | isOneOf(TEST_IP1, TEST_IP2))); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testDecode() throws IOException { |
| 101 | MockCodecContext context = new MockCodecContext(); |
| 102 | InputStream jsonStream = |
| 103 | VirtualHostCodecTest.class.getResourceAsStream("VirtualHost.json"); |
| 104 | JsonNode json = context.mapper().readTree(jsonStream); |
| 105 | assertThat(json, notNullValue()); |
| 106 | JsonCodec<VirtualHost> codec = context.codec(VirtualHost.class); |
| 107 | VirtualHost virtualHost = codec.decode((ObjectNode) json, context); |
| 108 | assertThat(virtualHost, notNullValue()); |
| 109 | |
| 110 | assertThat(virtualHost.networkId().id(), |
| 111 | is(TEST_NETWORK_ID)); |
| 112 | assertThat(virtualHost.id().toString(), |
| 113 | is(NetTestTools.hid(TEST_MAC_ADDRESS + "/12").toString())); |
| 114 | assertThat(virtualHost.mac().toString(), |
| 115 | is(TEST_MAC_ADDRESS)); |
| 116 | assertThat(virtualHost.vlan().id(), |
| 117 | is((short) TEST_VLAN_ID)); |
| 118 | assertThat(virtualHost.location().deviceId(), |
| 119 | is(CONNECT_POINT.deviceId())); |
| 120 | assertThat(virtualHost.location().port().toLong(), |
| 121 | is(CONNECT_POINT.port().toLong())); |
| 122 | |
| 123 | |
| 124 | assertThat(virtualHost.ipAddresses().contains(IpAddress.valueOf(TEST_IP1)), |
| 125 | is(true)); |
| 126 | assertThat(virtualHost.ipAddresses().contains(IpAddress.valueOf(TEST_IP2)), |
| 127 | is(true)); |
| 128 | } |
| 129 | } |