Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | |
| 2 | import unittest |
| 3 | from xosgenx.generator import XOSGenerator |
| 4 | from helpers import FakeArgs, XProtoTestHelpers |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 5 | |
| 6 | # Generate from xproto, then generate from equivalent proto |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 7 | class XPureProtobufGenerator(unittest.TestCase): |
| 8 | #FIXME this test is failinf |
| 9 | def _test_pure_proto(self): |
| 10 | xproto = \ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 11 | """ |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 12 | message VRouterPort (XOSBase){ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 13 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | proto = \ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 21 | """ |
| 22 | message VRouterPort { |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 23 | option bases = "XOSBase"; |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 24 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 30 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 31 | """ |
| 32 | from 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 Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 39 | {% if l.peer.name != m.name %} |
| 40 | from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 41 | {% endif %} |
| 42 | |
| 43 | {%- endfor %} |
| 44 | {% for b in m.bases %} |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 45 | {% if b!='XOSBase' and 'Mixin' not in b%} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 46 | from core.models.{{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 47 | {% endif %} |
| 48 | {% endfor %} |
| 49 | |
| 50 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 51 | class {{ m.name }}{{ xproto_base_def(m, m.bases) }}: |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 52 | # 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 Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 61 | {{ 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 Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 62 | {%- 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 69 | """) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 70 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 71 | args_xproto = FakeArgs() |
| 72 | args_xproto.inputs = xproto |
| 73 | args_xproto.target = target |
| 74 | xproto_gen = XOSGenerator.generate(args_xproto) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 75 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 76 | count1 = len(xproto_gen.split('\n')) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 77 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 78 | 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 Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 86 | |
| 87 | if __name__ == '__main__': |
| 88 | unittest.main() |
| 89 | |
| 90 | |