Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 1 | import unittest |
| 2 | from nose.tools import * |
| 3 | from nose.twistedtools import reactor, deferred |
| 4 | from twisted.internet import defer |
| 5 | from scapy.all import * |
| 6 | import time, monotonic |
| 7 | import os, sys |
| 8 | import tempfile |
| 9 | import random |
| 10 | import threading |
| 11 | from Stats import Stats |
| 12 | from OnosCtrl import OnosCtrl |
| 13 | from DHCP import DHCPTest |
| 14 | from EapTLS import TLSAuthTest |
| 15 | from Channels import Channels |
| 16 | log.setLevel('INFO') |
| 17 | |
| 18 | class Subscriber(Channels): |
| 19 | |
| 20 | STATS_RX = 0 |
| 21 | STATS_TX = 1 |
| 22 | STATS_JOIN = 2 |
| 23 | STATS_LEAVE = 3 |
| 24 | |
| 25 | def __init__(self, num = 1, iface = 'veth0', userId = 'sub1', iface_mcast = 'veth2', |
| 26 | mcast_cb = None, loginType = 'wireless'): |
| 27 | Channels.__init__(self, num, iface = iface, iface_mcast = iface_mcast, mcast_cb = mcast_cb) |
| 28 | self.userId = userId |
| 29 | self.loginType = loginType |
| 30 | ##start streaming channels |
| 31 | self.join_map = {} |
| 32 | ##accumulated join recv stats |
| 33 | self.join_rx_stats = Stats() |
| 34 | |
| 35 | def channel_join_update(self, chan, join_time): |
| 36 | self.join_map[chan] = ( Stats(), Stats(), Stats(), Stats() ) |
| 37 | self.channel_update(chan, self.STATS_JOIN, 1, t = join_time) |
| 38 | |
| 39 | def channel_join(self, chan = 0, delay = 2): |
| 40 | '''Join a channel and create a send/recv stats map''' |
| 41 | if self.join_map.has_key(chan): |
| 42 | del self.join_map[chan] |
| 43 | self.delay = delay |
| 44 | chan, join_time = self.join(chan) |
| 45 | self.channel_join_update(chan, join_time) |
| 46 | return chan |
| 47 | |
| 48 | def channel_join_next(self, delay = 2): |
| 49 | '''Joins the next channel leaving the last channel''' |
| 50 | if self.last_chan: |
| 51 | if self.join_map.has_key(self.last_chan): |
| 52 | del self.join_map[self.last_chan] |
| 53 | self.delay = delay |
| 54 | chan, join_time = self.join_next() |
| 55 | self.channel_join_update(chan, join_time) |
| 56 | return chan |
| 57 | |
| 58 | def channel_jump(self, delay = 2): |
| 59 | '''Jumps randomly to the next channel leaving the last channel''' |
| 60 | if self.last_chan is not None: |
| 61 | if self.join_map.has_key(self.last_chan): |
| 62 | del self.join_map[self.last_chan] |
| 63 | self.delay = delay |
| 64 | chan, join_time = self.jump() |
| 65 | self.channel_join_update(chan, join_time) |
| 66 | return chan |
| 67 | |
| 68 | def channel_leave(self, chan = 0): |
| 69 | if self.join_map.has_key(chan): |
| 70 | del self.join_map[chan] |
| 71 | self.leave(chan) |
| 72 | |
| 73 | def channel_update(self, chan, stats_type, packets, t=0): |
| 74 | if type(chan) == type(0): |
| 75 | chan_list = (chan,) |
| 76 | else: |
| 77 | chan_list = chan |
| 78 | for c in chan_list: |
| 79 | if self.join_map.has_key(c): |
| 80 | self.join_map[c][stats_type].update(packets = packets, t = t) |
| 81 | |
| 82 | def channel_receive(self, chan, cb = None, count = 1): |
| 83 | self.recv(chan, cb = cb, count = count) |
| 84 | |
| 85 | class subscriber_exchange(unittest.TestCase): |
| 86 | |
| 87 | apps = [ 'org.onosproject.aaa', 'org.onosproject.dhcp' ] |
| 88 | |
| 89 | dhcp_server_config = { |
| 90 | "ip": "10.1.11.50", |
| 91 | "mac": "ca:fe:ca:fe:ca:fe", |
| 92 | "subnet": "255.255.252.0", |
| 93 | "broadcast": "10.1.11.255", |
| 94 | "router": "10.1.8.1", |
| 95 | "domain": "8.8.8.8", |
| 96 | "ttl": "63", |
| 97 | "delay": "2", |
| 98 | "startip": "10.1.11.51", |
| 99 | "endip": "10.1.11.100" |
| 100 | } |
| 101 | |
| 102 | def setUp(self): |
| 103 | ''' Activate the dhcp and igmp apps''' |
| 104 | for app in self.apps: |
| 105 | onos_ctrl = OnosCtrl(app) |
| 106 | status, _ = onos_ctrl.activate() |
| 107 | assert_equal(status, True) |
| 108 | time.sleep(2) |
| 109 | |
| 110 | def teardown(self): |
| 111 | '''Deactivate the dhcp app''' |
| 112 | for app in self.apps: |
| 113 | onos_ctrl = OnosCtrl(app) |
| 114 | onos_ctrl.deactivate() |
| 115 | |
| 116 | def onos_aaa_load(self): |
| 117 | aaa_dict = {'apps' : { 'org.onosproject.aaa' : { 'AAA' : { 'radiusSecret': 'radius_password', |
| 118 | 'radiusIp': '172.17.0.2' } } } } |
| 119 | radius_ip = os.getenv('ONOS_AAA_IP') or '172.17.0.2' |
| 120 | aaa_dict['apps']['org.onosproject.aaa']['AAA']['radiusIp'] = radius_ip |
| 121 | self.onos_load_config('org.onosproject.aaa', aaa_dict) |
| 122 | |
| 123 | def onos_dhcp_table_load(self, config = None): |
| 124 | dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } } |
| 125 | dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp'] |
| 126 | if config: |
| 127 | for k in config.keys(): |
| 128 | if dhcp_config.has_key(k): |
| 129 | dhcp_config[k] = config[k] |
| 130 | self.onos_load_config('org.onosproject.dhcp', dhcp_dict) |
| 131 | |
| 132 | def onos_load_config(self, app, config): |
| 133 | onos_ctrl = OnosCtrl(app) |
| 134 | status, code = onos_ctrl.config(config) |
| 135 | if status is False: |
| 136 | log.info('JSON config request for app %s returned status %d' %(app, code)) |
| 137 | assert_equal(status, True) |
| 138 | time.sleep(2) |
| 139 | |
| 140 | def dhcp_sndrcv(self, update_seed = False): |
| 141 | cip, sip = self.dhcp.discover(update_seed = update_seed) |
| 142 | assert_not_equal(cip, None) |
| 143 | assert_not_equal(sip, None) |
| 144 | log.info('Got dhcp client IP %s from server %s for mac %s' % |
| 145 | (cip, sip, self.dhcp.get_mac(cip)[0])) |
| 146 | return cip,sip |
| 147 | |
| 148 | def dhcp_request(self, seed_ip = '10.10.10.1', iface = 'veth0'): |
| 149 | config = {'startip':'10.10.10.20', 'endip':'10.10.10.69', |
| 150 | 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe", |
| 151 | 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'} |
| 152 | self.onos_dhcp_table_load(config) |
| 153 | self.dhcp = DHCPTest(seed_ip = seed_ip, iface = iface) |
| 154 | cip, sip = self.dhcp_sndrcv() |
| 155 | return cip, sip |
| 156 | |
| 157 | def recv_channel_cb(self, pkt): |
| 158 | ##First verify that we have received the packet for the joined instance |
| 159 | chan = self.subscriber.caddr(pkt[IP].dst) |
| 160 | assert_equal(chan in self.subscriber.join_map.keys(), True) |
| 161 | recv_time = monotonic.monotonic() * 1000000 |
| 162 | join_time = self.subscriber.join_map[chan][self.subscriber.STATS_JOIN].start |
| 163 | delta = recv_time - join_time |
| 164 | self.subscriber.join_rx_stats.update(packets=1, t = delta, usecs = True) |
| 165 | self.subscriber.channel_update(chan, self.subscriber.STATS_RX, 1, t = delta) |
| 166 | log.info('Packet received in %.3f usecs for group %s after join' %(delta, pkt[IP].dst)) |
| 167 | self.test_status = True |
| 168 | |
| 169 | def test_subscriber_join_recv( self, chan = 0): |
| 170 | """Test 1 subscriber join and receive""" |
| 171 | self.test_status = False |
| 172 | self.subscriber = Subscriber() |
| 173 | self.subscriber.start() |
| 174 | self.onos_aaa_load() |
| 175 | |
| 176 | #tls = TLSAuthTest() |
| 177 | #tls.runTest() |
| 178 | |
| 179 | ##Next get dhcp |
| 180 | cip, sip = self.dhcp_request(iface = self.subscriber.iface) |
| 181 | log.info('Got client ip %s from server %s' %(cip, sip)) |
| 182 | self.subscriber.src_list = [cip] |
| 183 | for i in range(5): |
| 184 | log.info('Joining channel %d' %chan) |
| 185 | self.subscriber.channel_join(chan, delay = 0) |
| 186 | self.subscriber.channel_receive(chan, cb = self.recv_channel_cb, count = 1) |
| 187 | log.info('Leaving channel %d' %chan) |
| 188 | self.subscriber.channel_leave(chan) |
| 189 | time.sleep(3) |
| 190 | |
| 191 | log.info('Join RX stats %s' %self.subscriber.join_rx_stats) |
| 192 | self.subscriber.stop() |
| 193 | ##Terminate the tests on success |
| 194 | assert_equal(self.test_status, True) |
| 195 | |
| 196 | |
| 197 | def test_subscriber_join_jump(self): |
| 198 | """Test 1 subscriber join and receive""" |
| 199 | self.test_status = False |
| 200 | self.subscriber = Subscriber(50) |
| 201 | self.subscriber.start() |
| 202 | self.onos_aaa_load() |
| 203 | #tls = TLSAuthTest() |
| 204 | #tls.runTest() |
| 205 | ##Next get dhcp |
| 206 | cip, sip = self.dhcp_request(seed_ip = '10.10.200.1', iface = self.subscriber.iface) |
| 207 | log.info('Got client ip %s from server %s' %(cip, sip)) |
| 208 | self.subscriber.src_list = [cip] |
| 209 | for i in range(50): |
| 210 | log.info('Jumping channel') |
| 211 | chan = self.subscriber.channel_jump(delay=0) |
| 212 | self.subscriber.channel_receive(chan, cb = self.recv_channel_cb, count = 1) |
| 213 | log.info('Verified receive for channel %d' %chan) |
| 214 | time.sleep(3) |
| 215 | |
| 216 | log.info('Join RX stats %s' %self.subscriber.join_rx_stats) |
| 217 | self.subscriber.stop() |
| 218 | ##Terminate the tests on success |
| 219 | assert_equal(self.test_status, True) |