blob: f936113ec155f40136f74518bb4474915102bc17 [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
19from xosgenx.generator import XOSGenerator
20from helpers import FakeArgs
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020021import yaml
22
Matteo Scandolo67654fa2017-06-09 09:33:17 -070023PROTO_EXPECTED_OUTPUT = """
24message VRouterPort {
25 option bases = "XOSBase";
26 optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = ""port friendly name"", modifier = "optional", db_index = "False" ];
27 required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = ""port identifier in ONOS"", modifier = "required", db_index = "False" ];
28 required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", link_type = "manytoone", db_index = "True" ];
29 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 +020030}
31"""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070032VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020033
Matteo Scandolo67654fa2017-06-09 09:33:17 -070034# Generate other formats from xproto
35class XProtoTranslatorTest(unittest.TestCase):
36 def _test_proto_generator(self):
37 args = FakeArgs()
38 args.files = [VROUTER_XPROTO]
39 args.target = 'proto.xtarget'
40 output = XOSGenerator.generate(args)
41 self.assertEqual(output, PROTO_EXPECTED_OUTPUT)
42
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020043 def test_yaml_generator(self):
44 xproto = \
45"""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070046option app_label = "test";
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020047
48message Port (PlCoreBase,ParameterMixin){
49 required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
50 optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
51 optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
52 optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
53 optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
54 required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
55}
56
57message Instance (PlCoreBase){
58 optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
59 optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
60 required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
61 optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
62 optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
63 required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
64 optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
65 required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
66 required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
67 required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
68 required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
69 required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
70 optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
71 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];
72 optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
73 optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
74 required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
75}
76
77message Network (PlCoreBase,ParameterMixin) {
78 required string name = 1 [db_index = False, max_length = 32, null = False, blank = False];
79 required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False];
80 required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True];
81 required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True];
82 required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True];
83 optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True];
84 optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True];
85 required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False];
86 required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False];
87 required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True];
88 optional string topology_parameters = 11 [db_index = False, null = True, blank = True];
89 optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True];
90 optional string controller_parameters = 13 [db_index = False, null = True, blank = True];
91 optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True];
92 optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True];
93 optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True];
94 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];
95 required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True];
96 required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True];
97 required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True];
98}
99
100message Slice (PlCoreBase){
101 required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False];
102 required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True];
103 required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True];
104 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];
105 required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True];
106 required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False];
107 required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False];
108 optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True];
109 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'))"];
110 optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True];
111 optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True];
112 optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True];
113 optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True];
114 optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True];
115 optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True];
116 optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False];
117 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];
118 required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True];
119}
120"""
121
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700122 args = FakeArgs()
123 args.inputs = xproto
124 args.target = 'modeldefs.xtarget'
125 output = XOSGenerator.generate(args)
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200126
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200127 yaml_ir = yaml.load(output)
128 self.assertEqual(len(yaml_ir['items']), 4)
129
Matteo Scandolo53418592017-07-26 15:51:29 -0700130 def test_gui_hidden_models(self):
131 xproto = \
132"""
133option app_label = "test";
134
135message Foo {
136 option gui_hidden = "True";
137 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"""
145 args = FakeArgs()
146 args.inputs = xproto
147 args.target = 'modeldefs.xtarget'
148 output = XOSGenerator.generate(args)
149 yaml_ir = yaml.load(output)
150 self.assertEqual(len(yaml_ir['items']), 1)
151 self.assertIn('Bar', output)
152 self.assertNotIn('Foo', output)
153
154 def test_gui_hidden_model_fields(self):
155 xproto = \
156"""
157option 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"""
164 args = FakeArgs()
165 args.inputs = xproto
166 args.target = 'modeldefs.xtarget'
167 output = XOSGenerator.generate(args)
168 yaml_ir = yaml.load(output)
169 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):
174 xproto = \
175"""
176option app_label = "test";
177
178message Foo {
179 required string name = 1 [ null = "False", blank="False"];
180 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];
181}
182"""
183
184 args = FakeArgs()
185 args.inputs = xproto
186 args.target = 'modeldefs.xtarget'
187 output = XOSGenerator.generate(args)
188 self.assertIn("options:", output)
189 self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output)
190
191 def test_not_static_options(self):
192 xproto = \
193"""
194option app_label = "test";
195
196message Foo {
197 required string name = 1 [ null = "False", blank="False"];
198}
199"""
200
201 args = FakeArgs()
202 args.inputs = xproto
203 args.target = 'modeldefs.xtarget'
204 output = XOSGenerator.generate(args)
205 self.assertNotIn("options:", output)
206
207 def test_default_value_in_modeldef(self):
208 xproto = \
209"""
210option app_label = "test";
211
212message Foo {
213 required string name = 1 [ null = "False", blank="False", default = "bar"];
214 required string falsetrue = 1 [ null = "False", blank="False", default = False];
215 required string truefalse = 1 [ null = "False", blank="False", default = True];
216 required string some = 1 [ null = "False", blank="False", default = None];
217 required string zero = 1 [ null = "False", blank="False", default = 0];
218}
219"""
220
221 args = FakeArgs()
222 args.inputs = xproto
223 args.target = 'modeldefs.xtarget'
224 output = XOSGenerator.generate(args)
225 self.assertIn('default: "bar"', output)
226 self.assertIn('default: "false"', output)
227 self.assertIn('default: "true"', output)
228 self.assertIn('default: "null"', output)
229 self.assertIn('default: "0"', output)
230
231 def test_not_default_value_in_modeldef(self):
232 xproto = \
233"""
234option app_label = "test";
235
236message Foo {
237 required string name = 1 [ null = "False", blank="False"];
238}
239"""
240
241 args = FakeArgs()
242 args.inputs = xproto
243 args.target = 'modeldefs.xtarget'
244 output = XOSGenerator.generate(args)
245 self.assertNotIn('default:', output)
246
Matteo Scandolo1f826a42017-08-02 12:02:02 -0700247 def test_one_to_many_in_modeldef(self):
248 xproto = \
249"""
250option app_label = "test";
251
252message ServiceDependency {
253 required manytoone provider_service->Service:provided_dependencies = 1;
254 required manytoone subscriber_service->Service:subscribed_dependencies = 2;
255}
256
257message Service {
258 required string name = 1;
259}
260"""
261
262 args = FakeArgs()
263 args.inputs = xproto
264 args.target = 'modeldefs.xtarget'
265 output = XOSGenerator.generate(args)
266 # Service deps model
267 self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
268 self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
269
270 # Service model
271 self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
272 self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
273
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200274if __name__ == '__main__':
275 unittest.main()
276
277