blob: ae4289abdf8f8ffcdec386a6a854584fb39e3287 [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
18import os
Scott Baker1f7791d2018-10-04 13:21:20 -070019from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020020import yaml
21
Matteo Scandolo67654fa2017-06-09 09:33:17 -070022PROTO_EXPECTED_OUTPUT = """
23message VRouterPort {
24 option bases = "XOSBase";
25 optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = ""port friendly name"", modifier = "optional", db_index = "False" ];
26 required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = ""port identifier in ONOS"", modifier = "required", db_index = "False" ];
27 required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", link_type = "manytoone", db_index = "True" ];
28 required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", link_type = "manytoone", db_index = "True" ];
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020029}
30"""
Zack Williams045b63d2019-01-22 16:30:57 -070031VROUTER_XPROTO = os.path.abspath(
32 os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto"
33)
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020034
Matteo Scandolo67654fa2017-06-09 09:33:17 -070035# Generate other formats from xproto
Zack Williams045b63d2019-01-22 16:30:57 -070036
37
Matteo Scandolo67654fa2017-06-09 09:33:17 -070038class XProtoTranslatorTest(unittest.TestCase):
39 def _test_proto_generator(self):
Scott Baker1f7791d2018-10-04 13:21:20 -070040 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070041 args.files = [VROUTER_XPROTO]
Zack Williams045b63d2019-01-22 16:30:57 -070042 args.target = "proto.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080043 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070044 self.assertEqual(output, PROTO_EXPECTED_OUTPUT)
45
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020046 def test_yaml_generator(self):
Zack Williams045b63d2019-01-22 16:30:57 -070047 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070048option app_label = "test";
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020049
50message Port (PlCoreBase,ParameterMixin){
51 required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
52 optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
53 optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
54 optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
55 optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
56 required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
57}
58
59message Instance (PlCoreBase){
60 optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
61 optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
62 required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
63 optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
64 optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
65 required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
66 optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
67 required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
68 required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
69 required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
70 required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
71 required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
72 optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
73 required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False];
74 optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
75 optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
76 required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
77}
78
79message Network (PlCoreBase,ParameterMixin) {
80 required string name = 1 [db_index = False, max_length = 32, null = False, blank = False];
81 required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False];
82 required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True];
83 required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True];
84 required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True];
85 optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True];
86 optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True];
87 required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False];
88 required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False];
89 required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True];
90 optional string topology_parameters = 11 [db_index = False, null = True, blank = True];
91 optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True];
92 optional string controller_parameters = 13 [db_index = False, null = True, blank = True];
93 optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True];
94 optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True];
95 optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True];
96 required bool autoconnect = 17 [help_text = "This network can be autoconnected to the slice that owns it", default = True, null = False, db_index = False, blank = True];
97 required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True];
98 required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True];
99 required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True];
100}
101
102message Slice (PlCoreBase){
103 required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False];
104 required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True];
105 required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True];
106 required string description = 4 [help_text = "High level description of the slice and expected activities", max_length = 1024, null = False, db_index = False, blank = True];
107 required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True];
108 required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False];
109 required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False];
110 optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True];
111 optional string network = 9 [blank = True, max_length = 256, null = True, db_index = False, choices = "((None, 'Default'), ('host', 'Host'), ('bridged', 'Bridged'), ('noauto', 'No Automatic Networks'))"];
112 optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True];
113 optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True];
114 optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True];
115 optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True];
116 optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True];
117 optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True];
118 optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False];
119 required string default_isolation = 17 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False];
120 required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True];
121}
122"""
123
Scott Baker1f7791d2018-10-04 13:21:20 -0700124 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700125 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700126 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800127 output = XOSProcessor.process(args)
Zack Williamsbe5ee1c2019-03-18 15:33:07 -0700128 yaml_ir = yaml.safe_load(output)
Zack Williams045b63d2019-01-22 16:30:57 -0700129 self.assertEqual(len(yaml_ir["items"]), 4)
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200130
Matteo Scandolo53418592017-07-26 15:51:29 -0700131 def test_gui_hidden_models(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700132 xproto = """
Matteo Scandolo53418592017-07-26 15:51:29 -0700133option app_label = "test";
134
135message Foo {
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700136 option gui_hidden = True;
Matteo Scandolo53418592017-07-26 15:51:29 -0700137 required string name = 1 [ null = "False", blank="False"];
138}
139
140message Bar {
141 option gui_hidden = "False";
142 required string name = 1 [ null = "False", blank="False"];
143}
144"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700145 args = XOSProcessorArgs()
Matteo Scandolo53418592017-07-26 15:51:29 -0700146 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700147 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800148 output = XOSProcessor.process(args)
Zack Williamsbe5ee1c2019-03-18 15:33:07 -0700149 yaml_ir = yaml.safe_load(output)
Zack Williams045b63d2019-01-22 16:30:57 -0700150 self.assertEqual(len(yaml_ir["items"]), 1)
151 self.assertIn("Bar", output)
152 self.assertNotIn("Foo", output)
Matteo Scandolo53418592017-07-26 15:51:29 -0700153
154 def test_gui_hidden_model_fields(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700155 xproto = """
Matteo Scandolo53418592017-07-26 15:51:29 -0700156option app_label = "test";
157
158message Foo {
159 required string name = 1 [ null = "False", blank="False"];
160 required string secret = 1 [ null = "False", blank="False", gui_hidden = "True"];
161}
162"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700163 args = XOSProcessorArgs()
Matteo Scandolo53418592017-07-26 15:51:29 -0700164 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700165 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800166 output = XOSProcessor.process(args)
Zack Williamsbe5ee1c2019-03-18 15:33:07 -0700167 yaml_ir = yaml.safe_load(output)
Zack Williams045b63d2019-01-22 16:30:57 -0700168 self.assertEqual(len(yaml_ir["items"]), 1)
169 self.assertIn("name", output)
170 self.assertNotIn("secret", output)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700171
172 def test_static_options(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700173 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700174option app_label = "test";
175
176message Foo {
177 required string name = 1 [ null = "False", blank="False"];
178 required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False];
179}
180"""
181
Scott Baker1f7791d2018-10-04 13:21:20 -0700182 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700183 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700184 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800185 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700186 self.assertIn("options:", output)
187 self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output)
188
189 def test_not_static_options(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700190 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700191option app_label = "test";
192
193message Foo {
194 required string name = 1 [ null = "False", blank="False"];
195}
196"""
197
Scott Baker1f7791d2018-10-04 13:21:20 -0700198 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700199 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700200 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800201 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700202 self.assertNotIn("options:", output)
203
204 def test_default_value_in_modeldef(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700205 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700206option app_label = "test";
207
208message Foo {
209 required string name = 1 [ null = "False", blank="False", default = "bar"];
210 required string falsetrue = 1 [ null = "False", blank="False", default = False];
211 required string truefalse = 1 [ null = "False", blank="False", default = True];
212 required string some = 1 [ null = "False", blank="False", default = None];
213 required string zero = 1 [ null = "False", blank="False", default = 0];
214}
215"""
216
Scott Baker1f7791d2018-10-04 13:21:20 -0700217 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700218 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700219 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800220 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700221 self.assertIn('default: "bar"', output)
222 self.assertIn('default: "false"', output)
223 self.assertIn('default: "true"', output)
224 self.assertIn('default: "null"', output)
225 self.assertIn('default: "0"', output)
226
227 def test_not_default_value_in_modeldef(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700228 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700229option app_label = "test";
230
231message Foo {
232 required string name = 1 [ null = "False", blank="False"];
233}
234"""
235
Scott Baker1f7791d2018-10-04 13:21:20 -0700236 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700237 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700238 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800239 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700240 self.assertNotIn("default:", output)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700241
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700242 def test_one_to_many_in_modeldef(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700243 xproto = """
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700244option app_label = "test";
245
246message ServiceDependency {
247 required manytoone provider_service->Service:provided_dependencies = 1;
248 required manytoone subscriber_service->Service:subscribed_dependencies = 2;
249}
250
251message Service {
252 required string name = 1;
253}
254"""
255
Scott Baker1f7791d2018-10-04 13:21:20 -0700256 args = XOSProcessorArgs()
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700257 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700258 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800259 output = XOSProcessor.process(args)
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700260 # Service deps model
Zack Williams045b63d2019-01-22 16:30:57 -0700261 self.assertIn(
262 "{model: Service, type: manytoone, on_field: provider_service}", output
263 )
264 self.assertIn(
265 "{model: Service, type: manytoone, on_field: provider_service}", output
266 )
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700267
268 # Service model
Zack Williams045b63d2019-01-22 16:30:57 -0700269 self.assertIn(
270 "{model: ServiceDependency, type: onetomany, on_field: provider_service}",
271 output,
272 )
273 self.assertIn(
274 "{model: ServiceDependency, type: onetomany, on_field: provider_service}",
275 output,
276 )
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700277
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700278 def test_model_description(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700279 xproto = """
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700280option app_label = "test";
281
282message Foo {
283 option description="This is the Foo model";
284 required string name = 1 [ null = "False", blank="False"];
285 required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False];
286}
287
288message Bar {
289 required string name = 1;
290}
291"""
292
Scott Baker1f7791d2018-10-04 13:21:20 -0700293 args = XOSProcessorArgs()
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700294 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700295 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800296 output = XOSProcessor.process(args)
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700297 self.assertIn('description: "This is the Foo model"', output)
298
299 def test_model_verbose_name(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700300 xproto = """
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700301option app_label = "test";
302
303message Foo {
304 option verbose_name="Verbose Foo Name";
305 required string name = 1 [ null = "False", blank="False"];
306 required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False];
307}
308
309message Bar {
310 required string name = 1;
311}
312"""
313
Scott Baker1f7791d2018-10-04 13:21:20 -0700314 args = XOSProcessorArgs()
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700315 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700316 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800317 output = XOSProcessor.process(args)
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700318 self.assertIn('verbose_name: "Verbose Foo Name"', output)
319
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800320 def test_feedback_field(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700321 xproto = """
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800322option app_label = "test";
323
324message ParentFoo {
325 required string parent_name = 1 [null = False, blank = False, feedback_state = True];
326}
327
328message Foo (ParentFoo) {
329 required string name = 1 [null = False, blank = False, feedback_state = True];
330}
331"""
332
Scott Baker1f7791d2018-10-04 13:21:20 -0700333 args = XOSProcessorArgs()
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800334 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700335 args.target = "modeldefs.xtarget"
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800336 output = XOSProcessor.process(args)
337
Zack Williamsbe5ee1c2019-03-18 15:33:07 -0700338 read_only = [s for s in output.splitlines() if "read_only: true" in s]
Zack Williams045b63d2019-01-22 16:30:57 -0700339 self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800340
Zack Williams045b63d2019-01-22 16:30:57 -0700341
342if __name__ == "__main__":
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200343 unittest.main()