ONOS-2184 Adding VirtualHost support in Virtual Networks.
Change-Id: I1318f20a9b14f1f99dc8eb48b9660a208165bac1
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/DefaultVirtualHost.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/DefaultVirtualHost.java
new file mode 100644
index 0000000..2256fd3
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/DefaultVirtualHost.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.incubator.net.virtual;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultHost;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.Objects;
+import java.util.Set;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Default representation of a virtual host.
+ */
+public final class DefaultVirtualHost extends DefaultHost implements VirtualHost {
+
+ private static final String VIRTUAL = "virtual";
+ private static final ProviderId PID = new ProviderId(VIRTUAL, VIRTUAL);
+
+ private final NetworkId networkId;
+
+ /**
+ * Creates a virtual host attributed to the specified provider.
+ *
+ * @param networkId network identifier
+ * @param id host identifier
+ * @param mac host MAC address
+ * @param vlan host VLAN identifier
+ * @param location host location
+ * @param ips host IP addresses
+ */
+ public DefaultVirtualHost(NetworkId networkId, HostId id, MacAddress mac,
+ VlanId vlan, HostLocation location, Set<IpAddress> ips) {
+ super(PID, id, mac, vlan, location, ips, DefaultAnnotations.builder().build());
+ this.networkId = networkId;
+ }
+
+ @Override
+ public NetworkId networkId() {
+ return networkId;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(networkId);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof DefaultVirtualHost) {
+ DefaultVirtualHost that = (DefaultVirtualHost) obj;
+ return super.equals(that) && Objects.equals(this.networkId, that.networkId);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this).add("networkId", networkId).toString();
+ }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java
index c903e61..04d1911 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java
@@ -16,8 +16,13 @@
package org.onosproject.incubator.net.virtual;
import com.google.common.annotations.Beta;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
@@ -74,7 +79,7 @@
*
* @param networkId network identifier
* @param deviceId device identifier
- * @return newly created device
+ * @return newly created virtual device
* @throws org.onlab.util.ItemNotFoundException if no such network found
*/
VirtualDevice createVirtualDevice(NetworkId networkId, DeviceId deviceId);
@@ -88,6 +93,30 @@
*/
void removeVirtualDevice(NetworkId networkId, DeviceId deviceId);
+ /**
+ * Creates a new virtual host within the specified network. The host id
+ * must be unique within the bounds of the network.
+ *
+ * @param networkId network identifier
+ * @param hostId host identifier
+ * @param mac mac address
+ * @param vlan vlan identifier
+ * @param location host location
+ * @param ips set of ip addresses
+ * @return newly created virtual host
+ * @throws org.onlab.util.ItemNotFoundException if no such network found
+ */
+ VirtualHost createVirtualHost(NetworkId networkId, HostId hostId, MacAddress mac,
+ VlanId vlan, HostLocation location, Set<IpAddress> ips);
+
+ /**
+ * Removes the specified virtual host.
+ *
+ * @param networkId network identifier
+ * @param hostId host identifier
+ * @throws org.onlab.util.ItemNotFoundException if no such network or host found
+ */
+ void removeVirtualHost(NetworkId networkId, HostId hostId);
/**
* Creates a new virtual link within the specified network.
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java
index ebe0b69..530c67a 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java
@@ -51,6 +51,15 @@
Set<VirtualDevice> getVirtualDevices(NetworkId networkId);
/**
+ * Returns a collection of all virtual hosts in the specified network.
+ *
+ * @param networkId network identifier
+ * @return collection of hosts
+ * @throws org.onlab.util.ItemNotFoundException if no such network found
+ */
+ Set<VirtualHost> getVirtualHosts(NetworkId networkId);
+
+ /**
* Returns collection of all virtual links in the specified network.
*
* @param networkId network identifier
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkStore.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkStore.java
index 11bcc7b..c2bc250 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkStore.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkStore.java
@@ -15,9 +15,14 @@
*/
package org.onosproject.incubator.net.virtual;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
@@ -77,7 +82,7 @@
VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId);
/**
- * Renmoves the specified virtual device from the given network.
+ * Removes the specified virtual device from the given network.
*
* @param networkId network identifier
* @param deviceId device identifier
@@ -85,6 +90,28 @@
void removeDevice(NetworkId networkId, DeviceId deviceId);
/**
+ * Adds a new virtual host to the store.
+ *
+ * @param networkId network identifier
+ * @param hostId host identifier
+ * @param mac mac address
+ * @param vlan vlan identifier
+ * @param location host location
+ * @param ips set of ip addresses
+ * @return the virtual host
+ */
+ VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac,
+ VlanId vlan, HostLocation location, Set<IpAddress> ips);
+
+ /**
+ * Removes the specified virtual host from the store.
+ *
+ * @param networkId network identifier
+ * @param hostId host identifier
+ */
+ void removeHost(NetworkId networkId, HostId hostId);
+
+ /**
* Adds a new virtual link.
*
* @param networkId network identifier
@@ -147,7 +174,7 @@
/**
* Returns the virtual network for the given network identifier.
*
- * @param networkId network identifier
+ * @param networkId network identifier
* @return the virtual network
*/
VirtualNetwork getNetwork(NetworkId networkId);
@@ -161,6 +188,14 @@
Set<VirtualDevice> getDevices(NetworkId networkId);
/**
+ * Returns the list of hosts in the specified virtual network.
+ *
+ * @param networkId network identifier
+ * @return set of virtual hosts
+ */
+ Set<VirtualHost> getHosts(NetworkId networkId);
+
+ /**
* Returns the list of virtual links in the specified virtual network.
*
* @param networkId network identifier
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualDeviceTest.java b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualDeviceTest.java
index 04ba6a9..1fd97e3 100644
--- a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualDeviceTest.java
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualDeviceTest.java
@@ -18,16 +18,14 @@
import com.google.common.testing.EqualsTester;
import org.junit.Test;
-import org.onosproject.net.DeviceId;
+import org.onosproject.net.TestDeviceParams;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Test of the default virtual device model entity.
*/
-public class DefaultVirtualDeviceTest {
- final String deviceIdValue1 = "DEVICE_ID1";
- final String deviceIdValue2 = "DEVICE_ID2";
+public class DefaultVirtualDeviceTest extends TestDeviceParams {
/**
* Checks that the DefaultVirtualDevice class is immutable.
@@ -40,13 +38,13 @@
@Test
public void testEquality() {
DefaultVirtualDevice device1 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device2 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device3 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue2));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID2);
DefaultVirtualDevice device4 =
- new DefaultVirtualDevice(NetworkId.networkId(1), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(1), DID1);
new EqualsTester().addEqualityGroup(device1, device2).addEqualityGroup(device3)
.addEqualityGroup(device4).testEquals();
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualHostTest.java b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualHostTest.java
new file mode 100644
index 0000000..461088f
--- /dev/null
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualHostTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.incubator.net.virtual;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onosproject.net.TestDeviceParams;
+
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Test of the default virtual host model entity.
+ */
+public class DefaultVirtualHostTest extends TestDeviceParams {
+
+ /**
+ * Checks that the DefaultVirtualHost class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultVirtualHost.class);
+ }
+
+ /**
+ * Tests the DefaultVirtualHost equality method.
+ */
+ @Test
+ public void testEquality() {
+ DefaultVirtualHost host1 =
+ new DefaultVirtualHost(NetworkId.networkId(0), HID1, MAC1, VLAN1, LOC1, IPSET1);
+ DefaultVirtualHost host2 =
+ new DefaultVirtualHost(NetworkId.networkId(0), HID1, MAC1, VLAN1, LOC1, IPSET1);
+ DefaultVirtualHost host3 =
+ new DefaultVirtualHost(NetworkId.networkId(0), HID2, MAC1, VLAN1, LOC1, IPSET1);
+ DefaultVirtualHost host4 =
+ new DefaultVirtualHost(NetworkId.networkId(1), HID2, MAC1, VLAN1, LOC1, IPSET1);
+
+ new EqualsTester().addEqualityGroup(host1, host2).addEqualityGroup(host3)
+ .addEqualityGroup(host4).testEquals();
+ }
+}
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualLinkTest.java b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualLinkTest.java
index 71e214a..00b1db0 100644
--- a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualLinkTest.java
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualLinkTest.java
@@ -20,17 +20,15 @@
import org.junit.Test;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
+import org.onosproject.net.TestDeviceParams;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Test of the default virtual link model entity.
*/
-public class DefaultVirtualLinkTest {
- final String deviceIdValue1 = "DEVICE_ID1";
- final String deviceIdValue2 = "DEVICE_ID2";
+public class DefaultVirtualLinkTest extends TestDeviceParams {
/**
* Checks that the DefaultVirtualLink class is immutable.
@@ -46,9 +44,9 @@
@Test(expected = NullPointerException.class)
public void testBuilderNullSrc() {
DefaultVirtualDevice device1 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device2 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue2));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID2);
ConnectPoint src = new ConnectPoint(device1.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(device2.id(), PortNumber.portNumber(2));
@@ -63,9 +61,9 @@
@Test(expected = NullPointerException.class)
public void testBuilderNullDst() {
DefaultVirtualDevice device1 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device2 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue2));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID2);
ConnectPoint src = new ConnectPoint(device1.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(device2.id(), PortNumber.portNumber(2));
@@ -80,9 +78,9 @@
@Test(expected = NullPointerException.class)
public void testBuilderNullNetworkId() {
DefaultVirtualDevice device1 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device2 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue2));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID2);
ConnectPoint src = new ConnectPoint(device1.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(device2.id(), PortNumber.portNumber(2));
@@ -97,9 +95,9 @@
@Test
public void testEquality() {
DefaultVirtualDevice device1 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device2 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue2));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID2);
ConnectPoint src = new ConnectPoint(device1.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(device2.id(), PortNumber.portNumber(2));
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualPortTest.java b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualPortTest.java
index da560f9..6b83e3a 100644
--- a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualPortTest.java
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/DefaultVirtualPortTest.java
@@ -19,18 +19,16 @@
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import org.onosproject.net.DefaultPort;
-import org.onosproject.net.DeviceId;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
+import org.onosproject.net.TestDeviceParams;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Test of the default virtual port model entity.
*/
-public class DefaultVirtualPortTest {
- final String deviceIdValue1 = "DEVICE_ID1";
- final String deviceIdValue2 = "DEVICE_ID2";
+public class DefaultVirtualPortTest extends TestDeviceParams {
/**
* Checks that the DefaultVirtualPort class is immutable.
@@ -43,9 +41,9 @@
@Test
public void testEquality() {
DefaultVirtualDevice device1 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue1));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID1);
DefaultVirtualDevice device2 =
- new DefaultVirtualDevice(NetworkId.networkId(0), DeviceId.deviceId(deviceIdValue2));
+ new DefaultVirtualDevice(NetworkId.networkId(0), DID2);
Port portA = new DefaultPort(device1, PortNumber.portNumber(1), true);
Port portB = new DefaultPort(device1, PortNumber.portNumber(2), true);
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
index 4ac0117..6c06968 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
@@ -22,10 +22,14 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.incubator.net.virtual.NetworkId;
import org.onosproject.incubator.net.virtual.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
+import org.onosproject.incubator.net.virtual.VirtualHost;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
@@ -40,6 +44,8 @@
import org.onosproject.incubator.net.virtual.VirtualPort;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
@@ -141,6 +147,21 @@
}
@Override
+ public VirtualHost createVirtualHost(NetworkId networkId, HostId hostId, MacAddress mac,
+ VlanId vlan, HostLocation location, Set<IpAddress> ips) {
+ checkNotNull(networkId, NETWORK_NULL);
+ checkNotNull(hostId, DEVICE_NULL);
+ return store.addHost(networkId, hostId, mac, vlan, location, ips);
+ }
+
+ @Override
+ public void removeVirtualHost(NetworkId networkId, HostId hostId) {
+ checkNotNull(networkId, NETWORK_NULL);
+ checkNotNull(hostId, DEVICE_NULL);
+ store.removeHost(networkId, hostId);
+ }
+
+ @Override
public VirtualLink createVirtualLink(NetworkId networkId,
ConnectPoint src, ConnectPoint dst) {
checkNotNull(networkId, NETWORK_NULL);
@@ -248,6 +269,12 @@
}
@Override
+ public Set<VirtualHost> getVirtualHosts(NetworkId networkId) {
+ checkNotNull(networkId, NETWORK_NULL);
+ return store.getHosts(networkId);
+ }
+
+ @Override
public Set<VirtualLink> getVirtualLinks(NetworkId networkId) {
checkNotNull(networkId, NETWORK_NULL);
return store.getLinks(networkId);
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
index 7c3f9f8..df5ef28 100644
--- a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
@@ -31,6 +31,7 @@
import org.onosproject.incubator.net.virtual.NetworkId;
import org.onosproject.incubator.net.virtual.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
+import org.onosproject.incubator.net.virtual.VirtualHost;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
@@ -44,6 +45,7 @@
import org.onosproject.net.NetTestTools;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
+import org.onosproject.net.TestDeviceParams;
import org.onosproject.store.service.TestStorageService;
import java.util.Collection;
@@ -56,17 +58,15 @@
/**
* Junit tests for VirtualNetworkManager.
*/
-public class VirtualNetworkManagerTest {
+public class VirtualNetworkManagerTest extends TestDeviceParams {
private final String tenantIdValue1 = "TENANT_ID1";
private final String tenantIdValue2 = "TENANT_ID2";
- private final String deviceIdValue1 = "DEVICE_ID1";
- private final String deviceIdValue2 = "DEVICE_ID2";
private VirtualNetworkManager manager;
private VirtualNetworkService virtualNetworkManagerService;
private DistributedVirtualNetworkStore virtualNetworkManagerStore;
private CoreService coreService;
- protected TestListener listener = new TestListener();
+ private TestListener listener = new TestListener();
@Before
public void setUp() throws Exception {
@@ -84,7 +84,6 @@
NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
manager.activate();
virtualNetworkManagerService = manager;
-
}
@After
@@ -191,7 +190,7 @@
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(NetworkId.NONE, TenantId.tenantId(tenantIdValue1));
- manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
+ manager.createVirtualDevice(virtualNetwork.id(), DID1);
}
/**
@@ -202,8 +201,8 @@
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork2 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
- manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue1));
- manager.createVirtualDevice(virtualNetwork2.id(), DeviceId.deviceId(deviceIdValue2));
+ manager.createVirtualDevice(virtualNetwork1.id(), DID1);
+ manager.createVirtualDevice(virtualNetwork2.id(), DID2);
Set<VirtualDevice> virtualDevices1 = manager.getVirtualDevices(virtualNetwork1.id());
assertNotNull("The virtual device set should not be null", virtualDevices1);
@@ -222,8 +221,7 @@
assertTrue("The virtual device set should be empty.", virtualDevices1.isEmpty());
// Add/remove the virtual device again.
- VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork1.id(),
- DeviceId.deviceId(deviceIdValue1));
+ VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork1.id(), DID1);
manager.removeVirtualDevice(virtualDevice.networkId(), virtualDevice.id());
virtualDevices1 = manager.getVirtualDevices(virtualNetwork1.id());
assertTrue("The virtual device set should be empty.", virtualDevices1.isEmpty());
@@ -234,6 +232,62 @@
}
/**
+ * Tests adding a null virtual host.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateNullVirtualHost() {
+ manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
+ VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
+
+ manager.createVirtualHost(virtualNetwork.id(), null, null, null, null, null);
+ }
+
+ /**
+ * Tests adding a virtual host where no virtual network exists.
+ */
+ @Test(expected = IllegalStateException.class)
+ public void testCreateVirtualHostWithNoNetwork() {
+ manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
+ VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(NetworkId.NONE, TenantId.tenantId(tenantIdValue1));
+
+ manager.createVirtualHost(virtualNetwork.id(), HID1, null, null, null, null);
+ }
+
+ /**
+ * 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));
+ 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());
+ }
+
+ /**
* Tests add and remove of virtual links.
*/
@Test
@@ -241,9 +295,9 @@
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
- manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue1));
+ manager.createVirtualDevice(virtualNetwork1.id(), DID1);
VirtualDevice dstVirtualDevice =
- manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue2));
+ manager.createVirtualDevice(virtualNetwork1.id(), DID2);
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
manager.createVirtualLink(virtualNetwork1.id(), src, dst);
@@ -276,9 +330,9 @@
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
- manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue1));
+ manager.createVirtualDevice(virtualNetwork1.id(), DID1);
VirtualDevice dstVirtualDevice =
- manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue2));
+ manager.createVirtualDevice(virtualNetwork1.id(), DID2);
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
manager.createVirtualLink(virtualNetwork1.id(), src, dst);
@@ -293,7 +347,7 @@
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice virtualDevice =
- manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue1));
+ manager.createVirtualDevice(virtualNetwork1.id(), DID1);
Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true);
manager.createVirtualPort(virtualNetwork1.id(), virtualDevice.id(), PortNumber.portNumber(1), port);
@@ -345,7 +399,7 @@
*/
private static class TestListener implements VirtualNetworkListener {
- protected List<VirtualNetworkEvent> events = Lists.newArrayList();
+ private List<VirtualNetworkEvent> events = Lists.newArrayList();
@Override
public void event(VirtualNetworkEvent event) {
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/virtual/impl/DistributedVirtualNetworkStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/virtual/impl/DistributedVirtualNetworkStore.java
index 11836b7..a9b5ac0 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/virtual/impl/DistributedVirtualNetworkStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/virtual/impl/DistributedVirtualNetworkStore.java
@@ -23,17 +23,22 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
import org.onlab.util.KryoNamespace;
import org.onosproject.core.CoreService;
import org.onosproject.core.IdGenerator;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.incubator.net.virtual.DefaultVirtualDevice;
+import org.onosproject.incubator.net.virtual.DefaultVirtualHost;
import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork;
import org.onosproject.incubator.net.virtual.DefaultVirtualPort;
import org.onosproject.incubator.net.virtual.NetworkId;
import org.onosproject.incubator.net.virtual.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
+import org.onosproject.incubator.net.virtual.VirtualHost;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
@@ -44,6 +49,8 @@
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
@@ -111,6 +118,14 @@
private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap;
private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap;
+ // Track virtual hosts by host Id
+ private ConsistentMap<HostId, VirtualHost> hostIdVirtualHostConsistentMap;
+ private Map<HostId, VirtualHost> hostIdVirtualHostMap;
+
+ // Track host IDs by network Id
+ private ConsistentMap<NetworkId, Set<HostId>> networkIdHostIdSetConsistentMap;
+ private Map<NetworkId, Set<HostId>> networkIdHostIdSetMap;
+
// Track virtual links by network Id
private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap;
private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap;
@@ -127,6 +142,8 @@
.register(DefaultVirtualNetwork.class)
.register(VirtualDevice.class)
.register(DefaultVirtualDevice.class)
+ .register(VirtualHost.class)
+ .register(DefaultVirtualHost.class)
.register(VirtualLink.class)
.register(DefaultVirtualLink.class)
.register(VirtualPort.class)
@@ -180,6 +197,20 @@
.build();
networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap();
+ hostIdVirtualHostConsistentMap = storageService.<HostId, VirtualHost>consistentMapBuilder()
+ .withSerializer(SERIALIZER)
+ .withName("onos-hostId-virtualhost")
+ .withRelaxedReadConsistency()
+ .build();
+ hostIdVirtualHostMap = hostIdVirtualHostConsistentMap.asJavaMap();
+
+ networkIdHostIdSetConsistentMap = storageService.<NetworkId, Set<HostId>>consistentMapBuilder()
+ .withSerializer(SERIALIZER)
+ .withName("onos-networkId-hostIds")
+ .withRelaxedReadConsistency()
+ .build();
+ networkIdHostIdSetMap = networkIdHostIdSetConsistentMap.asJavaMap();
+
networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder()
.withSerializer(SERIALIZER)
.withName("onos-networkId-virtuallinks")
@@ -342,6 +373,48 @@
deviceIdVirtualDeviceMap.remove(deviceId);
}
+ //TODO remove virtual links and ports when removing the virtual device
+ }
+
+ @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.");
+ 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;
+ }
+
+ @Override
+ public void removeHost(NetworkId networkId, HostId hostId) {
+ checkState(networkExists(networkId), "The network has not been added.");
+ //TODO update both maps in one transaction.
+
+ Set<HostId> hostIdSet = new HashSet<>();
+ networkIdHostIdSetMap.get(networkId).forEach(hostId1 -> {
+ if (hostId1.equals(hostId)) {
+ hostIdSet.add(hostId1);
+ }
+ });
+
+ if (hostIdSet != null) {
+ networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> {
+ if (existingHostIds == null || existingHostIds.isEmpty()) {
+ return new HashSet<>();
+ } else {
+ return new HashSet<>(Sets.difference(existingHostIds, hostIdSet));
+ }
+ });
+
+ hostIdVirtualHostMap.remove(hostId);
+ }
}
@Override
@@ -476,6 +549,17 @@
}
@Override
+ public Set<VirtualHost> getHosts(NetworkId networkId) {
+ checkState(networkExists(networkId), "The network has not been added.");
+ Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId);
+ Set<VirtualHost> virtualHostSet = new HashSet<>();
+ if (hostIdSet != null) {
+ hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId)));
+ }
+ return ImmutableSet.copyOf(virtualHostSet);
+ }
+
+ @Override
public Set<VirtualLink> getLinks(NetworkId networkId) {
checkState(networkExists(networkId), "The network has not been added.");
Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);