Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 19 | from helpers import XProtoTestHelpers |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 20 | |
| 21 | # Generate from xproto, then generate from equivalent proto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 22 | |
| 23 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 24 | class XPureProtobufGenerator(unittest.TestCase): |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 25 | def test_pure_proto(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 26 | xproto = """ |
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 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 35 | proto = """ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 36 | message VRouterPort { |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 37 | option bases = "XOSBase"; |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 38 | optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = "port friendly name", modifier = "optional", db_index = "False" ]; |
| 39 | required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = "port identifier in ONOS", modifier = "required", db_index = "False" ]; |
| 40 | required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", db_index = "True", link = "manytoone"]; |
| 41 | required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", db_index = "True", link = "manytoone"]; |
| 42 | } |
| 43 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 44 | target = XProtoTestHelpers.write_tmp_target( |
| 45 | """ |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 46 | from header import * |
| 47 | {% for m in proto.messages %} |
| 48 | {% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{% endif %} |
| 49 | {% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %} |
| 50 | |
| 51 | {%- for l in m.links %} |
| 52 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 53 | {% if l.peer.name != m.name %} |
| 54 | from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 55 | {% endif %} |
| 56 | |
| 57 | {%- endfor %} |
| 58 | {% for b in m.bases %} |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 59 | {% if b!='XOSBase' and 'Mixin' not in b%} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 60 | from core.models.{{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 61 | {% endif %} |
| 62 | {% endfor %} |
| 63 | |
| 64 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 65 | class {{ m.name }}{{ xproto_base_def(m, m.bases) }}: |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 66 | # Primitive Fields (Not Relations) |
| 67 | {% for f in m.fields %} |
| 68 | {%- if not f.link -%} |
| 69 | {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} ) |
| 70 | {% endif %} |
| 71 | {%- endfor %} |
| 72 | |
| 73 | # Relations |
| 74 | {% for l in m.links %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 75 | {{ 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] | 76 | {%- endfor %} |
| 77 | |
| 78 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 79 | pass |
| 80 | |
| 81 | {% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %} |
| 82 | {% endfor %} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 83 | """ |
| 84 | ) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 85 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | args_xproto = XOSProcessorArgs() |
| 87 | args_xproto.inputs = xproto |
| 88 | args_xproto.target = target |
| 89 | xproto_gen = XOSProcessor.process(args_xproto) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 90 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 91 | count1 = len(xproto_gen.split("\n")) |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 92 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 93 | args_proto = XOSProcessorArgs() |
| 94 | args_proto.inputs = proto |
| 95 | args_proto.target = target |
| 96 | args_proto.rev = True |
| 97 | proto_gen = XOSProcessor.process(args_proto) |
| 98 | count2 = len(proto_gen.split("\n")) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 99 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 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): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 103 | xproto = """ |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 104 | policy my_policy < exists x:a=b > |
| 105 | """ |
| 106 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 107 | proto = """ |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 108 | option my_policy = "policy:< exists x:a=b >"; |
| 109 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 110 | target = XProtoTestHelpers.write_tmp_target( |
| 111 | """ |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 112 | {{ policies }} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 113 | """ |
| 114 | ) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 115 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 116 | args_xproto = XOSProcessorArgs() |
| 117 | args_xproto.inputs = xproto |
| 118 | args_xproto.target = target |
| 119 | xproto_gen = XOSProcessor.process(args_xproto) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 120 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 121 | args_proto = XOSProcessorArgs() |
| 122 | args_proto.inputs = proto |
| 123 | args_proto.target = target |
| 124 | args_proto.rev = True |
| 125 | proto_gen = XOSProcessor.process(args_proto) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 126 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 127 | self.assertEqual(proto_gen, xproto_gen) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 128 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 129 | |
| 130 | if __name__ == "__main__": |
Sapan Bhatia | 3d61cf8 | 2017-05-14 23:59:23 +0200 | [diff] [blame] | 131 | unittest.main() |