blob: f57ec9383bbc49bee18cc916a6554a6c22bdb07e [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
15from Channels import Channels
Chetan Gaonker41d2e072016-03-15 16:41:31 -070016from subscriberDb import SubscriberDB
Chetan Gaonkercbe79642016-03-09 17:45:58 -080017log.setLevel('INFO')
18
19class Subscriber(Channels):
20
21 STATS_RX = 0
22 STATS_TX = 1
23 STATS_JOIN = 2
24 STATS_LEAVE = 3
Chetan Gaonker41d2e072016-03-15 16:41:31 -070025 SUBSCRIBER_SERVICES = 'DHCP IGMP TLS'
26 def __init__(self, name = 'sub', service = SUBSCRIBER_SERVICES, num = 1, iface = 'veth0', iface_mcast = 'veth2',
Chetan Gaonkercbe79642016-03-09 17:45:58 -080027 mcast_cb = None, loginType = 'wireless'):
28 Channels.__init__(self, num, iface = iface, iface_mcast = iface_mcast, mcast_cb = mcast_cb)
Chetan Gaonker41d2e072016-03-15 16:41:31 -070029 self.name = name
30 self.service = service
31 self.service_map = {}
32 services = self.service.strip().split(' ')
33 for s in services:
34 self.service_map[s] = True
Chetan Gaonkercbe79642016-03-09 17:45:58 -080035 self.loginType = loginType
36 ##start streaming channels
37 self.join_map = {}
38 ##accumulated join recv stats
39 self.join_rx_stats = Stats()
40
Chetan Gaonker41d2e072016-03-15 16:41:31 -070041 def has_service(self, service):
42 if self.service_map.has_key(service):
43 return self.service_map[service]
44 if self.service_map.has_key(service.upper()):
45 return self.service_map[service.upper()]
46 return False
47
Chetan Gaonkercbe79642016-03-09 17:45:58 -080048 def channel_join_update(self, chan, join_time):
49 self.join_map[chan] = ( Stats(), Stats(), Stats(), Stats() )
50 self.channel_update(chan, self.STATS_JOIN, 1, t = join_time)
51
52 def channel_join(self, chan = 0, delay = 2):
53 '''Join a channel and create a send/recv stats map'''
54 if self.join_map.has_key(chan):
55 del self.join_map[chan]
56 self.delay = delay
57 chan, join_time = self.join(chan)
58 self.channel_join_update(chan, join_time)
59 return chan
60
61 def channel_join_next(self, delay = 2):
62 '''Joins the next channel leaving the last channel'''
63 if self.last_chan:
64 if self.join_map.has_key(self.last_chan):
65 del self.join_map[self.last_chan]
66 self.delay = delay
67 chan, join_time = self.join_next()
68 self.channel_join_update(chan, join_time)
69 return chan
70
71 def channel_jump(self, delay = 2):
72 '''Jumps randomly to the next channel leaving the last channel'''
73 if self.last_chan is not None:
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.jump()
78 self.channel_join_update(chan, join_time)
79 return chan
80
81 def channel_leave(self, chan = 0):
82 if self.join_map.has_key(chan):
83 del self.join_map[chan]
84 self.leave(chan)
85
86 def channel_update(self, chan, stats_type, packets, t=0):
87 if type(chan) == type(0):
88 chan_list = (chan,)
89 else:
90 chan_list = chan
91 for c in chan_list:
92 if self.join_map.has_key(c):
93 self.join_map[c][stats_type].update(packets = packets, t = t)
94
95 def channel_receive(self, chan, cb = None, count = 1):
96 self.recv(chan, cb = cb, count = count)
97
98class subscriber_exchange(unittest.TestCase):
99
100 apps = [ 'org.onosproject.aaa', 'org.onosproject.dhcp' ]
101
102 dhcp_server_config = {
103 "ip": "10.1.11.50",
104 "mac": "ca:fe:ca:fe:ca:fe",
105 "subnet": "255.255.252.0",
106 "broadcast": "10.1.11.255",
107 "router": "10.1.8.1",
108 "domain": "8.8.8.8",
109 "ttl": "63",
110 "delay": "2",
111 "startip": "10.1.11.51",
112 "endip": "10.1.11.100"
113 }
114
115 def setUp(self):
116 ''' Activate the dhcp and igmp apps'''
117 for app in self.apps:
118 onos_ctrl = OnosCtrl(app)
119 status, _ = onos_ctrl.activate()
120 assert_equal(status, True)
121 time.sleep(2)
122
123 def teardown(self):
124 '''Deactivate the dhcp app'''
125 for app in self.apps:
126 onos_ctrl = OnosCtrl(app)
127 onos_ctrl.deactivate()
128
129 def onos_aaa_load(self):
130 aaa_dict = {'apps' : { 'org.onosproject.aaa' : { 'AAA' : { 'radiusSecret': 'radius_password',
131 'radiusIp': '172.17.0.2' } } } }
132 radius_ip = os.getenv('ONOS_AAA_IP') or '172.17.0.2'
133 aaa_dict['apps']['org.onosproject.aaa']['AAA']['radiusIp'] = radius_ip
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700134 onos_ctrl = OnosCtrl('org.onosproject.aaa')
135 onos_ctrl.deactivate()
136 time.sleep(2)
137 onos_ctrl.activate()
138 time.sleep(2)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800139 self.onos_load_config('org.onosproject.aaa', aaa_dict)
140
141 def onos_dhcp_table_load(self, config = None):
142 dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } }
143 dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp']
144 if config:
145 for k in config.keys():
146 if dhcp_config.has_key(k):
147 dhcp_config[k] = config[k]
148 self.onos_load_config('org.onosproject.dhcp', dhcp_dict)
149
150 def onos_load_config(self, app, config):
151 onos_ctrl = OnosCtrl(app)
152 status, code = onos_ctrl.config(config)
153 if status is False:
154 log.info('JSON config request for app %s returned status %d' %(app, code))
155 assert_equal(status, True)
156 time.sleep(2)
157
158 def dhcp_sndrcv(self, update_seed = False):
159 cip, sip = self.dhcp.discover(update_seed = update_seed)
160 assert_not_equal(cip, None)
161 assert_not_equal(sip, None)
162 log.info('Got dhcp client IP %s from server %s for mac %s' %
163 (cip, sip, self.dhcp.get_mac(cip)[0]))
164 return cip,sip
165
166 def dhcp_request(self, seed_ip = '10.10.10.1', iface = 'veth0'):
167 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
168 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
169 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
170 self.onos_dhcp_table_load(config)
171 self.dhcp = DHCPTest(seed_ip = seed_ip, iface = iface)
172 cip, sip = self.dhcp_sndrcv()
173 return cip, sip
174
175 def recv_channel_cb(self, pkt):
176 ##First verify that we have received the packet for the joined instance
177 chan = self.subscriber.caddr(pkt[IP].dst)
178 assert_equal(chan in self.subscriber.join_map.keys(), True)
179 recv_time = monotonic.monotonic() * 1000000
180 join_time = self.subscriber.join_map[chan][self.subscriber.STATS_JOIN].start
181 delta = recv_time - join_time
182 self.subscriber.join_rx_stats.update(packets=1, t = delta, usecs = True)
183 self.subscriber.channel_update(chan, self.subscriber.STATS_RX, 1, t = delta)
184 log.info('Packet received in %.3f usecs for group %s after join' %(delta, pkt[IP].dst))
185 self.test_status = True
186
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700187 def subscriber_load(self, create = True, num = 10):
188 '''Load the subscriber from the database'''
189 self.subscriber_db = SubscriberDB(create = create)
190 if create is True:
191 self.subscriber_db.generate(num)
192 self.subscriber_info = self.subscriber_db.read(num)
193 self.subscriber_list = []
194 for info in self.subscriber_info:
195 self.subscriber_list.append(Subscriber(info['Name'], info['Service']))
196
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800197 def test_subscriber_join_recv( self, chan = 0):
198 """Test 1 subscriber join and receive"""
199 self.test_status = False
Chetan Gaonker382547a2016-03-15 16:43:29 -0700200 self.num_subscribers = 10
Chetan Gaonker41d2e072016-03-15 16:41:31 -0700201 self.subscriber_load(create = True, num = self.num_subscribers)
202 for subscriber in self.subscriber_list:
203 self.subscriber = subscriber
204 self.subscriber.start()
205 log.info('Testing subscriber %s for %s' %(subscriber.name, subscriber.service))
206 if self.subscriber.has_service('TLS'):
207 self.onos_aaa_load()
208 time.sleep(2)
209 tls = TLSAuthTest()
210 tls.runTest()
211 self.test_status = True
212 if self.subscriber.has_service('DHCP'):
213 cip, sip = self.dhcp_request(iface = self.subscriber.iface)
214 log.info('Got client ip %s from server %s' %(cip, sip))
215 self.subscriber.src_list = [cip]
216 self.test_status = True
217 if self.subscriber.has_service('IGMP'):
218 for i in range(5):
219 log.info('Joining channel %d' %chan)
220 self.subscriber.channel_join(chan, delay = 0)
221 self.subscriber.channel_receive(chan, cb = self.recv_channel_cb, count = 1)
222 log.info('Leaving channel %d' %chan)
223 self.subscriber.channel_leave(chan)
224 time.sleep(3)
225 log.info('Join RX stats %s' %self.subscriber.join_rx_stats)
226 self.subscriber.stop()
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800227 ##Terminate the tests on success
228 assert_equal(self.test_status, True)
229
230
231 def test_subscriber_join_jump(self):
232 """Test 1 subscriber join and receive"""
233 self.test_status = False
234 self.subscriber = Subscriber(50)
235 self.subscriber.start()
236 self.onos_aaa_load()
237 #tls = TLSAuthTest()
238 #tls.runTest()
239 ##Next get dhcp
240 cip, sip = self.dhcp_request(seed_ip = '10.10.200.1', iface = self.subscriber.iface)
241 log.info('Got client ip %s from server %s' %(cip, sip))
242 self.subscriber.src_list = [cip]
243 for i in range(50):
244 log.info('Jumping channel')
245 chan = self.subscriber.channel_jump(delay=0)
246 self.subscriber.channel_receive(chan, cb = self.recv_channel_cb, count = 1)
247 log.info('Verified receive for channel %d' %chan)
248 time.sleep(3)
249
250 log.info('Join RX stats %s' %self.subscriber.join_rx_stats)
251 self.subscriber.stop()
252 ##Terminate the tests on success
253 assert_equal(self.test_status, True)
Chetan Gaonker4b959fc2016-03-09 19:20:16 -0800254
255 def test_subscriber_join_next(self):
256 """Test subscriber join next for channels"""
257 self.test_status = False
258 self.subscriber = Subscriber(10)
259 self.subscriber.start()
260 self.onos_aaa_load()
261 #tls = TLSAuthTest()
262 #tls.runTest()
263 ##Next get dhcp
264 cip, sip = self.dhcp_request(seed_ip = '10.10.150.1', iface = self.subscriber.iface)
265 log.info('Got client ip %s from server %s' %(cip, sip))
266 self.subscriber.src_list = [cip]
267 for i in range(10):
268 if i:
269 chan = self.subscriber.channel_join_next(delay=0)
270 else:
271 chan = self.subscriber.channel_join(i, delay=0)
272 log.info('Joined next channel %d' %chan)
273 self.subscriber.channel_receive(chan, cb = self.recv_channel_cb, count=1)
274 log.info('Verified receive for channel %d' %chan)
275 time.sleep(3)
276
277 log.info('Join Next RX stats %s' %self.subscriber.join_rx_stats)
278 self.subscriber.stop()
279 ##Terminate the tests on success
280 assert_equal(self.test_status, True)