Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present Ciena Corporation |
| 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 | # |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 16 | import unittest |
| 17 | from nose.tools import * |
| 18 | from nose.twistedtools import reactor, deferred |
| 19 | from twisted.internet import defer |
| 20 | from scapy.all import * |
| 21 | import time |
| 22 | import json |
| 23 | import threading |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 24 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 25 | from OnosFlowCtrl import OnosFlowCtrl, get_mac |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 26 | from OltConfig import OltConfig |
| 27 | log.setLevel('INFO') |
| 28 | |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 29 | class flows_exchange(unittest.TestCase): |
| 30 | |
| 31 | #Use the first available device id as our device id to program flows |
| 32 | app = 'org.onosproject.cli' |
| 33 | PORT_TX_DEFAULT = 2 |
| 34 | PORT_RX_DEFAULT = 1 |
| 35 | INTF_TX_DEFAULT = 'veth2' |
| 36 | INTF_RX_DEFAULT = 'veth0' |
| 37 | default_port_map = { |
| 38 | PORT_TX_DEFAULT : INTF_TX_DEFAULT, |
| 39 | PORT_RX_DEFAULT : INTF_RX_DEFAULT, |
| 40 | INTF_TX_DEFAULT : PORT_TX_DEFAULT, |
| 41 | INTF_RX_DEFAULT : PORT_RX_DEFAULT |
| 42 | } |
| 43 | |
| 44 | @classmethod |
| 45 | def setUpClass(cls): |
| 46 | cls.olt = OltConfig() |
| 47 | cls.port_map = cls.olt.olt_port_map() |
| 48 | if not cls.port_map: |
| 49 | cls.port_map = cls.default_port_map |
Chetan Gaonker | a9b6fcb | 2016-04-15 17:35:24 -0700 | [diff] [blame] | 50 | cls.device_id = 'of:' + get_mac() ##match against our device id |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 51 | |
| 52 | def test_flow_mac(self): |
| 53 | '''Add and verify flows with MAC selectors''' |
| 54 | egress = 1 |
| 55 | ingress = 2 |
| 56 | egress_mac = '00:00:00:00:00:01' |
| 57 | ingress_mac = '00:00:00:00:00:02' |
| 58 | flow = OnosFlowCtrl(deviceId = self.device_id, |
| 59 | egressPort = egress, |
| 60 | ingressPort = ingress, |
| 61 | ethSrc = ingress_mac, |
| 62 | ethDst = egress_mac) |
| 63 | result = flow.addFlow() |
| 64 | assert_equal(result, True) |
| 65 | ##wait for flows to be added to ONOS |
| 66 | time.sleep(3) |
| 67 | self.success = False |
| 68 | def mac_recv_task(): |
| 69 | def recv_cb(pkt): |
| 70 | log.info('Pkt seen with ingress mac %s, egress mac %s' %(pkt.src, pkt.dst)) |
| 71 | self.success = True |
| 72 | sniff(count=2, timeout=5, lfilter = lambda p: p.src == ingress_mac, |
| 73 | prn = recv_cb, iface = self.port_map[egress]) |
| 74 | |
| 75 | t = threading.Thread(target = mac_recv_task) |
| 76 | t.start() |
| 77 | pkt = Ether(src = ingress_mac, dst = egress_mac)/IP() |
| 78 | log.info('Sending a packet to verify if flows are correct') |
| 79 | sendp(pkt, count=50, iface = self.port_map[ingress]) |
| 80 | t.join() |
| 81 | assert_equal(self.success, True) |
| 82 | |
| 83 | def test_flow_ip(self): |
| 84 | '''Add and verify flows with IPv4 selectors''' |
| 85 | egress = 1 |
| 86 | ingress = 2 |
| 87 | egress_map = { 'ether': '00:00:00:00:00:03', 'ip': '192.168.30.1' } |
| 88 | ingress_map = { 'ether': '00:00:00:00:00:04', 'ip': '192.168.40.1' } |
| 89 | flow = OnosFlowCtrl(deviceId = self.device_id, |
| 90 | egressPort = egress, |
| 91 | ingressPort = ingress, |
| 92 | ethType = '0x0800', |
| 93 | ipSrc = ('IPV4_SRC', ingress_map['ip']+'/32'), |
| 94 | ipDst = ('IPV4_DST', egress_map['ip']+'/32') |
| 95 | ) |
| 96 | result = flow.addFlow() |
| 97 | assert_equal(result, True) |
| 98 | ##wait for flows to be added to ONOS |
| 99 | time.sleep(3) |
| 100 | self.success = False |
| 101 | def mac_recv_task(): |
| 102 | def recv_cb(pkt): |
| 103 | log.info('Pkt seen with ingress ip %s, egress ip %s' %(pkt[IP].src, pkt[IP].dst)) |
| 104 | self.success = True |
| 105 | sniff(count=2, timeout=5, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 106 | lfilter = lambda p: IP in p and p[IP].dst == egress_map['ip'] and p[IP].src == ingress_map['ip'], |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 107 | prn = recv_cb, iface = self.port_map[egress]) |
| 108 | |
| 109 | t = threading.Thread(target = mac_recv_task) |
| 110 | t.start() |
| 111 | L2 = Ether(src = ingress_map['ether'], dst = egress_map['ether']) |
| 112 | L3 = IP(src = ingress_map['ip'], dst = egress_map['ip']) |
| 113 | pkt = L2/L3 |
| 114 | log.info('Sending a packet to verify if flows are correct') |
| 115 | sendp(pkt, count=50, iface = self.port_map[ingress]) |
| 116 | t.join() |
| 117 | assert_equal(self.success, True) |