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): |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 8 | def test_pure_proto(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 9 | xproto = \ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 10 | """ |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 11 | message VRouterPort (XOSBase){ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 12 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 19 | proto = \ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 20 | """ |
| 21 | message VRouterPort { |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 22 | option bases = "XOSBase"; |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 23 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 29 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 30 | """ |
| 31 | from 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 Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 38 | {% if l.peer.name != m.name %} |
| 39 | from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 40 | {% endif %} |
| 41 | |
| 42 | {%- endfor %} |
| 43 | {% for b in m.bases %} |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 44 | {% if b!='XOSBase' and 'Mixin' not in b%} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 45 | from core.models.{{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 46 | {% endif %} |
| 47 | {% endfor %} |
| 48 | |
| 49 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 50 | class {{ m.name }}{{ xproto_base_def(m, m.bases) }}: |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 51 | # 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 Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 60 | {{ 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] | 61 | {%- 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 68 | """) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 69 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 70 | args_xproto = FakeArgs() |
| 71 | args_xproto.inputs = xproto |
| 72 | args_xproto.target = target |
| 73 | xproto_gen = XOSGenerator.generate(args_xproto) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 74 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 75 | count1 = len(xproto_gen.split('\n')) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 76 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 77 | 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 Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 85 | |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 86 | def test_pure_policies(self): |
| 87 | xproto = \ |
| 88 | """ |
| 89 | policy my_policy < exists x:a=b > |
| 90 | """ |
| 91 | |
| 92 | proto = \ |
| 93 | """ |
| 94 | option 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 Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 114 | if __name__ == '__main__': |
| 115 | unittest.main() |
| 116 | |
| 117 | |