Open Kilda Java Documentation
test_flow_segments.py
Go to the documentation of this file.
1 #
2 # (1) Run this from the root directory, where topology_engine.properties lives (there is some code dependency on that.
3 # For example, run from $KILDA_HOME/services/topology-engine/queue-engine
4 #
5 # (2) Primary purpose of this file is to do manual testing of underlying functionality.
6 #
7 # Types of tests:
8 # 1. flow creation
9 # 2. get segments (validate segments were created
10 # 3. add isl .. validate bandwidth deducted.
11 #
12 # Example command line start:
13 # `PYTHONPATH= python tests/test_flow_segments.py`
14 #
15 
16 import sys, copy
17 from topologylistener import flow_utils, messageclasses, config
18 from multiprocessing import Pool
19 
20 
21 flow1 = {
22  'flowid' : 'fred_id',
23  'cookie': 1000,
24  'meter_id': 2000,
25  'bandwidth': 3000,
26  'ignore_bandwidth': False,
27  'src_port': 1,
28  'dst_port': 2,
29  'src_switch': 'DE:AD:BE:EF:11:11:11:11',
30  'dst_switch': 'DE:AD:BE:EF:33:33:33:33',
31  'src_vlan': 3,
32  'dst_vlan': 4,
33  'transit_vlan': 5,
34  'description': 'Test Flow #1',
35  'last_updated': 'Last Full Moon',
36  'flowpath': {
37  'latency_ns': 10,
38  'path': [
39  {'switch_id': 'DE:AD:BE:EF:11:11:11:11', 'port_no': 5, 'seq_id': 1, 'segment_latency': 11},
40  {'switch_id': 'DE:AD:BE:EF:22:22:22:22', 'port_no': 6, 'seq_id': 2, 'segment_latency': 22},
41  {'switch_id': 'DE:AD:BE:EF:22:22:22:22', 'port_no': 7, 'seq_id': 3, 'segment_latency': 33},
42  {'switch_id': 'DE:AD:BE:EF:33:33:33:33', 'port_no': 8, 'seq_id': 4, 'segment_latency': 44},
43  ]
44  }
45 }
46 
47 isls1 = [
48  {
49  'latency_ns': 100,
50  'speed': 1000,
51  'available_bandwidth': 10000,
52  'path': [{'switch_id': 'DE:AD:BE:EF:11:11:11:11','port_no': 5},
53  {'switch_id': 'DE:AD:BE:EF:22:22:22:22','port_no': 6}]
54  },
55  {
56  'latency_ns': 200,
57  'speed': 2000,
58  'available_bandwidth': 20000,
59  'path': [{'switch_id': 'DE:AD:BE:EF:22:22:22:22', 'port_no': 7},
60  {'switch_id': 'DE:AD:BE:EF:33:33:33:33', 'port_no': 8}]
61  }]
62 
63 print('Hello World')
64 
65 print("TEST #1 & #2 - create flow, get segments, spot check some of the values.")
66 flow_utils.store_flow(flow1)
67 
68 result = flow_utils.fetch_flow_segments(flow1['flowid'], flow1['cookie'])
69 print("Validate #1 - Length of segments should be 2: %d" % len(result))
70 for i,val in enumerate(result):
71  print("Validate #3.* - seq_id should be %d: is: %d" % (flow1['flowpath']['path'][i*2]['seq_id'], val['seq_id']))
72 
73 print("TEST #3 - create isls, validate bandwidth.")
74 for isl in isls1:
75  mc = messageclasses.MessageItem(payload=isl)
76  mc.create_isl()
77 
78 for i,isl in enumerate(messageclasses.MessageItem.fetch_isls()):
79  print("Validate #3.* - available_bandwidth should be %d: is: %d" % ((-3000+isls1[i]['available_bandwidth']), isl['available_bandwidth']))
80 
81 print("TEST #4 - remove flow, validate bandwidth.")
82 flow_utils.remove_flow(flow1)
83 
84 result = flow_utils.fetch_flow_segments(flow1['flowid'], flow1['cookie'])
85 print("Validate #4.1 - should have no flow segments: %s " % result)
86 
87 for i,isl in enumerate(messageclasses.MessageItem.fetch_isls()):
88  print("Validate #4.* - available_bandwidth should be %d: is: %d" % ((isls1[i]['available_bandwidth']), isl['available_bandwidth']))
89 
90 # CREATE INDEX ON :switch(name);
91 # CREATE INDEX ON :isl(src_switch,src_port,dst_switch,dst_port);
92 # CREATE INDEX ON :flow_segment(src_switch,src_port,dst_switch,dst_port);
93 
94 r1a=0x2000000000000001
95 r1b=0x2000000000000002
96 f1a=0x4000000000000001
97 f1b=0x4000000000000002
98 
99 print("\n\n")
100 create_flow = {
101  u'timestamp': 1519103068803,
102  u'correlation_id': 1519103068744,
103  u'payload': {u'timestamp': 1519103068797, u'clazz': u'org.openkilda.messaging.info.flow.FlowInfoData',
104  u'flowid': u'c3none-1519103023077', u'correlation_id': u'1519103068744', u'operation': u'CREATE',
105  u'payload': {u'forward': {u'last_updated': u'2018-02-20T05:04:28.796Z', u'description': u'c3none',
106  u'state': u'ALLOCATED', u'transit_vlan': 2, u'ignore_bandwidth': False,
107  u'dst_switch': u'de:ad:be:ef:00:00:00:05', u'flowid': u'c3none-1519103023077',
108  u'bandwidth': 10000, u'src_switch': u'de:ad:be:ef:00:00:00:03', u'cookie': f1a,
109  u'dst_port': 2, u'src_vlan': 0, u'dst_vlan': 0, u'src_port': 1,
110  u'flowpath': {u'path': [
111  {u'seq_id': 0, u'switch_id': u'de:ad:be:ef:00:00:00:03', u'port_no': 2, u'segment_latency': 62},
112  {u'seq_id': 1, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 1},
113  {u'seq_id': 2, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 2, u'segment_latency': 13},
114  {u'seq_id': 3, u'switch_id': u'de:ad:be:ef:00:00:00:05', u'port_no': 1}],
115  u'latency_ns': 75, u'timestamp': 1519103068795,
116  u'clazz': u'org.openkilda.messaging.info.event.PathInfoData'},
117  u'meter_id': 1},
118  u'reverse': {u'last_updated': u'2018-02-20T05:04:28.796Z', u'description': u'c3none',
119  u'state': u'ALLOCATED', u'transit_vlan': 3, u'ignore_bandwidth': False,
120  u'dst_switch': u'de:ad:be:ef:00:00:00:03', u'flowid': u'c3none-1519103023077',
121  u'bandwidth': 10000,u'src_switch': u'de:ad:be:ef:00:00:00:05',u'cookie': r1a,
122  u'dst_port': 1, u'src_vlan': 0, u'dst_vlan': 0, u'src_port': 2,
123  u'flowpath': {u'path': [
124  {u'seq_id': 0, u'switch_id': u'de:ad:be:ef:00:00:00:05', u'port_no': 1, u'segment_latency': 13},
125  {u'seq_id': 1, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 2},
126  {u'seq_id': 2, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 1, u'segment_latency': 62},
127  {u'seq_id': 3, u'switch_id': u'de:ad:be:ef:00:00:00:03', u'port_no': 2}],
128  u'latency_ns': 75, u'timestamp': 1519103068795,
129  u'clazz': u'org.openkilda.messaging.info.event.PathInfoData'},
130  u'meter_id': 1}
131  }
132  }
133 }
134 
135 delete_flow = {
136  u'timestamp': 1519103073339,
137  u'correlation_id': 1519103073300,
138  u'payload': {u'timestamp': 1519103073329, u'clazz': u'org.openkilda.messaging.info.flow.FlowInfoData',
139  u'flowid': u'c3none-1519103023077', u'correlation_id': u'1519103073300', u'operation': u'DELETE',
140  u'payload': {u'forward': {u'last_updated': u'2018-02-20T05:04:28.796Z', u'description': u'c3none',
141  u'state': u'UP', u'transit_vlan': 2, u'ignore_bandwidth': False,
142  u'dst_switch': u'de:ad:be:ef:00:00:00:05', u'flowid': u'c3none-1519103023077',
143  u'bandwidth': 10000, u'src_switch': u'de:ad:be:ef:00:00:00:03', u'cookie': f1a,
144  u'dst_port': 2, u'src_vlan': 0, u'dst_vlan': 0,u'src_port': 1,
145  u'flowpath': {u'path': [
146  {u'seq_id': 0, u'switch_id': u'de:ad:be:ef:00:00:00:03', u'port_no': 2, u'segment_latency': 62},
147  {u'seq_id': 1, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 1},
148  {u'seq_id': 2, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 2, u'segment_latency': 13},
149  {u'seq_id': 3, u'switch_id': u'de:ad:be:ef:00:00:00:05', u'port_no': 1}],
150  u'latency_ns': 75, u'timestamp': 1519103068795,
151  u'clazz': u'org.openkilda.messaging.info.event.PathInfoData'},
152  u'meter_id': 1},
153  u'reverse': {u'last_updated': u'2018-02-20T05:04:28.796Z', u'description': u'c3none',
154  u'state': u'UP', u'transit_vlan': 3, u'ignore_bandwidth': False,
155  u'dst_switch': u'de:ad:be:ef:00:00:00:03', u'flowid': u'c3none-1519103023077',
156  u'bandwidth': 10000, u'src_switch': u'de:ad:be:ef:00:00:00:05', u'cookie': r1a,
157  u'dst_port': 1, u'src_vlan': 0, u'dst_vlan': 0, u'src_port': 2,
158  u'flowpath': {u'path': [
159  {u'seq_id': 0, u'switch_id': u'de:ad:be:ef:00:00:00:05', u'port_no': 1, u'segment_latency': 13},
160  {u'seq_id': 1, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 2},
161  {u'seq_id': 2, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 1, u'segment_latency': 62},
162  {u'seq_id': 3, u'switch_id': u'de:ad:be:ef:00:00:00:03', u'port_no': 2}],
163  u'latency_ns': 75, u'timestamp': 1519103068795,
164  u'clazz': u'org.openkilda.messaging.info.event.PathInfoData'},
165  u'meter_id': 1
166  }
167  }
168  }
169 }
170 
171 update_flow = {
172  u'timestamp': 1519167032591,
173  u'correlation_id': 1519167032523,
174  u'payload': {u'timestamp': 1519167032580, u'clazz': u'org.openkilda.messaging.info.flow.FlowInfoData',
175  u'flowid': u'c3none-1519103023077', u'correlation_id': u'1519167032523', u'operation': u'UPDATE',
176  u'payload': {u'forward': {u'last_updated': u'2018-02-20T22:50:32.579Z', u'description': u'u3none',
177  u'state': u'ALLOCATED', u'transit_vlan': 4, u'ignore_bandwidth': False,
178  u'dst_switch': u'de:ad:be:ef:00:00:00:05', u'flowid': u'c3none-1519103023077',
179  u'bandwidth': 20000, u'src_switch': u'de:ad:be:ef:00:00:00:03', u'cookie': f1b,
180  u'dst_port': 2, u'src_vlan': 0, u'dst_vlan': 0, u'src_port': 1,
181  u'flowpath': {u'path': [
182  {u'seq_id': 0, u'switch_id': u'de:ad:be:ef:00:00:00:03', u'port_no': 2, u'segment_latency': 469},
183  {u'seq_id': 1, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 1},
184  {u'seq_id': 2, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 2, u'segment_latency': 357},
185  {u'seq_id': 3, u'switch_id': u'de:ad:be:ef:00:00:00:05', u'port_no': 1}],
186  u'latency_ns': 826, u'timestamp': 1519167032578,
187  u'clazz': u'org.openkilda.messaging.info.event.PathInfoData'},
188  u'meter_id': 2},
189  u'reverse': {u'last_updated': u'2018-02-20T22:50:32.579Z', u'description': u'u3none',
190  u'state': u'ALLOCATED', u'transit_vlan': 5, u'ignore_bandwidth': False,
191  u'dst_switch': u'de:ad:be:ef:00:00:00:03', u'flowid': u'c3none-1519103023077',
192  u'bandwidth': 20000, u'src_switch': u'de:ad:be:ef:00:00:00:05', u'cookie': r1b,
193  u'dst_port': 1, u'src_vlan': 0, u'dst_vlan': 0, u'src_port': 2,
194  u'flowpath': {u'path': [
195  {u'seq_id': 0, u'switch_id': u'de:ad:be:ef:00:00:00:05', u'port_no': 1, u'segment_latency': 357},
196  {u'seq_id': 1, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 2},
197  {u'seq_id': 2, u'switch_id': u'de:ad:be:ef:00:00:00:04', u'port_no': 1, u'segment_latency': 469},
198  {u'seq_id': 3, u'switch_id': u'de:ad:be:ef:00:00:00:03', u'port_no': 2}],
199  u'latency_ns': 826, u'timestamp': 1519167032578,
200  u'clazz': u'org.openkilda.messaging.info.event.PathInfoData'},
201  u'meter_id': 2}}}
202 
203 }
204 
205 print('TEST #5 - create flow through front door (almost .. starting with dict)')
206 event_create = messageclasses.MessageItem(**create_flow)
207 event_create.handle()
208 # TODO: test something here related to create
209 
210 import logging
211 logging.basicConfig()
212 logger = logging.getLogger('topologylistener.messageclasses')
213 
214 print('TEST #6 - update flow through front door (almost .. starting with dict)')
215 event_delete = messageclasses.MessageItem(**update_flow)
216 event_delete.handle()
217 # TODO: test something here related to delete
218 
219 print('TEST #7 - delete flow through front door (almost .. starting with dict)')
220 event_delete = messageclasses.MessageItem(**delete_flow)
221 event_delete.handle()
222 # TODO: test something here related to delete
223 
224 
225 print('TEST #8 - create ... A lot )')
226 print("==> If this fails, it will not stop, run: `ps -a | grep test_flow | cut -d ' ' -f 1 | xargs kill` ")
227 #
228 # NOTE: comment out the NORTHBOUND response in messageclasses::create_flow :: else ... otherwise you'll find that
229 # Kafka gets hung up. The lines to comment out:
230 #
231 # message_utils.send_to_topic(
232 # payload=data,
233 # correlation_id=correlation_id,
234 # message_type=message_utils.MT_INFO,
235 # destination="NORTHBOUND",
236 # topic=config.KAFKA_NORTHBOUND_TOPIC
237 
238 #
239 test_range = 20
240 num_proc = 5
241 
242 def do_push_alot(iteration):
243  if iteration % num_proc == 0:
244  sys.stdout.write(" {}".format(iteration))
245  sys.stdout.flush()
246  cf = copy.deepcopy(create_flow)
247  cf[u'payload'][u'operation'] = "PUSH"
248  messageclasses.MessageItem(**cf).handle()
249 
250 
251 p = Pool(processes=num_proc)
252 p.map(do_push_alot, range(test_range))
253 print(" DONE ")
254 print(" ")