@Override
    public VirtualHost decode(ObjectNode json, CodecContext context) {
        if (json == null || !json.isObject()) {
            return null;
        }

        NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
        MacAddress mac = MacAddress.valueOf(json.get("mac").asText());
        VlanId vlanId = VlanId.vlanId((short) json.get("vlan").asInt(VlanId.UNTAGGED));

        Set<HostLocation> locations = new HashSet<>();
        JsonNode locationNodes = json.get("locations");
        locationNodes.forEach(locationNode -> {
            PortNumber portNumber = PortNumber.portNumber(locationNode.get("port").asText());
            DeviceId deviceId = DeviceId.deviceId(locationNode.get("elementId").asText());
            locations.add(new HostLocation(deviceId, portNumber, 0));
        });

        HostId id = HostId.hostId(mac, vlanId);

        Iterator<JsonNode> ipStrings = json.get("ipAddresses").elements();
        Set<IpAddress> ips = new HashSet<>();
        while (ipStrings.hasNext()) {
            ips.add(IpAddress.valueOf(ipStrings.next().asText()));
        }

        return new DefaultVirtualHost(nId, id, mac, vlanId, locations, ips);
    }
@Test
    public void testDecode() throws IOException {
        MockCodecContext context = new MockCodecContext();
        InputStream jsonStream =
                VirtualHostCodecTest.class.getResourceAsStream("VirtualHost.json");
        JsonNode json = context.mapper().readTree(jsonStream);
        assertThat(json, notNullValue());
        JsonCodec<VirtualHost> codec = context.codec(VirtualHost.class);
        VirtualHost virtualHost = codec.decode((ObjectNode) json, context);
        assertThat(virtualHost, notNullValue());

        assertThat(virtualHost.networkId().id(),
                   is(TEST_NETWORK_ID));
        assertThat(virtualHost.id().toString(),
                   is(NetTestTools.hid(TEST_MAC_ADDRESS + "/12").toString()));
        assertThat(virtualHost.mac().toString(),
                   is(TEST_MAC_ADDRESS));
        assertThat(virtualHost.vlan().id(),
                   is((short) TEST_VLAN_ID));
        assertThat(virtualHost.location().deviceId(),
                   is(CONNECT_POINT.deviceId()));
        assertThat(virtualHost.location().port().toLong(),
                   is(CONNECT_POINT.port().toLong()));


        assertThat(virtualHost.ipAddresses().contains(IpAddress.valueOf(TEST_IP1)),
                   is(true));
        assertThat(virtualHost.ipAddresses().contains(IpAddress.valueOf(TEST_IP2)),
                   is(true));
    }