blob: fdf1fe7e3ddd9fe2b92809544ae4cf5d0f02923f [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# 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 Williams9a42f872019-02-15 17:56:04 -070016from __future__ import absolute_import
Matteo Scandolo67654fa2017-06-09 09:33:17 -070017import unittest
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
19from helpers import XProtoTestHelpers
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020020
21# Generate from xproto, then generate from equivalent proto
Zack Williams045b63d2019-01-22 16:30:57 -070022
23
Matteo Scandolo67654fa2017-06-09 09:33:17 -070024class XPureProtobufGenerator(unittest.TestCase):
Sapan Bhatia4c835602017-07-14 01:13:17 -040025 def test_pure_proto(self):
Zack Williams045b63d2019-01-22 16:30:57 -070026 xproto = """
Sapan Bhatia4e80a262017-05-19 23:10:51 +020027message VRouterPort (XOSBase){
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020028 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 Williams045b63d2019-01-22 16:30:57 -070035 proto = """
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020036message VRouterPort {
Sapan Bhatia4e80a262017-05-19 23:10:51 +020037 option bases = "XOSBase";
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020038 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 Williams045b63d2019-01-22 16:30:57 -070044 target = XProtoTestHelpers.write_tmp_target(
45 """
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020046from 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 Bhatia3cfdf632017-06-08 05:14:03 +020053{% if l.peer.name != m.name %}
54from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020055{% endif %}
56
57{%- endfor %}
58{% for b in m.bases %}
Sapan Bhatia4e80a262017-05-19 23:10:51 +020059{% if b!='XOSBase' and 'Mixin' not in b%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020060from core.models.{{b.name | lower}} import {{ b.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020061{% endif %}
62{% endfor %}
63
64
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020065class {{ m.name }}{{ xproto_base_def(m, m.bases) }}:
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020066 # 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 Bhatia3cfdf632017-06-08 05:14:03 +020075 {{ 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 +020076 {%- 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 Williams045b63d2019-01-22 16:30:57 -070083"""
84 )
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020085
Zack Williams045b63d2019-01-22 16:30:57 -070086 args_xproto = XOSProcessorArgs()
87 args_xproto.inputs = xproto
88 args_xproto.target = target
89 xproto_gen = XOSProcessor.process(args_xproto)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020090
Zack Williams045b63d2019-01-22 16:30:57 -070091 count1 = len(xproto_gen.split("\n"))
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020092
Zack Williams045b63d2019-01-22 16:30:57 -070093 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 Scandolo67654fa2017-06-09 09:33:17 -070099
Zack Williams045b63d2019-01-22 16:30:57 -0700100 self.assertEqual(count1, count2)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +0200101
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400102 def test_pure_policies(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700103 xproto = """
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400104policy my_policy < exists x:a=b >
105"""
106
Zack Williams045b63d2019-01-22 16:30:57 -0700107 proto = """
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400108option my_policy = "policy:< exists x:a=b >";
109"""
Zack Williams045b63d2019-01-22 16:30:57 -0700110 target = XProtoTestHelpers.write_tmp_target(
111 """
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400112{{ policies }}
Zack Williams045b63d2019-01-22 16:30:57 -0700113"""
114 )
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400115
Zack Williams045b63d2019-01-22 16:30:57 -0700116 args_xproto = XOSProcessorArgs()
117 args_xproto.inputs = xproto
118 args_xproto.target = target
119 xproto_gen = XOSProcessor.process(args_xproto)
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400120
Zack Williams045b63d2019-01-22 16:30:57 -0700121 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 Bhatiaea6ff752017-07-14 01:52:18 -0400126
Zack Williams045b63d2019-01-22 16:30:57 -0700127 self.assertEqual(proto_gen, xproto_gen)
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400128
Zack Williams045b63d2019-01-22 16:30:57 -0700129
130if __name__ == "__main__":
Sapan Bhatia3d61cf82017-05-14 23:59:23 +0200131 unittest.main()