blob: feae799840868ed21f181ff58efeb67a0c6ec2c3 [file] [log] [blame]
Chetan Gaonkercbe79642016-03-09 17:45:58 -08001import unittest
2from nose.tools import *
3from nose.twistedtools import reactor, deferred
4from twisted.internet import defer
5from scapy.all import *
6import time, monotonic
7import os, sys
8import tempfile
9import random
10import threading
11from Stats import Stats
12from OnosCtrl import OnosCtrl
13from DHCP import DHCPTest
14from EapTLS import TLSAuthTest
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070015from Channels import Channels, IgmpChannel
Chetan Gaonker41d2e072016-03-15 16:41:31 -070016from subscriberDb import SubscriberDB
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070017from threadPool import ThreadPool
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070018from portmaps import g_subscriber_port_map
19from portmaps import g_subscriber_reverse_port_map
Chetan Gaonkercbe79642016-03-09 17:45:58 -080020log.setLevel('INFO')
21
22class Subscriber(Channels):
23
24 STATS_RX = 0
25 STATS_TX = 1
26 STATS_JOIN = 2
27 STATS_LEAVE = 3
Chetan Gaonker41d2e072016-03-15 16:41:31 -070028 SUBSCRIBER_SERVICES = 'DHCP IGMP TLS'
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070029 def __init__(self, name = 'sub', service = SUBSCRIBER_SERVICES, num = 1, channel_start = 0,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070030 tx_port = 2, rx_port = 1,
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070031 iface = 'veth0', iface_mcast = 'veth2',
Chetan Gaonkercbe79642016-03-09 17:45:58 -080032 mcast_cb = None, loginType = 'wireless'):
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070033 self.tx_port = tx_port
34 self.rx_port = rx_port
35 self.tx_intf = g_subscriber_port_map[tx_port]
36 self.rx_intf = g_subscriber_port_map[rx_port]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070037 Channels.__init__(self, num, channel_start = channel_start,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070038 iface = self.rx_intf, iface_mcast = self.tx_intf, mcast_cb = mcast_cb)
Chetan Gaonker41d2e072016-03-15 16:41:31 -070039 self.name = name
40 self.service = service
41 self.service_map = {}
42 services = self.service.strip().split(' ')
43 for s in services:
44 self.service_map[s] = True
Chetan Gaonkercbe79642016-03-09 17:45:58 -080045 self.loginType = loginType
46 ##start streaming channels
47 self.join_map = {}
48 ##accumulated join recv stats
49 self.join_rx_stats = Stats()
50
Chetan Gaonker41d2e072016-03-15 16:41:31 -070051 def has_service(self, service):
52 if self.service_map.has_key(service):
53 return self.service_map[service]
54 if self.service_map.has_key(service.upper()):
55 return self.service_map[service.upper()]
56 return False
57
Chetan Gaonkercbe79642016-03-09 17:45:58 -080058 def channel_join_update(self, chan, join_time):
59 self.join_map[chan] = ( Stats(), Stats(), Stats(), Stats() )
60 self.channel_update(chan, self.STATS_JOIN, 1, t = join_time)
61
62 def channel_join(self, chan = 0, delay = 2):
63 '''Join a channel and create a send/recv stats map'''
64 if self.join_map.has_key(chan):
65 del self.join_map[chan]
66 self.delay = delay
67 chan, join_time = self.join(chan)
68 self.channel_join_update(chan, join_time)
69 return chan
70
71 def channel_join_next(self, delay = 2):
72 '''Joins the next channel leaving the last channel'''
73 if self.last_chan:
74 if self.join_map.has_key(self.last_chan):
75 del self.join_map[self.last_chan]
76 self.delay = delay
77 chan, join_time = self.join_next()
78 self.channel_join_update(chan, join_time)
79 return chan
80
81 def channel_jump(self, delay = 2):
82 '''Jumps randomly to the next channel leaving the last channel'''
83 if self.last_chan is not None:
84 if self.join_map.has_key(self.last_chan):
85 del self.join_map[self.last_chan]
86 self.delay = delay
87 chan, join_time = self.jump()
88 self.channel_join_update(chan, join_time)
89 return chan
90
91 def channel_leave(self, chan = 0):
92 if self.join_map.has_key(chan):
93 del self.join_map[chan]
94 self.leave(chan)
95
96 def channel_update(self, chan, stats_type, packets, t=0):
97 if type(chan) == type(0):
98 chan_list = (chan,)
99 else:
100 chan_list = chan
101 for c in chan_list:
102 if self.join_map.has_key(c):
103 self.join_map[c][stats_type].update(packets = packets, t = t)
104
105 def channel_receive(self, chan, cb = None, count = 1):
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700106 log.info('Subscriber %s receiving from group %s, channel %d' %(self.name, self.gaddr(chan), chan))
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800107 self.recv(chan, cb = cb, count = count)
108
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700109 def recv_channel_cb(self, pkt):
110 ##First verify that we have received the packet for the joined instance
111 log.debug('Packet received for group %s, subscriber %s' %(pkt[IP].dst, self.name))
112 chan = self.caddr(pkt[IP].dst)
113 assert_equal(chan in self.join_map.keys(), True)
114 recv_time = monotonic.monotonic() * 1000000
115 join_time = self.join_map[chan][self.STATS_JOIN].start
116 delta = recv_time - join_time
117 self.join_rx_stats.update(packets=1, t = delta, usecs = True)
118 self.channel_update(chan, self.STATS_RX, 1, t = delta)
119 log.debug('Packet received in %.3f usecs for group %s after join' %(delta, pkt[IP].dst))
120
121class subscriber_pool:
122
123 def __init__(self, subscriber, test_cbs):
124 self.subscriber = subscriber
125 self.test_cbs = test_cbs
126
127 def pool_cb(self):
128 for cb in self.test_cbs:
129 if cb:
130 cb(self.subscriber)
131
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800132class subscriber_exchange(unittest.TestCase):
133
134 apps = [ 'org.onosproject.aaa', 'org.onosproject.dhcp' ]
135
136 dhcp_server_config = {
137 "ip": "10.1.11.50",
138 "mac": "ca:fe:ca:fe:ca:fe",
139 "subnet": "255.255.252.0",
140 "broadcast": "10.1.11.255",
141 "router": "10.1.8.1",
142 "domain": "8.8.8.8",
143 "ttl": "63",
144 "delay": "2",
145 "startip": "10.1.11.51",
146 "endip": "10.1.11.100"
147 }
148
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700149 aaa_loaded = False
150
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800151 def setUp(self):
152 ''' Activate the dhcp and igmp apps'''
153 for app in self.apps:
154 onos_ctrl = OnosCtrl(app)
155 status, _ = onos_ctrl.activate()
156 assert_equal(status, True)
157 time.sleep(2)
158
159 def teardown(self):
160 '''Deactivate the dhcp app'''
161 for app in self.apps:
162 onos_ctrl = OnosCtrl(app)
163 onos_ctrl.deactivate()
164
165 def onos_aaa_load(self):
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700166 if self.aaa_loaded:
167 return
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800168 aaa_dict = {'apps' : { 'org.onosproject.aaa' : { 'AAA' : { 'radiusSecret': 'radius_password',
169 'radiusIp': '172.17.0.2' } } } }
170 radius_ip = os.getenv('ONOS_AAA_IP') or '172.17.0.2'
171 aaa_dict['apps']['org.onosproject.aaa']['AAA']['radiusIp'] = radius_ip
172 self.onos_load_config('org.onosproject.aaa', aaa_dict)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700173 self.aaa_loaded = True
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800174
175 def onos_dhcp_table_load(self, config = None):
176 dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } }
177 dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp']
178 if config:
179 for k in config.keys():
180 if dhcp_config.has_key(k):
181 dhcp_config[k] = config[k]
182 self.onos_load_config('org.onosproject.dhcp', dhcp_dict)
183
184 def onos_load_config(self, app, config):
185 onos_ctrl = OnosCtrl(app)
186 status, code = onos_ctrl.config(config)
187 if status is False:
188 log.info('JSON config request for app %s returned status %d' %(app, code))
189 assert_equal(status, True)
190 time.sleep(2)
191
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700192 def dhcp_sndrcv(self, dhcp, update_seed = False):
193 cip, sip = dhcp.discover(update_seed = update_seed)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800194 assert_not_equal(cip, None)
195 assert_not_equal(sip, None)
196 log.info('Got dhcp client IP %s from server %s for mac %s' %
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700197 (cip, sip, dhcp.get_mac(cip)[0]))
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800198 return cip,sip
199
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700200 def dhcp_request(self, subscriber, seed_ip = '10.10.10.1', update_seed = False):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800201 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
202 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
203 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
204 self.onos_dhcp_table_load(config)
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700205 dhcp = DHCPTest(seed_ip = seed_ip, iface = subscriber.iface)
206 cip, sip = self.dhcp_sndrcv(dhcp, update_seed = update_seed)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800207 return cip, sip
208
209 def recv_channel_cb(self, pkt):
210 ##First verify that we have received the packet for the joined instance
211 chan = self.subscriber.caddr(pkt[IP].dst)
212 assert_equal(chan in self.subscriber.join_map.keys(), True)
213 recv_time = monotonic.monotonic() * 1000000
214 join_time = self.subscriber.join_map[chan][self.subscriber.STATS_JOIN].start
215 delta = recv_time - join_time
216 self.subscriber.join_rx_stats.update(packets=1, t = delta, usecs = True)
217 self.subscriber.channel_update(chan, self.subscriber.STATS_RX, 1, t = delta)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700218 log.debug('Packet received in %.3f usecs for group %s after join' %(delta, pkt[IP].dst))
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800219 self.test_status = True
220
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700221 def tls_verify(self, subscriber):
222 if subscriber.has_service('TLS'):
223 time.sleep(2)
224 tls = TLSAuthTest()
225 log.info('Running subscriber %s tls auth test' %subscriber.name)
226 tls.runTest()
227 self.test_status = True
228
229 def dhcp_verify(self, subscriber):
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700230 cip, sip = self.dhcp_request(subscriber, update_seed = True)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700231 log.info('Subscriber %s got client ip %s from server %s' %(subscriber.name, cip, sip))
232 subscriber.src_list = [cip]
233 self.test_status = True
234
235 def dhcp_jump_verify(self, subscriber):
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700236 cip, sip = self.dhcp_request(subscriber, seed_ip = '10.10.200.1')
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700237 log.info('Subscriber %s got client ip %s from server %s' %(subscriber.name, cip, sip))
238 subscriber.src_list = [cip]
239 self.test_status = True
240
241 def dhcp_next_verify(self, subscriber):
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700242 cip, sip = self.dhcp_request(subscriber, seed_ip = '10.10.150.1')
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700243 log.info('Subscriber %s got client ip %s from server %s' %(subscriber.name, cip, sip))
244 subscriber.src_list = [cip]
245 self.test_status = True
246
247 def igmp_verify(self, subscriber):
248 chan = 0
249 if subscriber.has_service('IGMP'):
250 for i in range(5):
251 log.info('Joining channel %d for subscriber %s' %(chan, subscriber.name))
252 subscriber.channel_join(chan, delay = 0)
253 subscriber.channel_receive(chan, cb = subscriber.recv_channel_cb, count = 1)
254 log.info('Leaving channel %d for subscriber %s' %(chan, subscriber.name))
255 subscriber.channel_leave(chan)
256 time.sleep(3)
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700257 log.info('Interface %s Join RX stats for subscriber %s, %s' %(subscriber.iface, subscriber.name,subscriber.join_rx_stats))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700258 self.test_status = True
259
260 def igmp_jump_verify(self, subscriber):
261 if subscriber.has_service('IGMP'):
262 for i in xrange(subscriber.num):
263 log.info('Subscriber %s jumping channel' %subscriber.name)
264 chan = subscriber.channel_jump(delay=0)
265 subscriber.channel_receive(chan, cb = subscriber.recv_channel_cb, count = 1)
266 log.info('Verified receive for channel %d, subscriber %s' %(chan, subscriber.name))
267 time.sleep(3)
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700268 log.info('Interface %s Jump RX stats for subscriber %s, %s' %(subscriber.iface, subscriber.name, subscriber.join_rx_stats))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700269 self.test_status = True
270
271 def igmp_next_verify(self, subscriber):
272 if subscriber.has_service('IGMP'):
273 for i in xrange(subscriber.num):
274 if i:
275 chan = subscriber.channel_join_next(delay=0)
276 else:
277 chan = subscriber.channel_join(i, delay=0)
278 log.info('Joined next channel %d for subscriber %s' %(chan, subscriber.name))
279 subscriber.channel_receive(chan, cb = subscriber.recv_channel_cb, count=1)
280 log.info('Verified receive for channel %d, subscriber %s' %(chan, subscriber.name))
281 time.sleep(3)
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700282 log.info('Interface %s Join Next RX stats for subscriber %s, %s' %(subscriber.iface, subscriber.name, subscriber.join_rx_stats))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700283 self.test_status = True
284
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700285 def generate_port_list(self, num):
286 port_list = []
287 for i in xrange(num):
288 rx_port = 2*i+1
289 tx_port = 2*i+2
290 port_list.append((tx_port, rx_port))
291 return port_list
292
293 def subscriber_load(self, create = True, num = 10, num_channels = 1, channel_start = 0, port_list = []):
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700294 '''Load the subscriber from the database'''
295 self.subscriber_db = SubscriberDB(create = create)
296 if create is True:
297 self.subscriber_db.generate(num)
298 self.subscriber_info = self.subscriber_db.read(num)
299 self.subscriber_list = []
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700300 if not port_list:
301 port_list = self.generate_port_list(num)
302
303 index = 0
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700304 for info in self.subscriber_info:
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700305 self.subscriber_list.append(Subscriber(name=info['Name'],
306 service=info['Service'],
307 num=num_channels,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700308 channel_start = channel_start,
309 tx_port = port_list[index][0],
310 rx_port = port_list[index][1]))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700311 channel_start += num_channels
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700312 index += 1
313
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700314 #load the ssm list for all subscriber channels
315 igmpChannel = IgmpChannel()
316 ssm_groups = map(lambda sub: sub.channels, self.subscriber_list)
317 ssm_list = reduce(lambda ssm1, ssm2: ssm1+ssm2, ssm_groups)
318 igmpChannel.igmp_load_ssm_config(ssm_list)
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700319 #load the subscriber to mcast port map for cord
320 cord_port_map = {}
321 for sub in self.subscriber_list:
322 for chan in sub.channels:
323 cord_port_map[chan] = (sub.tx_port, sub.rx_port)
324
325 igmpChannel.cord_port_table_load(cord_port_map)
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700326
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700327 def subscriber_join_verify( self, num_subscribers = 10, num_channels = 1,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700328 channel_start = 0, cbs = None, port_list = []):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800329 self.test_status = False
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700330 self.num_subscribers = num_subscribers
331 self.subscriber_load(create = True, num = self.num_subscribers,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700332 num_channels = num_channels, channel_start = channel_start, port_list = port_list)
Chetan Gaonker55fc7882016-03-15 17:46:47 -0700333 self.onos_aaa_load()
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700334 self.thread_pool = ThreadPool(min(100, self.num_subscribers), queue_size=1, wait_timeout=1)
335 if cbs is None:
336 cbs = (self.tls_verify, self.dhcp_verify, self.igmp_verify)
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700337 for subscriber in self.subscriber_list:
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700338 subscriber.start()
339 pool_object = subscriber_pool(subscriber, cbs)
340 self.thread_pool.addTask(pool_object.pool_cb)
341 self.thread_pool.cleanUpThreads()
342 for subscriber in self.subscriber_list:
343 subscriber.stop()
344 return self.test_status
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800345
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700346 def test_subscriber_join_recv(self):
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700347 """Test subscriber join and receive"""
348 num_subscribers = 50
349 test_status = self.subscriber_join_verify(num_subscribers = num_subscribers,
350 num_channels = 1, port_list = self.generate_port_list(num_subscribers))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700351 assert_equal(test_status, True)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800352
353 def test_subscriber_join_jump(self):
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700354 """Test subscriber join and receive for channel surfing"""
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700355 num_subscribers = 5
356 test_status = self.subscriber_join_verify(num_subscribers = num_subscribers,
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700357 num_channels = 50,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700358 cbs = (self.tls_verify, self.dhcp_jump_verify, self.igmp_jump_verify),
359 port_list = self.generate_port_list(num_subscribers))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700360 assert_equal(test_status, True)
Chetan Gaonker4b959fc2016-03-09 19:20:16 -0800361
362 def test_subscriber_join_next(self):
363 """Test subscriber join next for channels"""
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700364 num_subscribers = 5
365 test_status = self.subscriber_join_verify(num_subscribers = num_subscribers,
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700366 num_channels = 50,
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700367 cbs = (self.tls_verify, self.dhcp_next_verify, self.igmp_next_verify),
368 port_list = self.generate_port_list(num_subscribers))
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700369 assert_equal(test_status, True)