Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 1 | import unittest |
| 2 | import os |
| 3 | from xosgenx.generator import XOSGenerator |
| 4 | from helpers import FakeArgs |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 5 | import yaml |
| 6 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 7 | PROTO_EXPECTED_OUTPUT = """ |
| 8 | message VRouterPort { |
| 9 | option bases = "XOSBase"; |
| 10 | optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = ""port friendly name"", modifier = "optional", db_index = "False" ]; |
| 11 | required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = ""port identifier in ONOS"", modifier = "required", db_index = "False" ]; |
| 12 | required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", link_type = "manytoone", db_index = "True" ]; |
| 13 | 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 Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 14 | } |
| 15 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 16 | VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto") |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 17 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 18 | # Generate other formats from xproto |
| 19 | class XProtoTranslatorTest(unittest.TestCase): |
| 20 | def _test_proto_generator(self): |
| 21 | args = FakeArgs() |
| 22 | args.files = [VROUTER_XPROTO] |
| 23 | args.target = 'proto.xtarget' |
| 24 | output = XOSGenerator.generate(args) |
| 25 | self.assertEqual(output, PROTO_EXPECTED_OUTPUT) |
| 26 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 27 | def test_yaml_generator(self): |
| 28 | xproto = \ |
| 29 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 30 | option app_label = "test"; |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 31 | |
| 32 | message Port (PlCoreBase,ParameterMixin){ |
| 33 | required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False]; |
| 34 | optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True]; |
| 35 | optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 36 | optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True]; |
| 37 | optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True]; |
| 38 | required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True]; |
| 39 | } |
| 40 | |
| 41 | message Instance (PlCoreBase){ |
| 42 | optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; |
| 43 | optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; |
| 44 | required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; |
| 45 | optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; |
| 46 | optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 47 | required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; |
| 48 | optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; |
| 49 | required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; |
| 50 | required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; |
| 51 | required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; |
| 52 | required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; |
| 53 | required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False]; |
| 54 | optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True]; |
| 55 | 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]; |
| 56 | optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; |
| 57 | optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; |
| 58 | required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True]; |
| 59 | } |
| 60 | |
| 61 | message Network (PlCoreBase,ParameterMixin) { |
| 62 | required string name = 1 [db_index = False, max_length = 32, null = False, blank = False]; |
| 63 | required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False]; |
| 64 | required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True]; |
| 65 | required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True]; |
| 66 | required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True]; |
| 67 | optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 68 | optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 69 | required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False]; |
| 70 | required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False]; |
| 71 | required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True]; |
| 72 | optional string topology_parameters = 11 [db_index = False, null = True, blank = True]; |
| 73 | optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 74 | optional string controller_parameters = 13 [db_index = False, null = True, blank = True]; |
| 75 | optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True]; |
| 76 | optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True]; |
| 77 | optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True]; |
| 78 | 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]; |
| 79 | required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True]; |
| 80 | required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True]; |
| 81 | required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True]; |
| 82 | } |
| 83 | |
| 84 | message Slice (PlCoreBase){ |
| 85 | required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False]; |
| 86 | required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True]; |
| 87 | required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True]; |
| 88 | 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]; |
| 89 | required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True]; |
| 90 | required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False]; |
| 91 | required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False]; |
| 92 | optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True]; |
| 93 | 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'))"]; |
| 94 | optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True]; |
| 95 | optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True]; |
| 96 | optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True]; |
| 97 | optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True]; |
| 98 | optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True]; |
| 99 | optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True]; |
| 100 | optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False]; |
| 101 | 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]; |
| 102 | required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True]; |
| 103 | } |
| 104 | """ |
| 105 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 106 | args = FakeArgs() |
| 107 | args.inputs = xproto |
| 108 | args.target = 'modeldefs.xtarget' |
| 109 | output = XOSGenerator.generate(args) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 110 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 111 | yaml_ir = yaml.load(output) |
| 112 | self.assertEqual(len(yaml_ir['items']), 4) |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | unittest.main() |
| 116 | |
| 117 | |