blob: 44914b2362e50f26dd90e547aba880740f18a8d9 [file] [log] [blame]
Jonathan Hart584ea2d2016-10-11 10:49:16 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart584ea2d2016-10-11 10:49:16 +02003 *
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
17package org.onosproject.incubator.net.neighbour.impl;
18
19import org.onlab.packet.ARP;
20import org.onlab.packet.Ethernet;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.incubator.net.intf.Interface;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.host.InterfaceIpAddress;
28
29import java.util.Collections;
30import java.util.List;
31
32/**
33 * Test utilities.
34 */
35public 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}