@Override
    public ObjectNode encode(VirtualHost vHost, CodecContext context) {
        checkNotNull(vHost, NULL_OBJECT_MSG);

        final JsonCodec<HostLocation> locationCodec =
                context.codec(HostLocation.class);
        final ObjectNode result = context.mapper().createObjectNode()
                .put(NETWORK_ID, vHost.networkId().toString())
                .put(HOST_ID, vHost.id().toString())
                .put(MAC_ADDRESS, vHost.mac().toString())
                .put(VLAN, vHost.vlan().toString());

        final ArrayNode jsonIpAddresses = result.putArray(IP_ADDRESSES);
        for (final IpAddress ipAddress : vHost.ipAddresses()) {
            jsonIpAddresses.add(ipAddress.toString());
        }
        result.set(IP_ADDRESSES, jsonIpAddresses);

        final ArrayNode jsonLocations = result.putArray("locations");
        for (final HostLocation location : vHost.locations()) {
            jsonLocations.add(locationCodec.encode(location, context));
        }
        result.set("locations", jsonLocations);

        return result;
    }
@Test
    public void testEncode() {
        MockCodecContext context = new MockCodecContext();
        NetworkId networkId = NetworkId.networkId(TEST_NETWORK_ID);
        HostId id = NetTestTools.hid(TEST_HOST_ID);
        MacAddress mac = MacAddress.valueOf(TEST_MAC_ADDRESS);
        VlanId vlan = VlanId.vlanId(TEST_VLAN_ID);
        HostLocation location =
                new HostLocation(CONNECT_POINT, 0L);
        Set<IpAddress> ips = ImmutableSet.of(IpAddress.valueOf(TEST_IP1),
                                             IpAddress.valueOf(TEST_IP2));
        VirtualHost host =
                new DefaultVirtualHost(networkId, id, mac, vlan, location, ips);
        JsonCodec<VirtualHost> codec = context.codec(VirtualHost.class);
        ObjectNode node = codec.encode(host, context);

        assertThat(node.get(VirtualHostCodec.NETWORK_ID).asLong(),
                   is(TEST_NETWORK_ID));
        assertThat(node.get(VirtualHostCodec.HOST_ID).asText(),
                   is(TEST_HOST_ID));
        assertThat(node.get(VirtualHostCodec.MAC_ADDRESS).asText(),
                   is(TEST_MAC_ADDRESS));
        assertThat(node.get(VirtualHostCodec.VLAN).asInt(),
                   is((int) TEST_VLAN_ID));
        assertThat(node.get(VirtualHostCodec.HOST_LOCATION).get(0).get("elementId").asText(),
                   is(location.deviceId().toString()));
        assertThat(node.get(VirtualHostCodec.HOST_LOCATION).get(0).get("port").asLong(),
                   is(location.port().toLong()));

        JsonNode jsonIps = node.get(VirtualHostCodec.IP_ADDRESSES);
        assertThat(jsonIps, notNullValue());
        assertThat(jsonIps.isArray(), is(true));
        assertThat(jsonIps.size(), is(ips.size()));

        IntStream.of(0, 1).forEach(index ->
            assertThat(jsonIps.get(index).asText(),
                       isOneOf(TEST_IP1, TEST_IP2)));
    }