blob: 511682edfb9b095c03c4b59f3a6aa7474594dabb [file] [log] [blame]
Matteo Scandolo67654fa2017-06-09 09:33:17 -07001
Matteo Scandolod2044a42017-08-07 16:08:28 -07002# 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 Scandolo67654fa2017-06-09 09:33:17 -070018import unittest
19from xosgenx.generator import XOSGenerator
20from helpers import FakeArgs, XProtoTestHelpers
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020021
22# Generate from xproto, then generate from equivalent proto
Matteo Scandolo67654fa2017-06-09 09:33:17 -070023class XPureProtobufGenerator(unittest.TestCase):
Sapan Bhatia4c835602017-07-14 01:13:17 -040024 def test_pure_proto(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070025 xproto = \
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020026"""
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
Matteo Scandolo67654fa2017-06-09 09:33:17 -070035 proto = \
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020036"""
37message VRouterPort {
Sapan Bhatia4e80a262017-05-19 23:10:51 +020038 option bases = "XOSBase";
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020039 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 Scandolo67654fa2017-06-09 09:33:17 -070045 target = XProtoTestHelpers.write_tmp_target(
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020046"""
47from 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 Bhatia3cfdf632017-06-08 05:14:03 +020054{% if l.peer.name != m.name %}
55from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020056{% endif %}
57
58{%- endfor %}
59{% for b in m.bases %}
Sapan Bhatia4e80a262017-05-19 23:10:51 +020060{% if b!='XOSBase' and 'Mixin' not in b%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020061from core.models.{{b.name | lower}} import {{ b.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020062{% endif %}
63{% endfor %}
64
65
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020066class {{ m.name }}{{ xproto_base_def(m, m.bases) }}:
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020067 # 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 Bhatia3cfdf632017-06-08 05:14:03 +020076 {{ 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 +020077 {%- 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 Scandolo67654fa2017-06-09 09:33:17 -070084""")
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020085
Matteo Scandolo67654fa2017-06-09 09:33:17 -070086 args_xproto = FakeArgs()
87 args_xproto.inputs = xproto
88 args_xproto.target = target
89 xproto_gen = XOSGenerator.generate(args_xproto)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020090
Matteo Scandolo67654fa2017-06-09 09:33:17 -070091 count1 = len(xproto_gen.split('\n'))
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020092
Matteo Scandolo67654fa2017-06-09 09:33:17 -070093 args_proto = FakeArgs()
94 args_proto.inputs = proto
95 args_proto.target = target
96 args_proto.rev = True
97 proto_gen = XOSGenerator.generate(args_proto)
98 count2 = len(proto_gen.split('\n'))
99
100 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):
103 xproto = \
104"""
105policy my_policy < exists x:a=b >
106"""
107
108 proto = \
109"""
110option my_policy = "policy:< exists x:a=b >";
111"""
112 target = XProtoTestHelpers.write_tmp_target(
113"""
114{{ policies }}
115""")
116
117 args_xproto = FakeArgs()
118 args_xproto.inputs = xproto
119 args_xproto.target = target
120 xproto_gen = XOSGenerator.generate(args_xproto)
121
122 args_proto = FakeArgs()
123 args_proto.inputs = proto
124 args_proto.target = target
125 args_proto.rev = True
126 proto_gen = XOSGenerator.generate(args_proto)
127
128 self.assertEqual(proto_gen, xproto_gen)
129
Sapan Bhatia3d61cf82017-05-14 23:59:23 +0200130if __name__ == '__main__':
131 unittest.main()
132
133