Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 19 | package org.onlab.onos.provider.host.impl; |
| 20 | |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import static org.junit.Assert.assertNull; |
| 24 | |
| 25 | import java.nio.ByteBuffer; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.onlab.onos.net.ConnectPoint; |
| 32 | import org.onlab.onos.net.DeviceId; |
| 33 | import org.onlab.onos.net.HostId; |
| 34 | import org.onlab.onos.net.PortNumber; |
| 35 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 36 | import org.onlab.onos.net.host.HostDescription; |
| 37 | import org.onlab.onos.net.host.HostProvider; |
| 38 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 39 | import org.onlab.onos.net.host.HostProviderService; |
| 40 | import org.onlab.onos.net.packet.DefaultInboundPacket; |
| 41 | import org.onlab.onos.net.packet.InboundPacket; |
| 42 | import org.onlab.onos.net.packet.OutboundPacket; |
| 43 | import org.onlab.onos.net.packet.PacketContext; |
| 44 | import org.onlab.onos.net.packet.PacketProcessor; |
| 45 | import org.onlab.onos.net.packet.PacketService; |
| 46 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 47 | import org.onlab.onos.net.provider.ProviderId; |
| 48 | import org.onlab.onos.net.topology.Topology; |
| 49 | |
| 50 | import org.onlab.onos.net.topology.TopologyServiceAdapter; |
| 51 | import org.onlab.packet.ARP; |
| 52 | import org.onlab.packet.Ethernet; |
| 53 | import org.onlab.packet.MacAddress; |
| 54 | import org.onlab.packet.VlanId; |
| 55 | |
| 56 | public class HostLocationProviderTest { |
| 57 | |
| 58 | private static final Integer INPORT = 10; |
| 59 | private static final String DEV1 = "of:1"; |
| 60 | private static final String DEV2 = "of:2"; |
| 61 | private static final String DEV3 = "of:3"; |
| 62 | |
| 63 | private static final VlanId VLAN = VlanId.vlanId(); |
| 64 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); |
| 65 | private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff"); |
| 66 | private static final byte[] IP = new byte[]{10, 0, 0, 1}; |
| 67 | |
| 68 | private final HostLocationProvider provider = new HostLocationProvider(); |
| 69 | private final TestHostRegistry hostService = new TestHostRegistry(); |
| 70 | private final TestTopologyService topoService = new TestTopologyService(); |
| 71 | private final TestPacketService packetService = new TestPacketService(); |
| 72 | |
| 73 | private PacketProcessor testProcessor; |
| 74 | private TestHostProviderService providerService; |
| 75 | |
| 76 | @Before |
| 77 | public void setUp() { |
| 78 | provider.providerRegistry = hostService; |
| 79 | provider.topologyService = topoService; |
| 80 | provider.pktService = packetService; |
| 81 | |
| 82 | provider.activate(); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void basics() { |
| 88 | assertNotNull("registration expected", providerService); |
| 89 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void events() { |
| 94 | // new host |
| 95 | |
| 96 | |
| 97 | testProcessor.process(new TestPacketContext(DEV1)); |
| 98 | assertNotNull("new host expected", providerService.added); |
| 99 | assertNull("host motion unexpected", providerService.moved); |
| 100 | |
| 101 | // the host moved to new switch |
| 102 | testProcessor.process(new TestPacketContext(DEV2)); |
| 103 | assertNotNull("host motion expected", providerService.moved); |
| 104 | |
| 105 | // the host was misheard on a spine |
| 106 | testProcessor.process(new TestPacketContext(DEV3)); |
| 107 | assertNull("host misheard on spine switch", providerService.spine); |
| 108 | } |
| 109 | |
| 110 | @After |
| 111 | public void tearDown() { |
| 112 | provider.deactivate(); |
| 113 | provider.providerRegistry = null; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | private class TestHostRegistry implements HostProviderRegistry { |
| 118 | |
| 119 | @Override |
| 120 | public HostProviderService register(HostProvider provider) { |
| 121 | providerService = new TestHostProviderService(provider); |
| 122 | return providerService; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void unregister(HostProvider provider) { |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Set<ProviderId> getProviders() { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | private class TestHostProviderService |
| 137 | extends AbstractProviderService<HostProvider> |
| 138 | implements HostProviderService { |
| 139 | |
| 140 | DeviceId added = null; |
| 141 | DeviceId moved = null; |
| 142 | DeviceId spine = null; |
| 143 | |
| 144 | protected TestHostProviderService(HostProvider provider) { |
| 145 | super(provider); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void hostDetected(HostId hostId, HostDescription hostDescription) { |
| 150 | DeviceId descr = hostDescription.location().deviceId(); |
| 151 | if (added == null) { |
| 152 | added = descr; |
| 153 | } else if ((moved == null) && !descr.equals(added)) { |
| 154 | moved = descr; |
| 155 | } else { |
| 156 | spine = descr; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void hostVanished(HostId hostId) { |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | private class TestPacketService implements PacketService { |
| 167 | |
| 168 | @Override |
| 169 | public void addProcessor(PacketProcessor processor, int priority) { |
| 170 | testProcessor = processor; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void removeProcessor(PacketProcessor processor) { |
| 175 | |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void emit(OutboundPacket packet) { |
| 180 | |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | private class TestTopologyService extends TopologyServiceAdapter { |
| 186 | @Override |
| 187 | public boolean isInfrastructure(Topology topology, |
| 188 | ConnectPoint connectPoint) { |
| 189 | //simulate DPID3 as an infrastructure switch |
| 190 | if ((connectPoint.deviceId()).equals(DeviceId.deviceId(DEV3))) { |
| 191 | return true; |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private class TestPacketContext implements PacketContext { |
| 198 | |
| 199 | private final String deviceId; |
| 200 | |
| 201 | public TestPacketContext(String deviceId) { |
| 202 | this.deviceId = deviceId; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public long time() { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public InboundPacket inPacket() { |
| 212 | ARP arp = new ARP(); |
| 213 | arp.setSenderProtocolAddress(IP) |
| 214 | .setSenderHardwareAddress(MAC.toBytes()) |
| 215 | .setTargetHardwareAddress(BCMAC.toBytes()) |
| 216 | .setTargetProtocolAddress(IP); |
| 217 | |
| 218 | Ethernet eth = new Ethernet(); |
| 219 | eth.setEtherType(Ethernet.TYPE_ARP) |
| 220 | .setVlanID(VLAN.toShort()) |
| 221 | .setSourceMACAddress(MAC.toBytes()) |
| 222 | .setDestinationMACAddress(BCMAC.getAddress()) |
| 223 | .setPayload(arp); |
| 224 | ConnectPoint receivedFrom = new ConnectPoint(DeviceId.deviceId(deviceId), |
| 225 | PortNumber.portNumber(INPORT)); |
| 226 | return new DefaultInboundPacket(receivedFrom, eth, |
| 227 | ByteBuffer.wrap(eth.serialize())); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public OutboundPacket outPacket() { |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public TrafficTreatment.Builder treatmentBuilder() { |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void send() { |
| 242 | |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public boolean block() { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public boolean isHandled() { |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 | } |