Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
Matteo Scandolo | 57af5d1 | 2019-04-29 17:11:41 -0700 | [diff] [blame] | 17 | package org.opencord.dhcpl2relay.impl; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 18 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.fail; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.nio.charset.StandardCharsets; |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collection; |
| 27 | import java.util.Dictionary; |
| 28 | import java.util.Hashtable; |
| 29 | import java.util.LinkedList; |
| 30 | import java.util.List; |
| 31 | import java.util.Set; |
| 32 | import java.util.concurrent.Callable; |
| 33 | import java.util.concurrent.ExecutionException; |
| 34 | import java.util.concurrent.Future; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 35 | import java.util.concurrent.ScheduledExecutorService; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 36 | import java.util.concurrent.ScheduledFuture; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 37 | import java.util.concurrent.TimeUnit; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 38 | import java.util.concurrent.TimeoutException; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 39 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 40 | import org.onlab.packet.BasePacket; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 41 | import org.onlab.packet.ChassisId; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 42 | import org.onlab.packet.DHCP; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 43 | import org.onlab.packet.Ethernet; |
| 44 | import org.onlab.packet.IPv4; |
| 45 | import org.onlab.packet.Ip4Address; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 46 | import org.onlab.packet.IpAddress; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 47 | import org.onlab.packet.MacAddress; |
| 48 | import org.onlab.packet.UDP; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 49 | import org.onlab.packet.VlanId; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 50 | import org.onlab.packet.dhcp.DhcpOption; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 51 | import org.onosproject.core.ApplicationId; |
| 52 | import org.onosproject.core.CoreServiceAdapter; |
| 53 | import org.onosproject.core.DefaultApplicationId; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 54 | import org.onosproject.event.DefaultEventSinkRegistry; |
| 55 | import org.onosproject.event.Event; |
| 56 | import org.onosproject.event.EventDeliveryService; |
| 57 | import org.onosproject.event.EventSink; |
| 58 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 59 | import org.onosproject.net.AnnotationKeys; |
| 60 | import org.onosproject.net.Annotations; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 61 | import org.onosproject.net.ConnectPoint; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 62 | import org.onosproject.net.DefaultAnnotations; |
| 63 | import org.onosproject.net.DefaultDevice; |
| 64 | import org.onosproject.net.DefaultHost; |
| 65 | import org.onosproject.net.Device; |
| 66 | import org.onosproject.net.DeviceId; |
| 67 | import org.onosproject.net.Element; |
| 68 | import org.onosproject.net.Host; |
| 69 | import org.onosproject.net.HostId; |
| 70 | import org.onosproject.net.HostLocation; |
| 71 | import org.onosproject.net.Port; |
| 72 | import org.onosproject.net.PortNumber; |
| 73 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 74 | import org.onosproject.net.host.HostServiceAdapter; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 75 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 76 | import org.onosproject.net.packet.DefaultPacketContext; |
| 77 | import org.onosproject.net.packet.InboundPacket; |
| 78 | import org.onosproject.net.packet.OutboundPacket; |
| 79 | import org.onosproject.net.packet.PacketContext; |
| 80 | import org.onosproject.net.packet.PacketProcessor; |
| 81 | import org.onosproject.net.packet.PacketServiceAdapter; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 82 | import org.onosproject.net.provider.ProviderId; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 83 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82Data; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 84 | import org.opencord.sadis.BandwidthProfileInformation; |
| 85 | import org.opencord.sadis.BaseInformationService; |
| 86 | import org.opencord.sadis.SadisService; |
| 87 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 88 | import org.opencord.sadis.UniTagInformation; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 89 | import org.osgi.framework.Bundle; |
| 90 | import org.osgi.framework.BundleContext; |
| 91 | import org.osgi.framework.ServiceReference; |
| 92 | import org.osgi.service.component.ComponentContext; |
| 93 | import org.osgi.service.component.ComponentInstance; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 94 | import org.slf4j.Logger; |
| 95 | import org.slf4j.LoggerFactory; |
| 96 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 97 | import com.google.common.collect.ImmutableSet; |
| 98 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 99 | |
| 100 | /** |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 101 | * Common methods for DHCP app testing. |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 102 | */ |
| 103 | public class DhcpL2RelayTestBase { |
| 104 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 105 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 106 | static final VlanId CLIENT_C_TAG = VlanId.vlanId((short) 2); |
| 107 | static final VlanId CLIENT_S_TAG = VlanId.vlanId((short) 4); |
| 108 | static final short CLIENT_C_PBIT = 7; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 109 | static final String CLIENT_ID_1 = "SUBSCRIBER_ID_1"; |
| 110 | static final String CLIENT_NAS_PORT_ID = "PON 1/1"; |
| 111 | static final String CLIENT_CIRCUIT_ID = "CIR-PON 1/1"; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 112 | static final String CLIENT32_CIRCUIT_ID = ""; |
| 113 | static final String CLIENT4112_CIRCUIT_ID = null; |
| 114 | public static final String CLIENT_REMOTE_ID = "I am an RG"; |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 115 | static final short NOT_PROVIDED = 0; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 116 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 117 | static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 118 | static final MacAddress SERVER_MAC = MacAddress.valueOf("bb:bb:bb:bb:bb:bb"); |
| 119 | static final String DESTINATION_ADDRESS_IP = "1.1.1.1"; |
| 120 | static final String DHCP_CLIENT_IP_ADDRESS = "2.2.2.2"; |
| 121 | static final int UPLINK_PORT = 5; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 122 | static final int CLIENT_PORT = 1; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 123 | |
| 124 | static final String EXPECTED_IP = "10.2.0.2"; |
| 125 | static final String OLT_DEV_ID = "of:00000000000000aa"; |
| 126 | static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID); |
| 127 | static final int TRANSACTION_ID = 1000; |
| 128 | static final String SCHEME_NAME = "dhcpl2relay"; |
| 129 | static final MacAddress OLT_MAC_ADDRESS = MacAddress.valueOf("01:02:03:04:05:06"); |
| 130 | |
| 131 | static final ConnectPoint SERVER_CONNECT_POINT = |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 132 | ConnectPoint.deviceConnectPoint("of:00000000000000aa/" + |
| 133 | String.valueOf(UPLINK_PORT)); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 134 | |
| 135 | static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder() |
| 136 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 137 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 138 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 139 | List<BasePacket> savedPackets = new LinkedList<>(); |
| 140 | PacketProcessor packetProcessor; |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Saves the given packet onto the saved packets list. |
| 145 | * |
| 146 | * @param packet packet to save |
| 147 | */ |
| 148 | void savePacket(BasePacket packet) { |
| 149 | savedPackets.add(packet); |
| 150 | } |
| 151 | |
| 152 | BasePacket getPacket() { |
| 153 | return savedPackets.remove(0); |
| 154 | } |
| 155 | |
| 156 | /** |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 157 | * Mock core service adaptor that provides an appId. |
| 158 | */ |
| 159 | class MockCoreServiceAdapter extends CoreServiceAdapter { |
| 160 | |
| 161 | @Override |
| 162 | public ApplicationId registerApplication(String name) { |
| 163 | return new DefaultApplicationId(10, name); |
| 164 | } |
| 165 | } |
| 166 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 167 | class MockDeviceService extends DeviceServiceAdapter { |
| 168 | |
| 169 | private ProviderId providerId = new ProviderId("of", "foo"); |
| 170 | private final Device device1 = new DhcpL2RelayTestBase.MockDevice(providerId, DEVICE_ID_1, Device.Type.SWITCH, |
| 171 | "foo.inc", "0", "0", OLT_DEV_ID, new ChassisId(), |
| 172 | DEVICE_ANNOTATIONS); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 173 | private final Device otherDevice = new DhcpL2RelayTestBase.MockDevice( |
| 174 | providerId, |
| 175 | DeviceId.deviceId("of:0000b86a974385f7"), |
| 176 | Device.Type.SWITCH, |
| 177 | "foo.inc", "0", "0", "EC1838000853", new ChassisId(), |
| 178 | DEVICE_ANNOTATIONS); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 179 | @Override |
| 180 | public Device getDevice(DeviceId devId) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 181 | if (devId.equals(DEVICE_ID_1)) { |
| 182 | return device1; |
| 183 | } else { |
| 184 | return otherDevice; |
| 185 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public Port getPort(ConnectPoint cp) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 190 | return new DhcpL2RelayTestBase.MockPort(cp); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 195 | return new DhcpL2RelayTestBase.MockPort(new ConnectPoint(deviceId, |
| 196 | portNumber)); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public boolean isAvailable(DeviceId d) { |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | class MockDevice extends DefaultDevice { |
| 206 | |
| 207 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 208 | String manufacturer, String hwVersion, String swVersion, |
| 209 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 210 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 211 | chassisId, annotations); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | class MockHostService extends HostServiceAdapter { |
| 216 | |
| 217 | @Override |
| 218 | public Set<Host> getHostsByMac(MacAddress mac) { |
| 219 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 220 | HostLocation loc = new HostLocation(DEVICE_ID_1, PortNumber |
| 221 | .portNumber(CLIENT_PORT), 0); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 222 | |
| 223 | IpAddress ip = IpAddress.valueOf("10.100.200.10"); |
| 224 | |
| 225 | Host h = new DefaultHost(ProviderId.NONE, HostId.hostId(mac, VlanId.NONE), |
| 226 | mac, VlanId.NONE, loc, ImmutableSet.of(ip)); |
| 227 | |
| 228 | return ImmutableSet.of(h); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | class MockMastershipService extends MastershipServiceAdapter { |
| 233 | @Override |
| 234 | public boolean isLocalMaster(DeviceId d) { |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | class MockPort implements Port { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 240 | private ConnectPoint cp; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 241 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 242 | public MockPort(ConnectPoint cp) { |
| 243 | this.cp = cp; |
| 244 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 245 | @Override |
| 246 | public boolean isEnabled() { |
| 247 | return true; |
| 248 | } |
| 249 | @Override |
| 250 | public long portSpeed() { |
| 251 | return 1000; |
| 252 | } |
| 253 | @Override |
| 254 | public Element element() { |
| 255 | return null; |
| 256 | } |
| 257 | @Override |
| 258 | public PortNumber number() { |
| 259 | return null; |
| 260 | } |
| 261 | @Override |
| 262 | public Annotations annotations() { |
| 263 | return new MockAnnotations(); |
| 264 | } |
| 265 | @Override |
| 266 | public Type type() { |
| 267 | return Port.Type.FIBER; |
| 268 | } |
| 269 | |
| 270 | private class MockAnnotations implements Annotations { |
| 271 | |
| 272 | @Override |
| 273 | public String value(String val) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 274 | if (cp.port().toLong() == 32) { |
| 275 | return "ALPHe3d1cea3-1"; |
| 276 | } else if (cp.port().toLong() == 4112) { |
| 277 | return "ALPHe3d1ceb7-1"; |
| 278 | } else { |
| 279 | return "PON 1/1"; |
| 280 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 281 | } |
| 282 | @Override |
| 283 | public Set<String> keys() { |
| 284 | return null; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 289 | /** |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 290 | * Keeps a reference to the PacketProcessor and saves the OutboundPackets. |
| 291 | */ |
| 292 | class MockPacketService extends PacketServiceAdapter { |
| 293 | |
| 294 | @Override |
| 295 | public void addProcessor(PacketProcessor processor, int priority) { |
| 296 | packetProcessor = processor; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void emit(OutboundPacket packet) { |
| 301 | try { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 302 | Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(), |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 303 | 0, packet.data().array().length); |
| 304 | savePacket(eth); |
| 305 | } catch (Exception e) { |
| 306 | fail(e.getMessage()); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 311 | class MockSadisService implements SadisService { |
| 312 | @Override |
| 313 | public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() { |
| 314 | return new DhcpL2RelayTestBase.MockSubService(); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() { |
| 319 | return null; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> { |
| 324 | DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation device = |
| 325 | new DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation(OLT_DEV_ID, VlanId.NONE, VlanId.NONE, null, |
| 326 | null, OLT_MAC_ADDRESS, Ip4Address.valueOf("10.10.10.10"), UPLINK_PORT); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 327 | DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation otherDevice = |
| 328 | new DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation( |
| 329 | "EC1838000853", VlanId.NONE, VlanId.NONE, null, |
| 330 | null, OLT_MAC_ADDRESS, Ip4Address.valueOf("10.10.10.10"), UPLINK_PORT); |
| 331 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 332 | DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation sub = |
| 333 | new DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation(CLIENT_ID_1, CLIENT_C_TAG, |
| 334 | CLIENT_S_TAG, CLIENT_NAS_PORT_ID, CLIENT_CIRCUIT_ID, null, null, -1); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 335 | DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation sub32 = |
| 336 | new DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation("ALPHe3d1cea3-1", VlanId.vlanId((short) 801), |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 337 | VlanId.vlanId((short) 111), CLIENT_NAS_PORT_ID, CLIENT32_CIRCUIT_ID, null, null, -1); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 338 | DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation sub4112 = |
| 339 | new DhcpL2RelayTestBase.MockSubscriberAndDeviceInformation("ALPHe3d1ceb7-1", VlanId.vlanId((short) 101), |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 340 | VlanId.vlanId((short) 222), CLIENT_NAS_PORT_ID, CLIENT4112_CIRCUIT_ID, null, null, -1); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 341 | @Override |
| 342 | public SubscriberAndDeviceInformation get(String id) { |
| 343 | if (id.equals(OLT_DEV_ID)) { |
| 344 | return device; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 345 | } else if (id.equals("EC1838000853")) { |
| 346 | return otherDevice; |
| 347 | } else if (id.equals("ALPHe3d1cea3-1")) { |
| 348 | return sub32; |
| 349 | } else if (id.equals("ALPHe3d1ceb7-1")) { |
| 350 | return sub4112; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 351 | } else { |
| 352 | return sub; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | @Override |
Ilayda Ozdemir | 6b623ea | 2021-02-23 21:06:38 +0000 | [diff] [blame] | 357 | public void clearLocalData() { |
| 358 | |
| 359 | } |
| 360 | |
| 361 | @Override |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 362 | public void invalidateAll() {} |
| 363 | @Override |
| 364 | public void invalidateId(String id) {} |
| 365 | @Override |
| 366 | public SubscriberAndDeviceInformation getfromCache(String id) { |
| 367 | return null; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation { |
| 372 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 373 | MockSubscriberAndDeviceInformation(String id, VlanId cTag, |
| 374 | VlanId sTag, String nasPortId, |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 375 | String circuitId, MacAddress hardId, |
| 376 | Ip4Address ipAddress, int uplinkPort) { |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 377 | this.setHardwareIdentifier(hardId); |
| 378 | this.setId(id); |
| 379 | this.setIPAddress(ipAddress); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 380 | this.setNasPortId(nasPortId); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 381 | this.setUplinkPort(uplinkPort); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 382 | this.setCircuitId(circuitId); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 383 | |
| 384 | List<UniTagInformation> uniTagInformationList = new ArrayList<>(); |
| 385 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 386 | UniTagInformation.Builder b = new UniTagInformation.Builder() |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 387 | .setPonCTag(cTag) |
| 388 | .setPonSTag(sTag) |
| 389 | .setUsPonCTagPriority(CLIENT_C_PBIT) |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 390 | .setIsDhcpRequired(true); |
| 391 | |
| 392 | if (id.equals("ALPHe3d1ceb7-1")) { |
| 393 | // null remoteId, ds pbit is defined |
| 394 | b.setDsPonCTagPriority(5); |
| 395 | } else { |
| 396 | this.setRemoteId(CLIENT_REMOTE_ID); |
| 397 | } |
| 398 | |
| 399 | uniTagInformationList.add(b.build()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 400 | |
| 401 | if (id.equals("ALPHe3d1cea3-1")) { |
| 402 | // a second service on the same UNI |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 403 | UniTagInformation uniTagInformation = new UniTagInformation.Builder() |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 404 | .setPonCTag(VlanId.vlanId(((short) (cTag.toShort() + 1)))) |
| 405 | .setPonSTag(sTag) |
| 406 | .setUsPonCTagPriority(CLIENT_C_PBIT) |
| 407 | .setIsDhcpRequired(true) |
| 408 | .build(); |
| 409 | uniTagInformationList.add(uniTagInformation); |
| 410 | } |
| 411 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 412 | this.setUniTagList(uniTagInformationList); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | class MockComponentContext implements ComponentContext { |
| 417 | |
| 418 | @Override |
| 419 | public Dictionary<String, Object> getProperties() { |
| 420 | Dictionary<String, Object> cfgDict = new Hashtable<String, Object>(); |
| 421 | cfgDict.put("publishCountersRate", 10); |
| 422 | return cfgDict; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public Object locateService(String name) { |
| 427 | // TODO Auto-generated method stub |
| 428 | return null; |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public Object locateService(String name, ServiceReference reference) { |
| 433 | // TODO Auto-generated method stub |
| 434 | return null; |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public Object[] locateServices(String name) { |
| 439 | // TODO Auto-generated method stub |
| 440 | return null; |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public BundleContext getBundleContext() { |
| 445 | // TODO Auto-generated method stub |
| 446 | return null; |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public Bundle getUsingBundle() { |
| 451 | // TODO Auto-generated method stub |
| 452 | return null; |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public ComponentInstance getComponentInstance() { |
| 457 | // TODO Auto-generated method stub |
| 458 | return null; |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void enableComponent(String name) { |
| 463 | // TODO Auto-generated method stub |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void disableComponent(String name) { |
| 468 | // TODO Auto-generated method stub |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public ServiceReference getServiceReference() { |
| 473 | // TODO Auto-generated method stub |
| 474 | return null; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 479 | /** |
| 480 | * Mocks the DefaultPacketContext. |
| 481 | */ |
| 482 | final class TestPacketContext extends DefaultPacketContext { |
| 483 | |
| 484 | private TestPacketContext(long time, InboundPacket inPkt, |
| 485 | OutboundPacket outPkt, boolean block) { |
| 486 | super(time, inPkt, outPkt, block); |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void send() { |
| 491 | // We don't send anything out. |
| 492 | } |
| 493 | } |
| 494 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 495 | public static class TestEventDispatcher extends DefaultEventSinkRegistry |
| 496 | implements EventDeliveryService { |
| 497 | @Override |
| 498 | @SuppressWarnings("unchecked") |
| 499 | public synchronized void post(Event event) { |
| 500 | EventSink sink = getSink(event.getClass()); |
| 501 | checkState(sink != null, "No sink for event %s", event); |
| 502 | sink.process(event); |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void setDispatchTimeLimit(long millis) { |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public long getDispatchTimeLimit() { |
| 511 | return 0; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Creates a mock object for a scheduled executor service. |
| 517 | * |
| 518 | */ |
| 519 | public static final class MockExecutor implements ScheduledExecutorService { |
| 520 | private ScheduledExecutorService executor; |
| 521 | |
| 522 | MockExecutor(ScheduledExecutorService executor) { |
| 523 | this.executor = executor; |
| 524 | } |
| 525 | |
| 526 | String lastMethodCalled = ""; |
| 527 | long lastInitialDelay; |
| 528 | long lastDelay; |
| 529 | TimeUnit lastUnit; |
| 530 | |
| 531 | public void assertLastMethodCalled(String method, long initialDelay, long delay, TimeUnit unit) { |
| 532 | assertEquals(method, lastMethodCalled); |
| 533 | assertEquals(initialDelay, lastInitialDelay); |
| 534 | assertEquals(delay, lastDelay); |
| 535 | assertEquals(unit, lastUnit); |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { |
| 540 | lastMethodCalled = "scheduleRunnable"; |
| 541 | lastDelay = delay; |
| 542 | lastUnit = unit; |
| 543 | return null; |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
| 548 | lastMethodCalled = "scheduleCallable"; |
| 549 | lastDelay = delay; |
| 550 | lastUnit = unit; |
| 551 | return null; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public ScheduledFuture<?> scheduleAtFixedRate( |
| 556 | Runnable command, long initialDelay, long period, TimeUnit unit) { |
| 557 | lastMethodCalled = "scheduleAtFixedRate"; |
| 558 | lastInitialDelay = initialDelay; |
| 559 | lastDelay = period; |
| 560 | lastUnit = unit; |
| 561 | return null; |
| 562 | } |
| 563 | |
| 564 | @Override |
| 565 | public ScheduledFuture<?> scheduleWithFixedDelay( |
| 566 | Runnable command, long initialDelay, long delay, TimeUnit unit) { |
| 567 | lastMethodCalled = "scheduleWithFixedDelay"; |
| 568 | lastInitialDelay = initialDelay; |
| 569 | lastDelay = delay; |
| 570 | lastUnit = unit; |
| 571 | command.run(); |
| 572 | return null; |
| 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public boolean awaitTermination(long timeout, TimeUnit unit) { |
| 577 | throw new UnsupportedOperationException(); |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) |
| 582 | throws InterruptedException { |
| 583 | throw new UnsupportedOperationException(); |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public <T> List<Future<T>> invokeAll( |
| 588 | Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) |
| 589 | throws InterruptedException { |
| 590 | throw new UnsupportedOperationException(); |
| 591 | } |
| 592 | |
| 593 | @Override |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 594 | @SuppressWarnings({"TypeParameterUnusedInFormals"}) |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 595 | public <T> T invokeAny(Collection<? extends Callable<T>> tasks) |
| 596 | throws ExecutionException, InterruptedException { |
| 597 | throw new UnsupportedOperationException(); |
| 598 | } |
| 599 | |
| 600 | @Override |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 601 | @SuppressWarnings({"TypeParameterUnusedInFormals"}) |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 602 | public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) |
| 603 | throws ExecutionException, InterruptedException, TimeoutException { |
| 604 | throw new UnsupportedOperationException(); |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public boolean isShutdown() { |
| 609 | throw new UnsupportedOperationException(); |
| 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public boolean isTerminated() { |
| 614 | throw new UnsupportedOperationException(); |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public void shutdown() { |
| 619 | throw new UnsupportedOperationException(); |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public List<Runnable> shutdownNow() { |
| 624 | return null; |
| 625 | } |
| 626 | |
| 627 | @Override |
Matteo Scandolo | abcc73e | 2020-11-25 16:02:17 -0800 | [diff] [blame] | 628 | @SuppressWarnings({"TypeParameterUnusedInFormals"}) |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 629 | public <T> Future<T> submit(Callable<T> task) { |
| 630 | throw new UnsupportedOperationException(); |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | public Future<?> submit(Runnable task) { |
| 635 | throw new UnsupportedOperationException(); |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public <T> Future<T> submit(Runnable task, T result) { |
| 640 | throw new UnsupportedOperationException(); |
| 641 | } |
| 642 | |
| 643 | @Override |
| 644 | public void execute(Runnable command) { |
| 645 | throw new UnsupportedOperationException(); |
| 646 | } |
| 647 | } |
| 648 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 649 | /** |
| 650 | * Sends an Ethernet packet to the process method of the Packet Processor. |
| 651 | * |
| 652 | * @param pkt Ethernet packet |
| 653 | */ |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 654 | void sendPacket(Ethernet pkt, ConnectPoint cp) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 655 | final ByteBuffer byteBuffer = ByteBuffer.wrap(pkt.serialize()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 656 | InboundPacket inPacket = new DefaultInboundPacket(cp, pkt, byteBuffer); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 657 | |
| 658 | PacketContext context = new TestPacketContext(127L, inPacket, null, false); |
| 659 | packetProcessor.process(context); |
| 660 | } |
| 661 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 662 | private Ethernet constructEthernetPacket(MacAddress srcMac, MacAddress dstMac, |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 663 | String dstIp, byte dhcpReqRsp, |
| 664 | MacAddress clientHwAddress, |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 665 | Ip4Address dhcpClientIpAddress, |
| 666 | VlanId clientVlan, short clientPbit) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 667 | // Ethernet Frame. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 668 | Ethernet ethPkt = new Ethernet(); |
| 669 | ethPkt.setSourceMACAddress(srcMac); |
| 670 | ethPkt.setDestinationMACAddress(dstMac); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 671 | ethPkt.setEtherType(Ethernet.TYPE_IPV4); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 672 | ethPkt.setVlanID(clientVlan.toShort()); |
| 673 | ethPkt.setPriorityCode((byte) clientPbit); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 674 | |
| 675 | if (DHCP.OPCODE_REPLY == dhcpReqRsp) { |
| 676 | ethPkt.setQinQPriorityCode((byte) 3); |
| 677 | ethPkt.setQinQVID((short) 4); |
| 678 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 679 | |
| 680 | // IP Packet |
| 681 | IPv4 ipv4Reply = new IPv4(); |
| 682 | ipv4Reply.setSourceAddress(0); |
| 683 | ipv4Reply.setDestinationAddress(dstIp); |
| 684 | |
| 685 | ipv4Reply.setTtl((byte) 127); |
| 686 | |
| 687 | // UDP Datagram. |
| 688 | UDP udpReply = new UDP(); |
| 689 | udpReply.setSourcePort((byte) UDP.DHCP_CLIENT_PORT); |
| 690 | udpReply.setDestinationPort((byte) UDP.DHCP_SERVER_PORT); |
| 691 | |
| 692 | // DHCP Payload. |
| 693 | DHCP dhcpReply = new DHCP(); |
| 694 | dhcpReply.setOpCode(dhcpReqRsp); |
| 695 | |
| 696 | dhcpReply.setYourIPAddress(dhcpClientIpAddress.toInt()); |
| 697 | dhcpReply.setServerIPAddress(0); |
| 698 | |
| 699 | final byte[] serverNameBytes = new byte[64]; |
| 700 | String result = new String(serverNameBytes, StandardCharsets.US_ASCII).trim(); |
| 701 | dhcpReply.setServerName(result); |
| 702 | |
| 703 | final byte[] bootFileBytes = new byte[128]; |
| 704 | String result1 = new String(bootFileBytes, StandardCharsets.US_ASCII).trim(); |
| 705 | dhcpReply.setBootFileName(result1); |
| 706 | |
| 707 | dhcpReply.setTransactionId(TRANSACTION_ID); |
| 708 | dhcpReply.setClientHardwareAddress(clientHwAddress.toBytes()); |
| 709 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 710 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 711 | |
| 712 | udpReply.setPayload(dhcpReply); |
| 713 | ipv4Reply.setPayload(udpReply); |
| 714 | ethPkt.setPayload(ipv4Reply); |
| 715 | |
| 716 | return ethPkt; |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 717 | |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Constructs an Ethernet packet with IP/UDP/DHCP payload and client |
| 722 | * VLAN information. |
| 723 | * |
| 724 | * @return Ethernet packet |
| 725 | */ |
| 726 | private Ethernet construcEthernetPacket(MacAddress srcMac, MacAddress dstMac, |
| 727 | String dstIp, byte dhcpReqRsp, |
| 728 | MacAddress clientHwAddress, |
| 729 | Ip4Address dhcpClientIpAddress) { |
| 730 | return constructEthernetPacket(srcMac, dstMac, dstIp, dhcpReqRsp, |
| 731 | clientHwAddress, dhcpClientIpAddress, |
| 732 | CLIENT_C_TAG, CLIENT_C_PBIT); |
| 733 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Constructs DHCP Discover Packet. |
| 738 | * |
| 739 | * @return Ethernet packet |
| 740 | */ |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 741 | Ethernet constructDhcpDiscoverPacket(MacAddress clientMac) { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 742 | Ethernet pkt = construcEthernetPacket(clientMac, MacAddress.BROADCAST, |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 743 | "255.255.255.255", |
| 744 | DHCP.OPCODE_REQUEST, clientMac, |
| 745 | Ip4Address.valueOf("0.0.0.0")); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 746 | |
| 747 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 748 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 749 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 750 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 751 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPDISCOVER)); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 752 | log.info("Sending discover packet {}", dhcpPacket.getOptions()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 753 | |
| 754 | return pkt; |
| 755 | } |
| 756 | |
| 757 | /** |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 758 | * Constructs DHCP Discover Packet with client VLAN information. |
| 759 | * |
| 760 | * @return Ethernet packet |
| 761 | */ |
| 762 | Ethernet constructDhcpDiscoverPacket(MacAddress clientMac, VlanId clientVlan, |
| 763 | short clientPbit) { |
| 764 | Ethernet pkt = constructEthernetPacket(clientMac, MacAddress.BROADCAST, |
| 765 | "255.255.255.255", DHCP.OPCODE_REQUEST, clientMac, |
| 766 | Ip4Address.valueOf("0.0.0.0"), clientVlan, clientPbit); |
| 767 | |
| 768 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 769 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 770 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 771 | |
| 772 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPDISCOVER)); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 773 | return pkt; |
| 774 | } |
| 775 | |
| 776 | /** |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 777 | * Constructs DHCP Request Packet. |
| 778 | * |
| 779 | * @return Ethernet packet |
| 780 | */ |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 781 | Ethernet constructDhcpRequestPacket(MacAddress clientMac) { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 782 | Ethernet pkt = construcEthernetPacket(clientMac, MacAddress.BROADCAST, |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 783 | "255.255.255.255", DHCP.OPCODE_REQUEST, clientMac, |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 784 | Ip4Address.valueOf("0.0.0.0")); |
| 785 | |
| 786 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 787 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 788 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 789 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 790 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPREQUEST)); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 791 | |
| 792 | return pkt; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Constructs DHCP Offer Packet. |
| 797 | * |
| 798 | * @return Ethernet packet |
| 799 | */ |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 800 | Ethernet constructDhcpOfferPacket(MacAddress servMac, MacAddress clientMac, |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 801 | String ipAddress, String dhcpClientIpAddress) { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 802 | Ethernet pkt = construcEthernetPacket(servMac, clientMac, ipAddress, DHCP.OPCODE_REPLY, |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 803 | clientMac, Ip4Address.valueOf(dhcpClientIpAddress)); |
| 804 | |
| 805 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 806 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 807 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 808 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 809 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPOFFER)); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 810 | log.info("Sending offer packet {}", dhcpPacket.getOptions()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 811 | return pkt; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Constructs DHCP Ack Packet. |
| 816 | * |
| 817 | * @return Ethernet packet |
| 818 | */ |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 819 | Ethernet constructDhcpAckPacket(MacAddress servMac, MacAddress clientMac, |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 820 | String ipAddress, String dhcpClientIpAddress) { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 821 | Ethernet pkt = construcEthernetPacket(servMac, clientMac, ipAddress, DHCP.OPCODE_REPLY, |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 822 | clientMac, Ip4Address.valueOf(dhcpClientIpAddress)); |
| 823 | |
| 824 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 825 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 826 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 827 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 828 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPACK)); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 829 | |
| 830 | return pkt; |
| 831 | } |
| 832 | |
| 833 | /** |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 834 | * Constructs DHCP Nak Packet. |
| 835 | * |
| 836 | * @return Ethernet packet |
| 837 | */ |
| 838 | Ethernet constructDhcpNakPacket(MacAddress servMac, MacAddress clientMac, |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 839 | String ipAddress, String dhcpClientIpAddress, |
| 840 | VlanId clientVlan, short clientPcp) { |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 841 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 842 | Ethernet pkt = constructEthernetPacket(servMac, clientMac, ipAddress, |
| 843 | DHCP.OPCODE_REPLY, clientMac, |
| 844 | Ip4Address.valueOf(dhcpClientIpAddress), |
| 845 | clientVlan, clientPcp); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 846 | |
| 847 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 848 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 849 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 850 | |
| 851 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPNAK)); |
| 852 | |
| 853 | return pkt; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Constructs DHCP Decline Packet. |
| 858 | * |
| 859 | * @return Ethernet packet |
| 860 | */ |
| 861 | Ethernet constructDhcpDeclinePacket(MacAddress clientMac) { |
| 862 | |
| 863 | Ethernet pkt = construcEthernetPacket(clientMac, MacAddress.BROADCAST, |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 864 | "255.255.255.255", DHCP.OPCODE_REQUEST, clientMac, |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 865 | Ip4Address.valueOf("0.0.0.0")); |
| 866 | |
| 867 | IPv4 ipv4Packet = (IPv4) pkt.getPayload(); |
| 868 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 869 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 870 | |
| 871 | dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPDECLINE)); |
| 872 | |
| 873 | return pkt; |
| 874 | } |
| 875 | |
| 876 | /** |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 877 | * Constructs DHCP Discover Options. |
| 878 | * |
| 879 | * @return Ethernet packet |
| 880 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 881 | private List<DhcpOption> constructDhcpOptions(DHCP.MsgType packetType) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 882 | |
| 883 | // DHCP Options. |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 884 | DhcpOption option = new DhcpOption(); |
| 885 | List<DhcpOption> optionList = new ArrayList<>(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 886 | |
| 887 | |
| 888 | // DHCP Message Type. |
| 889 | option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 890 | option.setLength((byte) 1); |
| 891 | byte[] optionData = {(byte) packetType.getValue()}; |
| 892 | option.setData(optionData); |
| 893 | optionList.add(option); |
| 894 | |
| 895 | // DHCP Requested IP. |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 896 | option = new DhcpOption(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 897 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()); |
| 898 | option.setLength((byte) 4); |
| 899 | optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets(); |
| 900 | option.setData(optionData); |
| 901 | optionList.add(option); |
| 902 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 903 | // Tests app defined Option82 |
| 904 | if (packetType.equals(DHCP.MsgType.DHCPOFFER)) { |
| 905 | option = new DhcpOption(); |
| 906 | option.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 907 | DhcpOption82Data option82 = new DhcpOption82Data(); |
| 908 | option82.setAgentCircuitId(OLT_DEV_ID + "/" + CLIENT_PORT + ":vlan" |
| 909 | + CLIENT_C_TAG + ":pcp" + CLIENT_C_PBIT); |
| 910 | option82.setAgentRemoteId("bababababa"); |
| 911 | option.setData(option82.toByteArray()); |
| 912 | option.setLength(option82.length()); |
| 913 | log.info("Added option82 {}", option); |
| 914 | optionList.add(option); |
| 915 | } |
| 916 | // Tests operator configured Option82, resulting in host lookup |
| 917 | if (packetType.equals(DHCP.MsgType.DHCPACK)) { |
| 918 | option = new DhcpOption(); |
| 919 | option.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 920 | DhcpOption82Data option82 = new DhcpOption82Data(); |
| 921 | option82.setAgentCircuitId(CLIENT_CIRCUIT_ID); |
| 922 | option82.setAgentRemoteId(CLIENT_REMOTE_ID); |
| 923 | option.setData(option82.toByteArray()); |
| 924 | option.setLength(option82.length()); |
| 925 | log.info("Added option82 {}", option); |
| 926 | optionList.add(option); |
| 927 | } |
| 928 | // Tests app-defined option82, but uses incorrect connectPoint - packet |
| 929 | // should still be forwarded to this connectPoint (ie without host lookup). |
| 930 | // Also pbit in circuitId is -1, which means original pbit should be retained |
| 931 | // Finally remoteId is missing |
| 932 | if (packetType.equals(DHCP.MsgType.DHCPNAK)) { |
| 933 | option = new DhcpOption(); |
| 934 | option.setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()); |
| 935 | DhcpOption82Data option82 = new DhcpOption82Data(); |
| 936 | option82.setAgentCircuitId("of:0000b86a974385f7/32" + ":vlan" |
| 937 | + VlanId.vlanId((short) 111) + ":pcp-1"); |
| 938 | option.setData(option82.toByteArray()); |
| 939 | option.setLength(option82.length()); |
| 940 | log.info("Added option82 {}", option); |
| 941 | optionList.add(option); |
| 942 | } |
| 943 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 944 | // End Option. |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 945 | option = new DhcpOption(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 946 | option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 947 | option.setLength((byte) 1); |
| 948 | optionList.add(option); |
| 949 | |
| 950 | return optionList; |
| 951 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 952 | } |