Search in sources :

Example 1 with VirtualHost

use of org.onosproject.incubator.net.virtual.VirtualHost 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 VirtualHost

use of org.onosproject.incubator.net.virtual.VirtualHost in project onos by opennetworkinglab.

the class VirtualHostListCommand method getSortedVirtualHosts.

/**
 * Returns the list of virtual hosts sorted using the device identifier.
 *
 * @return virtual host list
 */
private List<VirtualHost> getSortedVirtualHosts() {
    VirtualNetworkService service = get(VirtualNetworkService.class);
    List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.addAll(service.getVirtualHosts(NetworkId.networkId(networkId)));
    return virtualHosts;
}
Also used : VirtualNetworkService(org.onosproject.incubator.net.virtual.VirtualNetworkService) ArrayList(java.util.ArrayList) VirtualHost(org.onosproject.incubator.net.virtual.VirtualHost)

Example 3 with VirtualHost

use of org.onosproject.incubator.net.virtual.VirtualHost in project onos by opennetworkinglab.

the class VirtualNetworkWebResource method removeVirtualHost.

/**
 * Removes the virtual network host from the JSON input stream.
 *
 * @param networkId network identifier
 * @param stream    virtual host JSON stream
 * @return 204 NO CONTENT
 * @onos.rsModel VirtualHost
 */
@DELETE
@Path("{networkId}/hosts")
@Consumes(MediaType.APPLICATION_JSON)
public Response removeVirtualHost(@PathParam("networkId") long networkId, InputStream stream) {
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        JsonNode specifiedNetworkId = jsonTree.get("networkId");
        if (specifiedNetworkId != null && specifiedNetworkId.asLong() != (networkId)) {
            throw new IllegalArgumentException(INVALID_FIELD + "networkId");
        }
        final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
        vnetAdminService.removeVirtualHost(vhostReq.networkId(), vhostReq.id());
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return Response.noContent().build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) VirtualHost(org.onosproject.incubator.net.virtual.VirtualHost) IOException(java.io.IOException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes)

Example 4 with VirtualHost

use of org.onosproject.incubator.net.virtual.VirtualHost in project onos by opennetworkinglab.

the class VirtualNetworkManagerTest method testAddRemoveVirtualHost.

/**
 * Tests add and remove of virtual hosts.
 */
@Test
public void testAddRemoveVirtualHost() {
    manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
    VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
    VirtualNetwork virtualNetwork2 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
    VirtualDevice virtualDevice1 = manager.createVirtualDevice(virtualNetwork1.id(), DID1);
    VirtualDevice virtualDevice2 = manager.createVirtualDevice(virtualNetwork2.id(), DID2);
    ConnectPoint hostCp1 = new ConnectPoint(DID1, P1);
    ConnectPoint hostCp2 = new ConnectPoint(DID2, P2);
    manager.createVirtualPort(virtualNetwork1.id(), hostCp1.deviceId(), hostCp1.port(), new ConnectPoint(virtualDevice1.id(), hostCp1.port()));
    manager.createVirtualPort(virtualNetwork2.id(), hostCp2.deviceId(), hostCp2.port(), new ConnectPoint(virtualDevice2.id(), hostCp2.port()));
    manager.createVirtualHost(virtualNetwork1.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
    manager.createVirtualHost(virtualNetwork2.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
    Set<VirtualHost> virtualHosts1 = manager.getVirtualHosts(virtualNetwork1.id());
    assertNotNull("The virtual host set should not be null", virtualHosts1);
    assertEquals("The virtual host set size did not match.", 1, virtualHosts1.size());
    Set<VirtualHost> virtualHosts2 = manager.getVirtualHosts(virtualNetwork2.id());
    assertNotNull("The virtual host set should not be null", virtualHosts2);
    assertEquals("The virtual host set size did not match.", 1, virtualHosts2.size());
    for (VirtualHost virtualHost : virtualHosts1) {
        manager.removeVirtualHost(virtualNetwork1.id(), virtualHost.id());
        // attempt to remove the same virtual host again.
        manager.removeVirtualHost(virtualNetwork1.id(), virtualHost.id());
    }
    virtualHosts1 = manager.getVirtualHosts(virtualNetwork1.id());
    assertTrue("The virtual host set should be empty.", virtualHosts1.isEmpty());
    // Add/remove the virtual host again.
    VirtualHost virtualHost = manager.createVirtualHost(virtualNetwork1.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
    manager.removeVirtualHost(virtualHost.networkId(), virtualHost.id());
    virtualHosts1 = manager.getVirtualHosts(virtualNetwork1.id());
    assertTrue("The virtual host set should be empty.", virtualHosts1.isEmpty());
}
Also used : DefaultVirtualNetwork(org.onosproject.incubator.net.virtual.DefaultVirtualNetwork) VirtualNetwork(org.onosproject.incubator.net.virtual.VirtualNetwork) VirtualDevice(org.onosproject.incubator.net.virtual.VirtualDevice) VirtualHost(org.onosproject.incubator.net.virtual.VirtualHost) ConnectPoint(org.onosproject.net.ConnectPoint) Test(org.junit.Test)

Example 5 with VirtualHost

use of org.onosproject.incubator.net.virtual.VirtualHost in project onos by opennetworkinglab.

the class VirtualNetworkWebResourceTest method testGetVirtualHostsArray.

/**
 * Tests the result of the REST API GET when virtual hosts are defined.
 */
@Test
public void testGetVirtualHostsArray() {
    NetworkId networkId = networkId3;
    final Set<VirtualHost> vhostSet = ImmutableSet.of(vhost1, vhost2);
    expect(mockVnetService.getVirtualHosts(networkId)).andReturn(vhostSet).anyTimes();
    replay(mockVnetService);
    WebTarget wt = target();
    String location = "vnets/" + networkId.toString() + "/hosts";
    String response = wt.path(location).request().get(String.class);
    assertThat(response, containsString("{\"hosts\":["));
    final JsonObject result = Json.parse(response).asObject();
    assertThat(result, notNullValue());
    assertThat(result.names(), hasSize(1));
    assertThat(result.names().get(0), is("hosts"));
    final JsonArray vnetJsonArray = result.get("hosts").asArray();
    assertThat(vnetJsonArray, notNullValue());
    assertEquals("Virtual hosts array is not the correct size.", vhostSet.size(), vnetJsonArray.size());
    vhostSet.forEach(vhost -> assertThat(vnetJsonArray, hasVhost(vhost)));
    verify(mockVnetService);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) DefaultVirtualHost(org.onosproject.incubator.net.virtual.DefaultVirtualHost) VirtualHost(org.onosproject.incubator.net.virtual.VirtualHost) WebTarget(javax.ws.rs.client.WebTarget) Matchers.containsString(org.hamcrest.Matchers.containsString) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) LinksResourceTest(org.onosproject.rest.resources.LinksResourceTest) ResourceTest(org.onosproject.rest.resources.ResourceTest) HostResourceTest(org.onosproject.rest.resources.HostResourceTest) Test(org.junit.Test)

Aggregations

VirtualHost (org.onosproject.incubator.net.virtual.VirtualHost)12 Test (org.junit.Test)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 DefaultVirtualHost (org.onosproject.incubator.net.virtual.DefaultVirtualHost)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Path (javax.ws.rs.Path)3 NetworkId (org.onosproject.incubator.net.virtual.NetworkId)3 VirtualNetwork (org.onosproject.incubator.net.virtual.VirtualNetwork)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 Produces (javax.ws.rs.Produces)2 MockCodecContext (org.onosproject.codec.impl.MockCodecContext)2 DefaultVirtualNetwork (org.onosproject.incubator.net.virtual.DefaultVirtualNetwork)2 VirtualDevice (org.onosproject.incubator.net.virtual.VirtualDevice)2 VirtualNetworkService (org.onosproject.incubator.net.virtual.VirtualNetworkService)2 ConnectPoint (org.onosproject.net.ConnectPoint)2 HostId (org.onosproject.net.HostId)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonObject (com.eclipsesource.json.JsonObject)1