[CORD-1433] DHCP Relay Store
Change-Id: Ibb92e07d570c631ea35ce1b826e4ee630ba8f5db
diff --git a/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/store/DhcpRecordTest.java b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/store/DhcpRecordTest.java
new file mode 100644
index 0000000..f42e72c
--- /dev/null
+++ b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/store/DhcpRecordTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2017-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.dhcprelay.store;
+
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.DHCP;
+import org.onlab.packet.DHCP6;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip6Address;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+
+import java.util.Optional;
+
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for DHCP record.
+ */
+public class DhcpRecordTest {
+ private static final MacAddress MAC = MacAddress.valueOf("1a:1a:1a:1a:1a:1a");
+ private static final VlanId VLAN = VlanId.vlanId("100");
+ private static final HostId HOST_ID = HostId.hostId(MAC, VLAN);
+ private static final HostLocation HL1 =
+ new HostLocation(ConnectPoint.deviceConnectPoint("of:0000000000000001/1"), 0);
+ private static final HostLocation HL2 =
+ new HostLocation(ConnectPoint.deviceConnectPoint("of:0000000000000001/2"), 0);
+ private static final Ip4Address IP4ADDR = Ip4Address.valueOf("10.0.2.1");
+ private static final MacAddress GW_MAC = MacAddress.valueOf("00:00:00:00:04:01");
+ private static final Ip6Address IP6ADDR = Ip6Address.valueOf("2001::1");
+
+ /**
+ * Test creating a DHCP relay record.
+ */
+ @Test
+ public void testCreateRecord() {
+ DhcpRecord record = new DhcpRecord(HOST_ID)
+ .addLocation(HL1)
+ .addLocation(HL2)
+ .ip4Address(IP4ADDR)
+ .nextHop(GW_MAC)
+ .ip4Status(DHCP.MsgType.DHCPACK)
+ .ip6Address(IP6ADDR)
+ .ip6Status(DHCP6.MsgType.REPLY)
+ .setDirectlyConnected(true);
+
+ assertThat(record.locations().size(), is(2));
+ assertThat(record.locations(), containsInAnyOrder(HL1, HL2));
+ assertThat(record.ip4Address(), is(Optional.of(IP4ADDR)));
+ assertThat(record.nextHop(), is(Optional.of(GW_MAC)));
+ assertThat(record.ip4Status(), is(Optional.of(DHCP.MsgType.DHCPACK)));
+ assertThat(record.ip6Address(), is(Optional.of(IP6ADDR)));
+ assertThat(record.ip6Status(), is(Optional.of(DHCP6.MsgType.REPLY)));
+ assertThat(record.directlyConnected(), is(true));
+
+ DhcpRecord record2 = new DhcpRecord(HOST_ID)
+ .nextHop(GW_MAC)
+ .addLocation(HL2)
+ .ip6Address(IP6ADDR)
+ .addLocation(HL1)
+ .ip6Status(DHCP6.MsgType.REPLY)
+ .ip4Address(IP4ADDR)
+ .ip4Status(DHCP.MsgType.DHCPACK)
+ .setDirectlyConnected(true);
+
+ TestUtils.setField(record, "lastSeen", 0);
+ TestUtils.setField(record2, "lastSeen", 0);
+
+ assertThat(record, equalTo(record2));
+ assertThat(record.hashCode(), equalTo(record2.hashCode()));
+ }
+
+ /**
+ * Test clone a DHCP record.
+ */
+ @Test
+ public void testCloneRecord() {
+ DhcpRecord record = new DhcpRecord(HOST_ID)
+ .addLocation(HL1)
+ .addLocation(HL2)
+ .ip4Address(IP4ADDR)
+ .nextHop(GW_MAC)
+ .ip4Status(DHCP.MsgType.DHCPACK)
+ .ip6Address(IP6ADDR)
+ .ip6Status(DHCP6.MsgType.REPLY)
+ .setDirectlyConnected(true);
+ DhcpRecord clonedRecord = record.clone();
+ assertEquals(record, clonedRecord);
+ }
+}
diff --git a/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/store/DistributedDhcpRelayStoreTest.java b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/store/DistributedDhcpRelayStoreTest.java
new file mode 100644
index 0000000..583bc9d
--- /dev/null
+++ b/apps/dhcprelay/src/test/java/org/onosproject/dhcprelay/store/DistributedDhcpRelayStoreTest.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2017-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.dhcprelay.store;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.onlab.packet.DHCP.MsgType.DHCPREQUEST;
+
+public class DistributedDhcpRelayStoreTest {
+ private static final ConnectPoint CP = ConnectPoint.deviceConnectPoint("of:1/1");
+ private static final MacAddress MAC = MacAddress.valueOf("00:00:00:00:00:01");
+ private static final VlanId VLAN_ID = VlanId.vlanId("100");
+ private static final HostId HOST_ID = HostId.hostId(MAC, VLAN_ID);
+ private static final Ip4Address IP = Ip4Address.valueOf("192.168.1.10");
+ private static final MacAddress GW_MAC = MacAddress.valueOf("00:00:00:00:01:01");
+ private DistributedDhcpRelayStore store;
+
+ @Before
+ public void setup() {
+ store = new DistributedDhcpRelayStore();
+ store.storageService = new TestStorageService();
+ store.activated();
+ }
+
+ @After
+ public void teerDown() {
+ store.deactivated();
+ }
+
+ /**
+ * Puts and removes a record, should received UPDATED and REMOVED event.
+ */
+ @Test
+ public void testPutAndRemoveRecord() {
+ // dhcp request, no IP
+ HostId hostId = HostId.hostId(MAC, VLAN_ID);
+ DhcpRecord record = new DhcpRecord(hostId);
+ record.addLocation(new HostLocation(CP, System.currentTimeMillis()));
+ record.setDirectlyConnected(true);
+ record.nextHop(GW_MAC);
+ record.ip4Status(DHCPREQUEST);
+
+ CompletableFuture<DhcpRelayStoreEvent> recordComplete = new CompletableFuture<>();
+ store.setDelegate(recordComplete::complete);
+ store.updateDhcpRecord(HOST_ID, record);
+ DhcpRelayStoreEvent event = recordComplete.join();
+ assertEquals(record, event.subject());
+ assertEquals(DhcpRelayStoreEvent.Type.UPDATED, event.type());
+ DhcpRecord recordInStore = store.getDhcpRecord(HOST_ID).orElse(null);
+ assertNotNull(recordInStore);
+ assertEquals(record, recordInStore);
+ Collection<DhcpRecord> recordsInStore = store.getDhcpRecords();
+ assertEquals(1, recordsInStore.size());
+ assertEquals(record, recordsInStore.iterator().next());
+
+ // dhcp request, with IP
+ record = new DhcpRecord(hostId);
+ record.addLocation(new HostLocation(CP, System.currentTimeMillis()));
+ record.setDirectlyConnected(true);
+ record.ip4Address(IP);
+ record.nextHop(GW_MAC);
+ record.ip4Status(DHCPREQUEST);
+
+ recordComplete = new CompletableFuture<>();
+ store.setDelegate(recordComplete::complete);
+ store.updateDhcpRecord(HOST_ID, record);
+ event = recordComplete.join();
+ DhcpRecord subject = event.subject();
+ assertEquals(record.locations(), subject.locations());
+ assertEquals(record.vlanId(), subject.vlanId());
+ assertEquals(record.macAddress(), subject.macAddress());
+ assertEquals(record.ip4Address(), subject.ip4Address());
+ assertEquals(record.nextHop(), subject.nextHop());
+ assertEquals(record.ip4Status(), subject.ip4Status());
+ assertEquals(record.ip6Address(), subject.ip6Address());
+ assertEquals(record.ip6Status(), subject.ip6Status());
+ assertEquals(record.directlyConnected(), subject.directlyConnected());
+
+ assertEquals(DhcpRelayStoreEvent.Type.UPDATED, event.type());
+ recordInStore = store.getDhcpRecord(HOST_ID).orElse(null);
+ assertNotNull(recordInStore);
+ assertEquals(record.locations(), recordInStore.locations());
+ assertEquals(record.vlanId(), recordInStore.vlanId());
+ assertEquals(record.macAddress(), recordInStore.macAddress());
+ assertEquals(record.ip4Address(), recordInStore.ip4Address());
+ assertEquals(record.nextHop(), recordInStore.nextHop());
+ assertEquals(record.ip4Status(), recordInStore.ip4Status());
+ assertEquals(record.ip6Address(), recordInStore.ip6Address());
+ assertEquals(record.ip6Status(), recordInStore.ip6Status());
+ assertEquals(record.directlyConnected(), recordInStore.directlyConnected());
+ recordsInStore = store.getDhcpRecords();
+ assertEquals(1, recordsInStore.size());
+
+ // removes record
+ recordComplete = new CompletableFuture<>();
+ store.setDelegate(recordComplete::complete);
+ DhcpRecord removedRecord = store.removeDhcpRecord(HOST_ID).orElse(null);
+ assertEquals(record.locations(), removedRecord.locations());
+ assertEquals(record.vlanId(), removedRecord.vlanId());
+ assertEquals(record.macAddress(), removedRecord.macAddress());
+ assertEquals(record.ip4Address(), removedRecord.ip4Address());
+ assertEquals(record.nextHop(), removedRecord.nextHop());
+ assertEquals(record.ip4Status(), removedRecord.ip4Status());
+ assertEquals(record.ip6Address(), removedRecord.ip6Address());
+ assertEquals(record.ip6Status(), removedRecord.ip6Status());
+ assertEquals(record.directlyConnected(), removedRecord.directlyConnected());
+ event = recordComplete.join();
+ assertEquals(null, event.subject());
+ assertEquals(DhcpRelayStoreEvent.Type.REMOVED, event.type());
+ recordInStore = store.getDhcpRecord(HOST_ID).orElse(null);
+ assertNull(recordInStore);
+ recordsInStore = store.getDhcpRecords();
+ assertEquals(0, recordsInStore.size());
+ }
+}