Open Kilda Java Documentation
create-simple-topology.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Copyright 2017 Telstra Open Source
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 
17 from kafka import KafkaConsumer, KafkaProducer
18 import json, time, requests
19 
20 MT_INFO = "org.openkilda.messaging.info.InfoMessage"
21 MT_SWITCH = "org.openkilda.messaging.info.event.SwitchInfoData"
22 MT_ISL = "org.openkilda.messaging.info.event.IslInfoData"
23 
24 bootstrapServer = 'kafka.pendev:9092'
25 topic = 'kilda-test'
26 
27 producer = KafkaProducer(bootstrap_servers=bootstrapServer)
28 
29 loopSize = 3
30 
31 loopSize += 1
32 topology = []
33 
34 for n in range(1, loopSize):
35  node = {}
36  if loopSize > 99:
37  loopSize = 99
38 
39  switch_id = n
40  linked_id_next = switch_id + 1
41  linked_id_prev = switch_id - 1
42 
43  if linked_id_next == loopSize:
44  linked_id_next = 1
45 
46  if linked_id_prev < 1:
47  linked_id_prev = loopSize - 1
48 
49  switch_id = str(switch_id).zfill(2)
50  linked_id_next = str(linked_id_next).zfill(2)
51  linked_id_prev = str(linked_id_prev).zfill(2)
52 
53  node = {}
54  node['name'] = "00:00:00:00:00:00:00:{}".format(switch_id)
55  outgoing_relationships = []
56  outgoing_relationships.append("00:00:00:00:00:00:00:{}".format(linked_id_prev))
57  outgoing_relationships.append("00:00:00:00:00:00:00:{}".format(linked_id_next))
58  outgoing_relationships.sort()
59  node['outgoing_relationships'] = outgoing_relationships
60  topology.append(node)
61 
62  producer.send(topic, b'{{"clazz": "{}", "timestamp": 23478952134, "payload": {{"clazz": "{}", "switch_id": "00:00:00:00:00:00:00:{}", "state": "ADDED"}}}}'.format(MT_INFO, MT_SWITCH, switch_id))
63  producer.send(topic, b'{{"clazz": "{}", "timestamp": 23478952136, "payload": {{'
64  b'"clazz": "{}", "latency_ns": 1123, "path": [{{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 1, "seq_id": "0", "segment_latency": 1123}}, {{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 2, "seq_id": "1"}}]}}}}'.format(MT_INFO, MT_ISL, switch_id, linked_id_next))
65  producer.send(topic, b'{{"clazz": "{}", "timestamp": 23478952136, "payload": {{'
66  b'"clazz": "{}", "latency_ns": 1123, "path": [{{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 2, "seq_id": "0", "segment_latency": 1123}}, {{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 1, "seq_id": "1"}}]}}}}'.format(MT_INFO, MT_ISL, switch_id, linked_id_prev))
67 
68  producer.send(topic, b'{{"clazz": "{}", "timestamp": 23478952136, "payload": {{'
69  b'"clazz": "{}", "latency_ns": 1123, "path": [{{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 3, "seq_id": "0", "segment_latency": 1123}}, {{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 4, "seq_id": "1"}}]}}}}'.format(MT_INFO, MT_ISL, switch_id, linked_id_next))
70  producer.send(topic, b'{{"clazz": "{}", "timestamp": 23478952136, "payload": {{'
71  b'"clazz": "{}", "latency_ns": 1123, "path": [{{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 4, "seq_id": "0", "segment_latency": 1123}}, {{"switch_id": "00:00:00:00:00:00:00:{}", "port_no": 3, "seq_id": "1"}}]}}}}'.format(MT_INFO, MT_ISL, switch_id, linked_id_prev))
72 
73 
74 headers = {'Content-Type': 'application/json'}
75 time.sleep(5)
76 result_recv = requests.get('http://localhost', headers=headers)
77 
78 recv_topo = result_recv.text
79 sent_topo = json.dumps(topology, default=lambda o: o.__dict__, sort_keys=True)
80 
81 if recv_topo == sent_topo:
82  print "Topology created and validated"
83 else:
84  print "Error in test please check."
85  print "Sent topo:"
86  print sent_topo
87  print "Recv topo"
88  print recv_topo