Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 2 | # Copyright 2017-present Open Networking Foundation |
| 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 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 18 | import unittest |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 19 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 20 | from helpers import XProtoTestHelpers |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 21 | |
| 22 | # Generate from xproto, then generate from equivalent proto |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 23 | class XPureProtobufGenerator(unittest.TestCase): |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 24 | def test_pure_proto(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 25 | xproto = \ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 26 | """ |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 27 | message VRouterPort (XOSBase){ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 28 | optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True]; |
| 29 | required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False]; |
| 30 | required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False]; |
| 31 | required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False]; |
| 32 | } |
| 33 | """ |
| 34 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | proto = \ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 36 | """ |
| 37 | message VRouterPort { |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 38 | option bases = "XOSBase"; |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 39 | optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = "port friendly name", modifier = "optional", db_index = "False" ]; |
| 40 | required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = "port identifier in ONOS", modifier = "required", db_index = "False" ]; |
| 41 | required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", db_index = "True", link = "manytoone"]; |
| 42 | required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", db_index = "True", link = "manytoone"]; |
| 43 | } |
| 44 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 45 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 46 | """ |
| 47 | from header import * |
| 48 | {% for m in proto.messages %} |
| 49 | {% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{% endif %} |
| 50 | {% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %} |
| 51 | |
| 52 | {%- for l in m.links %} |
| 53 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 54 | {% if l.peer.name != m.name %} |
| 55 | from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 56 | {% endif %} |
| 57 | |
| 58 | {%- endfor %} |
| 59 | {% for b in m.bases %} |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 60 | {% if b!='XOSBase' and 'Mixin' not in b%} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 61 | from core.models.{{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 62 | {% endif %} |
| 63 | {% endfor %} |
| 64 | |
| 65 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 66 | class {{ m.name }}{{ xproto_base_def(m, m.bases) }}: |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 67 | # Primitive Fields (Not Relations) |
| 68 | {% for f in m.fields %} |
| 69 | {%- if not f.link -%} |
| 70 | {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} ) |
| 71 | {% endif %} |
| 72 | {%- endfor %} |
| 73 | |
| 74 | # Relations |
| 75 | {% for l in m.links %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 76 | {{ 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] | 77 | {%- endfor %} |
| 78 | |
| 79 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 80 | pass |
| 81 | |
| 82 | {% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %} |
| 83 | {% endfor %} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 84 | """) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 85 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 86 | args_xproto = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 87 | args_xproto.inputs = xproto |
| 88 | args_xproto.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 89 | xproto_gen = XOSProcessor.process(args_xproto) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 90 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 91 | count1 = len(xproto_gen.split('\n')) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 92 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 93 | args_proto = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 94 | args_proto.inputs = proto |
| 95 | args_proto.target = target |
| 96 | args_proto.rev = True |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 97 | proto_gen = XOSProcessor.process(args_proto) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 98 | count2 = len(proto_gen.split('\n')) |
| 99 | |
| 100 | self.assertEqual(count1, count2) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 101 | |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 102 | def test_pure_policies(self): |
| 103 | xproto = \ |
| 104 | """ |
| 105 | policy my_policy < exists x:a=b > |
| 106 | """ |
| 107 | |
| 108 | proto = \ |
| 109 | """ |
| 110 | option my_policy = "policy:< exists x:a=b >"; |
| 111 | """ |
| 112 | target = XProtoTestHelpers.write_tmp_target( |
| 113 | """ |
| 114 | {{ policies }} |
| 115 | """) |
| 116 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 117 | args_xproto = XOSProcessorArgs() |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 118 | args_xproto.inputs = xproto |
| 119 | args_xproto.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 120 | xproto_gen = XOSProcessor.process(args_xproto) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 121 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 122 | args_proto = XOSProcessorArgs() |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 123 | args_proto.inputs = proto |
| 124 | args_proto.target = target |
| 125 | args_proto.rev = True |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 126 | proto_gen = XOSProcessor.process(args_proto) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 127 | |
| 128 | self.assertEqual(proto_gen, xproto_gen) |
| 129 | |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 130 | if __name__ == '__main__': |
| 131 | unittest.main() |
| 132 | |
| 133 | |