blob: dc3ce91acc7c086961979cb2449c38f2d7f3bcd9 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
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
16
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070017#
18# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
23#
24# http://www.apache.org/licenses/LICENSE-2.0
25#
26# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
32import json
33import requests
34import os,sys,time
A.R Karthick2e99c472017-03-22 19:13:51 -070035import logging
36logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070037from scapy.all import *
A R Karthick76a497a2017-04-12 10:59:39 -070038from CordTestUtils import get_mac, get_controller, log_test
A.R Karthickbe7768c2017-03-17 11:39:41 -070039from OnosCtrl import OnosCtrl
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070040from OnosFlowCtrl import OnosFlowCtrl
A R Karthick76a497a2017-04-12 10:59:39 -070041log_test.setLevel('INFO')
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070042
43conf.verb = 0 # Disable Scapy verbosity
44conf.checkIPaddr = 0 # Don't check response packets for matching destination IPs
45
46class ACLTest:
47
48 auth = ('karaf', 'karaf')
A R Karthick456e9cf2016-10-03 14:37:44 -070049 controller = get_controller()
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070050 add_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules' %(controller)
51 remove_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules/%s' %(controller, id)
52 clear_all_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules' %(controller)
53 iface_create_onos_url = 'http://%s:8181/onos/v1/network/configuration' %(controller)
A R Karthicka337f4d2016-10-06 13:53:15 -070054 device_id = 'of:' + get_mac()
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070055 MAX_PORTS = 100
56
57 def __init__(self, ipv4Prefix ='v4', srcIp ='null', dstIp ='null', ipProto = 'null', dstTpPort = 0, action = 'null', ingress_iface = 1, egress_iface = 2,iface_num = 0, iface_name = 'null', iface_count = 0, iface_ip = 'null'):
58 self.ipv4Prefix = ipv4Prefix
59 self.srcIp = srcIp
60 self.ingress_iface = ingress_iface
61 self.egress_iface = egress_iface
62 self.dstIp = dstIp
63 self.ipProto = ipProto
64 self.dstTpPort = dstTpPort
65 self.action = action
66 self.iface_count = iface_count
67 self.iface_num = iface_num
68 self.iface_name = iface_name
69 self.iface_ip = iface_ip
A R Karthicka337f4d2016-10-06 13:53:15 -070070 self.device_id = OnosCtrl.get_device_id()
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070071
ChetanGaonker689b3862016-10-17 16:25:01 -070072 def adding_acl_rule(self, ipv4Prefix, srcIp, dstIp, ipProto ='null', dstTpPort='null', action= 'include',controller=None):
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070073 '''This function is generating ACL json file and post to ONOS for creating a ACL rule'''
74 if ipv4Prefix is 'v4':
A R Karthick2b93d6a2016-09-06 15:19:09 -070075 acl_dict = {}
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070076 if srcIp and dstIp and action:
77 acl_dict['srcIp'] = '{}'.format(srcIp)
78 acl_dict['dstIp'] = '{}'.format(dstIp)
79 acl_dict['action'] = '{}'.format(action)
80 if ipProto is not 'null':
81 acl_dict['ipProto'] = '{}'.format(ipProto)
82 if dstTpPort is not 'null':
83 acl_dict['dstTpPort'] = '{}'.format(dstTpPort)
84 json_data = json.dumps(acl_dict)
ChetanGaonker689b3862016-10-17 16:25:01 -070085 if controller is None:
86 # if controller ip is not passed, it will default controller ip
87 resp = requests.post(self.add_acl_rule_url, auth = self.auth, data = json_data)
88 else:
89 add_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules' %(controller)
A R Karthick76a497a2017-04-12 10:59:39 -070090 log_test.info('add_acl_rule_acl url is %s'%add_acl_rule_url)
ChetanGaonker689b3862016-10-17 16:25:01 -070091 resp = requests.post(add_acl_rule_url, auth = self.auth, data = json_data)
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070092 return resp.ok, resp.status_code
93
ChetanGaonker689b3862016-10-17 16:25:01 -070094 def get_acl_rules(self,controller=None):
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070095 '''This function is getting a ACL rules from ONOS with json formate'''
ChetanGaonker689b3862016-10-17 16:25:01 -070096 if controller is None:
97 resp = requests.get(self.add_acl_rule_url, auth = self.auth)
98 else:
99 add_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules' %(controller)
A R Karthick76a497a2017-04-12 10:59:39 -0700100 log_test.info('get_acl_rule_url is %s'%add_acl_rule_url)
ChetanGaonker689b3862016-10-17 16:25:01 -0700101 resp = requests.get(add_acl_rule_url, auth = self.auth)
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -0700102 return resp
103
104 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700105 def remove_acl_rule(cls,id = None,controller=None):
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -0700106 '''This function is delete one or all ACL rules in ONOS'''
107 if id is None:
ChetanGaonker689b3862016-10-17 16:25:01 -0700108 if controller is None:
109 remove_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules' %(cls.controller)
110 else:
111 remove_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules' %(controller)
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -0700112 else:
ChetanGaonker689b3862016-10-17 16:25:01 -0700113 if controller is None:
114 remove_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules/%s' %(cls.controller, id)
115 else:
116 remove_acl_rule_url = 'http://%s:8181/onos/v1/acl/rules/%s' %(controller, id)
A R Karthick76a497a2017-04-12 10:59:39 -0700117 log_test.info('remove_acl_rule_url is %s'%remove_acl_rule_url)
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -0700118 resp = requests.delete(remove_acl_rule_url, auth = cls.auth)
119 return resp.ok, resp.status_code
A R Karthick2b93d6a2016-09-06 15:19:09 -0700120
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -0700121 def generate_onos_interface_config(self,iface_num = 4, iface_name = 'null',iface_count = 1,iface_ip = '198.162.10.1'):
122 '''This function is generate interface config data in json format and post to ONOS for creating it '''
123 ''' To add interfaces on ONOS to test acl with trffic'''
124 num = 0
125 egress_host_list = []
126 interface_list = []
127 ip = iface_ip.split('/')[0]
128 start_iface_ip = ip.split('.')
129 start_ip = ( int(start_iface_ip[0]) << 24) | ( int(start_iface_ip[1]) << 16) | ( int(start_iface_ip[2]) << 8) | 0
130 end_ip = ( 200 << 24 ) | (168 << 16) | (10 << 8) | 0
131 ports_dict = { 'ports' : {} }
132 for n in xrange(start_ip, end_ip, 256):
133 port_map = ports_dict['ports']
134 port = iface_num if num < self.MAX_PORTS - 1 else self.MAX_PORTS - 1
135 device_port_key = '{0}/{1}'.format(self.device_id, port)
136 try:
137 interfaces = port_map[device_port_key]['interfaces']
138 except:
139 port_map[device_port_key] = { 'interfaces' : [] }
140 interfaces = port_map[device_port_key]['interfaces']
141 ip = n + 2
142 peer_ip = n + 1
143 ips = '%d.%d.%d.%d/%d'%( (ip >> 24) & 0xff, ( (ip >> 16) & 0xff ), ( (ip >> 8 ) & 0xff ), ip & 0xff, int(iface_ip.split('/')[1]))
144 peer = '%d.%d.%d.%d' % ( (peer_ip >> 24) & 0xff, ( ( peer_ip >> 16) & 0xff ), ( (peer_ip >> 8 ) & 0xff ), peer_ip & 0xff )
145 mac = RandMAC()._fix()
146 egress_host_list.append((peer, mac))
147 if num < self.MAX_PORTS - 1:
148 interface_dict = { 'name' : '{0}-{1}'.format(iface_name,port), 'ips': [ips], 'mac' : mac }
149 interfaces.append(interface_dict)
150 interface_list.append(interface_dict['name'])
151 else:
152 interfaces[0]['ips'].append(ips)
153 num += 1
154 if num == iface_count:
155 break
156 json_data = json.dumps(ports_dict)
157 resp = requests.post(self.iface_create_onos_url, auth = self.auth, data = json_data)
158 return resp.ok, resp.status_code, egress_host_list