alshabib

added test for hostlocation provider

Change-Id: I37b8efccf2e6c440a416e0d26e519a4d230d7b2a
...@@ -85,6 +85,9 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid ...@@ -85,6 +85,9 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid
85 85
86 @Override 86 @Override
87 public void process(PacketContext context) { 87 public void process(PacketContext context) {
88 + if (context == null) {
89 + return;
90 + }
88 Ethernet eth = context.inPacket().parsed(); 91 Ethernet eth = context.inPacket().parsed();
89 92
90 VlanId vlan = VlanId.vlanId(eth.getVlanID()); 93 VlanId vlan = VlanId.vlanId(eth.getVlanID());
......
1 +package org.onlab.onos.provider.host.impl;
2 +
3 +import static org.junit.Assert.assertEquals;
4 +import static org.junit.Assert.assertNotNull;
5 +import static org.junit.Assert.assertNull;
6 +
7 +import java.nio.ByteBuffer;
8 +import java.util.Set;
9 +
10 +import org.junit.After;
11 +import org.junit.Before;
12 +import org.junit.Test;
13 +import org.onlab.onos.net.ConnectPoint;
14 +import org.onlab.onos.net.DeviceId;
15 +import org.onlab.onos.net.HostId;
16 +import org.onlab.onos.net.PortNumber;
17 +import org.onlab.onos.net.flow.TrafficTreatment;
18 +import org.onlab.onos.net.host.HostDescription;
19 +import org.onlab.onos.net.host.HostProvider;
20 +import org.onlab.onos.net.host.HostProviderRegistry;
21 +import org.onlab.onos.net.host.HostProviderService;
22 +import org.onlab.onos.net.packet.DefaultInboundPacket;
23 +import org.onlab.onos.net.packet.InboundPacket;
24 +import org.onlab.onos.net.packet.OutboundPacket;
25 +import org.onlab.onos.net.packet.PacketContext;
26 +import org.onlab.onos.net.packet.PacketProcessor;
27 +import org.onlab.onos.net.packet.PacketService;
28 +import org.onlab.onos.net.provider.AbstractProviderService;
29 +import org.onlab.onos.net.provider.ProviderId;
30 +import org.onlab.onos.net.topology.Topology;
31 +
32 +import org.onlab.onos.net.topology.TopologyServiceAdapter;
33 +import org.onlab.packet.ARP;
34 +import org.onlab.packet.Ethernet;
35 +import org.onlab.packet.MacAddress;
36 +import org.onlab.packet.VlanId;
37 +
38 +public class HostLocationProviderTest {
39 +
40 + private static final Integer INPORT = 10;
41 + private static final String DEV1 = "of:1";
42 + private static final String DEV2 = "of:2";
43 + private static final String DEV3 = "of:3";
44 +
45 + private static final VlanId VLAN = VlanId.vlanId();
46 + private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
47 + private static final MacAddress BCMAC = MacAddress.valueOf("ff:ff:ff:ff:ff:ff");
48 + private static final byte[] IP = new byte[]{10, 0, 0, 1};
49 +
50 + private final HostLocationProvider provider = new HostLocationProvider();
51 + private final TestHostRegistry hostService = new TestHostRegistry();
52 + private final TestTopologyService topoService = new TestTopologyService();
53 + private final TestPacketService packetService = new TestPacketService();
54 +
55 + private PacketProcessor testProcessor;
56 + private TestHostProviderService providerService;
57 +
58 + @Before
59 + public void setUp() {
60 + provider.providerRegistry = hostService;
61 + provider.topologyService = topoService;
62 + provider.pktService = packetService;
63 +
64 + provider.activate();
65 +
66 + }
67 +
68 + @Test
69 + public void basics() {
70 + assertNotNull("registration expected", providerService);
71 + assertEquals("incorrect provider", provider, providerService.provider());
72 + }
73 +
74 + @Test
75 + public void events() {
76 + // new host
77 +
78 +
79 + testProcessor.process(new TestPacketContext(DEV1));
80 + assertNotNull("new host expected", providerService.added);
81 + assertNull("host motion unexpected", providerService.moved);
82 +
83 + // the host moved to new switch
84 + testProcessor.process(new TestPacketContext(DEV2));
85 + assertNotNull("host motion expected", providerService.moved);
86 +
87 + // the host was misheard on a spine
88 + testProcessor.process(new TestPacketContext(DEV3));
89 + assertNull("host misheard on spine switch", providerService.spine);
90 + }
91 +
92 + @After
93 + public void tearDown() {
94 + provider.deactivate();
95 + provider.providerRegistry = null;
96 +
97 + }
98 +
99 + private class TestHostRegistry implements HostProviderRegistry {
100 +
101 + @Override
102 + public HostProviderService register(HostProvider provider) {
103 + providerService = new TestHostProviderService(provider);
104 + return providerService;
105 + }
106 +
107 + @Override
108 + public void unregister(HostProvider provider) {
109 + }
110 +
111 + @Override
112 + public Set<ProviderId> getProviders() {
113 + return null;
114 + }
115 +
116 + }
117 +
118 + private class TestHostProviderService
119 + extends AbstractProviderService<HostProvider>
120 + implements HostProviderService {
121 +
122 + DeviceId added = null;
123 + DeviceId moved = null;
124 + DeviceId spine = null;
125 +
126 + protected TestHostProviderService(HostProvider provider) {
127 + super(provider);
128 + }
129 +
130 + @Override
131 + public void hostDetected(HostId hostId, HostDescription hostDescription) {
132 + DeviceId descr = hostDescription.location().deviceId();
133 + if (added == null) {
134 + added = descr;
135 + } else if ((moved == null) && !descr.equals(added)) {
136 + moved = descr;
137 + } else {
138 + spine = descr;
139 + }
140 + }
141 +
142 + @Override
143 + public void hostVanished(HostId hostId) {
144 + }
145 +
146 + }
147 +
148 + private class TestPacketService implements PacketService {
149 +
150 + @Override
151 + public void addProcessor(PacketProcessor processor, int priority) {
152 + testProcessor = processor;
153 + }
154 +
155 + @Override
156 + public void removeProcessor(PacketProcessor processor) {
157 +
158 + }
159 +
160 + @Override
161 + public void emit(OutboundPacket packet) {
162 +
163 + }
164 + }
165 +
166 +
167 + private class TestTopologyService extends TopologyServiceAdapter {
168 + @Override
169 + public boolean isInfrastructure(Topology topology,
170 + ConnectPoint connectPoint) {
171 + //simulate DPID3 as an infrastructure switch
172 + if ((connectPoint.deviceId()).equals(DeviceId.deviceId(DEV3))) {
173 + return true;
174 + }
175 + return false;
176 + }
177 + }
178 +
179 + private class TestPacketContext implements PacketContext {
180 +
181 + private final String deviceId;
182 +
183 + public TestPacketContext(String deviceId) {
184 + this.deviceId = deviceId;
185 + }
186 +
187 + @Override
188 + public long time() {
189 + return 0;
190 + }
191 +
192 + @Override
193 + public InboundPacket inPacket() {
194 + ARP arp = new ARP();
195 + arp.setSenderProtocolAddress(IP)
196 + .setSenderHardwareAddress(MAC.toBytes())
197 + .setTargetHardwareAddress(BCMAC.toBytes())
198 + .setTargetProtocolAddress(IP);
199 +
200 + Ethernet eth = new Ethernet();
201 + eth.setEtherType(Ethernet.TYPE_ARP)
202 + .setVlanID(VLAN.toShort())
203 + .setSourceMACAddress(MAC.toBytes())
204 + .setDestinationMACAddress(BCMAC.getAddress())
205 + .setPayload(arp);
206 + ConnectPoint receivedFrom = new ConnectPoint(DeviceId.deviceId(deviceId),
207 + PortNumber.portNumber(INPORT));
208 + return new DefaultInboundPacket(receivedFrom, eth,
209 + ByteBuffer.wrap(eth.serialize()));
210 + }
211 +
212 + @Override
213 + public OutboundPacket outPacket() {
214 + return null;
215 + }
216 +
217 + @Override
218 + public TrafficTreatment.Builder treatmentBuilder() {
219 + return null;
220 + }
221 +
222 + @Override
223 + public void send() {
224 +
225 + }
226 +
227 + @Override
228 + public boolean block() {
229 + return false;
230 + }
231 +
232 + @Override
233 + public boolean isHandled() {
234 + return false;
235 + }
236 + }
237 +}
...@@ -152,6 +152,9 @@ public class LLDPLinkProvider extends AbstractProvider implements LinkProvider { ...@@ -152,6 +152,9 @@ public class LLDPLinkProvider extends AbstractProvider implements LinkProvider {
152 152
153 @Override 153 @Override
154 public void process(PacketContext context) { 154 public void process(PacketContext context) {
155 + if (context == null) {
156 + return;
157 + }
155 LinkDiscovery ld = discoverers.get( 158 LinkDiscovery ld = discoverers.get(
156 context.inPacket().receivedFrom().deviceId()); 159 context.inPacket().receivedFrom().deviceId());
157 if (ld == null) { 160 if (ld == null) {
......