blob: f3476cddbac223e2fefea56a3e0c051bb48dfcf5 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# 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
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"""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070031VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020032
Matteo Scandolo67654fa2017-06-09 09:33:17 -070033# Generate other formats from xproto
34class XProtoTranslatorTest(unittest.TestCase):
35 def _test_proto_generator(self):
Scott Baker1f7791d2018-10-04 13:21:20 -070036 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070037 args.files = [VROUTER_XPROTO]
38 args.target = 'proto.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080039 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070040 self.assertEqual(output, PROTO_EXPECTED_OUTPUT)
41
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020042 def test_yaml_generator(self):
43 xproto = \
44"""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070045option app_label = "test";
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020046
47message Port (PlCoreBase,ParameterMixin){
48 required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
49 optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
50 optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
51 optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
52 optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
53 required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
54}
55
56message Instance (PlCoreBase){
57 optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
58 optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
59 required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
60 optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
61 optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
62 required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
63 optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
64 required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
65 required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
66 required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
67 required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
68 required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
69 optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
70 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];
71 optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
72 optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
73 required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
74}
75
76message Network (PlCoreBase,ParameterMixin) {
77 required string name = 1 [db_index = False, max_length = 32, null = False, blank = False];
78 required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False];
79 required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True];
80 required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True];
81 required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True];
82 optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True];
83 optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True];
84 required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False];
85 required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False];
86 required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True];
87 optional string topology_parameters = 11 [db_index = False, null = True, blank = True];
88 optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True];
89 optional string controller_parameters = 13 [db_index = False, null = True, blank = True];
90 optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True];
91 optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True];
92 optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True];
93 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];
94 required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True];
95 required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True];
96 required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True];
97}
98
99message Slice (PlCoreBase){
100 required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False];
101 required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True];
102 required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True];
103 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];
104 required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True];
105 required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False];
106 required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False];
107 optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True];
108 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'))"];
109 optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True];
110 optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True];
111 optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True];
112 optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True];
113 optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True];
114 optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True];
115 optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False];
116 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];
117 required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True];
118}
119"""
120
Scott Baker1f7791d2018-10-04 13:21:20 -0700121 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700122 args.inputs = xproto
123 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800124 output = XOSProcessor.process(args)
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200125
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200126 yaml_ir = yaml.load(output)
127 self.assertEqual(len(yaml_ir['items']), 4)
128
Matteo Scandolo53418592017-07-26 15:51:29 -0700129 def test_gui_hidden_models(self):
130 xproto = \
131"""
132option app_label = "test";
133
134message Foo {
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700135 option gui_hidden = True;
Matteo Scandolo53418592017-07-26 15:51:29 -0700136 required string name = 1 [ null = "False", blank="False"];
137}
138
139message Bar {
140 option gui_hidden = "False";
141 required string name = 1 [ null = "False", blank="False"];
142}
143"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700144 args = XOSProcessorArgs()
Matteo Scandolo53418592017-07-26 15:51:29 -0700145 args.inputs = xproto
146 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800147 output = XOSProcessor.process(args)
Matteo Scandolo53418592017-07-26 15:51:29 -0700148 yaml_ir = yaml.load(output)
149 self.assertEqual(len(yaml_ir['items']), 1)
150 self.assertIn('Bar', output)
151 self.assertNotIn('Foo', output)
152
153 def test_gui_hidden_model_fields(self):
154 xproto = \
155"""
156option 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
165 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800166 output = XOSProcessor.process(args)
Matteo Scandolo53418592017-07-26 15:51:29 -0700167 yaml_ir = yaml.load(output)
168 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):
173 xproto = \
174"""
175option 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
185 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):
191 xproto = \
192"""
193option app_label = "test";
194
195message Foo {
196 required string name = 1 [ null = "False", blank="False"];
197}
198"""
199
Scott Baker1f7791d2018-10-04 13:21:20 -0700200 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700201 args.inputs = xproto
202 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800203 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700204 self.assertNotIn("options:", output)
205
206 def test_default_value_in_modeldef(self):
207 xproto = \
208"""
209option app_label = "test";
210
211message Foo {
212 required string name = 1 [ null = "False", blank="False", default = "bar"];
213 required string falsetrue = 1 [ null = "False", blank="False", default = False];
214 required string truefalse = 1 [ null = "False", blank="False", default = True];
215 required string some = 1 [ null = "False", blank="False", default = None];
216 required string zero = 1 [ null = "False", blank="False", default = 0];
217}
218"""
219
Scott Baker1f7791d2018-10-04 13:21:20 -0700220 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700221 args.inputs = xproto
222 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800223 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700224 self.assertIn('default: "bar"', output)
225 self.assertIn('default: "false"', output)
226 self.assertIn('default: "true"', output)
227 self.assertIn('default: "null"', output)
228 self.assertIn('default: "0"', output)
229
230 def test_not_default_value_in_modeldef(self):
231 xproto = \
232"""
233option app_label = "test";
234
235message Foo {
236 required string name = 1 [ null = "False", blank="False"];
237}
238"""
239
Scott Baker1f7791d2018-10-04 13:21:20 -0700240 args = XOSProcessorArgs()
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700241 args.inputs = xproto
242 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800243 output = XOSProcessor.process(args)
Matteo Scandolo292cc2a2017-07-31 19:02:12 -0700244 self.assertNotIn('default:', output)
245
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700246 def test_one_to_many_in_modeldef(self):
247 xproto = \
248"""
249option app_label = "test";
250
251message ServiceDependency {
252 required manytoone provider_service->Service:provided_dependencies = 1;
253 required manytoone subscriber_service->Service:subscribed_dependencies = 2;
254}
255
256message Service {
257 required string name = 1;
258}
259"""
260
Scott Baker1f7791d2018-10-04 13:21:20 -0700261 args = XOSProcessorArgs()
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700262 args.inputs = xproto
263 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800264 output = XOSProcessor.process(args)
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700265 # Service deps model
266 self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
267 self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
268
269 # Service model
270 self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
271 self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
272
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700273 def test_model_description(self):
274 xproto = \
275"""
276option app_label = "test";
277
278message Foo {
279 option description="This is the Foo model";
280 required string name = 1 [ null = "False", blank="False"];
281 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];
282}
283
284message Bar {
285 required string name = 1;
286}
287"""
288
Scott Baker1f7791d2018-10-04 13:21:20 -0700289 args = XOSProcessorArgs()
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700290 args.inputs = xproto
291 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800292 output = XOSProcessor.process(args)
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700293 self.assertIn('description: "This is the Foo model"', output)
294
295 def test_model_verbose_name(self):
296 xproto = \
297"""
298option app_label = "test";
299
300message Foo {
301 option verbose_name="Verbose Foo Name";
302 required string name = 1 [ null = "False", blank="False"];
303 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];
304}
305
306message Bar {
307 required string name = 1;
308}
309"""
310
Scott Baker1f7791d2018-10-04 13:21:20 -0700311 args = XOSProcessorArgs()
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700312 args.inputs = xproto
313 args.target = 'modeldefs.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800314 output = XOSProcessor.process(args)
Matteo Scandoloe425f9d2017-08-15 15:56:19 -0700315 self.assertIn('verbose_name: "Verbose Foo Name"', output)
316
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800317 def test_feedback_field(self):
318 xproto = \
319"""
320option app_label = "test";
321
322message ParentFoo {
323 required string parent_name = 1 [null = False, blank = False, feedback_state = True];
324}
325
326message Foo (ParentFoo) {
327 required string name = 1 [null = False, blank = False, feedback_state = True];
328}
329"""
330
Scott Baker1f7791d2018-10-04 13:21:20 -0700331 args = XOSProcessorArgs()
Matteo Scandolo23cf15f2018-03-06 18:12:36 -0800332 args.inputs = xproto
333 args.target = 'modeldefs.xtarget'
334 output = XOSProcessor.process(args)
335
336 read_only = filter(lambda s: 'read_only: True' in s, output.splitlines())
337 self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo
338
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200339if __name__ == '__main__':
340 unittest.main()
341
342