blob: b7330d0185b69edd5bb5d7dd73975fbc749bfa05 [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):
8 #FIXME this test is failinf
9 def _test_pure_proto(self):
10 xproto = \
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020011"""
Sapan Bhatia4e80a262017-05-19 23:10:51 +020012message VRouterPort (XOSBase){
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020013 optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True];
14 required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
15 required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False];
16 required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
17}
18"""
19
Matteo Scandolo67654fa2017-06-09 09:33:17 -070020 proto = \
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020021"""
22message VRouterPort {
Sapan Bhatia4e80a262017-05-19 23:10:51 +020023 option bases = "XOSBase";
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020024 optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = "port friendly name", modifier = "optional", db_index = "False" ];
25 required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = "port identifier in ONOS", modifier = "required", db_index = "False" ];
26 required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", db_index = "True", link = "manytoone"];
27 required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", db_index = "True", link = "manytoone"];
28}
29"""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070030 target = XProtoTestHelpers.write_tmp_target(
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020031"""
32from header import *
33{% for m in proto.messages %}
34{% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{% endif %}
35{% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %}
36
37{%- for l in m.links %}
38
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020039{% if l.peer.name != m.name %}
40from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020041{% endif %}
42
43{%- endfor %}
44{% for b in m.bases %}
Sapan Bhatia4e80a262017-05-19 23:10:51 +020045{% if b!='XOSBase' and 'Mixin' not in b%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020046from core.models.{{b.name | lower}} import {{ b.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020047{% endif %}
48{% endfor %}
49
50
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020051class {{ m.name }}{{ xproto_base_def(m, m.bases) }}:
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020052 # Primitive Fields (Not Relations)
53 {% for f in m.fields %}
54 {%- if not f.link -%}
55 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
56 {% endif %}
57 {%- endfor %}
58
59 # Relations
60 {% for l in m.links %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020061 {{ 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 +020062 {%- endfor %}
63
64 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
65 pass
66
67{% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %}
68{% endfor %}
Matteo Scandolo67654fa2017-06-09 09:33:17 -070069""")
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020070
Matteo Scandolo67654fa2017-06-09 09:33:17 -070071 args_xproto = FakeArgs()
72 args_xproto.inputs = xproto
73 args_xproto.target = target
74 xproto_gen = XOSGenerator.generate(args_xproto)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020075
Matteo Scandolo67654fa2017-06-09 09:33:17 -070076 count1 = len(xproto_gen.split('\n'))
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020077
Matteo Scandolo67654fa2017-06-09 09:33:17 -070078 args_proto = FakeArgs()
79 args_proto.inputs = proto
80 args_proto.target = target
81 args_proto.rev = True
82 proto_gen = XOSGenerator.generate(args_proto)
83 count2 = len(proto_gen.split('\n'))
84
85 self.assertEqual(count1, count2)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020086
87if __name__ == '__main__':
88 unittest.main()
89
90