blob: 40e49afc9f8629ce71ec0c9fb5f1dc141385a27c [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
Ray Milkeyb65d7842017-08-03 16:28:24 -070017package org.onosproject.net.neighbour.impl;
Jonathan Hart584ea2d2016-10-11 10:49:16 +020018
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Sets;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.ARP;
24import org.onlab.packet.Ethernet;
25import org.onlab.packet.Ip4Address;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
Ray Milkeyfacf2862017-08-03 11:58:29 -070029import org.onosproject.net.intf.Interface;
Jonathan Hart584ea2d2016-10-11 10:49:16 +020030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.edge.EdgePortServiceAdapter;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.packet.DefaultOutboundPacket;
35import org.onosproject.net.packet.OutboundPacket;
36import org.onosproject.net.packet.PacketService;
37
38import java.nio.ByteBuffer;
39import java.util.Collections;
40import java.util.List;
41
42import static org.easymock.EasyMock.createMock;
43import static org.easymock.EasyMock.expectLastCall;
44import static org.easymock.EasyMock.replay;
45import static org.easymock.EasyMock.verify;
Ray Milkeyb65d7842017-08-03 16:28:24 -070046import static org.onosproject.net.neighbour.impl.DefaultNeighbourMessageContext.createContext;
Jonathan Hart584ea2d2016-10-11 10:49:16 +020047
48/**
49 * Unit tests for DefaultNeighbourMessageActions.
50 */
51public class DefaultNeighbourMessageActionsTest {
52
53 private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1");
54 private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint("of:0000000000000001/2");
55 private static final ConnectPoint CP3 = ConnectPoint.deviceConnectPoint("of:0000000000000002/1");
56
57 private static final List<ConnectPoint> EDGE_PORTS =
58 ImmutableList.<ConnectPoint>builder()
59 .add(CP1)
60 .add(CP2)
61 .add(CP3)
62 .build();
63
64 private static final MacAddress MAC1 = MacAddress.valueOf(1);
65 private static final MacAddress MAC2 = MacAddress.valueOf(2);
66 private static final IpAddress IP1 = IpAddress.valueOf(1);
67 private static final IpAddress IP2 = IpAddress.valueOf(2);
68
69 private static final VlanId VLAN1 = VlanId.vlanId((short) 1);
70
Ray Milkeyb65d7842017-08-03 16:28:24 -070071 private static final Interface INTF1 = NeighbourTestUtils.intf(CP1, IP1, MAC1, VLAN1);
72 private static final Interface INTF2 = NeighbourTestUtils.intf(CP2, IP2, MAC2, VLAN1);
Jonathan Hart584ea2d2016-10-11 10:49:16 +020073
74 private DefaultNeighbourMessageActions actions;
75 private PacketService packetService;
76
77 @Before
78 public void setUp() throws Exception {
79 packetService = createMock(PacketService.class);
80 actions = new DefaultNeighbourMessageActions(packetService, new TestEdgeService());
81 }
82
83 @Test
84 public void reply() throws Exception {
Ray Milkeyb65d7842017-08-03 16:28:24 -070085 Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
Jonathan Hart584ea2d2016-10-11 10:49:16 +020086
87 Ip4Address ip4Address = INTF1.ipAddressesList().get(0).ipAddress().getIp4Address();
88 Ethernet response = ARP.buildArpReply(ip4Address, MAC2, request);
89
90 packetService.emit(outbound(response, CP1));
91 expectLastCall().once();
92 replay(packetService);
93
94 actions.reply(createContext(request, CP1, null), MAC2);
95
96 verify(packetService);
97 }
98
99 @Test
100 public void forwardToConnectPoint() {
Ray Milkeyb65d7842017-08-03 16:28:24 -0700101 Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200102
103 packetService.emit(outbound(request, CP2));
104 expectLastCall().once();
105 replay(packetService);
106
107 actions.forward(createContext(request, CP1, null), CP2);
108
109 verify(packetService);
110 }
111
112 @Test
113 public void forwardToInterface() {
Ray Milkeyb65d7842017-08-03 16:28:24 -0700114 Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200115
Ray Milkeyf0c47612017-09-28 11:29:38 -0700116 Ethernet forwardedRequest = request.duplicate();
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200117 forwardedRequest.setSourceMACAddress(INTF2.mac());
118 forwardedRequest.setVlanID(INTF2.vlan().toShort());
119
120 packetService.emit(outbound(forwardedRequest, CP2));
121 expectLastCall().once();
122 replay(packetService);
123
124 actions.forward(createContext(request, CP1, null), INTF2);
125
126 verify(packetService);
127 }
128
129 @Test
130 public void flood() {
Ray Milkeyb65d7842017-08-03 16:28:24 -0700131 Ethernet request = NeighbourTestUtils.createArpRequest(IP1);
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200132
133 // Expect the packet to be emitted out all ports apart from the in port
134 Sets.difference(Sets.newLinkedHashSet(EDGE_PORTS), Collections.singleton(CP1))
135 .forEach(cp -> {
136 packetService.emit(outbound(request, cp));
137 expectLastCall().once();
138 });
139 replay(packetService);
140
141 actions.flood(createContext(request, CP1, null));
142
143 verify(packetService);
144 }
145
146 private static OutboundPacket outbound(Ethernet packet, ConnectPoint outPort) {
147 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(outPort.port()).build();
148 return new DefaultOutboundPacket(outPort.deviceId(),
149 treatment, ByteBuffer.wrap(packet.serialize()));
150 }
151
152 private class TestEdgeService extends EdgePortServiceAdapter {
153
154 @Override
155 public boolean isEdgePoint(ConnectPoint point) {
156 return true;
157 }
158
159 @Override
160 public Iterable<ConnectPoint> getEdgePoints() {
161 return EDGE_PORTS;
162 }
163
164 }
165
166}