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