/
test_util.py
169 lines (151 loc) · 6.48 KB
/
test_util.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
#!/usr/bin/python
################################################################################
# The Frenetic Project #
# frenetic@frenetic-lang.org #
################################################################################
# Licensed to the Frenetic Project by one or more contributors. See the #
# NOTICE file distributed with this work for additional information #
# regarding copyright and ownership. The Frenetic Project licenses this #
# file to you under the following license. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided the following conditions are met: #
# - Redistributions of source code must retain the above copyright #
# notice, this list of conditions and the following disclaimer. #
# - Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in #
# the documentation or other materials provided with the distribution. #
# - The names of the copyright holds and contributors may not be used to #
# endorse or promote products derived from this work without specific #
# prior written permission. #
# #
# 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 file distributed with this work for specific language governing #
# permissions and limitations under the License. #
################################################################################
# /slices/test_util.py #
# Utilities for testing netcore and slices #
################################################################################
import networkx as nx
import nxtopo
import slicing
import netcore as nc
from examples import policy_gen
def linear(*linear_nodes):
"""Linear graph on four nodes, slices from input."""
topo = nxtopo.from_graph(nx.path_graph(4))
topo.finalize()
topos = [topo.subgraph(nodes) for nodes in linear_nodes]
policies = [policy_gen.flood_observe(t) for t in topos]
return topo, policies
def linear_hosts(*linear_nodes):
"""Returns topo, [(slice, policy)]"""
topo = nxtopo.from_graph(nx.path_graph(4))
nodes = [set(ns) for ns in linear_nodes]
for n in topo.nodes():
h1 = 100 + n*10 + 1
h2 = 100 + n*10 + 2
topo.add_host(h1)
topo.add_host(h2)
topo.add_link(n, h1)
topo.add_link(n, h2)
for ns in nodes:
if n in ns:
ns.update([h1, h2])
topo.finalize()
edge_policies = []
for ns in linear_nodes:
predicates = {}
for n in ns:
h1 = 100 + n*10 + 1
h2 = 100 + n*10 + 2
h1_port = topo.node[h1]['port'][0]
h2_port = topo.node[h2]['port'][0]
predicates[h1_port] = nc.Top()
predicates[h2_port] = nc.Top()
edge_policies.append(predicates)
topos = [topo.subgraph(ns) for ns in nodes]
slices = [slicing.ident_map_slice(t, pol)
for t, pol in zip(topos, edge_policies)]
combined = zip(slices, [policy_gen.flood_observe(t) for t in topos])
return (topo, combined)
def linear_all_ports(*linear_nodes):
"""Linear graph on four nodes with reflexive forwarding."""
topo = nxtopo.from_graph(nx.path_graph(4))
topo.finalize()
topos = [topo.subgraph(nodes) for nodes in linear_nodes]
policies = [policy_gen.flood_observe(t, all_ports=True) for t in topos]
return topo, policies
k10_nodes = [
(0, 1, 2, 3, 4),
(1, 2, 3, 4, 5),
(2, 3, 4, 5, 6),
(3, 4, 5, 6, 7),
(4, 5, 6, 7, 8),
(5, 6, 7, 8, 9),
(6, 7, 8, 9, 0),
(7, 8, 9, 0, 1),
(8, 9, 0, 1, 2),
(9, 0, 1, 2, 3),
]
def k10():
"""K10 complete graph."""
topo = nxtopo.from_graph(nx.complete_graph(10))
topo.finalize()
topos = [topo.subgraph(nodes) for nodes in k10_nodes]
edge_policies = [{} for t in topos]
slices = [slicing.ident_map_slice(t, pol)
for t, pol in zip(topos, edge_policies)]
combined = zip(slices, [policy_gen.flood_observe(t) for t in topos])
return (topo, combined)
k4_nodes = [
(0, 1, 2),
(1, 2, 3),
(2, 3, 0),
(3, 0, 1),
]
def k4():
"""K4 complete graph."""
topo = nxtopo.from_graph(nx.complete_graph(4))
topo.finalize()
topos = [topo.subgraph(nodes) for nodes in k4_nodes]
edge_policies = [{} for t in topos]
slices = [slicing.ident_map_slice(t, pol)
for t, pol in zip(topos, edge_policies)]
combined = zip(slices, [policy_gen.flood_observe(t) for t in topos])
return (topo, combined)
def k4hosts():
"""K4 complete graph with two hosts on each node."""
topo = nxtopo.from_graph(nx.complete_graph(4))
nodes = [set(ns) for ns in k4_nodes]
for n in topo.nodes():
h1 = 100 + n*10 + 1
h2 = 100 + n*10 + 2
topo.add_host(h1)
topo.add_host(h2)
topo.add_link(n, h1)
topo.add_link(n, h2)
for ns in nodes:
if n in ns:
ns.update([h1, h2])
topo.finalize()
edge_policies = []
for ns in k4_nodes:
predicates = {}
for n in ns:
h1 = 100 + n*10 + 1
h2 = 100 + n*10 + 2
h1_port = topo.node[h1]['port'][0]
h2_port = topo.node[h2]['port'][0]
predicates[h1_port] = nc.Top()
predicates[h2_port] = nc.Top()
edge_policies.append(predicates)
topos = [topo.subgraph(ns) for ns in nodes]
slices = [slicing.ident_map_slice(t, pol)
for t, pol in zip(topos, edge_policies)]
combined = zip(slices, [policy_gen.flood_observe(t) for t in topos])
return (topo, combined)
if __name__ == '__main__':
unittest.main()