blob: 9cc26d424d3a090b3b867e1463cf45c9606da90c [file] [log] [blame]
Ray Milkey85267002016-11-16 11:06:35 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey85267002016-11-16 11:06:35 -08003 *
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 Milkey5d08e1e2016-11-02 13:02:12 +010016package org.onosproject.codec.impl;
17
18import java.io.IOException;
19import java.io.InputStream;
20import java.util.Set;
21import java.util.stream.IntStream;
22
23import org.junit.Test;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.incubator.net.virtual.DefaultVirtualHost;
29import org.onosproject.incubator.net.virtual.NetworkId;
30import org.onosproject.incubator.net.virtual.VirtualHost;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.HostId;
33import org.onosproject.net.HostLocation;
34import org.onosproject.net.NetTestTools;
35
36import com.fasterxml.jackson.databind.JsonNode;
37import com.fasterxml.jackson.databind.node.ObjectNode;
38import com.google.common.collect.ImmutableSet;
39
40import static org.hamcrest.MatcherAssert.assertThat;
41import static org.hamcrest.Matchers.is;
42import static org.hamcrest.Matchers.isOneOf;
43import static org.hamcrest.Matchers.notNullValue;
44
45/**
46 * Tests VirtualHostCodec class.
47 */
48
49public 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 Milkey5d08e1e2016-11-02 13:02:12 +010076 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 Chancd06c692017-04-27 20:46:06 -070084 assertThat(node.get(VirtualHostCodec.HOST_LOCATION).get(0).get("elementId").asText(),
Ray Milkey5d08e1e2016-11-02 13:02:12 +010085 is(location.deviceId().toString()));
Charles Chancd06c692017-04-27 20:46:06 -070086 assertThat(node.get(VirtualHostCodec.HOST_LOCATION).get(0).get("port").asLong(),
Ray Milkey5d08e1e2016-11-02 13:02:12 +010087 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}