Search in sources :

Example 1 with DefaultVirtualHost

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)));
}
Also used : DefaultVirtualHost(org.onosproject.incubator.net.virtual.DefaultVirtualHost) MockCodecContext(org.onosproject.codec.impl.MockCodecContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) HostId(org.onosproject.net.HostId) MacAddress(org.onlab.packet.MacAddress) HostLocation(org.onosproject.net.HostLocation) IpAddress(org.onlab.packet.IpAddress) VirtualHost(org.onosproject.incubator.net.virtual.VirtualHost) DefaultVirtualHost(org.onosproject.incubator.net.virtual.DefaultVirtualHost) VlanId(org.onlab.packet.VlanId) Test(org.junit.Test)

Example 2 with DefaultVirtualHost

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);
}
Also used : DefaultVirtualHost(org.onosproject.incubator.net.virtual.DefaultVirtualHost) DeviceId(org.onosproject.net.DeviceId) JsonNode(com.fasterxml.jackson.databind.JsonNode) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) MacAddress(org.onlab.packet.MacAddress) HostId(org.onosproject.net.HostId) HostLocation(org.onosproject.net.HostLocation) IpAddress(org.onlab.packet.IpAddress) PortNumber(org.onosproject.net.PortNumber) VlanId(org.onlab.packet.VlanId) HashSet(java.util.HashSet)

Example 3 with DefaultVirtualHost

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;
}
Also used : DefaultVirtualHost(org.onosproject.incubator.net.virtual.DefaultVirtualHost) DefaultVirtualHost(org.onosproject.incubator.net.virtual.DefaultVirtualHost) VirtualHost(org.onosproject.incubator.net.virtual.VirtualHost) HostId(org.onosproject.net.HostId)

Aggregations

DefaultVirtualHost (org.onosproject.incubator.net.virtual.DefaultVirtualHost)3 HostId (org.onosproject.net.HostId)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IpAddress (org.onlab.packet.IpAddress)2 MacAddress (org.onlab.packet.MacAddress)2 VlanId (org.onlab.packet.VlanId)2 NetworkId (org.onosproject.incubator.net.virtual.NetworkId)2 VirtualHost (org.onosproject.incubator.net.virtual.VirtualHost)2 HostLocation (org.onosproject.net.HostLocation)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 MockCodecContext (org.onosproject.codec.impl.MockCodecContext)1 DeviceId (org.onosproject.net.DeviceId)1 PortNumber (org.onosproject.net.PortNumber)1