blob: 4edbc70428440f14550c84084e5d30c80b1a6aec [file] [log] [blame]
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -08001/*
2 * Copyright 2015-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.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.Test;
21import org.onlab.packet.EthType;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.net.Annotations;
27import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.DefaultHost;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Host;
31import org.onosproject.net.HostId;
32import org.onosproject.net.HostLocation;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.provider.ProviderId;
35
36import java.io.IOException;
37import java.io.InputStream;
38import java.util.HashSet;
39import java.util.Set;
40
41import static org.junit.Assert.assertEquals;
42import static org.junit.Assert.assertNotNull;
43import static org.onosproject.codec.impl.JsonCodecUtils.assertJsonEncodable;
44
45/**
46 * Unit test for HostCodec.
47 */
48public class HostCodecTest {
49
50 // Make sure this Host and the corresponding JSON file have the same values
51 private static final MacAddress MAC;
52 private static final VlanId VLAN_ID;
53 private static final HostId HOST_ID;
54 private static final DeviceId DEVICE_ID;
55 private static final PortNumber PORT_NUM;
56 private static final Set<HostLocation> HOST_LOCATIONS = new HashSet<>();
57 private static final PortNumber AUX_PORT_NUM;
58 private static final Set<HostLocation> AUX_HOST_LOCATIONS = new HashSet<>();
59 private static final Set<IpAddress> IPS;
60 private static final VlanId INNER_VLAN_ID;
61 private static final EthType OUTER_TPID;
62 private static final Annotations ANNOTATIONS;
63 private static final Host HOST;
64
65 private MockCodecContext context = new MockCodecContext();
66 private JsonCodec<Host> hostCodec = context.codec(Host.class);
67
68 private static final String JSON_FILE = "simple-host.json";
69
70 static {
71 // Make sure these members have same values with the corresponding JSON fields
72 MAC = MacAddress.valueOf("46:E4:3C:A4:17:C8");
73 VLAN_ID = VlanId.vlanId("None");
74 HOST_ID = HostId.hostId(MAC, VLAN_ID);
75 DEVICE_ID = DeviceId.deviceId("of:0000000000000002");
76 PORT_NUM = PortNumber.portNumber("3");
77 HOST_LOCATIONS.add(new HostLocation(DEVICE_ID, PORT_NUM, 0));
78 AUX_PORT_NUM = PortNumber.portNumber("4");
79 AUX_HOST_LOCATIONS.add(new HostLocation(DEVICE_ID, AUX_PORT_NUM, 0));
80 IPS = new HashSet<>();
81 IPS.add(IpAddress.valueOf("127.0.0.1"));
82 INNER_VLAN_ID = VlanId.vlanId("10");
83 OUTER_TPID = EthType.EtherType.lookup((short) (Integer.decode("0x88a8") & 0xFFFF)).ethType();
84 ANNOTATIONS = DefaultAnnotations.builder().set("key1", "val1").build();
85 HOST = new DefaultHost(ProviderId.NONE, HOST_ID, MAC, VLAN_ID,
86 HOST_LOCATIONS, AUX_HOST_LOCATIONS, IPS, INNER_VLAN_ID,
87 OUTER_TPID, false, false, ANNOTATIONS);
88 }
89
90 @Test
91 public void testCodec() {
92 assertNotNull(hostCodec);
93 assertJsonEncodable(context, hostCodec, HOST);
94 }
95
96 @Test
97 public void testDecode() throws IOException {
98 InputStream jsonStream = HostCodec.class.getResourceAsStream(JSON_FILE);
99 JsonNode jsonString = context.mapper().readTree(jsonStream);
100
101 Host expected = hostCodec.decode((ObjectNode) jsonString, context);
102 assertEquals(expected, HOST);
103 }
104
105 @Test
106 public void testEncode() throws IOException {
107 InputStream jsonStream = HostCodec.class.getResourceAsStream(JSON_FILE);
108 JsonNode jsonString = context.mapper().readTree(jsonStream);
109
110 ObjectNode expected = hostCodec.encode(HOST, context);
111 // Host ID is not a field in Host but rather derived from MAC + VLAN.
112 // Derived information should not be part of the JSON really.
113 // However, we keep it as is for backward compatibility.
114 expected.remove(HostCodec.HOST_ID);
115
116 assertEquals(expected, jsonString);
117 }
118
119}