Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.host.impl; |
| 17 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNotNull; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.onos.net.ConnectPoint; |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.HostId; |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.host.HostDescription; |
| 30 | import org.onlab.onos.net.host.HostProvider; |
| 31 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 32 | import org.onlab.onos.net.host.HostProviderService; |
| 33 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 34 | import org.onlab.onos.net.provider.ProviderId; |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.topology.Topology; |
tom | 61359e9 | 2014-09-16 15:50:27 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.topology.TopologyServiceAdapter; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 37 | import org.onlab.onos.openflow.controller.Dpid; |
| 38 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
| 39 | import org.onlab.onos.openflow.controller.OpenflowControllerAdapter; |
| 40 | import org.onlab.onos.openflow.controller.PacketListener; |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 41 | import org.onlab.packet.ARP; |
| 42 | import org.onlab.packet.Ethernet; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 43 | import org.onlab.packet.MacAddress; |
| 44 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 46 | import org.projectfloodlight.openflow.types.OFPort; |
| 47 | |
| 48 | public class OpenFlowHostProviderTest { |
| 49 | |
| 50 | private static final Integer INPORT = 10; |
| 51 | private static final Dpid DPID1 = new Dpid(100); |
| 52 | private static final Dpid DPID2 = new Dpid(200); |
| 53 | private static final Dpid DPID3 = new Dpid(300); |
| 54 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 55 | private static final VlanId VLAN = VlanId.vlanId(); |
| 56 | private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01"); |
| 57 | private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff"); |
tom | 43526fc | 2014-09-16 15:12:22 -0700 | [diff] [blame] | 58 | private static final byte[] IP = new byte[]{10, 0, 0, 1}; |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 59 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 60 | private final OpenFlowHostProvider provider = new OpenFlowHostProvider(); |
| 61 | private final TestHostRegistry hostService = new TestHostRegistry(); |
| 62 | private final TestController controller = new TestController(); |
| 63 | private final TestTopologyService topoService = new TestTopologyService(); |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 64 | private TestHostProviderService providerService; |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() { |
| 68 | provider.providerRegistry = hostService; |
| 69 | provider.controller = controller; |
| 70 | provider.topologyService = topoService; |
| 71 | provider.activate(); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void basics() { |
| 76 | assertNotNull("registration expected", providerService); |
| 77 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void events() { |
| 82 | // new host |
| 83 | controller.processPacket(DPID1, null); |
| 84 | assertNotNull("new host expected", providerService.added); |
| 85 | assertNull("host motion unexpected", providerService.moved); |
| 86 | |
| 87 | // the host moved to new switch |
| 88 | controller.processPacket(DPID2, null); |
| 89 | assertNotNull("host motion expected", providerService.moved); |
| 90 | |
| 91 | // the host was misheard on a spine |
| 92 | controller.processPacket(DPID3, null); |
| 93 | assertNull("host misheard on spine switch", providerService.spine); |
| 94 | } |
| 95 | |
| 96 | @After |
| 97 | public void tearDown() { |
| 98 | provider.deactivate(); |
| 99 | provider.providerRegistry = null; |
| 100 | provider.controller = null; |
| 101 | } |
| 102 | |
| 103 | private class TestHostRegistry implements HostProviderRegistry { |
| 104 | |
| 105 | @Override |
| 106 | public HostProviderService register(HostProvider provider) { |
| 107 | providerService = new TestHostProviderService(provider); |
| 108 | return providerService; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void unregister(HostProvider provider) { |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Set<ProviderId> getProviders() { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | private class TestHostProviderService |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 123 | extends AbstractProviderService<HostProvider> |
| 124 | implements HostProviderService { |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 125 | |
| 126 | Dpid added = null; |
| 127 | Dpid moved = null; |
| 128 | Dpid spine = null; |
| 129 | |
| 130 | protected TestHostProviderService(HostProvider provider) { |
| 131 | super(provider); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void hostDetected(HostId hostId, HostDescription hostDescription) { |
| 136 | Dpid descr = Dpid.dpid(hostDescription.location().deviceId().uri()); |
| 137 | if (added == null) { |
| 138 | added = descr; |
| 139 | } else if ((moved == null) && !descr.equals(added)) { |
| 140 | moved = descr; |
| 141 | } else { |
| 142 | spine = descr; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void hostVanished(HostId hostId) { |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
tom | 61359e9 | 2014-09-16 15:50:27 -0700 | [diff] [blame] | 152 | private class TestController extends OpenflowControllerAdapter { |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 153 | PacketListener pktListener; |
| 154 | |
| 155 | @Override |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 156 | public void addPacketListener(int priority, PacketListener listener) { |
| 157 | pktListener = listener; |
| 158 | } |
| 159 | |
| 160 | @Override |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 161 | public void processPacket(Dpid dpid, OFMessage msg) { |
tom | 61359e9 | 2014-09-16 15:50:27 -0700 | [diff] [blame] | 162 | OpenFlowPacketContext ctx = new TestPacketContext(dpid); |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 163 | pktListener.handlePacket(ctx); |
| 164 | } |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 165 | } |
| 166 | |
tom | 61359e9 | 2014-09-16 15:50:27 -0700 | [diff] [blame] | 167 | private class TestTopologyService extends TopologyServiceAdapter { |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public boolean isInfrastructure(Topology topology, |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 170 | ConnectPoint connectPoint) { |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 171 | //simulate DPID3 as an infrastructure switch |
| 172 | if (Dpid.dpid(connectPoint.deviceId().uri()).equals(DPID3)) { |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private class TestPacketContext implements OpenFlowPacketContext { |
| 180 | |
| 181 | protected Dpid swid; |
| 182 | |
| 183 | public TestPacketContext(Dpid dpid) { |
| 184 | swid = dpid; |
| 185 | } |
| 186 | |
| 187 | @Override |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 188 | public boolean block() { |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void send() { |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void build(OFPort outPort) { |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void build(Ethernet ethFrame, OFPort outPort) { |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Ethernet parsed() { |
| 206 | // just things we (and serializers) need |
| 207 | ARP arp = new ARP(); |
| 208 | arp.setSenderProtocolAddress(IP) |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 209 | .setSenderHardwareAddress(MAC.toBytes()) |
| 210 | .setTargetHardwareAddress(BCMAC.toBytes()) |
| 211 | .setTargetProtocolAddress(IP); |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 212 | |
| 213 | Ethernet eth = new Ethernet(); |
| 214 | eth.setEtherType(Ethernet.TYPE_ARP) |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 215 | .setVlanID(VLAN.toShort()) |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 216 | .setSourceMACAddress(MAC) |
| 217 | .setDestinationMACAddress(BCMAC) |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 218 | .setPayload(arp); |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 219 | |
| 220 | return eth; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public byte[] unparsed() { |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Dpid dpid() { |
| 230 | return swid; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Integer inPort() { |
| 235 | return INPORT; |
| 236 | } |
| 237 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 238 | @Override |
| 239 | public boolean isHandled() { |
| 240 | return false; |
| 241 | } |
| 242 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public boolean isBuffered() { |
| 245 | return false; |
| 246 | } |
| 247 | |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 248 | } |
| 249 | } |