blob: c4f680d15cd5c13eefd3bd333afe5244770acd1e [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
Matteo Scandolo67654fa2017-06-09 09:33:17 -070016import unittest
Scott Baker1f7791d2018-10-04 13:21:20 -070017from xosgenx.generator import XOSProcessor, XOSProcessorArgs
18from helpers import XProtoTestHelpers
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020019
20# Generate from xproto, then generate from equivalent proto
Zack Williams045b63d2019-01-22 16:30:57 -070021
22
Matteo Scandolo67654fa2017-06-09 09:33:17 -070023class XPureProtobufGenerator(unittest.TestCase):
Sapan Bhatia4c835602017-07-14 01:13:17 -040024 def test_pure_proto(self):
Zack Williams045b63d2019-01-22 16:30:57 -070025 xproto = """
Sapan Bhatia4e80a262017-05-19 23:10:51 +020026message VRouterPort (XOSBase){
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020027 optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True];
28 required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
29 required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False];
30 required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
31}
32"""
33
Zack Williams045b63d2019-01-22 16:30:57 -070034 proto = """
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020035message VRouterPort {
Sapan Bhatia4e80a262017-05-19 23:10:51 +020036 option bases = "XOSBase";
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020037 optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = "port friendly name", modifier = "optional", db_index = "False" ];
38 required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = "port identifier in ONOS", modifier = "required", db_index = "False" ];
39 required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", db_index = "True", link = "manytoone"];
40 required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", db_index = "True", link = "manytoone"];
41}
42"""
Zack Williams045b63d2019-01-22 16:30:57 -070043 target = XProtoTestHelpers.write_tmp_target(
44 """
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020045from header import *
46{% for m in proto.messages %}
47{% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{% endif %}
48{% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %}
49
50{%- for l in m.links %}
51
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020052{% if l.peer.name != m.name %}
53from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020054{% endif %}
55
56{%- endfor %}
57{% for b in m.bases %}
Sapan Bhatia4e80a262017-05-19 23:10:51 +020058{% if b!='XOSBase' and 'Mixin' not in b%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020059from core.models.{{b.name | lower}} import {{ b.name }}
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020060{% endif %}
61{% endfor %}
62
63
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020064class {{ m.name }}{{ xproto_base_def(m, m.bases) }}:
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020065 # Primitive Fields (Not Relations)
66 {% for f in m.fields %}
67 {%- if not f.link -%}
68 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
69 {% endif %}
70 {%- endfor %}
71
72 # Relations
73 {% for l in m.links %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020074 {{ 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 +020075 {%- endfor %}
76
77 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
78 pass
79
80{% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %}
81{% endfor %}
Zack Williams045b63d2019-01-22 16:30:57 -070082"""
83 )
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020084
Zack Williams045b63d2019-01-22 16:30:57 -070085 args_xproto = XOSProcessorArgs()
86 args_xproto.inputs = xproto
87 args_xproto.target = target
88 xproto_gen = XOSProcessor.process(args_xproto)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020089
Zack Williams045b63d2019-01-22 16:30:57 -070090 count1 = len(xproto_gen.split("\n"))
Sapan Bhatia3d61cf82017-05-14 23:59:23 +020091
Zack Williams045b63d2019-01-22 16:30:57 -070092 args_proto = XOSProcessorArgs()
93 args_proto.inputs = proto
94 args_proto.target = target
95 args_proto.rev = True
96 proto_gen = XOSProcessor.process(args_proto)
97 count2 = len(proto_gen.split("\n"))
Matteo Scandolo67654fa2017-06-09 09:33:17 -070098
Zack Williams045b63d2019-01-22 16:30:57 -070099 self.assertEqual(count1, count2)
Sapan Bhatia3d61cf82017-05-14 23:59:23 +0200100
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400101 def test_pure_policies(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700102 xproto = """
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400103policy my_policy < exists x:a=b >
104"""
105
Zack Williams045b63d2019-01-22 16:30:57 -0700106 proto = """
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400107option my_policy = "policy:< exists x:a=b >";
108"""
Zack Williams045b63d2019-01-22 16:30:57 -0700109 target = XProtoTestHelpers.write_tmp_target(
110 """
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400111{{ policies }}
Zack Williams045b63d2019-01-22 16:30:57 -0700112"""
113 )
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400114
Zack Williams045b63d2019-01-22 16:30:57 -0700115 args_xproto = XOSProcessorArgs()
116 args_xproto.inputs = xproto
117 args_xproto.target = target
118 xproto_gen = XOSProcessor.process(args_xproto)
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400119
Zack Williams045b63d2019-01-22 16:30:57 -0700120 args_proto = XOSProcessorArgs()
121 args_proto.inputs = proto
122 args_proto.target = target
123 args_proto.rev = True
124 proto_gen = XOSProcessor.process(args_proto)
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400125
Zack Williams045b63d2019-01-22 16:30:57 -0700126 self.assertEqual(proto_gen, xproto_gen)
Sapan Bhatiaea6ff752017-07-14 01:52:18 -0400127
Zack Williams045b63d2019-01-22 16:30:57 -0700128
129if __name__ == "__main__":
Sapan Bhatia3d61cf82017-05-14 23:59:23 +0200130 unittest.main()