blob: f28a9927d8bb978e7b3eb5ffe25c59714f9e79fa [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
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 Gaonker7ab338c2016-04-15 17:23:17 -070016import unittest
17from nose.tools import *
18from nose.twistedtools import reactor, deferred
19from twisted.internet import defer
20from scapy.all import *
21import time
22import json
23import threading
Chetan Gaonker7ab338c2016-04-15 17:23:17 -070024from OnosCtrl import OnosCtrl
Chetan Gaonker3533faa2016-04-25 17:50:14 -070025from OnosFlowCtrl import OnosFlowCtrl, get_mac
Chetan Gaonker7ab338c2016-04-15 17:23:17 -070026from OltConfig import OltConfig
27log.setLevel('INFO')
28
Chetan Gaonker7ab338c2016-04-15 17:23:17 -070029class 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 Gaonkera9b6fcb2016-04-15 17:35:24 -070050 cls.device_id = 'of:' + get_mac() ##match against our device id
Chetan Gaonker7ab338c2016-04-15 17:23:17 -070051
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 Gaonker3533faa2016-04-25 17:50:14 -0700106 lfilter = lambda p: IP in p and p[IP].dst == egress_map['ip'] and p[IP].src == ingress_map['ip'],
Chetan Gaonker7ab338c2016-04-15 17:23:17 -0700107 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)