blob: f98894bcee89681147a0666f96e5d122802610a5 [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)
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200128
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200129 yaml_ir = yaml.load(output)
Zack Williams045b63d2019-01-22 16:30:57 -0700130 self.assertEqual(len(yaml_ir["items"]), 4)
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200131
Matteo Scandolo53418592017-07-26 15:51:29 -0700132 def test_gui_hidden_models(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700133 xproto = """
Matteo Scandolo53418592017-07-26 15:51:29 -0700134option app_label = "test";
135
136message Foo {
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700137 option gui_hidden = True;
Matteo Scandolo53418592017-07-26 15:51:29 -0700138 required string name = 1 [ null = "False", blank="False"];
139}
140
141message Bar {
142 option gui_hidden = "False";
143 required string name = 1 [ null = "False", blank="False"];
144}
145"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700146 args = XOSProcessorArgs()
Matteo Scandolo53418592017-07-26 15:51:29 -0700147 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700148 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800149 output = XOSProcessor.process(args)
Matteo Scandolo53418592017-07-26 15:51:29 -0700150 yaml_ir = yaml.load(output)
Zack Williams045b63d2019-01-22 16:30:57 -0700151 self.assertEqual(len(yaml_ir["items"]), 1)
152 self.assertIn("Bar", output)
153 self.assertNotIn("Foo", output)
Matteo Scandolo53418592017-07-26 15:51:29 -0700154
155 def test_gui_hidden_model_fields(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700156 xproto = """
Matteo Scandolo53418592017-07-26 15:51:29 -0700157option app_label = "test";
158
159message Foo {
160 required string name = 1 [ null = "False", blank="False"];
161 required string secret = 1 [ null = "False", blank="False", gui_hidden = "True"];
162}
163"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700164 args = XOSProcessorArgs()
Matteo Scandolo53418592017-07-26 15:51:29 -0700165 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700166 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800167 output = XOSProcessor.process(args)
Matteo Scandolo53418592017-07-26 15:51:29 -0700168 yaml_ir = yaml.load(output)
Zack Williams045b63d2019-01-22 16:30:57 -0700169 self.assertEqual(len(yaml_ir["items"]), 1)
170 self.assertIn("name", output)
171 self.assertNotIn("secret", output)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700172
173 def test_static_options(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700174 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700175option app_label = "test";
176
177message Foo {
178 required string name = 1 [ null = "False", blank="False"];
179 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];
180}
181"""
182
Scott Baker1f7791d2018-10-04 13:21:20 -0700183 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700184 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700185 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800186 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700187 self.assertIn("options:", output)
188 self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output)
189
190 def test_not_static_options(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700191 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700192option app_label = "test";
193
194message Foo {
195 required string name = 1 [ null = "False", blank="False"];
196}
197"""
198
Scott Baker1f7791d2018-10-04 13:21:20 -0700199 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700200 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700201 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800202 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700203 self.assertNotIn("options:", output)
204
205 def test_default_value_in_modeldef(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700206 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700207option app_label = "test";
208
209message Foo {
210 required string name = 1 [ null = "False", blank="False", default = "bar"];
211 required string falsetrue = 1 [ null = "False", blank="False", default = False];
212 required string truefalse = 1 [ null = "False", blank="False", default = True];
213 required string some = 1 [ null = "False", blank="False", default = None];
214 required string zero = 1 [ null = "False", blank="False", default = 0];
215}
216"""
217
Scott Baker1f7791d2018-10-04 13:21:20 -0700218 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700219 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700220 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800221 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700222 self.assertIn('default: "bar"', output)
223 self.assertIn('default: "false"', output)
224 self.assertIn('default: "true"', output)
225 self.assertIn('default: "null"', output)
226 self.assertIn('default: "0"', output)
227
228 def test_not_default_value_in_modeldef(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700229 xproto = """
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700230option app_label = "test";
231
232message Foo {
233 required string name = 1 [ null = "False", blank="False"];
234}
235"""
236
Scott Baker1f7791d2018-10-04 13:21:20 -0700237 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700238 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700239 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800240 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700241 self.assertNotIn("default:", output)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700242
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700243 def test_one_to_many_in_modeldef(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700244 xproto = """
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700245option app_label = "test";
246
247message ServiceDependency {
248 required manytoone provider_service->Service:provided_dependencies = 1;
249 required manytoone subscriber_service->Service:subscribed_dependencies = 2;
250}
251
252message Service {
253 required string name = 1;
254}
255"""
256
Scott Baker1f7791d2018-10-04 13:21:20 -0700257 args = XOSProcessorArgs()
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700258 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700259 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800260 output = XOSProcessor.process(args)
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700261 # Service deps model
Zack Williams045b63d2019-01-22 16:30:57 -0700262 self.assertIn(
263 "{model: Service, type: manytoone, on_field: provider_service}", output
264 )
265 self.assertIn(
266 "{model: Service, type: manytoone, on_field: provider_service}", output
267 )
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700268
269 # Service model
Zack Williams045b63d2019-01-22 16:30:57 -0700270 self.assertIn(
271 "{model: ServiceDependency, type: onetomany, on_field: provider_service}",
272 output,
273 )
274 self.assertIn(
275 "{model: ServiceDependency, type: onetomany, on_field: provider_service}",
276 output,
277 )
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700278
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700279 def test_model_description(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700280 xproto = """
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700281option app_label = "test";
282
283message Foo {
284 option description="This is the Foo model";
285 required string name = 1 [ null = "False", blank="False"];
286 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];
287}
288
289message Bar {
290 required string name = 1;
291}
292"""
293
Scott Baker1f7791d2018-10-04 13:21:20 -0700294 args = XOSProcessorArgs()
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700295 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700296 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800297 output = XOSProcessor.process(args)
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700298 self.assertIn('description: "This is the Foo model"', output)
299
300 def test_model_verbose_name(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700301 xproto = """
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700302option app_label = "test";
303
304message Foo {
305 option verbose_name="Verbose Foo Name";
306 required string name = 1 [ null = "False", blank="False"];
307 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];
308}
309
310message Bar {
311 required string name = 1;
312}
313"""
314
Scott Baker1f7791d2018-10-04 13:21:20 -0700315 args = XOSProcessorArgs()
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700316 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700317 args.target = "modeldefs.xtarget"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800318 output = XOSProcessor.process(args)
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700319 self.assertIn('verbose_name: "Verbose Foo Name"', output)
320
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800321 def test_feedback_field(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700322 xproto = """
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800323option app_label = "test";
324
325message ParentFoo {
326 required string parent_name = 1 [null = False, blank = False, feedback_state = True];
327}
328
329message Foo (ParentFoo) {
330 required string name = 1 [null = False, blank = False, feedback_state = True];
331}
332"""
333
Scott Baker1f7791d2018-10-04 13:21:20 -0700334 args = XOSProcessorArgs()
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800335 args.inputs = xproto
Zack Williams045b63d2019-01-22 16:30:57 -0700336 args.target = "modeldefs.xtarget"
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800337 output = XOSProcessor.process(args)
338
Zack Williams9a42f872019-02-15 17:56:04 -0700339 read_only = [s for s in output.splitlines() if "read_only: True" in s]
Zack Williams045b63d2019-01-22 16:30:57 -0700340 self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800341
Zack Williams045b63d2019-01-22 16:30:57 -0700342
343if __name__ == "__main__":
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200344 unittest.main()