Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -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 | 7a38f9a | 2017-06-09 14:36:40 -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 | package org.onosproject.dhcprelay; |
| 17 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Streams; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 26 | import com.google.common.io.Resources; |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 27 | import org.apache.commons.io.Charsets; |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 28 | import org.apache.commons.lang3.tuple.Pair; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 29 | import org.easymock.Capture; |
| 30 | import org.easymock.CaptureType; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 31 | import org.easymock.EasyMock; |
| 32 | import org.junit.After; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.onlab.packet.ARP; |
| 36 | import org.onlab.packet.DHCP; |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 37 | import org.onlab.packet.DeserializationException; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 38 | import org.onlab.packet.Ethernet; |
| 39 | import org.onlab.packet.IPv4; |
| 40 | import org.onlab.packet.Ip4Address; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 41 | import org.onlab.packet.Ip6Address; |
| 42 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 43 | import org.onlab.packet.IpAddress; |
| 44 | import org.onlab.packet.MacAddress; |
| 45 | import org.onlab.packet.UDP; |
| 46 | import org.onlab.packet.VlanId; |
| 47 | import org.onlab.packet.dhcp.CircuitId; |
| 48 | import org.onlab.packet.dhcp.DhcpOption; |
| 49 | import org.onlab.packet.dhcp.DhcpRelayAgentOption; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 50 | import org.onlab.packet.dhcp.Dhcp6InterfaceIdOption; |
| 51 | import org.onlab.packet.dhcp.Dhcp6RelayOption; |
| 52 | import org.onlab.packet.dhcp.Dhcp6IaNaOption; |
| 53 | import org.onlab.packet.dhcp.Dhcp6IaPdOption; |
| 54 | import org.onlab.packet.dhcp.Dhcp6IaAddressOption; |
| 55 | import org.onlab.packet.dhcp.Dhcp6IaPrefixOption; |
| 56 | import org.onlab.packet.dhcp.Dhcp6Option; |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 57 | import org.onlab.packet.dhcp.Dhcp6ClientIdOption; |
| 58 | import org.onlab.packet.dhcp.Dhcp6Duid; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 59 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
| 60 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 61 | import org.onosproject.dhcprelay.store.DhcpRelayStoreEvent; |
| 62 | import org.onosproject.dhcprelay.store.DhcpRelayCounters; |
| 63 | import org.onosproject.dhcprelay.store.DhcpRelayCountersStore; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 64 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 65 | import org.onosproject.TestApplicationId; |
| 66 | import org.onosproject.cfg.ComponentConfigService; |
| 67 | import org.onosproject.core.ApplicationId; |
| 68 | import org.onosproject.core.CoreService; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 69 | import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig; |
| 70 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 71 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 72 | import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 73 | import org.onosproject.net.Device; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 74 | import org.onosproject.net.DeviceId; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 75 | import org.onosproject.net.behaviour.Pipeliner; |
| 76 | import org.onosproject.net.device.DeviceEvent; |
| 77 | import org.onosproject.net.device.DeviceService; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 78 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 79 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 80 | import org.onosproject.net.flow.TrafficSelector; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 81 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 82 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 83 | import org.onosproject.net.flowobjective.Objective; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 84 | import org.onosproject.net.host.HostProviderService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 85 | import org.onosproject.net.intf.Interface; |
| 86 | import org.onosproject.net.intf.InterfaceServiceAdapter; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 87 | import org.onosproject.net.packet.PacketPriority; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 88 | import org.onosproject.routeservice.Route; |
| 89 | import org.onosproject.routeservice.RouteStoreAdapter; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 90 | import org.onosproject.net.DefaultHost; |
| 91 | import org.onosproject.net.Host; |
| 92 | import org.onosproject.net.HostId; |
| 93 | import org.onosproject.net.HostLocation; |
| 94 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 95 | import org.onosproject.net.host.HostDescription; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 96 | import org.onosproject.net.host.HostService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 97 | import org.onosproject.net.host.InterfaceIpAddress; |
| 98 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 99 | import org.onosproject.net.packet.InboundPacket; |
| 100 | import org.onosproject.net.packet.OutboundPacket; |
| 101 | import org.onosproject.net.packet.PacketContext; |
| 102 | import org.onosproject.net.packet.PacketContextAdapter; |
| 103 | import org.onosproject.net.packet.PacketProcessor; |
| 104 | import org.onosproject.net.packet.PacketServiceAdapter; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 105 | import org.onosproject.store.StoreDelegate; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | |
| 109 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 110 | import org.osgi.service.component.ComponentContext; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 111 | import org.onlab.packet.DHCP6; |
| 112 | import org.onlab.packet.IPv6; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 113 | |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 114 | import java.io.IOException; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 115 | import java.nio.ByteBuffer; |
| 116 | import java.util.Collection; |
| 117 | import java.util.Dictionary; |
| 118 | import java.util.List; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 119 | import java.util.ArrayList; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 120 | import java.util.Map; |
| 121 | import java.util.Optional; |
| 122 | import java.util.Set; |
| 123 | import java.util.stream.Collectors; |
| 124 | |
| 125 | import static org.easymock.EasyMock.*; |
| 126 | import static org.junit.Assert.*; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 127 | import static org.onosproject.dhcprelay.DhcpRelayManager.DHCP_RELAY_APP; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 128 | |
| 129 | public class DhcpRelayManagerTest { |
Kalhee Kim | a0b8f65 | 2017-10-18 22:00:30 +0000 | [diff] [blame] | 130 | private static final short VLAN_LEN = 2; |
| 131 | private static final short SEPARATOR_LEN = 1; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 132 | private static final String CONFIG_FILE_PATH = "dhcp-relay.json"; |
| 133 | private static final DeviceId DEV_1_ID = DeviceId.deviceId("of:0000000000000001"); |
| 134 | private static final DeviceId DEV_2_ID = DeviceId.deviceId("of:0000000000000002"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 135 | // Ip address for interfaces |
| 136 | private static final InterfaceIpAddress INTERFACE_IP = InterfaceIpAddress.valueOf("10.0.3.254/32"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 137 | private static final InterfaceIpAddress INTERFACE_IP_V6 = InterfaceIpAddress.valueOf("2001:db8:1::254/128"); |
| 138 | private static final List<InterfaceIpAddress> INTERFACE_IPS = ImmutableList.of(INTERFACE_IP, INTERFACE_IP_V6); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 139 | |
| 140 | // DHCP client (will send without option 82) |
| 141 | private static final Ip4Address IP_FOR_CLIENT = Ip4Address.valueOf("10.0.0.1"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 142 | private static final Ip6Address IP_FOR_CLIENT_V6 = Ip6Address.valueOf("2001:db8:1::110"); |
| 143 | private static final IpPrefix PREFIX_FOR_CLIENT_V6 = IpPrefix.valueOf("2001:db8:10::/56"); |
| 144 | private static final IpPrefix PREFIX_FOR_ZERO = IpPrefix.valueOf("::/0"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 145 | private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 146 | private static final VlanId CLIENT_VLAN = VlanId.vlanId("100"); |
| 147 | private static final ConnectPoint CLIENT_CP = ConnectPoint.deviceConnectPoint("of:0000000000000001/1"); |
| 148 | private static final MacAddress CLIENT_IFACE_MAC = MacAddress.valueOf("00:00:00:00:11:01"); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 149 | private static final HostLocation CLIENT_LOCATION = new HostLocation(CLIENT_CP, 0); |
| 150 | private static final HostId CLIENT_HOST_ID = HostId.hostId(CLIENT_MAC, CLIENT_VLAN); |
| 151 | private static final Ip6Address CLIENT_LL_IP_V6 = Ip6Address.valueOf("fe80::200:00ff:fe00:0001"); |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 152 | private static final Host EXISTS_HOST = new DefaultHost(Dhcp4HandlerImpl.PROVIDER_ID, |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 153 | CLIENT_HOST_ID, CLIENT_MAC, CLIENT_VLAN, |
| 154 | CLIENT_LOCATION, ImmutableSet.of(CLIENT_LL_IP_V6)); |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 155 | private static final Interface CLIENT_INTERFACE = createInterface("C1", |
| 156 | CLIENT_CP, |
| 157 | INTERFACE_IPS, |
| 158 | CLIENT_IFACE_MAC, |
| 159 | CLIENT_VLAN, |
| 160 | null); |
| 161 | |
| 162 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 163 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 164 | // Dual homing test |
| 165 | private static final ConnectPoint CLIENT_DH_CP = ConnectPoint.deviceConnectPoint("of:0000000000000001/3"); |
| 166 | private static final HostLocation CLIENT_DH_LOCATION = new HostLocation(CLIENT_DH_CP, 0); |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 167 | private static final Interface CLIENT_DH_INTERFACE = createInterface("C1-DH", |
| 168 | CLIENT_DH_CP, |
| 169 | INTERFACE_IPS, |
| 170 | CLIENT_IFACE_MAC, |
| 171 | CLIENT_VLAN, |
| 172 | null); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 173 | |
| 174 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 175 | // DHCP client 2 (will send with option 82, so the vlan should equals to vlan from server) |
| 176 | private static final MacAddress CLIENT2_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 177 | private static final VlanId CLIENT2_VLAN = VlanId.NONE; |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 178 | private static final VlanId CLIENT2_VLAN_NATIVE = VlanId.vlanId("20"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 179 | private static final ConnectPoint CLIENT2_CP = ConnectPoint.deviceConnectPoint("of:0000000000000001/2"); |
| 180 | private static final MacAddress CLIENT2_IFACE_MAC = MacAddress.valueOf("00:00:00:00:11:01"); |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 181 | private static final Interface CLIENT2_INTERFACE = createInterface("C2", |
| 182 | CLIENT2_CP, |
| 183 | INTERFACE_IPS, |
| 184 | CLIENT2_IFACE_MAC, |
| 185 | CLIENT2_VLAN, |
| 186 | CLIENT2_VLAN_NATIVE); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 187 | private static final VlanId CLIENT_BOGUS_VLAN = VlanId.vlanId("108"); |
| 188 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 189 | // Outer relay information |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 190 | private static final Ip4Address OUTER_RELAY_IP = Ip4Address.valueOf("10.0.6.253"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 191 | private static final Ip6Address OUTER_RELAY_IP_V6 = Ip6Address.valueOf("2001:db8:1::4"); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 192 | private static final Ip6Address OUTER_RELAY_LL_IP_V6 = Ip6Address.valueOf("fe80::200:0102:0304:0501"); |
| 193 | private static final Set<IpAddress> OUTER_RELAY_IPS = ImmutableSet.of(OUTER_RELAY_IP, |
| 194 | OUTER_RELAY_IP_V6, |
| 195 | OUTER_RELAY_LL_IP_V6); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 196 | private static final MacAddress OUTER_RELAY_MAC = MacAddress.valueOf("00:01:02:03:04:05"); |
| 197 | private static final VlanId OUTER_RELAY_VLAN = VlanId.NONE; |
| 198 | private static final ConnectPoint OUTER_RELAY_CP = ConnectPoint.deviceConnectPoint("of:0000000000000001/2"); |
| 199 | private static final HostLocation OUTER_REPLAY_HL = new HostLocation(OUTER_RELAY_CP, 0); |
| 200 | private static final HostId OUTER_RELAY_HOST_ID = HostId.hostId(OUTER_RELAY_MAC, OUTER_RELAY_VLAN); |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 201 | private static final Host OUTER_RELAY_HOST = new DefaultHost(Dhcp4HandlerImpl.PROVIDER_ID, |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 202 | OUTER_RELAY_HOST_ID, |
| 203 | OUTER_RELAY_MAC, |
| 204 | OUTER_RELAY_VLAN, |
| 205 | OUTER_REPLAY_HL, |
| 206 | OUTER_RELAY_IPS); |
| 207 | |
| 208 | // DHCP Server |
| 209 | private static final MacAddress SERVER_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 210 | private static final VlanId SERVER_VLAN = VlanId.NONE; |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 211 | private static final VlanId SERVER_VLAN_NATIVE = VlanId.vlanId("10"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 212 | private static final ConnectPoint SERVER_CONNECT_POINT = |
| 213 | ConnectPoint.deviceConnectPoint("of:0000000000000001/5"); |
| 214 | private static final HostLocation SERVER_LOCATION = |
| 215 | new HostLocation(SERVER_CONNECT_POINT, 0); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 216 | private static final Ip4Address GATEWAY_IP = Ip4Address.valueOf("10.0.5.253"); |
| 217 | private static final Ip6Address GATEWAY_IP_V6 = Ip6Address.valueOf("2000::105:253"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 218 | private static final Ip4Address SERVER_IP = Ip4Address.valueOf("10.0.3.253"); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 219 | private static final Ip6Address SERVER_IP_V6 = Ip6Address.valueOf("2000::103:253"); |
| 220 | private static final Ip6Address SERVER_IP_V6_MCAST = Ip6Address.valueOf("ff02::1:2"); |
| 221 | private static final Set<IpAddress> DHCP_SERVER_IPS = ImmutableSet.of(SERVER_IP, SERVER_IP_V6); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 222 | private static final HostId SERVER_HOST_ID = HostId.hostId(SERVER_MAC, SERVER_VLAN); |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 223 | private static final Host SERVER_HOST = new DefaultHost(Dhcp4HandlerImpl.PROVIDER_ID, |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 224 | SERVER_HOST_ID, |
| 225 | SERVER_MAC, |
| 226 | SERVER_VLAN, |
| 227 | SERVER_LOCATION, |
| 228 | DHCP_SERVER_IPS); |
| 229 | private static final MacAddress SERVER_IFACE_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 230 | private static final Interface SERVER_INTERFACE = createInterface("SERVER", |
| 231 | SERVER_CONNECT_POINT, |
| 232 | INTERFACE_IPS, |
| 233 | SERVER_IFACE_MAC, |
| 234 | SERVER_VLAN, |
| 235 | SERVER_VLAN_NATIVE); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 236 | |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 237 | // Relay agent config |
| 238 | private static final Ip4Address RELAY_AGENT_IP = Ip4Address.valueOf("10.0.4.254"); |
| 239 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 240 | private static final List<TrafficSelector> DHCP_SELECTORS = buildClientDhcpSelectors(); |
| 241 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 242 | // Components |
| 243 | private static final ApplicationId APP_ID = TestApplicationId.create(DhcpRelayManager.DHCP_RELAY_APP); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 244 | private static final DefaultDhcpRelayConfig CONFIG = new MockDefaultDhcpRelayConfig(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 245 | private static final IndirectDhcpRelayConfig CONFIG_INDIRECT = new MockIndirectDhcpRelayConfig(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 246 | private static final Set<Interface> INTERFACES = ImmutableSet.of( |
| 247 | CLIENT_INTERFACE, |
| 248 | CLIENT2_INTERFACE, |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 249 | SERVER_INTERFACE, |
| 250 | CLIENT_DH_INTERFACE |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 251 | ); |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 252 | private static final String NON_ONOS_CID = "Non-ONOS circuit ID"; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 253 | private static final VlanId IGNORED_VLAN = VlanId.vlanId("100"); |
| 254 | private static final int IGNORE_CONTROL_PRIORITY = PacketPriority.CONTROL.priorityValue() + 1000; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 255 | |
| 256 | private DhcpRelayManager manager; |
| 257 | private MockPacketService packetService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 258 | private MockRouteStore mockRouteStore; |
| 259 | private MockDhcpRelayStore mockDhcpRelayStore; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 260 | private MockDhcpRelayCountersStore mockDhcpRelayCountersStore; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 261 | private HostProviderService mockHostProviderService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 262 | private FlowObjectiveService flowObjectiveService; |
| 263 | private DeviceService deviceService; |
| 264 | private Dhcp4HandlerImpl v4Handler; |
| 265 | private Dhcp6HandlerImpl v6Handler; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 266 | |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 267 | private static Interface createInterface(String name, ConnectPoint connectPoint, |
| 268 | List<InterfaceIpAddress> interfaceIps, |
| 269 | MacAddress macAddress, |
| 270 | VlanId vlanId, |
| 271 | VlanId vlanNative) { |
| 272 | |
| 273 | if (vlanId.equals(VlanId.NONE)) { |
| 274 | return new Interface(name, connectPoint, interfaceIps, macAddress, vlanId, |
| 275 | null, null, vlanNative); |
| 276 | } else { |
| 277 | return new Interface(name, connectPoint, interfaceIps, macAddress, vlanId, |
| 278 | null, ImmutableSet.of(vlanId), null); |
| 279 | } |
| 280 | } |
| 281 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 282 | @Before |
| 283 | public void setup() { |
| 284 | manager = new DhcpRelayManager(); |
| 285 | manager.cfgService = createNiceMock(NetworkConfigRegistry.class); |
| 286 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 287 | expect(manager.cfgService.getConfig(APP_ID, DefaultDhcpRelayConfig.class)) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 288 | .andReturn(CONFIG) |
| 289 | .anyTimes(); |
| 290 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 291 | expect(manager.cfgService.getConfig(APP_ID, IndirectDhcpRelayConfig.class)) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 292 | .andReturn(CONFIG_INDIRECT) |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 293 | .anyTimes(); |
| 294 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 295 | manager.coreService = createNiceMock(CoreService.class); |
| 296 | expect(manager.coreService.registerApplication(anyString())) |
| 297 | .andReturn(APP_ID).anyTimes(); |
| 298 | |
| 299 | manager.hostService = createNiceMock(HostService.class); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 300 | |
| 301 | expect(manager.hostService.getHostsByIp(OUTER_RELAY_IP_V6)) |
| 302 | .andReturn(ImmutableSet.of(OUTER_RELAY_HOST)).anyTimes(); |
| 303 | expect(manager.hostService.getHostsByIp(SERVER_IP)) |
| 304 | .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes(); |
| 305 | expect(manager.hostService.getHostsByIp(SERVER_IP_V6)) |
| 306 | .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes(); |
| 307 | expect(manager.hostService.getHostsByIp(GATEWAY_IP)) |
| 308 | .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes(); |
| 309 | expect(manager.hostService.getHostsByIp(GATEWAY_IP_V6)) |
| 310 | .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes(); |
| 311 | expect(manager.hostService.getHostsByIp(CLIENT_LL_IP_V6)) |
| 312 | .andReturn(ImmutableSet.of(EXISTS_HOST)).anyTimes(); |
| 313 | |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 314 | expect(manager.hostService.getHost(OUTER_RELAY_HOST_ID)).andReturn(OUTER_RELAY_HOST).anyTimes(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 315 | |
| 316 | packetService = new MockPacketService(); |
| 317 | manager.packetService = packetService; |
| 318 | manager.compCfgService = createNiceMock(ComponentConfigService.class); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 319 | deviceService = createNiceMock(DeviceService.class); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 320 | |
| 321 | Device device = createNiceMock(Device.class); |
| 322 | expect(device.is(Pipeliner.class)).andReturn(true).anyTimes(); |
| 323 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 324 | expect(deviceService.getDevice(DEV_1_ID)).andReturn(device).anyTimes(); |
| 325 | expect(deviceService.getDevice(DEV_2_ID)).andReturn(device).anyTimes(); |
| 326 | replay(deviceService, device); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 327 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 328 | mockRouteStore = new MockRouteStore(); |
| 329 | mockDhcpRelayStore = new MockDhcpRelayStore(); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 330 | mockDhcpRelayCountersStore = new MockDhcpRelayCountersStore(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 331 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 332 | manager.dhcpRelayStore = mockDhcpRelayStore; |
| 333 | |
| 334 | manager.deviceService = deviceService; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 335 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 336 | manager.interfaceService = new MockInterfaceService(); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 337 | flowObjectiveService = EasyMock.niceMock(FlowObjectiveService.class); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 338 | mockHostProviderService = createNiceMock(HostProviderService.class); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 339 | v4Handler = new Dhcp4HandlerImpl(); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 340 | v4Handler.providerService = mockHostProviderService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 341 | v4Handler.dhcpRelayStore = mockDhcpRelayStore; |
| 342 | v4Handler.hostService = manager.hostService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 343 | v4Handler.interfaceService = manager.interfaceService; |
| 344 | v4Handler.packetService = manager.packetService; |
| 345 | v4Handler.routeStore = mockRouteStore; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 346 | v4Handler.coreService = createNiceMock(CoreService.class); |
| 347 | v4Handler.flowObjectiveService = flowObjectiveService; |
| 348 | v4Handler.appId = TestApplicationId.create(Dhcp4HandlerImpl.DHCP_V4_RELAY_APP); |
| 349 | v4Handler.deviceService = deviceService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 350 | manager.v4Handler = v4Handler; |
| 351 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 352 | v6Handler = new Dhcp6HandlerImpl(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 353 | v6Handler.dhcpRelayStore = mockDhcpRelayStore; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 354 | v6Handler.dhcpRelayCountersStore = mockDhcpRelayCountersStore; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 355 | v6Handler.hostService = manager.hostService; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 356 | v6Handler.interfaceService = manager.interfaceService; |
| 357 | v6Handler.packetService = manager.packetService; |
| 358 | v6Handler.routeStore = mockRouteStore; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 359 | v6Handler.providerService = mockHostProviderService; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 360 | v6Handler.coreService = createNiceMock(CoreService.class); |
| 361 | v6Handler.flowObjectiveService = flowObjectiveService; |
| 362 | v6Handler.appId = TestApplicationId.create(Dhcp6HandlerImpl.DHCP_V6_RELAY_APP); |
| 363 | v6Handler.deviceService = deviceService; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 364 | manager.v6Handler = v6Handler; |
| 365 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 366 | // properties |
| 367 | Dictionary<String, Object> dictionary = createNiceMock(Dictionary.class); |
| 368 | expect(dictionary.get("arpEnabled")).andReturn(true).anyTimes(); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 369 | expect(dictionary.get("dhcpPollInterval")).andReturn(120).anyTimes(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 370 | ComponentContext context = createNiceMock(ComponentContext.class); |
| 371 | expect(context.getProperties()).andReturn(dictionary).anyTimes(); |
| 372 | |
Yi Tseng | 4025a10 | 2017-09-30 11:35:42 +0800 | [diff] [blame] | 373 | replay(manager.cfgService, manager.coreService, manager.hostService, |
| 374 | manager.compCfgService, dictionary, context); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 375 | manager.activate(context); |
| 376 | } |
| 377 | |
| 378 | @After |
| 379 | public void tearDown() { |
| 380 | manager.deactivate(); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Relay a DHCP packet without option 82. |
| 385 | * Should add new host to host store after dhcp ack. |
| 386 | */ |
| 387 | @Test |
| 388 | public void relayDhcpWithoutAgentInfo() { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 389 | replay(mockHostProviderService); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 390 | // send request |
| 391 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT_MAC, |
| 392 | CLIENT_VLAN, |
| 393 | CLIENT_CP, |
| 394 | INTERFACE_IP.ipAddress().getIp4Address(), |
| 395 | false)); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 396 | // won't trigger the host provider service |
| 397 | verify(mockHostProviderService); |
| 398 | reset(mockHostProviderService); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 399 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 400 | assertEquals(0, mockRouteStore.routes.size()); |
| 401 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 402 | HostId expectHostId = HostId.hostId(CLIENT_MAC, CLIENT_VLAN); |
| 403 | Capture<HostDescription> capturedHostDesc = newCapture(); |
| 404 | mockHostProviderService.hostDetected(eq(expectHostId), capture(capturedHostDesc), eq(false)); |
| 405 | replay(mockHostProviderService); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 406 | // send ack |
| 407 | packetService.processPacket(new TestDhcpAckPacketContext(CLIENT_CP, CLIENT_MAC, |
| 408 | CLIENT_VLAN, INTERFACE_IP.ipAddress().getIp4Address(), |
| 409 | false)); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 410 | verify(mockHostProviderService); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 411 | assertEquals(0, mockRouteStore.routes.size()); |
| 412 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 413 | HostDescription host = capturedHostDesc.getValue(); |
| 414 | assertEquals(false, host.configured()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 415 | assertEquals(CLIENT_CP.deviceId(), host.location().elementId()); |
| 416 | assertEquals(CLIENT_CP.port(), host.location().port()); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 417 | assertEquals(1, host.ipAddress().size()); |
| 418 | assertEquals(IP_FOR_CLIENT, host.ipAddress().iterator().next()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Relay a DHCP packet with option 82 (Indirectly connected host). |
| 423 | */ |
| 424 | @Test |
| 425 | public void relayDhcpWithAgentInfo() { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 426 | replay(mockHostProviderService); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 427 | // Assume outer dhcp relay agent exists in store already |
| 428 | // send request |
| 429 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT2_MAC, |
| 430 | CLIENT2_VLAN, |
| 431 | CLIENT2_CP, |
| 432 | INTERFACE_IP.ipAddress().getIp4Address(), |
| 433 | true)); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 434 | // No routes |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 435 | assertEquals(0, mockRouteStore.routes.size()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 436 | // send ack |
| 437 | packetService.processPacket(new TestDhcpAckPacketContext(CLIENT2_CP, |
| 438 | CLIENT2_MAC, |
| 439 | CLIENT2_VLAN, |
| 440 | INTERFACE_IP.ipAddress().getIp4Address(), |
| 441 | true)); |
| 442 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 443 | // won't trigger the host provider service |
| 444 | verify(mockHostProviderService); |
| 445 | reset(mockHostProviderService); |
| 446 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 447 | assertEquals(1, mockRouteStore.routes.size()); |
| 448 | |
| 449 | Route route = mockRouteStore.routes.get(0); |
| 450 | assertEquals(OUTER_RELAY_IP, route.nextHop()); |
| 451 | assertEquals(IP_FOR_CLIENT.toIpPrefix(), route.prefix()); |
Charles Chan | 6305b69 | 2018-04-04 11:43:54 -0700 | [diff] [blame] | 452 | assertEquals(Route.Source.DHCP, route.source()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | @Test |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 456 | public void testWithRelayAgentConfig() throws DeserializationException { |
| 457 | manager.v4Handler |
| 458 | .setDefaultDhcpServerConfigs(ImmutableList.of(new MockDhcpServerConfig(RELAY_AGENT_IP))); |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 459 | manager.v4Handler |
| 460 | .setIndirectDhcpServerConfigs(ImmutableList.of(new MockDhcpServerConfig(RELAY_AGENT_IP))); |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 461 | packetService.processPacket(new TestDhcpRequestPacketContext(CLIENT2_MAC, |
| 462 | CLIENT2_VLAN, |
| 463 | CLIENT2_CP, |
| 464 | INTERFACE_IP.ipAddress().getIp4Address(), |
| 465 | true)); |
| 466 | OutboundPacket outPacket = packetService.emittedPacket; |
| 467 | byte[] outData = outPacket.data().array(); |
| 468 | Ethernet eth = Ethernet.deserializer().deserialize(outData, 0, outData.length); |
| 469 | IPv4 ip = (IPv4) eth.getPayload(); |
| 470 | UDP udp = (UDP) ip.getPayload(); |
| 471 | DHCP dhcp = (DHCP) udp.getPayload(); |
| 472 | assertEquals(RELAY_AGENT_IP.toInt(), dhcp.getGatewayIPAddress()); |
| 473 | } |
| 474 | |
| 475 | @Test |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 476 | public void testArpRequest() throws Exception { |
| 477 | packetService.processPacket(new TestArpRequestPacketContext(CLIENT_INTERFACE)); |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 478 | OutboundPacket outboundPacket = packetService.emittedPacket; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 479 | byte[] outPacketData = outboundPacket.data().array(); |
| 480 | Ethernet eth = Ethernet.deserializer().deserialize(outPacketData, 0, outPacketData.length); |
| 481 | |
| 482 | assertEquals(eth.getEtherType(), Ethernet.TYPE_ARP); |
| 483 | ARP arp = (ARP) eth.getPayload(); |
| 484 | assertArrayEquals(arp.getSenderHardwareAddress(), CLIENT_INTERFACE.mac().toBytes()); |
| 485 | } |
| 486 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 487 | /** |
| 488 | * Ignores specific vlans from specific devices if config. |
| 489 | * |
| 490 | * @throws Exception the exception from this test |
| 491 | */ |
| 492 | @Test |
| 493 | public void testIgnoreVlan() throws Exception { |
| 494 | ObjectMapper om = new ObjectMapper(); |
| 495 | JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH)); |
| 496 | IgnoreDhcpConfig config = new IgnoreDhcpConfig(); |
| 497 | json = json.path("apps").path(DHCP_RELAY_APP).path(IgnoreDhcpConfig.KEY); |
| 498 | config.init(APP_ID, IgnoreDhcpConfig.KEY, json, om, null); |
| 499 | |
| 500 | Capture<Objective> capturedFromDev1 = newCapture(CaptureType.ALL); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 501 | flowObjectiveService.apply(eq(DEV_1_ID), capture(capturedFromDev1)); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 502 | expectLastCall().times(DHCP_SELECTORS.size()); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 503 | Capture<Objective> capturedFromDev2 = newCapture(CaptureType.ALL); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 504 | flowObjectiveService.apply(eq(DEV_2_ID), capture(capturedFromDev2)); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 505 | expectLastCall().times(DHCP_SELECTORS.size()); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 506 | replay(flowObjectiveService); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 507 | manager.updateConfig(config); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 508 | verify(flowObjectiveService); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 509 | |
| 510 | List<Objective> objectivesFromDev1 = capturedFromDev1.getValues(); |
| 511 | List<Objective> objectivesFromDev2 = capturedFromDev2.getValues(); |
| 512 | |
| 513 | assertTrue(objectivesFromDev1.containsAll(objectivesFromDev2)); |
| 514 | assertTrue(objectivesFromDev2.containsAll(objectivesFromDev1)); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 515 | |
| 516 | for (int index = 0; index < objectivesFromDev1.size(); index++) { |
| 517 | TrafficSelector selector = |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 518 | DefaultTrafficSelector.builder(DHCP_SELECTORS.get(index)) |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 519 | .matchVlanId(IGNORED_VLAN) |
| 520 | .build(); |
| 521 | ForwardingObjective fwd = (ForwardingObjective) objectivesFromDev1.get(index); |
| 522 | assertEquals(selector, fwd.selector()); |
Yi Tseng | dbabeed | 2017-11-29 10:49:20 -0800 | [diff] [blame] | 523 | assertEquals(DefaultTrafficTreatment.emptyTreatment(), fwd.treatment()); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 524 | assertEquals(IGNORE_CONTROL_PRIORITY, fwd.priority()); |
| 525 | assertEquals(ForwardingObjective.Flag.VERSATILE, fwd.flag()); |
| 526 | assertEquals(Objective.Operation.ADD, fwd.op()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 527 | fwd.context().ifPresent(ctx -> { |
| 528 | ctx.onSuccess(fwd); |
| 529 | }); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 530 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 531 | objectivesFromDev2.forEach(obj -> obj.context().ifPresent(ctx -> ctx.onSuccess(obj))); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 532 | assertEquals(2, v4Handler.ignoredVlans.size()); |
| 533 | assertEquals(2, v6Handler.ignoredVlans.size()); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /** |
| 537 | * "IgnoreVlan" policy should be removed when the config removed. |
| 538 | */ |
| 539 | @Test |
| 540 | public void testRemoveIgnoreVlan() { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 541 | v4Handler.ignoredVlans.put(DEV_1_ID, IGNORED_VLAN); |
| 542 | v4Handler.ignoredVlans.put(DEV_2_ID, IGNORED_VLAN); |
| 543 | v6Handler.ignoredVlans.put(DEV_1_ID, IGNORED_VLAN); |
| 544 | v6Handler.ignoredVlans.put(DEV_2_ID, IGNORED_VLAN); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 545 | IgnoreDhcpConfig config = new IgnoreDhcpConfig(); |
| 546 | |
| 547 | Capture<Objective> capturedFromDev1 = newCapture(CaptureType.ALL); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 548 | flowObjectiveService.apply(eq(DEV_1_ID), capture(capturedFromDev1)); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 549 | expectLastCall().times(DHCP_SELECTORS.size()); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 550 | Capture<Objective> capturedFromDev2 = newCapture(CaptureType.ALL); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 551 | flowObjectiveService.apply(eq(DEV_2_ID), capture(capturedFromDev2)); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 552 | expectLastCall().times(DHCP_SELECTORS.size()); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 553 | replay(flowObjectiveService); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 554 | manager.removeConfig(config); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 555 | verify(flowObjectiveService); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 556 | |
| 557 | List<Objective> objectivesFromDev1 = capturedFromDev1.getValues(); |
| 558 | List<Objective> objectivesFromDev2 = capturedFromDev2.getValues(); |
| 559 | |
| 560 | assertTrue(objectivesFromDev1.containsAll(objectivesFromDev2)); |
| 561 | assertTrue(objectivesFromDev2.containsAll(objectivesFromDev1)); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 562 | |
| 563 | for (int index = 0; index < objectivesFromDev1.size(); index++) { |
| 564 | TrafficSelector selector = |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 565 | DefaultTrafficSelector.builder(DHCP_SELECTORS.get(index)) |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 566 | .matchVlanId(IGNORED_VLAN) |
| 567 | .build(); |
| 568 | ForwardingObjective fwd = (ForwardingObjective) objectivesFromDev1.get(index); |
| 569 | assertEquals(selector, fwd.selector()); |
Yi Tseng | dbabeed | 2017-11-29 10:49:20 -0800 | [diff] [blame] | 570 | assertEquals(DefaultTrafficTreatment.emptyTreatment(), fwd.treatment()); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 571 | assertEquals(IGNORE_CONTROL_PRIORITY, fwd.priority()); |
| 572 | assertEquals(ForwardingObjective.Flag.VERSATILE, fwd.flag()); |
| 573 | assertEquals(Objective.Operation.REMOVE, fwd.op()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 574 | fwd.context().ifPresent(ctx -> { |
| 575 | ctx.onSuccess(fwd); |
| 576 | }); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 577 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 578 | objectivesFromDev2.forEach(obj -> obj.context().ifPresent(ctx -> ctx.onSuccess(obj))); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 579 | assertEquals(0, v4Handler.ignoredVlans.size()); |
| 580 | assertEquals(0, v6Handler.ignoredVlans.size()); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 583 | /** |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 584 | * Should ignore ignore rules installation when device not available. |
| 585 | */ |
| 586 | @Test |
| 587 | public void testIgnoreUnknownDevice() throws IOException { |
| 588 | reset(manager.deviceService); |
| 589 | Device device = createNiceMock(Device.class); |
| 590 | expect(device.is(Pipeliner.class)).andReturn(true).anyTimes(); |
| 591 | |
| 592 | expect(manager.deviceService.getDevice(DEV_1_ID)).andReturn(device).anyTimes(); |
| 593 | expect(manager.deviceService.getDevice(DEV_2_ID)).andReturn(null).anyTimes(); |
| 594 | |
| 595 | ObjectMapper om = new ObjectMapper(); |
| 596 | JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH)); |
| 597 | IgnoreDhcpConfig config = new IgnoreDhcpConfig(); |
| 598 | json = json.path("apps").path(DHCP_RELAY_APP).path(IgnoreDhcpConfig.KEY); |
| 599 | config.init(APP_ID, IgnoreDhcpConfig.KEY, json, om, null); |
| 600 | |
| 601 | Capture<Objective> capturedFromDev1 = newCapture(CaptureType.ALL); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 602 | flowObjectiveService.apply(eq(DEV_1_ID), capture(capturedFromDev1)); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 603 | expectLastCall().times(DHCP_SELECTORS.size()); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 604 | replay(flowObjectiveService, manager.deviceService, device); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 605 | |
| 606 | manager.updateConfig(config); |
| 607 | capturedFromDev1.getValues().forEach(obj -> obj.context().ifPresent(ctx -> ctx.onSuccess(obj))); |
| 608 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 609 | assertEquals(1, v4Handler.ignoredVlans.size()); |
| 610 | assertEquals(1, v6Handler.ignoredVlans.size()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Should try install ignore rules when device comes up. |
| 615 | */ |
| 616 | @Test |
| 617 | public void testInstallIgnoreRuleWhenDeviceComesUp() throws IOException { |
| 618 | ObjectMapper om = new ObjectMapper(); |
| 619 | JsonNode json = om.readTree(Resources.getResource(CONFIG_FILE_PATH)); |
| 620 | IgnoreDhcpConfig config = new IgnoreDhcpConfig(); |
| 621 | json = json.path("apps").path(DHCP_RELAY_APP).path(IgnoreDhcpConfig.KEY); |
| 622 | config.init(APP_ID, IgnoreDhcpConfig.KEY, json, om, null); |
| 623 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 624 | reset(manager.cfgService, flowObjectiveService, manager.deviceService); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 625 | expect(manager.cfgService.getConfig(APP_ID, IgnoreDhcpConfig.class)) |
| 626 | .andReturn(config).anyTimes(); |
| 627 | |
| 628 | Device device = createNiceMock(Device.class); |
| 629 | expect(device.is(Pipeliner.class)).andReturn(true).anyTimes(); |
| 630 | expect(device.id()).andReturn(DEV_1_ID).anyTimes(); |
| 631 | expect(manager.deviceService.getDevice(DEV_1_ID)).andReturn(device).anyTimes(); |
| 632 | DeviceEvent event = new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device); |
| 633 | Capture<Objective> capturedFromDev1 = newCapture(CaptureType.ALL); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 634 | flowObjectiveService.apply(eq(DEV_1_ID), capture(capturedFromDev1)); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 635 | expectLastCall().times(DHCP_SELECTORS.size()); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 636 | replay(manager.cfgService, flowObjectiveService, manager.deviceService, device); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 637 | manager.deviceListener.event(event); |
| 638 | capturedFromDev1.getValues().forEach(obj -> obj.context().ifPresent(ctx -> ctx.onSuccess(obj))); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 639 | assertEquals(1, v4Handler.ignoredVlans.size()); |
| 640 | assertEquals(1, v6Handler.ignoredVlans.size()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 644 | * Relay a DHCP6 packet without relay option |
| 645 | * Note: Should add new host to host store after dhcp ack. |
| 646 | */ |
| 647 | @Test |
| 648 | public void relayDhcp6WithoutAgentInfo() { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 649 | replay(mockHostProviderService); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 650 | // send request |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 651 | packetService.processPacket(new TestDhcp6RequestPacketContext(DHCP6.MsgType.REQUEST.value(), |
| 652 | CLIENT_MAC, |
| 653 | CLIENT_VLAN, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 654 | CLIENT_CP, |
| 655 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
| 656 | 0)); |
| 657 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 658 | verify(mockHostProviderService); |
| 659 | reset(mockHostProviderService); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 660 | assertEquals(0, mockRouteStore.routes.size()); |
| 661 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 662 | Capture<HostDescription> capturedHostDesc = newCapture(); |
| 663 | mockHostProviderService.hostDetected(eq(HostId.hostId(CLIENT_MAC, CLIENT_VLAN)), |
| 664 | capture(capturedHostDesc), eq(false)); |
| 665 | replay(mockHostProviderService); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 666 | // send reply |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 667 | packetService.processPacket(new TestDhcp6ReplyPacketContext(DHCP6.MsgType.REPLY.value(), |
| 668 | CLIENT_CP, CLIENT_MAC, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 669 | CLIENT_VLAN, |
| 670 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 671 | 0, false, CLIENT_VLAN)); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 672 | verify(mockHostProviderService); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 673 | assertEquals(0, mockRouteStore.routes.size()); |
| 674 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 675 | HostDescription host = capturedHostDesc.getValue(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 676 | assertEquals(CLIENT_VLAN, host.vlan()); |
| 677 | assertEquals(CLIENT_CP.deviceId(), host.location().elementId()); |
| 678 | assertEquals(CLIENT_CP.port(), host.location().port()); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 679 | assertEquals(1, host.ipAddress().size()); |
| 680 | assertEquals(IP_FOR_CLIENT_V6, host.ipAddress().iterator().next()); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 681 | |
| 682 | // send release |
| 683 | packetService.processPacket(new TestDhcp6RequestPacketContext(DHCP6.MsgType.RELEASE.value(), |
| 684 | CLIENT_MAC, |
| 685 | CLIENT_VLAN, |
| 686 | CLIENT_CP, |
| 687 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
| 688 | 0)); |
| 689 | |
| 690 | assertEquals(null, manager.hostService.getHost(HostId.hostId(CLIENT_MAC, CLIENT_VLAN))); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Relay a DHCP6 packet with Relay Message opion (Indirectly connected host). |
| 695 | */ |
| 696 | @Test |
| 697 | public void relayDhcp6WithAgentInfo() { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 698 | replay(mockHostProviderService); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 699 | // Assume outer dhcp6 relay agent exists in store already |
| 700 | // send request |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 701 | packetService.processPacket(new TestDhcp6RequestPacketContext(DHCP6.MsgType.REQUEST.value(), |
| 702 | CLIENT2_MAC, |
| 703 | CLIENT2_VLAN, |
| 704 | CLIENT2_CP, |
| 705 | OUTER_RELAY_IP_V6, |
| 706 | 1)); |
| 707 | |
| 708 | assertEquals(0, mockRouteStore.routes.size()); |
| 709 | |
| 710 | // send reply |
| 711 | packetService.processPacket(new TestDhcp6ReplyPacketContext(DHCP6.MsgType.REPLY.value(), CLIENT2_CP, |
| 712 | CLIENT2_MAC, |
| 713 | CLIENT2_VLAN, |
| 714 | OUTER_RELAY_IP_V6, |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 715 | 1, false, CLIENT2_VLAN)); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 716 | |
| 717 | // won't trigger the host provider service |
| 718 | verify(mockHostProviderService); |
| 719 | reset(mockHostProviderService); |
| 720 | assertEquals(2, mockRouteStore.routes.size()); // ipAddress and prefix |
| 721 | |
| 722 | Route aRoute = mockRouteStore.routes.stream() |
| 723 | .filter(rt -> rt.prefix().contains(IP_FOR_CLIENT_V6)) |
| 724 | .findFirst() |
| 725 | .orElse(null); |
| 726 | assertNotEquals(null, aRoute); |
| 727 | |
| 728 | aRoute = mockRouteStore.routes.stream() |
| 729 | .filter(rt -> rt.prefix().contains(PREFIX_FOR_CLIENT_V6)) |
| 730 | .findFirst() |
| 731 | .orElse(null); |
| 732 | assertNotEquals(null, aRoute); |
| 733 | |
| 734 | // send release msg |
| 735 | packetService.processPacket(new TestDhcp6RequestPacketContext(DHCP6.MsgType.RELEASE.value(), |
| 736 | CLIENT2_MAC, |
| 737 | CLIENT2_VLAN, |
| 738 | CLIENT2_CP, |
| 739 | OUTER_RELAY_IP_V6, |
| 740 | 1)); |
| 741 | |
| 742 | aRoute = mockRouteStore.routes.stream() |
| 743 | .filter(rt -> rt.prefix().contains(IP_FOR_CLIENT_V6)) |
| 744 | .findFirst() |
| 745 | .orElse(null); |
| 746 | assertEquals(null, aRoute); |
| 747 | |
| 748 | aRoute = mockRouteStore.routes.stream() |
| 749 | .filter(rt -> rt.prefix().contains(PREFIX_FOR_CLIENT_V6)) |
| 750 | .findFirst() |
| 751 | .orElse(null); |
| 752 | assertEquals(null, aRoute); |
| 753 | |
| 754 | assertEquals(0, mockRouteStore.routes.size()); |
| 755 | |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Relay a DHCP6 packet with Relay Message opion (Indirectly connected host) and server responded |
| 760 | * with vlan differnt from client interface vlan. |
| 761 | */ |
| 762 | @Test |
| 763 | public void relayDhcp6WithAgentInfoWrongVlan() { |
| 764 | replay(mockHostProviderService); |
| 765 | // Assume outer dhcp6 relay agent exists in store already |
| 766 | // send request |
| 767 | packetService.processPacket(new TestDhcp6RequestPacketContext(DHCP6.MsgType.REQUEST.value(), |
| 768 | CLIENT2_MAC, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 769 | CLIENT2_VLAN, |
| 770 | CLIENT2_CP, |
| 771 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
| 772 | 1)); |
| 773 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 774 | assertEquals(0, mockRouteStore.routes.size()); |
| 775 | |
| 776 | // send reply |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 777 | packetService.processPacket(new TestDhcp6ReplyPacketContext(DHCP6.MsgType.REPLY.value(), |
| 778 | CLIENT2_CP, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 779 | CLIENT2_MAC, |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 780 | CLIENT2_VLAN, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 781 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 782 | 1, true, |
| 783 | CLIENT_BOGUS_VLAN // mismatch |
| 784 | )); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 785 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 786 | // won't trigger the host provider service |
| 787 | verify(mockHostProviderService); |
| 788 | reset(mockHostProviderService); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 789 | assertEquals(0, mockRouteStore.routes.size()); // ipAddress and prefix |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 790 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 791 | } |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 792 | |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 793 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 794 | @Test |
| 795 | public void testDhcp4DualHome() { |
| 796 | PacketContext packetContext = |
| 797 | new TestDhcpAckPacketContext(CLIENT_DH_CP, CLIENT_MAC, CLIENT_VLAN, |
| 798 | INTERFACE_IP.ipAddress().getIp4Address(), |
| 799 | false); |
| 800 | reset(manager.hostService); |
| 801 | expect(manager.hostService.getHost(CLIENT_HOST_ID)).andReturn(EXISTS_HOST).anyTimes(); |
| 802 | Capture<HostDescription> capturedHostDesc = newCapture(); |
| 803 | mockHostProviderService.hostDetected(eq(CLIENT_HOST_ID), capture(capturedHostDesc), eq(false)); |
| 804 | replay(mockHostProviderService, manager.hostService); |
| 805 | packetService.processPacket(packetContext); |
| 806 | verify(mockHostProviderService); |
| 807 | |
| 808 | HostDescription hostDesc = capturedHostDesc.getValue(); |
| 809 | Set<HostLocation> hostLocations = hostDesc.locations(); |
| 810 | assertEquals(2, hostLocations.size()); |
| 811 | assertTrue(hostLocations.contains(CLIENT_LOCATION)); |
| 812 | assertTrue(hostLocations.contains(CLIENT_DH_LOCATION)); |
| 813 | } |
| 814 | |
| 815 | @Test |
| 816 | public void testDhcp6DualHome() { |
| 817 | PacketContext packetContext = |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 818 | new TestDhcp6ReplyPacketContext(DHCP6.MsgType.REPLY.value(), |
| 819 | CLIENT_DH_CP, CLIENT_MAC, CLIENT_VLAN, |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 820 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 821 | 0, false, CLIENT_VLAN); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 822 | reset(manager.hostService); |
| 823 | expect(manager.hostService.getHostsByIp(CLIENT_LL_IP_V6)).andReturn(ImmutableSet.of(EXISTS_HOST)).anyTimes(); |
| 824 | |
| 825 | // FIXME: currently DHCPv6 has a bug, we can't get correct vlan of client...... |
| 826 | // XXX: The vlan relied from DHCP6 handler might be wrong, do hack here |
| 827 | HostId hostId = HostId.hostId(CLIENT_MAC, VlanId.NONE); |
| 828 | expect(manager.hostService.getHost(hostId)).andReturn(EXISTS_HOST).anyTimes(); |
| 829 | |
| 830 | // XXX: sometimes this will work, sometimes not |
| 831 | expect(manager.hostService.getHost(CLIENT_HOST_ID)).andReturn(EXISTS_HOST).anyTimes(); |
| 832 | |
| 833 | Capture<HostDescription> capturedHostDesc = newCapture(); |
| 834 | |
| 835 | // XXX: also a hack here |
| 836 | mockHostProviderService.hostDetected(eq(hostId), capture(capturedHostDesc), eq(false)); |
| 837 | expectLastCall().anyTimes(); |
| 838 | |
| 839 | mockHostProviderService.hostDetected(eq(CLIENT_HOST_ID), capture(capturedHostDesc), eq(false)); |
| 840 | expectLastCall().anyTimes(); |
| 841 | replay(mockHostProviderService, manager.hostService); |
| 842 | packetService.processPacket(packetContext); |
| 843 | verify(mockHostProviderService); |
| 844 | |
| 845 | HostDescription hostDesc = capturedHostDesc.getValue(); |
| 846 | Set<HostLocation> hostLocations = hostDesc.locations(); |
| 847 | assertEquals(2, hostLocations.size()); |
| 848 | assertTrue(hostLocations.contains(CLIENT_LOCATION)); |
| 849 | assertTrue(hostLocations.contains(CLIENT_DH_LOCATION)); |
| 850 | } |
| 851 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 852 | private static class MockDefaultDhcpRelayConfig extends DefaultDhcpRelayConfig { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 853 | @Override |
| 854 | public boolean isValid() { |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 859 | public List<DhcpServerConfig> dhcpServerConfigs() { |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 860 | return ImmutableList.of(new MockDhcpServerConfig(null)); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 864 | private static class MockIndirectDhcpRelayConfig extends IndirectDhcpRelayConfig { |
| 865 | @Override |
| 866 | public boolean isValid() { |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | @Override |
| 871 | public List<DhcpServerConfig> dhcpServerConfigs() { |
| 872 | return ImmutableList.of(new MockDhcpServerConfig(null)); |
| 873 | } |
| 874 | } |
| 875 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 876 | private static class MockDhcpServerConfig extends DhcpServerConfig { |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 877 | Ip4Address relayAgentIp; |
| 878 | |
| 879 | /** |
| 880 | * Create mocked version DHCP server config. |
| 881 | * |
| 882 | * @param relayAgentIp the relay agent Ip config; null if we don't need it |
| 883 | */ |
| 884 | public MockDhcpServerConfig(Ip4Address relayAgentIp) { |
| 885 | this.relayAgentIp = relayAgentIp; |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 886 | this.relayAgentIps.put(DEV_1_ID, Pair.of(relayAgentIp, null)); |
| 887 | this.relayAgentIps.put(DEV_2_ID, Pair.of(relayAgentIp, null)); |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | @Override |
Yi Tseng | 25bfe37 | 2017-11-03 16:27:32 -0700 | [diff] [blame] | 891 | public Optional<Ip4Address> getRelayAgentIp4(DeviceId deviceId) { |
| 892 | return Optional.ofNullable(this.relayAgentIps.get(deviceId).getLeft()); |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 895 | @Override |
| 896 | public Optional<ConnectPoint> getDhcpServerConnectPoint() { |
| 897 | return Optional.of(SERVER_CONNECT_POINT); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 900 | @Override |
| 901 | public Optional<Ip4Address> getDhcpServerIp4() { |
| 902 | return Optional.of(SERVER_IP); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 903 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 904 | |
| 905 | @Override |
| 906 | public Optional<Ip4Address> getDhcpGatewayIp4() { |
| 907 | return Optional.of(GATEWAY_IP); |
| 908 | } |
| 909 | |
| 910 | @Override |
| 911 | public Optional<Ip6Address> getDhcpServerIp6() { |
| 912 | return Optional.of(SERVER_IP_V6); |
| 913 | } |
| 914 | |
| 915 | @Override |
| 916 | public Optional<Ip6Address> getDhcpGatewayIp6() { |
| 917 | return Optional.of(GATEWAY_IP_V6); |
| 918 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 921 | private class MockRouteStore extends RouteStoreAdapter { |
| 922 | private List<Route> routes = Lists.newArrayList(); |
| 923 | |
| 924 | @Override |
| 925 | public void updateRoute(Route route) { |
| 926 | routes.add(route); |
| 927 | } |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 928 | |
| 929 | @Override |
| 930 | public void removeRoute(Route route) { |
| 931 | routes.remove(route); |
| 932 | } |
Daniel Ginsburg | c1d6aaf | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 933 | |
| 934 | public void replaceRoute(Route route) { |
| 935 | routes.remove(route); |
| 936 | routes.add(route); |
| 937 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | private class MockInterfaceService extends InterfaceServiceAdapter { |
| 941 | |
| 942 | @Override |
| 943 | public Set<Interface> getInterfaces() { |
| 944 | return INTERFACES; |
| 945 | } |
| 946 | |
| 947 | @Override |
| 948 | public Set<Interface> getInterfacesByIp(IpAddress ip) { |
| 949 | return INTERFACES.stream() |
| 950 | .filter(iface -> { |
| 951 | return iface.ipAddressesList().stream() |
| 952 | .anyMatch(ifaceIp -> ifaceIp.ipAddress().equals(ip)); |
| 953 | }) |
| 954 | .collect(Collectors.toSet()); |
| 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
| 959 | return INTERFACES.stream() |
| 960 | .filter(iface -> iface.connectPoint().equals(port)) |
| 961 | .collect(Collectors.toSet()); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | private class MockDhcpRelayStore implements DhcpRelayStore { |
| 966 | StoreDelegate<DhcpRelayStoreEvent> delegate; |
| 967 | private Map<HostId, DhcpRecord> records = Maps.newHashMap(); |
| 968 | |
| 969 | @Override |
| 970 | public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) { |
| 971 | records.put(hostId, dhcpRecord); |
| 972 | DhcpRelayStoreEvent event = new DhcpRelayStoreEvent(DhcpRelayStoreEvent.Type.UPDATED, |
| 973 | dhcpRecord); |
| 974 | if (delegate != null) { |
| 975 | delegate.notify(event); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | @Override |
| 980 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 981 | return Optional.ofNullable(records.get(hostId)); |
| 982 | } |
| 983 | |
| 984 | @Override |
| 985 | public Collection<DhcpRecord> getDhcpRecords() { |
| 986 | return records.values(); |
| 987 | } |
| 988 | |
| 989 | @Override |
| 990 | public Optional<DhcpRecord> removeDhcpRecord(HostId hostId) { |
| 991 | DhcpRecord dhcpRecord = records.remove(hostId); |
| 992 | if (dhcpRecord != null) { |
| 993 | DhcpRelayStoreEvent event = new DhcpRelayStoreEvent(DhcpRelayStoreEvent.Type.REMOVED, |
| 994 | dhcpRecord); |
| 995 | if (delegate != null) { |
| 996 | delegate.notify(event); |
| 997 | } |
| 998 | } |
| 999 | return Optional.ofNullable(dhcpRecord); |
| 1000 | } |
| 1001 | |
| 1002 | @Override |
| 1003 | public void setDelegate(StoreDelegate<DhcpRelayStoreEvent> delegate) { |
| 1004 | this.delegate = delegate; |
| 1005 | } |
| 1006 | |
| 1007 | @Override |
| 1008 | public void unsetDelegate(StoreDelegate<DhcpRelayStoreEvent> delegate) { |
| 1009 | this.delegate = null; |
| 1010 | } |
| 1011 | |
| 1012 | @Override |
| 1013 | public boolean hasDelegate() { |
| 1014 | return this.delegate != null; |
| 1015 | } |
| 1016 | } |
| 1017 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1018 | private class MockDhcpRelayCountersStore implements DhcpRelayCountersStore { |
| 1019 | private Map<String, DhcpRelayCounters> counters = Maps.newHashMap(); |
| 1020 | |
| 1021 | public void incrementCounter(String coutnerClass, String counterName) { |
| 1022 | DhcpRelayCounters countersRecord; |
| 1023 | |
| 1024 | DhcpRelayCounters classCounters = counters.get(coutnerClass); |
| 1025 | if (classCounters == null) { |
| 1026 | classCounters = new DhcpRelayCounters(); |
| 1027 | } |
| 1028 | classCounters.incrementCounter(counterName); |
| 1029 | counters.put(coutnerClass, classCounters); |
| 1030 | } |
| 1031 | |
| 1032 | @Override |
| 1033 | public Set<Map.Entry<String, DhcpRelayCounters>> getAllCounters() { |
| 1034 | return counters.entrySet(); |
| 1035 | } |
| 1036 | |
| 1037 | @Override |
| 1038 | public Optional<DhcpRelayCounters> getCounters(String counterClass) { |
| 1039 | DhcpRelayCounters classCounters = counters.get(counterClass); |
| 1040 | if (classCounters == null) { |
| 1041 | return Optional.empty(); |
| 1042 | } |
| 1043 | return Optional.of(classCounters); |
| 1044 | } |
| 1045 | |
| 1046 | @Override |
| 1047 | public void resetAllCounters() { |
| 1048 | counters.clear(); |
| 1049 | } |
| 1050 | |
| 1051 | @Override |
| 1052 | public void resetCounters(String counterClass) { |
| 1053 | DhcpRelayCounters classCounters = counters.get(counterClass); |
| 1054 | classCounters.resetCounters(); |
| 1055 | counters.put(counterClass, classCounters); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1060 | private class MockPacketService extends PacketServiceAdapter { |
| 1061 | Set<PacketProcessor> packetProcessors = Sets.newHashSet(); |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 1062 | OutboundPacket emittedPacket; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1063 | |
| 1064 | @Override |
| 1065 | public void addProcessor(PacketProcessor processor, int priority) { |
| 1066 | packetProcessors.add(processor); |
| 1067 | } |
| 1068 | |
| 1069 | public void processPacket(PacketContext packetContext) { |
| 1070 | packetProcessors.forEach(p -> p.process(packetContext)); |
| 1071 | } |
| 1072 | |
| 1073 | @Override |
| 1074 | public void emit(OutboundPacket packet) { |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 1075 | this.emittedPacket = packet; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | /** |
| 1082 | * Generates DHCP REQUEST packet. |
| 1083 | */ |
| 1084 | private class TestDhcpRequestPacketContext extends PacketContextAdapter { |
| 1085 | |
| 1086 | |
| 1087 | private InboundPacket inPacket; |
| 1088 | |
| 1089 | public TestDhcpRequestPacketContext(MacAddress clientMac, VlanId vlanId, |
| 1090 | ConnectPoint clientCp, |
| 1091 | Ip4Address clientGwAddr, |
| 1092 | boolean withNonOnosRelayInfo) { |
| 1093 | super(0, null, null, false); |
| 1094 | byte[] dhcpMsgType = new byte[1]; |
| 1095 | dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPREQUEST.getValue(); |
| 1096 | |
| 1097 | DhcpOption dhcpOption = new DhcpOption(); |
| 1098 | dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 1099 | dhcpOption.setData(dhcpMsgType); |
| 1100 | dhcpOption.setLength((byte) 1); |
| 1101 | DhcpOption endOption = new DhcpOption(); |
| 1102 | endOption.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 1103 | |
| 1104 | DHCP dhcp = new DHCP(); |
| 1105 | dhcp.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 1106 | dhcp.setHardwareAddressLength((byte) 6); |
| 1107 | dhcp.setClientHardwareAddress(clientMac.toBytes()); |
| 1108 | if (withNonOnosRelayInfo) { |
| 1109 | DhcpRelayAgentOption relayOption = new DhcpRelayAgentOption(); |
| 1110 | DhcpOption circuitIdOption = new DhcpOption(); |
| 1111 | CircuitId circuitId = new CircuitId("Custom option", VlanId.NONE); |
| 1112 | byte[] cid = circuitId.serialize(); |
| 1113 | circuitIdOption.setCode(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue()); |
| 1114 | circuitIdOption.setLength((byte) cid.length); |
| 1115 | circuitIdOption.setData(cid); |
| 1116 | relayOption.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 1117 | relayOption.addSubOption(circuitIdOption); |
| 1118 | dhcp.setOptions(ImmutableList.of(dhcpOption, relayOption, endOption)); |
| 1119 | dhcp.setGatewayIPAddress(OUTER_RELAY_IP.getIp4Address().toInt()); |
| 1120 | } else { |
| 1121 | dhcp.setOptions(ImmutableList.of(dhcpOption, endOption)); |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | UDP udp = new UDP(); |
| 1126 | udp.setPayload(dhcp); |
| 1127 | udp.setSourcePort(UDP.DHCP_CLIENT_PORT); |
| 1128 | udp.setDestinationPort(UDP.DHCP_SERVER_PORT); |
| 1129 | |
| 1130 | IPv4 ipv4 = new IPv4(); |
| 1131 | ipv4.setPayload(udp); |
| 1132 | ipv4.setDestinationAddress(SERVER_IP.toInt()); |
| 1133 | ipv4.setSourceAddress(clientGwAddr.toInt()); |
| 1134 | |
| 1135 | Ethernet eth = new Ethernet(); |
| 1136 | if (withNonOnosRelayInfo) { |
| 1137 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 1138 | .setVlanID(vlanId.toShort()) |
| 1139 | .setSourceMACAddress(OUTER_RELAY_MAC) |
| 1140 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 1141 | .setPayload(ipv4); |
| 1142 | } else { |
| 1143 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 1144 | .setVlanID(vlanId.toShort()) |
| 1145 | .setSourceMACAddress(clientMac) |
| 1146 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 1147 | .setPayload(ipv4); |
| 1148 | } |
| 1149 | |
| 1150 | this.inPacket = new DefaultInboundPacket(clientCp, eth, |
| 1151 | ByteBuffer.wrap(eth.serialize())); |
| 1152 | } |
| 1153 | |
| 1154 | @Override |
| 1155 | public InboundPacket inPacket() { |
| 1156 | return this.inPacket; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * Generates DHCP ACK packet. |
| 1162 | */ |
| 1163 | private class TestDhcpAckPacketContext extends PacketContextAdapter { |
| 1164 | private InboundPacket inPacket; |
| 1165 | |
| 1166 | public TestDhcpAckPacketContext(ConnectPoint clientCp, MacAddress clientMac, |
| 1167 | VlanId clientVlan, Ip4Address clientGwAddr, |
| 1168 | boolean withNonOnosRelayInfo) { |
| 1169 | super(0, null, null, false); |
| 1170 | |
| 1171 | byte[] dhcpMsgType = new byte[1]; |
| 1172 | dhcpMsgType[0] = (byte) DHCP.MsgType.DHCPACK.getValue(); |
| 1173 | |
| 1174 | DhcpOption dhcpOption = new DhcpOption(); |
| 1175 | dhcpOption.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 1176 | dhcpOption.setData(dhcpMsgType); |
| 1177 | dhcpOption.setLength((byte) 1); |
| 1178 | |
| 1179 | DhcpOption endOption = new DhcpOption(); |
| 1180 | endOption.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 1181 | |
| 1182 | DHCP dhcp = new DHCP(); |
| 1183 | if (withNonOnosRelayInfo) { |
| 1184 | DhcpRelayAgentOption relayOption = new DhcpRelayAgentOption(); |
| 1185 | DhcpOption circuitIdOption = new DhcpOption(); |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 1186 | String circuitId = NON_ONOS_CID; |
| 1187 | byte[] cid = circuitId.getBytes(Charsets.US_ASCII); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1188 | circuitIdOption.setCode(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue()); |
| 1189 | circuitIdOption.setLength((byte) cid.length); |
| 1190 | circuitIdOption.setData(cid); |
| 1191 | relayOption.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 1192 | relayOption.addSubOption(circuitIdOption); |
| 1193 | dhcp.setOptions(ImmutableList.of(dhcpOption, relayOption, endOption)); |
| 1194 | dhcp.setGatewayIPAddress(OUTER_RELAY_IP.getIp4Address().toInt()); |
| 1195 | } else { |
| 1196 | CircuitId cid = new CircuitId(clientCp.toString(), clientVlan); |
| 1197 | byte[] circuitId = cid.serialize(); |
| 1198 | DhcpOption circuitIdSubOption = new DhcpOption(); |
| 1199 | circuitIdSubOption.setCode(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue()); |
| 1200 | circuitIdSubOption.setData(circuitId); |
| 1201 | circuitIdSubOption.setLength((byte) circuitId.length); |
| 1202 | |
| 1203 | DhcpRelayAgentOption relayInfoOption = new DhcpRelayAgentOption(); |
| 1204 | relayInfoOption.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 1205 | relayInfoOption.addSubOption(circuitIdSubOption); |
| 1206 | dhcp.setOptions(ImmutableList.of(dhcpOption, relayInfoOption, endOption)); |
| 1207 | dhcp.setGatewayIPAddress(clientGwAddr.toInt()); |
| 1208 | } |
| 1209 | dhcp.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 1210 | dhcp.setHardwareAddressLength((byte) 6); |
| 1211 | dhcp.setClientHardwareAddress(clientMac.toBytes()); |
| 1212 | dhcp.setYourIPAddress(IP_FOR_CLIENT.toInt()); |
| 1213 | |
| 1214 | UDP udp = new UDP(); |
| 1215 | udp.setPayload(dhcp); |
| 1216 | udp.setSourcePort(UDP.DHCP_SERVER_PORT); |
| 1217 | udp.setDestinationPort(UDP.DHCP_CLIENT_PORT); |
| 1218 | IPv4 ipv4 = new IPv4(); |
| 1219 | ipv4.setPayload(udp); |
| 1220 | ipv4.setDestinationAddress(IP_FOR_CLIENT.toString()); |
| 1221 | ipv4.setSourceAddress(SERVER_IP.toString()); |
| 1222 | Ethernet eth = new Ethernet(); |
| 1223 | if (withNonOnosRelayInfo) { |
| 1224 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 1225 | .setVlanID(SERVER_VLAN.toShort()) |
| 1226 | .setSourceMACAddress(SERVER_MAC) |
| 1227 | .setDestinationMACAddress(OUTER_RELAY_MAC) |
| 1228 | .setPayload(ipv4); |
| 1229 | } else { |
| 1230 | eth.setEtherType(Ethernet.TYPE_IPV4) |
| 1231 | .setVlanID(SERVER_VLAN.toShort()) |
| 1232 | .setSourceMACAddress(SERVER_MAC) |
| 1233 | .setDestinationMACAddress(CLIENT_MAC) |
| 1234 | .setPayload(ipv4); |
| 1235 | } |
| 1236 | |
| 1237 | this.inPacket = new DefaultInboundPacket(SERVER_CONNECT_POINT, eth, |
| 1238 | ByteBuffer.wrap(eth.serialize())); |
| 1239 | |
| 1240 | } |
| 1241 | |
| 1242 | @Override |
| 1243 | public InboundPacket inPacket() { |
| 1244 | return this.inPacket; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | private class TestArpRequestPacketContext extends PacketContextAdapter { |
| 1249 | private InboundPacket inPacket; |
| 1250 | |
| 1251 | public TestArpRequestPacketContext(Interface fromInterface) { |
| 1252 | super(0, null, null, false); |
| 1253 | ARP arp = new ARP(); |
| 1254 | arp.setOpCode(ARP.OP_REQUEST); |
| 1255 | |
| 1256 | IpAddress targetIp = fromInterface.ipAddressesList().get(0).ipAddress(); |
| 1257 | arp.setTargetProtocolAddress(targetIp.toOctets()); |
| 1258 | arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes()); |
| 1259 | arp.setSenderHardwareAddress(MacAddress.NONE.toBytes()); |
| 1260 | arp.setSenderProtocolAddress(Ip4Address.valueOf(0).toOctets()); |
| 1261 | arp.setHardwareAddressLength((byte) MacAddress.MAC_ADDRESS_LENGTH); |
| 1262 | Ethernet eth = new Ethernet(); |
| 1263 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 1264 | eth.setSourceMACAddress(MacAddress.NONE); |
| 1265 | eth.setDestinationMACAddress(MacAddress.BROADCAST); |
| 1266 | eth.setVlanID(fromInterface.vlan().toShort()); |
| 1267 | eth.setPayload(arp); |
| 1268 | |
| 1269 | this.inPacket = new DefaultInboundPacket(fromInterface.connectPoint(), eth, |
| 1270 | ByteBuffer.wrap(eth.serialize())); |
| 1271 | } |
| 1272 | |
| 1273 | @Override |
| 1274 | public InboundPacket inPacket() { |
| 1275 | return this.inPacket; |
| 1276 | } |
| 1277 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1278 | |
| 1279 | /** |
| 1280 | * Generates DHCP6 REQUEST packet. |
| 1281 | */ |
| 1282 | private void buildDhcp6Packet(DHCP6 dhcp6, byte msgType, Ip6Address ip6Addr, IpPrefix prefix) { |
| 1283 | |
| 1284 | // build address option |
| 1285 | Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption(); |
| 1286 | iaAddressOption.setCode(DHCP6.OptionCode.IAADDR.value()); |
| 1287 | iaAddressOption.setIp6Address(ip6Addr); |
| 1288 | iaAddressOption.setPreferredLifetime(3600); |
| 1289 | iaAddressOption.setValidLifetime(1200); |
| 1290 | iaAddressOption.setLength((short) Dhcp6IaAddressOption.DEFAULT_LEN); |
| 1291 | |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1292 | Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption(); |
| 1293 | Dhcp6Duid dhcp6Duip = new Dhcp6Duid(); |
| 1294 | dhcp6Duip.setDuidType(Dhcp6Duid.DuidType.DUID_LLT); |
| 1295 | dhcp6Duip.setHardwareType((short) 0x01); // Ethernet |
| 1296 | dhcp6Duip.setDuidTime(1234); |
| 1297 | dhcp6Duip.setLinkLayerAddress(CLIENT_MAC.toBytes()); |
| 1298 | clientIdOption.setDuid(dhcp6Duip); |
| 1299 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1300 | Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption(); |
| 1301 | iaNaOption.setCode(DHCP6.OptionCode.IA_NA.value()); |
| 1302 | iaNaOption.setIaId(0); |
| 1303 | iaNaOption.setT1(302400); |
| 1304 | iaNaOption.setT2(483840); |
| 1305 | List<Dhcp6Option> iaNaSubOptions = new ArrayList<Dhcp6Option>(); |
| 1306 | iaNaSubOptions.add(iaAddressOption); |
| 1307 | iaNaOption.setOptions(iaNaSubOptions); |
| 1308 | iaNaOption.setLength((short) (Dhcp6IaNaOption.DEFAULT_LEN + iaAddressOption.getLength())); |
| 1309 | |
| 1310 | // build prefix option |
| 1311 | Dhcp6IaPrefixOption iaPrefixOption = new Dhcp6IaPrefixOption(); |
| 1312 | iaPrefixOption.setCode(DHCP6.OptionCode.IAPREFIX.value()); |
| 1313 | iaPrefixOption.setIp6Prefix(prefix.address().getIp6Address()); |
| 1314 | iaPrefixOption.setPrefixLength((byte) prefix.prefixLength()); |
| 1315 | iaPrefixOption.setPreferredLifetime(3601); |
| 1316 | iaPrefixOption.setValidLifetime(1201); |
| 1317 | iaPrefixOption.setLength((short) Dhcp6IaPrefixOption.DEFAULT_LEN); |
| 1318 | |
| 1319 | Dhcp6IaPdOption iaPdOption = new Dhcp6IaPdOption(); |
| 1320 | iaPdOption.setCode(DHCP6.OptionCode.IA_PD.value()); |
| 1321 | iaPdOption.setIaId(0); |
| 1322 | iaPdOption.setT1(302401); |
| 1323 | iaPdOption.setT2(483841); |
| 1324 | List<Dhcp6Option> iaPdSubOptions = new ArrayList<Dhcp6Option>(); |
| 1325 | iaPdSubOptions.add(iaPrefixOption); |
| 1326 | iaPdOption.setOptions(iaPdSubOptions); |
| 1327 | iaPdOption.setLength((short) (Dhcp6IaPdOption.DEFAULT_LEN + iaPrefixOption.getLength())); |
| 1328 | |
| 1329 | dhcp6.setMsgType(msgType); |
| 1330 | List<Dhcp6Option> dhcp6Options = new ArrayList<Dhcp6Option>(); |
| 1331 | dhcp6Options.add(iaNaOption); |
Kalhee Kim | 495c9b2 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1332 | dhcp6Options.add(clientIdOption); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1333 | dhcp6Options.add(iaPdOption); |
| 1334 | dhcp6.setOptions(dhcp6Options); |
| 1335 | |
| 1336 | } |
| 1337 | |
| 1338 | private void buildRelayMsg(DHCP6 dhcp6Relay, byte msgType, Ip6Address linkAddr, |
| 1339 | Ip6Address peerAddr, byte hop, byte[] interfaceIdBytes, |
| 1340 | DHCP6 dhcp6Payload) { |
| 1341 | |
| 1342 | dhcp6Relay.setMsgType(msgType); |
| 1343 | |
| 1344 | dhcp6Relay.setLinkAddress(linkAddr.toOctets()); |
| 1345 | dhcp6Relay.setPeerAddress(peerAddr.toOctets()); |
| 1346 | dhcp6Relay.setHopCount(hop); |
| 1347 | List<Dhcp6Option> options = new ArrayList<Dhcp6Option>(); |
| 1348 | |
| 1349 | // interfaceId option |
| 1350 | Dhcp6Option interfaceId = new Dhcp6Option(); |
| 1351 | interfaceId.setCode(DHCP6.OptionCode.INTERFACE_ID.value()); |
| 1352 | |
| 1353 | |
| 1354 | interfaceId.setData(interfaceIdBytes); |
| 1355 | interfaceId.setLength((short) interfaceIdBytes.length); |
| 1356 | Dhcp6InterfaceIdOption interfaceIdOption = new Dhcp6InterfaceIdOption(interfaceId); |
Kalhee Kim | a0b8f65 | 2017-10-18 22:00:30 +0000 | [diff] [blame] | 1357 | byte[] optionData = interfaceIdOption.getData(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1358 | ByteBuffer bb = ByteBuffer.wrap(interfaceIdBytes); |
| 1359 | |
| 1360 | byte[] macAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
Kalhee Kim | a0b8f65 | 2017-10-18 22:00:30 +0000 | [diff] [blame] | 1361 | byte[] port = new byte[optionData.length - MacAddress.MAC_ADDRESS_LENGTH - |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 1362 | VLAN_LEN - SEPARATOR_LEN * 2]; |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1363 | short vlan; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1364 | bb.get(macAddr); |
| 1365 | bb.get(); // separator |
| 1366 | bb.get(port); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1367 | bb.get(); // separator |
| 1368 | vlan = bb.getShort(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1369 | interfaceIdOption.setMacAddress(MacAddress.valueOf(macAddr)); |
| 1370 | interfaceIdOption.setInPort(port); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1371 | interfaceIdOption.setVlanId(vlan); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1372 | |
| 1373 | options.add(interfaceIdOption); |
| 1374 | |
| 1375 | // relay message option |
| 1376 | Dhcp6Option relayMsgOption = new Dhcp6Option(); |
| 1377 | relayMsgOption.setCode(DHCP6.OptionCode.RELAY_MSG.value()); |
| 1378 | byte[] dhcp6PayloadByte = dhcp6Payload.serialize(); |
| 1379 | relayMsgOption.setLength((short) dhcp6PayloadByte.length); |
| 1380 | relayMsgOption.setPayload(dhcp6Payload); |
| 1381 | Dhcp6RelayOption relayOpt = new Dhcp6RelayOption(relayMsgOption); |
| 1382 | |
| 1383 | options.add(relayOpt); |
| 1384 | |
| 1385 | dhcp6Relay.setOptions(options); |
| 1386 | } |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1387 | private byte[] buildInterfaceId(MacAddress clientMac, short vlanId, ConnectPoint clientCp) { |
| 1388 | String inPortString = "-" + clientCp.toString() + ":"; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1389 | byte[] clientSoureMacBytes = clientMac.toBytes(); |
| 1390 | byte[] inPortStringBytes = inPortString.getBytes(); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1391 | byte[] vlanIdBytes = new byte[2]; |
| 1392 | vlanIdBytes[0] = (byte) ((vlanId >> 8) & 0xff); // high-order byte first |
| 1393 | vlanIdBytes[1] = (byte) (vlanId & 0xff); |
| 1394 | byte[] interfaceIdBytes = new byte[clientSoureMacBytes.length + inPortStringBytes.length + vlanIdBytes.length]; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1395 | |
| 1396 | System.arraycopy(clientSoureMacBytes, 0, interfaceIdBytes, 0, clientSoureMacBytes.length); |
| 1397 | System.arraycopy(inPortStringBytes, 0, interfaceIdBytes, clientSoureMacBytes.length, inPortStringBytes.length); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1398 | System.arraycopy(vlanIdBytes, 0, interfaceIdBytes, clientSoureMacBytes.length + inPortStringBytes.length, |
| 1399 | vlanIdBytes.length); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1400 | |
| 1401 | return interfaceIdBytes; |
| 1402 | } |
| 1403 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 1404 | private static List<TrafficSelector> buildClientDhcpSelectors() { |
| 1405 | return Streams.concat(Dhcp4HandlerImpl.DHCP_SELECTORS.stream(), |
| 1406 | Dhcp6HandlerImpl.DHCP_SELECTORS.stream()) |
| 1407 | .collect(Collectors.toList()); |
| 1408 | } |
| 1409 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1410 | private class TestDhcp6RequestPacketContext extends PacketContextAdapter { |
| 1411 | |
| 1412 | |
| 1413 | private InboundPacket inPacket; |
| 1414 | |
| 1415 | |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1416 | public TestDhcp6RequestPacketContext(byte msgType, MacAddress clientMac, VlanId vlanId, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1417 | ConnectPoint clientCp, |
| 1418 | Ip6Address clientGwAddr, |
| 1419 | int relayLevel) { |
| 1420 | super(0, null, null, false); |
| 1421 | |
| 1422 | DHCP6 dhcp6 = new DHCP6(); |
| 1423 | if (relayLevel > 0) { |
| 1424 | DHCP6 dhcp6Payload = new DHCP6(); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1425 | buildDhcp6Packet(dhcp6Payload, msgType, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1426 | IP_FOR_CLIENT_V6, |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1427 | msgType == DHCP6.MsgType.REQUEST.value() ? PREFIX_FOR_ZERO : PREFIX_FOR_CLIENT_V6); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1428 | DHCP6 dhcp6Parent = null; |
| 1429 | DHCP6 dhcp6Child = dhcp6Payload; |
| 1430 | for (int i = 0; i < relayLevel; i++) { |
| 1431 | dhcp6Parent = new DHCP6(); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1432 | byte[] interfaceId = buildInterfaceId(clientMac, vlanId.toShort(), clientCp); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1433 | buildRelayMsg(dhcp6Parent, DHCP6.MsgType.RELAY_FORW.value(), |
| 1434 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
| 1435 | OUTER_RELAY_IP_V6, |
| 1436 | (byte) (relayLevel - 1), interfaceId, |
| 1437 | dhcp6Child); |
| 1438 | dhcp6Child = dhcp6Parent; |
| 1439 | } |
| 1440 | if (dhcp6Parent != null) { |
| 1441 | dhcp6 = dhcp6Parent; |
| 1442 | } |
| 1443 | } else { |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1444 | buildDhcp6Packet(dhcp6, msgType, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1445 | IP_FOR_CLIENT_V6, |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1446 | msgType == DHCP6.MsgType.REQUEST.value() ? PREFIX_FOR_ZERO : PREFIX_FOR_CLIENT_V6); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | UDP udp = new UDP(); |
| 1450 | udp.setPayload(dhcp6); |
| 1451 | if (relayLevel > 0) { |
| 1452 | udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT); |
| 1453 | } else { |
| 1454 | udp.setSourcePort(UDP.DHCP_V6_CLIENT_PORT); |
| 1455 | } |
| 1456 | udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT); |
| 1457 | |
| 1458 | IPv6 ipv6 = new IPv6(); |
| 1459 | ipv6.setPayload(udp); |
| 1460 | ipv6.setNextHeader(IPv6.PROTOCOL_UDP); |
| 1461 | ipv6.setDestinationAddress(SERVER_IP_V6_MCAST.toOctets()); |
| 1462 | ipv6.setSourceAddress(clientGwAddr.toOctets()); |
| 1463 | |
| 1464 | Ethernet eth = new Ethernet(); |
| 1465 | if (relayLevel > 0) { |
| 1466 | eth.setEtherType(Ethernet.TYPE_IPV6) |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1467 | .setVlanID(OUTER_RELAY_VLAN.toShort()) |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1468 | .setSourceMACAddress(OUTER_RELAY_MAC) |
| 1469 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:01:00:02")) |
| 1470 | .setPayload(ipv6); |
| 1471 | } else { |
| 1472 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 1473 | .setVlanID(vlanId.toShort()) |
| 1474 | .setSourceMACAddress(clientMac) |
| 1475 | .setDestinationMACAddress(MacAddress.valueOf("33:33:00:01:00:02")) |
| 1476 | .setPayload(ipv6); |
| 1477 | } |
| 1478 | this.inPacket = new DefaultInboundPacket(clientCp, eth, |
| 1479 | ByteBuffer.wrap(eth.serialize())); |
| 1480 | } |
| 1481 | |
| 1482 | @Override |
| 1483 | public InboundPacket inPacket() { |
| 1484 | return this.inPacket; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * Generates DHCP6 REPLY packet. |
| 1490 | */ |
| 1491 | |
| 1492 | private class TestDhcp6ReplyPacketContext extends PacketContextAdapter { |
| 1493 | private InboundPacket inPacket; |
| 1494 | |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1495 | public TestDhcp6ReplyPacketContext(byte msgType, ConnectPoint clientCp, MacAddress clientMac, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1496 | VlanId clientVlan, Ip6Address clientGwAddr, |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1497 | int relayLevel, boolean overWriteFlag, VlanId overWriteVlan) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1498 | super(0, null, null, false); |
| 1499 | |
| 1500 | |
| 1501 | DHCP6 dhcp6Payload = new DHCP6(); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 1502 | buildDhcp6Packet(dhcp6Payload, msgType, |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1503 | IP_FOR_CLIENT_V6, |
| 1504 | PREFIX_FOR_CLIENT_V6); |
Kalhee Kim | 121ba92 | 2017-11-01 17:56:44 +0000 | [diff] [blame] | 1505 | byte[] interfaceId = null; |
| 1506 | if (relayLevel > 0) { |
| 1507 | interfaceId = buildInterfaceId(OUTER_RELAY_MAC, |
| 1508 | overWriteFlag ? overWriteVlan.toShort() : OUTER_RELAY_VLAN.toShort(), |
| 1509 | OUTER_RELAY_CP); |
| 1510 | } else { |
| 1511 | interfaceId = buildInterfaceId(clientMac, clientVlan.toShort(), clientCp); |
| 1512 | } |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1513 | DHCP6 dhcp6 = new DHCP6(); |
| 1514 | buildRelayMsg(dhcp6, DHCP6.MsgType.RELAY_REPL.value(), |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 1515 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
| 1516 | CLIENT_LL_IP_V6, |
| 1517 | (byte) 0, interfaceId, |
| 1518 | dhcp6Payload); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1519 | |
| 1520 | DHCP6 dhcp6Parent = null; |
| 1521 | DHCP6 dhcp6Child = dhcp6; |
| 1522 | for (int i = 0; i < relayLevel; i++) { // relayLevel 0 : no relay |
| 1523 | dhcp6Parent = new DHCP6(); |
| 1524 | |
| 1525 | buildRelayMsg(dhcp6Parent, DHCP6.MsgType.RELAY_REPL.value(), |
| 1526 | INTERFACE_IP_V6.ipAddress().getIp6Address(), |
| 1527 | OUTER_RELAY_IP_V6, |
| 1528 | (byte) relayLevel, interfaceId, |
| 1529 | dhcp6Child); |
| 1530 | |
| 1531 | dhcp6Child = dhcp6Parent; |
| 1532 | } |
| 1533 | if (dhcp6Parent != null) { |
| 1534 | dhcp6 = dhcp6Parent; |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | UDP udp = new UDP(); |
| 1539 | udp.setPayload(dhcp6); |
| 1540 | udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT); |
| 1541 | udp.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT); |
| 1542 | IPv6 ipv6 = new IPv6(); |
| 1543 | ipv6.setPayload(udp); |
| 1544 | ipv6.setNextHeader(IPv6.PROTOCOL_UDP); |
| 1545 | ipv6.setDestinationAddress(IP_FOR_CLIENT_V6.toOctets()); |
| 1546 | ipv6.setSourceAddress(SERVER_IP_V6.toOctets()); |
| 1547 | Ethernet eth = new Ethernet(); |
| 1548 | if (relayLevel > 0) { |
| 1549 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 1550 | .setVlanID(SERVER_VLAN.toShort()) |
| 1551 | .setSourceMACAddress(SERVER_MAC) |
| 1552 | .setDestinationMACAddress(OUTER_RELAY_MAC) |
| 1553 | .setPayload(ipv6); |
| 1554 | } else { |
| 1555 | eth.setEtherType(Ethernet.TYPE_IPV6) |
| 1556 | .setVlanID(SERVER_VLAN.toShort()) |
| 1557 | .setSourceMACAddress(SERVER_MAC) |
| 1558 | .setDestinationMACAddress(CLIENT_MAC) |
| 1559 | .setPayload(ipv6); |
| 1560 | } |
| 1561 | |
| 1562 | this.inPacket = new DefaultInboundPacket(SERVER_CONNECT_POINT, eth, |
| 1563 | ByteBuffer.wrap(eth.serialize())); |
| 1564 | |
| 1565 | } |
| 1566 | |
| 1567 | @Override |
| 1568 | public InboundPacket inPacket() { |
| 1569 | return this.inPacket; |
| 1570 | } |
| 1571 | } |
| 1572 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1573 | } |