-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathBinding.py
317 lines (236 loc) · 11.5 KB
/
Binding.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from __future__ import annotations
from .Printable import Printable
from .Emulator import Emulator
from .Node import Node
from .Filter import Filter
from .BaseSystem import BaseSystem
from enum import Enum
from typing import List
from ipaddress import IPv4Network, IPv4Address
from sys import stderr
import re, random, string
from .Scope import NodeScope, NodeScopeTier
class Action(Enum):
"""!
@brief actions to take when a binding matches a node.
"""
## pick randomly from the candidates.
RANDOM = 0
## pick the first candidate.
FIRST = 1
## pick the last candidate.
LAST = 2
## create a node matching the given conditions. Note that 'NEW' nodes are
# only created during render, and attempt to resolve it beforehand
# (like resolvVnode) is not possible.
NEW = 3
class Binding(Printable):
"""!
@brief Binding class.
A binding class defines how to bind virtual nodes to physical nodes.
"""
source: str
action: Action
filter: Filter
def __init__(self, source, action = Action.RANDOM, filter = Filter()):
"""!
@brief create new binding.
@param source virtual node name. can be regexp to match multiple virtual
nodes.
@param action (optional) candidate selection. Default to random.
@param filter (optional) filter. Default to empty filter (all physical
nodes).
"""
## regexp of virtual node name that should be handled by this binding.
self.source = source
## candidate selection after the filter completes.
self.action = action
## physical node filter.
self.filter = filter
def __filterBaseSystemConflict(self, vnode:str, node:Node, emulator:Emulator) -> bool:
"""!
@brief filter a base_system conflict between vnode and node when binding.
@param vnode virtual node name.
@param node candidate physical name to bind with vnode.
@param emulator emulator instance to get server object by vnode name.
@returns True if it does not have any conflict.
"""
nodeBaseSystem = node.getBaseSystem()
server = emulator.getServerByVirtualNodeName(vnode)
vnodeBaseSystem = server.getBaseSystem()
if nodeBaseSystem == vnodeBaseSystem:
return True
if BaseSystem.doesAContainB(A=vnodeBaseSystem, B=nodeBaseSystem):
return True
if BaseSystem.doesAContainB(A=nodeBaseSystem, B=vnodeBaseSystem):
server.setBaseSystem(nodeBaseSystem)
return True
return False
def __create(self, emulator: Emulator) -> Node:
"""!
@brief create a node matching given condition.
@returns node created.
"""
self.__log('binding: NEW: try to create a node matching filter condition(s)...')
reg = emulator.getRegistry()
base = emulator.getLayer('Base')
f = self.filter
assert f.custom == None, 'binding: NEW: custom filter function is not supported with NEW action.'
assert f.asn == None or f.asn in base.getAsns(), 'binding: NEW: AS{} is set in filter but not in emulator.'.format(f.asn)
assert f.ip == None or f.prefix == None, 'binding: NEW: both ip and prefix is set. Please set only one of them.'
if f.allowBound: self.__log('binding: NEW: WARN: allowBound has not effect when using Action.NEW')
asn = f.asn
netName = None
# ip is set: find net matching the condition.
if f.ip != None:
self.__log('binding: NEW: IP {} is given to host: finding networks with this IP in range.'.format(f.ip))
for _asn in base.getAsns():
hit = False
if f.asn != None and f.asn != _asn: continue
asObject = base.getAutonomousSystem(_asn)
for net in asObject.getNetworks():
netObject = asObject.getNetwork(net)
if IPv4Address(f.ip) in netObject.getPrefix():
self.__log('match found: as{}/{}'.format(_asn, net))
asn = _asn
netName = net
hit = True
break
if hit: break
# prefix is set: find net matching the condition
if f.prefix != None:
self.__log('binding: NEW: Prefix {} is given to host: finding networks in range.'.format(f.prefix))
for _asn in base.getAsns():
hit = False
if f.asn != None and f.asn != _asn: continue
asObject = base.getAutonomousSystem(_asn)
for net in asObject.getNetworks():
netObject = asObject.getNetwork(net)
if IPv4Network(f.prefix).overlaps(netObject.getPrefix()):
self.__log('binding: NEW: match found: as{}/{}'.format(_asn, net))
asn = _asn
netName = net
hit = True
break
if hit: break
if f.prefix != None or f.ip != None:
assert netName != None, 'binding: NEW: cannot satisfy prefix/ip rule set by filter.'
# no as selected: randomly choose one
if asn == None:
asn = random.choice(base.getAsns())
self.__log('binding: NEW: asn not set, using random as: {}'.format(asn))
asObject = base.getAutonomousSystem(asn)
# no net selected: randomly choose one
if netName == None:
netName = random.choice(asObject.getNetworks())
self.__log('binding: NEW: ip/prefix not set, using random net: as{}/{}'.format(asn, netName))
nodeName = f.nodeName
# no nodename given: randomly create one
if nodeName == None:
nodeName = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
self.__log('binding: NEW: nodeName not set, using random name: {}'.format(nodeName))
self.__log('binding: NEW: creating new host...'.format(nodeName))
# create the host in as
host = asObject.createHost(nodeName)
# inherit AS level option overrides...
asObject.handDown(host)
# 'host' didnt exist back when Base::configure() installed
# the global default sysctl options on all nodes
for o in base.getAvailableOptions():
host.setOption(o, NodeScope(NodeScopeTier.Global))
# set name servers
host.setNameServers(asObject.getNameServers())
# join net
host.joinNetwork(netName, 'auto' if f.ip == None else f.ip)
# register - usually this is done by AS in configure stage, since we have passed that point, we need to do it ourself.
reg.register(str(asn), 'hnode', nodeName, host)
# configure - usually this is done by AS in configure stage, since we have passed that point, we need to do it ourself.
# >> here at the latest, any relevant options must be set on the node or they wont be considered
host.configure(emulator)
return host
def shoudBind(self, vnode: str) -> bool:
"""!
@brief test if this binding applies to a virtual node.
@param vnode name of vnode.
@returns true if applies, false otherwise.
"""
return re.compile(self.source).match(vnode)
def getCandidate(self, vnode: str, emulator: Emulator, peek: bool = False) -> Node:
"""!
@brief get a binding candidate from given emulator. Note that this will
make change to the node by adding a "bound = true" attribute to the
node object.
@param vnode name of vnode
@param emulator emulator to select candidate from.
@param peek (optional) peek mode - ignore bound attribute and don't set
it when node is selected.
@return candidate node, or none if not found.
"""
if not self.shoudBind(vnode): return None
self.__log('looking for binding for {}'.format(vnode))
if self.action == Action.NEW:
if peek: return None
node = self.__create(emulator)
node.setAttribute('bound', True)
return node
registry = emulator.getRegistry()
candidates: List[Node] = []
for (scope, type, name), obj in registry.getAll().items():
if type not in ['hnode', 'rnode', 'csnode']: continue
node: Node = obj
filter = self.filter
self.__log('trying node as{}/{}...'.format(scope, name ))
if filter.asn != None and node.getAsn() != filter.asn:
self.__log('node asn ({}) != filter asn ({}), trying next node.'.format(node.getAsn(), filter.asn))
continue
if filter.nodeName != None and not re.compile(filter.nodeName).match(name):
self.__log('node name ({}) cat\'t match filter name ({}), trying next node.'.format(name, filter.nodeName))
continue
if filter.ip != None:
has_match = False
for iface in node.getInterfaces():
if str(iface.getAddress()) == filter.ip:
has_match = True
break
if not has_match:
self.__log('node as{}/{} does not have IP {}, trying next node.'.format(scope, name, filter.ip))
continue
if filter.prefix != None:
has_match = False
net = IPv4Network(filter.prefix)
for iface in node.getInterfaces():
if iface.getAddress() in net:
has_match = True
break
if not has_match:
self.__log('node as{}/{} not in prefix {}, trying next node.'.format(scope, name, filter.prefix))
continue
if filter.custom != None and not filter.custom(vnode, node):
self.__log('custom function returned false for node as{}/{}, trying next node.'.format(scope, name))
continue
if node.hasAttribute('bound') and not filter.allowBound and not peek:
self.__log('node as{}/{} is already bound and re-bind is not allowed, trying next node.'.format(scope, name))
continue
if not self.__filterBaseSystemConflict(vnode, node, emulator):
self.__log('node as{}/{} base_system is not compatible'.format(scope, name))
continue
self.__log('node as{}/{} added as candidate. looking for more candidates.'.format(scope, name))
if self.action == Action.FIRST:
self.__log('{} as{}/{}.'.format('peek: picked' if peek else 'bound to', node.getAsn(), node.getName()))
if not peek: node.setAttribute('bound', True)
return node
candidates.append(node)
if len(candidates) == 0: return None
node = None
if self.action == Action.LAST: node = candidates[-1]
if self.action == Action.RANDOM: node = random.choice(candidates)
if node != None:
self.__log('{} as{}/{}.'.format('peek: picked' if peek else 'bound to', node.getAsn(), node.getName()))
if not peek: node.setAttribute('bound', True)
return node
def __log(self, message: str):
"""!
@brief log to stderr.
@param message message.
"""
print('==== Binding: {}: {}'.format(self.source, message), file=stderr)