blob: 7e84f8eef636193e7ea9405745f0383ffbedea2f [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Pingping32fa30c2014-10-23 15:24:26 -070017
18import java.util.HashSet;
19import java.util.Random;
20import java.util.Set;
21import java.util.concurrent.Executors;
22import java.util.concurrent.ScheduledExecutorService;
23import java.util.concurrent.TimeUnit;
24
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DefaultHost;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
30import org.onosproject.net.HostLocation;
31import org.onosproject.net.host.HostEvent;
32import org.onosproject.net.host.HostListener;
33import org.onosproject.net.host.HostService;
34import org.onosproject.net.host.PortAddresses;
35import org.onosproject.net.provider.ProviderId;
36import org.onosproject.sdnip.Router.InternalHostListener;
Pingping32fa30c2014-10-23 15:24:26 -070037import org.onlab.packet.IpAddress;
38import org.onlab.packet.MacAddress;
39import org.onlab.packet.VlanId;
40
41import com.google.common.collect.Sets;
42
43/**
44 * Test version of the HostService which is used to simulate delays in
45 * receiving ARP replies, as you would see in a real system due to the time
46 * it takes to proxy ARP packets to/from the host. Requests are asynchronous,
47 * and replies may come back to the requestor in a different order than the
48 * requests were sent, which again you would expect to see in a real system.
49 */
50public class TestHostService implements HostService {
51
52 /**
53 * The maximum possible delay before an ARP reply is received.
54 */
55 private static final int MAX_ARP_REPLY_DELAY = 30; // milliseconds
56
57 /**
58 * The probability that we already have the MAC address cached when the
59 * caller calls {@link #getHostsByIp(IpAddress ipAddress)}.
60 */
61 private static final float MAC_ALREADY_KNOWN_PROBABILITY = 0.3f;
62
63 private final ScheduledExecutorService replyTaskExecutor;
64 private final Random random;
65
66 /**
67 * Class constructor.
68 */
69 public TestHostService() {
70 replyTaskExecutor = Executors.newSingleThreadScheduledExecutor();
71 random = new Random();
72 }
73
74 /**
75 * Task used to reply to ARP requests from a different thread. Replies
76 * usually come on a different thread in the real system, so we need to
77 * ensure we test this behavior.
78 */
79 private class ReplyTask implements Runnable {
80 private HostListener listener;
81 private IpAddress ipAddress;
82
83 /**
84 * Class constructor.
85 *
86 * @param listener the client who requests and waits the MAC address
87 * @param ipAddress the target IP address of the request
88 */
89 public ReplyTask(InternalHostListener listener,
90 IpAddress ipAddress) {
91 this.listener = listener;
92 this.ipAddress = ipAddress;
93 }
94
95 @Override
96 public void run() {
97 Host host = getHostsByIp(ipAddress).iterator().next();
98 HostEvent hostevent =
99 new HostEvent(HostEvent.Type.HOST_ADDED, host);
100 listener.event(hostevent);
101 }
102 }
103
104 @Override
105 public Set<Host> getHostsByIp(IpAddress ipAddress) {
106 float replyChance = random.nextFloat();
107
108 // We don't care what the attachment point is in the test,
109 // so for all the hosts, we use a same ConnectPoint.
110 Host host = new DefaultHost(ProviderId.NONE, HostId.NONE,
111 SdnIpTest.generateMacAddress(ipAddress), VlanId.NONE,
112 new HostLocation(SdnIpTest.SW1_ETH1, 1),
113 Sets.newHashSet(ipAddress));
114
115 if (replyChance < MAC_ALREADY_KNOWN_PROBABILITY) {
116 // Some percentage of the time we already know the MAC address, so
117 // we reply directly when the requestor asks for the MAC address
118 return Sets.newHashSet(host);
119 }
120 return new HashSet<Host>();
121 }
122
123 @Override
124 public void startMonitoringIp(IpAddress ipAddress) {
125
126 // Randomly select an amount of time to delay the reply coming back to
127 int delay = random.nextInt(MAX_ARP_REPLY_DELAY);
128 ReplyTask replyTask = new ReplyTask(
129 (SdnIpTest.router.new InternalHostListener()), ipAddress);
130 replyTaskExecutor.schedule(replyTask, delay, TimeUnit.MILLISECONDS);
131 }
132
133 @Override
134 public int getHostCount() {
135 return 0;
136 }
137
138 @Override
139 public Iterable<Host> getHosts() {
140 return null;
141 }
142
143 @Override
144 public Host getHost(HostId hostId) {
145 return null;
146 }
147
148 @Override
149 public Set<Host> getHostsByVlan(VlanId vlanId) {
150 return null;
151 }
152
153 @Override
154 public Set<Host> getHostsByMac(MacAddress mac) {
155 return null;
156 }
157
158 @Override
159 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
160 return null;
161 }
162
163 @Override
164 public Set<Host> getConnectedHosts(DeviceId deviceId) {
165 return null;
166 }
167
168 @Override
169 public void stopMonitoringIp(IpAddress ip) {
170
171 }
172
173 @Override
174 public void requestMac(IpAddress ip) {
175
176 }
177
178 @Override
179 public Set<PortAddresses> getAddressBindings() {
180 return null;
181 }
182
183 @Override
184 public Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint) {
185 return null;
186 }
187
188 @Override
189 public void addListener(HostListener listener) {
190
191 }
192
193 @Override
194 public void removeListener(HostListener listener) {
195
196 }
197
198}