blob: 1f165fbbd74c0b7e7e220dddb2292c27a3edbcad [file] [log] [blame]
Matteo Scandolo67654fa2017-06-09 09:33:17 -07001
2import unittest
3from xosgenx.generator import XOSGenerator
4from helpers import FakeArgs, XProtoTestHelpers
Sapan Bhatia3d61cf82017-05-14 23:59:23 +02005
6# Generate from xproto, then generate from equivalent proto
Matteo Scandolo67654fa2017-06-09 09:33:17 -07007class XPureProtobufGenerator(unittest.TestCase):
Sapan Bhatia4c835602017-07-14 01:13:17 -04008 def test_pure_proto(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -07009 xproto = \
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020010"""
Sapan Bhatia4e80a262017-05-19 23:10:51 +020011message VRouterPort (XOSBase){
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020012 optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True];
13 required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
14 required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False];
15 required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
16}
17"""
18
Matteo Scandolo67654fa2017-06-09 09:33:17 -070019 proto = \
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020020"""
21message VRouterPort {
Sapan Bhatia4e80a262017-05-19 23:10:51 +020022 option bases = "XOSBase";
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020023 optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = "port friendly name", modifier = "optional", db_index = "False" ];
24 required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = "port identifier in ONOS", modifier = "required", db_index = "False" ];
25 required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", db_index = "True", link = "manytoone"];
26 required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", db_index = "True", link = "manytoone"];
27}
28"""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070029 target = XProtoTestHelpers.write_tmp_target(
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020030"""
31from header import *
32{% for m in proto.messages %}
33{% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{% endif %}
34{% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %}
35
36{%- for l in m.links %}
37
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020038{% if l.peer.name != m.name %}
39from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020040{% endif %}
41
42{%- endfor %}
43{% for b in m.bases %}
Sapan Bhatia4e80a262017-05-19 23:10:51 +020044{% if b!='XOSBase' and 'Mixin' not in b%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020045from core.models.{{b.name | lower}} import {{ b.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020046{% endif %}
47{% endfor %}
48
49
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020050class {{ m.name }}{{ xproto_base_def(m, m.bases) }}:
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020051 # Primitive Fields (Not Relations)
52 {% for f in m.fields %}
53 {%- if not f.link -%}
54 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
55 {% endif %}
56 {%- endfor %}
57
58 # Relations
59 {% for l in m.links %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020060 {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer.name==m.name -%}'self'{%- else -%}{{ l.peer.name }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020061 {%- endfor %}
62
63 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
64 pass
65
66{% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %}
67{% endfor %}
Matteo Scandolo67654fa2017-06-09 09:33:17 -070068""")
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020069
Matteo Scandolo67654fa2017-06-09 09:33:17 -070070 args_xproto = FakeArgs()
71 args_xproto.inputs = xproto
72 args_xproto.target = target
73 xproto_gen = XOSGenerator.generate(args_xproto)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020074
Matteo Scandolo67654fa2017-06-09 09:33:17 -070075 count1 = len(xproto_gen.split('\n'))
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020076
Matteo Scandolo67654fa2017-06-09 09:33:17 -070077 args_proto = FakeArgs()
78 args_proto.inputs = proto
79 args_proto.target = target
80 args_proto.rev = True
81 proto_gen = XOSGenerator.generate(args_proto)
82 count2 = len(proto_gen.split('\n'))
83
84 self.assertEqual(count1, count2)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020085
Sapan Bhatiaea6ff752017-07-14 01:52:18 -040086 def test_pure_policies(self):
87 xproto = \
88"""
89policy my_policy < exists x:a=b >
90"""
91
92 proto = \
93"""
94option my_policy = "policy:< exists x:a=b >";
95"""
96 target = XProtoTestHelpers.write_tmp_target(
97"""
98{{ policies }}
99""")
100
101 args_xproto = FakeArgs()
102 args_xproto.inputs = xproto
103 args_xproto.target = target
104 xproto_gen = XOSGenerator.generate(args_xproto)
105
106 args_proto = FakeArgs()
107 args_proto.inputs = proto
108 args_proto.target = target
109 args_proto.rev = True
110 proto_gen = XOSGenerator.generate(args_proto)
111
112 self.assertEqual(proto_gen, xproto_gen)
113
Sapan Bhatia3d61cf82017-05-14 23:59:23 +0200114if __name__ == '__main__':
115 unittest.main()
116
117