Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.dhcprelay.store; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.HostId; |
| 27 | import org.onosproject.net.HostLocation; |
| 28 | import org.onosproject.store.service.TestStorageService; |
| 29 | |
| 30 | import java.util.Collection; |
| 31 | import java.util.concurrent.CompletableFuture; |
| 32 | |
| 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.junit.Assert.assertNotNull; |
| 35 | import static org.junit.Assert.assertNull; |
| 36 | import static org.onlab.packet.DHCP.MsgType.DHCPREQUEST; |
| 37 | |
| 38 | public class DistributedDhcpRelayStoreTest { |
| 39 | private static final ConnectPoint CP = ConnectPoint.deviceConnectPoint("of:1/1"); |
| 40 | private static final MacAddress MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 41 | private static final VlanId VLAN_ID = VlanId.vlanId("100"); |
| 42 | private static final HostId HOST_ID = HostId.hostId(MAC, VLAN_ID); |
| 43 | private static final Ip4Address IP = Ip4Address.valueOf("192.168.1.10"); |
| 44 | private static final MacAddress GW_MAC = MacAddress.valueOf("00:00:00:00:01:01"); |
| 45 | private DistributedDhcpRelayStore store; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | store = new DistributedDhcpRelayStore(); |
| 50 | store.storageService = new TestStorageService(); |
| 51 | store.activated(); |
| 52 | } |
| 53 | |
| 54 | @After |
| 55 | public void teerDown() { |
| 56 | store.deactivated(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Puts and removes a record, should received UPDATED and REMOVED event. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testPutAndRemoveRecord() { |
| 64 | // dhcp request, no IP |
| 65 | HostId hostId = HostId.hostId(MAC, VLAN_ID); |
| 66 | DhcpRecord record = new DhcpRecord(hostId); |
| 67 | record.addLocation(new HostLocation(CP, System.currentTimeMillis())); |
| 68 | record.setDirectlyConnected(true); |
| 69 | record.nextHop(GW_MAC); |
| 70 | record.ip4Status(DHCPREQUEST); |
| 71 | |
| 72 | CompletableFuture<DhcpRelayStoreEvent> recordComplete = new CompletableFuture<>(); |
| 73 | store.setDelegate(recordComplete::complete); |
| 74 | store.updateDhcpRecord(HOST_ID, record); |
| 75 | DhcpRelayStoreEvent event = recordComplete.join(); |
| 76 | assertEquals(record, event.subject()); |
| 77 | assertEquals(DhcpRelayStoreEvent.Type.UPDATED, event.type()); |
| 78 | DhcpRecord recordInStore = store.getDhcpRecord(HOST_ID).orElse(null); |
| 79 | assertNotNull(recordInStore); |
| 80 | assertEquals(record, recordInStore); |
| 81 | Collection<DhcpRecord> recordsInStore = store.getDhcpRecords(); |
| 82 | assertEquals(1, recordsInStore.size()); |
| 83 | assertEquals(record, recordsInStore.iterator().next()); |
| 84 | |
| 85 | // dhcp request, with IP |
| 86 | record = new DhcpRecord(hostId); |
| 87 | record.addLocation(new HostLocation(CP, System.currentTimeMillis())); |
| 88 | record.setDirectlyConnected(true); |
| 89 | record.ip4Address(IP); |
| 90 | record.nextHop(GW_MAC); |
| 91 | record.ip4Status(DHCPREQUEST); |
| 92 | |
| 93 | recordComplete = new CompletableFuture<>(); |
| 94 | store.setDelegate(recordComplete::complete); |
| 95 | store.updateDhcpRecord(HOST_ID, record); |
| 96 | event = recordComplete.join(); |
| 97 | DhcpRecord subject = event.subject(); |
| 98 | assertEquals(record.locations(), subject.locations()); |
| 99 | assertEquals(record.vlanId(), subject.vlanId()); |
| 100 | assertEquals(record.macAddress(), subject.macAddress()); |
| 101 | assertEquals(record.ip4Address(), subject.ip4Address()); |
| 102 | assertEquals(record.nextHop(), subject.nextHop()); |
| 103 | assertEquals(record.ip4Status(), subject.ip4Status()); |
| 104 | assertEquals(record.ip6Address(), subject.ip6Address()); |
| 105 | assertEquals(record.ip6Status(), subject.ip6Status()); |
| 106 | assertEquals(record.directlyConnected(), subject.directlyConnected()); |
| 107 | |
| 108 | assertEquals(DhcpRelayStoreEvent.Type.UPDATED, event.type()); |
| 109 | recordInStore = store.getDhcpRecord(HOST_ID).orElse(null); |
| 110 | assertNotNull(recordInStore); |
| 111 | assertEquals(record.locations(), recordInStore.locations()); |
| 112 | assertEquals(record.vlanId(), recordInStore.vlanId()); |
| 113 | assertEquals(record.macAddress(), recordInStore.macAddress()); |
| 114 | assertEquals(record.ip4Address(), recordInStore.ip4Address()); |
| 115 | assertEquals(record.nextHop(), recordInStore.nextHop()); |
| 116 | assertEquals(record.ip4Status(), recordInStore.ip4Status()); |
| 117 | assertEquals(record.ip6Address(), recordInStore.ip6Address()); |
| 118 | assertEquals(record.ip6Status(), recordInStore.ip6Status()); |
| 119 | assertEquals(record.directlyConnected(), recordInStore.directlyConnected()); |
| 120 | recordsInStore = store.getDhcpRecords(); |
| 121 | assertEquals(1, recordsInStore.size()); |
| 122 | |
| 123 | // removes record |
| 124 | recordComplete = new CompletableFuture<>(); |
| 125 | store.setDelegate(recordComplete::complete); |
| 126 | DhcpRecord removedRecord = store.removeDhcpRecord(HOST_ID).orElse(null); |
| 127 | assertEquals(record.locations(), removedRecord.locations()); |
| 128 | assertEquals(record.vlanId(), removedRecord.vlanId()); |
| 129 | assertEquals(record.macAddress(), removedRecord.macAddress()); |
| 130 | assertEquals(record.ip4Address(), removedRecord.ip4Address()); |
| 131 | assertEquals(record.nextHop(), removedRecord.nextHop()); |
| 132 | assertEquals(record.ip4Status(), removedRecord.ip4Status()); |
| 133 | assertEquals(record.ip6Address(), removedRecord.ip6Address()); |
| 134 | assertEquals(record.ip6Status(), removedRecord.ip6Status()); |
| 135 | assertEquals(record.directlyConnected(), removedRecord.directlyConnected()); |
| 136 | event = recordComplete.join(); |
| 137 | assertEquals(null, event.subject()); |
| 138 | assertEquals(DhcpRelayStoreEvent.Type.REMOVED, event.type()); |
| 139 | recordInStore = store.getDhcpRecord(HOST_ID).orElse(null); |
| 140 | assertNull(recordInStore); |
| 141 | recordsInStore = store.getDhcpRecords(); |
| 142 | assertEquals(0, recordsInStore.size()); |
| 143 | } |
| 144 | } |