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)));
}
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;
}
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();
}
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());
}
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);
}
Aggregations