-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdp.py
138 lines (115 loc) · 4.37 KB
/
dp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright (C) 2015 Brad Cowie, Christopher Lorier and Joe Stringer.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from vlan import VLAN
from port import Port
class DP:
dpid = None
vlans = None
ports = None
acls = None
running = False
config_all = None
config_default = None
config_acls = None
# Defaults (override in config file)
default_type = 'untagged'
def __init__(self, dpid, config, conf_all, conf_default, conf_acls):
self.dpid = dpid
self.vlans = {}
self.ports = {}
self.acls = conf_acls
self.config = config
self.config_all = conf_all if type(conf_all) is list else [{}]
self.config_default = conf_default if type(conf_default) is dict else {}
self.config_acls = conf_acls if type(conf_acls) is dict else {}
self.parse()
def parse(self):
# parse ports
for k, v in self.config.items():
self.add_port(k, v)
def add_port(self, k, v = {}):
# add port specific vlans or fall back to defaults
v = copy.copy(v) if v else {}
if 'exclude' in self.config_default:
excluded = self.is_excluded(self.config_default['exclude'], k)
else:
excluded = False
# set default vlans if we have any
if not excluded and 'vlans' in self.config_default:
v.setdefault('vlans', self.config_default['vlans'])
else:
v.setdefault('vlans', [])
# set default type
if not excluded and 'type' in self.config_default:
v.setdefault('type', self.config_default['type'])
else:
v.setdefault('type', self.default_type)
# set default acls
if not excluded and 'acls' in self.config_default:
v.setdefault('acls', self.config_default['acls'])
else:
v.setdefault('acls', [])
# add vlans & acls configured on a port
for vid in v['vlans']:
if vid not in self.vlans:
self.vlans[vid] = VLAN(vid)
if k not in self.ports:
self.ports[k] = Port(k, v['type'], v['acls'])
self.vlans[vid].add_port(self.ports[k])
# add configuration that should be on all ports
for c in self.config_all:
c.setdefault('vlans', [])
c.setdefault('type', v['type'])
c.setdefault('exclude', [])
c.setdefault('acls', [])
# exclude ports
if self.is_excluded(c['exclude'], k):
continue
# add port to 'all' vlans
for vid in c['vlans']:
if k not in self.ports:
self.ports[k] = Port(k, c['type'], v['acls'])
port = self.ports[k]
if vid in self.vlans and port in self.vlans[vid].get_ports():
# port is already in vlan, skip
continue
if vid not in self.vlans:
self.vlans[vid] = VLAN(vid)
self.vlans[vid].add_port(port)
# add 'all' acls to port
for acl in c['acls']:
self.ports[k].add_acl(acl)
def is_excluded(self, config_exclude, port):
excluded = False
for e in config_exclude:
if type(e) is str and ':' in e:
d, p = e.split(':')
if self.dpid == int(d) and port == int(p):
excluded = True
continue
else:
if port == e:
excluded = True
continue
return excluded
def get_native_vlan(self, port_num):
if port_num not in self.ports:
return None
port = self.ports[port_num]
for vid, vlan in self.vlans.items():
if port in vlan.untagged:
return vlan
def __str__(self):
return "dpid:%s" % self.dpid