use of org.onosproject.incubator.net.virtual.DefaultVirtualHost in project onos by opennetworkinglab.
the class VirtualHostCodecTest method testEncode.
@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)));
}
use of org.onosproject.incubator.net.virtual.DefaultVirtualHost in project onos by opennetworkinglab.
the class VirtualHostCodec method decode.
@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);
}
use of org.onosproject.incubator.net.virtual.DefaultVirtualHost in project onos by opennetworkinglab.
the class DistributedVirtualNetworkStore method addHost.
@Override
public VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac, VlanId vlan, HostLocation location, Set<IpAddress> ips) {
checkState(networkExists(networkId), "The network has not been added.");
checkState(virtualPortExists(networkId, location.deviceId(), location.port()), "The virtual port has not been created.");
Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId);
if (hostIdSet == null) {
hostIdSet = new HashSet<>();
}
VirtualHost virtualhost = new DefaultVirtualHost(networkId, hostId, mac, vlan, location, ips);
// TODO update both maps in one transaction.
hostIdVirtualHostMap.put(hostId, virtualhost);
hostIdSet.add(hostId);
networkIdHostIdSetMap.put(networkId, hostIdSet);
return virtualhost;
}
Aggregations