Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.net.neighbour.impl; |
| 18 | |
| 19 | import org.onlab.packet.ARP; |
| 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.intf.Interface; |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.host.InterfaceIpAddress; |
| 28 | |
| 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Test utilities. |
| 34 | */ |
| 35 | public final class NeighbourTestUtils { |
| 36 | |
| 37 | private static final MacAddress MAC1 = MacAddress.valueOf(1); |
| 38 | private static final MacAddress MAC2 = MacAddress.valueOf(2); |
| 39 | private static final IpAddress IP2 = IpAddress.valueOf(2); |
| 40 | |
| 41 | private NeighbourTestUtils() { |
| 42 | |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Creates an ARP request for the given target IP. |
| 47 | * |
| 48 | * @param targetIp IP address |
| 49 | * @return ARP request packet |
| 50 | */ |
| 51 | public static Ethernet createArpRequest(IpAddress targetIp) { |
| 52 | Ethernet eth = new Ethernet(); |
| 53 | eth.setDestinationMACAddress(MAC1); |
| 54 | eth.setSourceMACAddress(MAC2); |
| 55 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 56 | |
| 57 | ARP arp = new ARP(); |
| 58 | arp.setOpCode(ARP.OP_REPLY); |
| 59 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 60 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
| 61 | |
| 62 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
| 63 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 64 | arp.setSenderHardwareAddress(MAC2.toBytes()); |
| 65 | arp.setTargetHardwareAddress(MacAddress.ZERO.toBytes()); |
| 66 | |
| 67 | arp.setTargetProtocolAddress(targetIp.toOctets()); |
| 68 | arp.setSenderProtocolAddress(IP2.toOctets()); |
| 69 | |
| 70 | eth.setPayload(arp); |
| 71 | return eth; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates an interface with the given parameters. The IP prefix is assumed |
| 76 | * to be a /24. |
| 77 | * |
| 78 | * @param cp connect point |
| 79 | * @param ip IP address |
| 80 | * @param mac MAC address |
| 81 | * @param vlan VLAN ID |
| 82 | * @return interface |
| 83 | */ |
| 84 | public static Interface intf(ConnectPoint cp, IpAddress ip, MacAddress mac, VlanId vlan) { |
| 85 | List<InterfaceIpAddress> ips = |
| 86 | Collections.singletonList(InterfaceIpAddress.valueOf(ip.toString() + "/24")); |
| 87 | return new Interface("foo", cp, ips, mac, vlan); |
| 88 | } |
| 89 | } |