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