Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame^] | 1 | from xproto_test_base import * |
| 2 | import yaml |
| 3 | |
| 4 | # Generate other formats from xproto |
| 5 | class XProtoTranslatorTest(XProtoTest): |
| 6 | def test_proto_generator(self): |
| 7 | xproto = \ |
| 8 | """ |
| 9 | message VRouterPort (XOSBase){ |
| 10 | optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True]; |
| 11 | required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False]; |
| 12 | required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False]; |
| 13 | required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False]; |
| 14 | } |
| 15 | """ |
| 16 | target = \ |
| 17 | """ |
| 18 | {% for m in proto.messages %} |
| 19 | message {{ m.name }} { |
| 20 | option bases = "{{ m.bases | join(",") }}"; |
| 21 | {%- for f in m.fields %} |
| 22 | {{ f.modifier }} {{f.type}} {{f.name}} = {{ f.id }}{% if f.options %} [{% for k,v in f.options.iteritems() %} {{ k }} = "{{ xproto_unquote(v)}}"{% if not loop.last %},{% endif %} {% endfor %}]{% endif %}; |
| 23 | {%- endfor %} |
| 24 | } |
| 25 | {% endfor %} |
| 26 | """ |
| 27 | |
| 28 | self.generate(xproto = xproto, target = target) |
| 29 | self.generate(xproto = self.get_output(), target = "{{ proto }}") |
| 30 | output = self.get_output() |
| 31 | self.assertIn("VRouterService", output) |
| 32 | |
| 33 | def test_yaml_generator(self): |
| 34 | xproto = \ |
| 35 | """ |
| 36 | option app_name = "test"; |
| 37 | |
| 38 | message Port (PlCoreBase,ParameterMixin){ |
| 39 | required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False]; |
| 40 | optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True]; |
| 41 | optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 42 | optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True]; |
| 43 | optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True]; |
| 44 | required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True]; |
| 45 | } |
| 46 | |
| 47 | message Instance (PlCoreBase){ |
| 48 | optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; |
| 49 | optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; |
| 50 | required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; |
| 51 | optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; |
| 52 | optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 53 | required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; |
| 54 | optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; |
| 55 | required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; |
| 56 | required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; |
| 57 | required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; |
| 58 | required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; |
| 59 | required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False]; |
| 60 | optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True]; |
| 61 | 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]; |
| 62 | optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; |
| 63 | optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; |
| 64 | required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True]; |
| 65 | } |
| 66 | |
| 67 | message Network (PlCoreBase,ParameterMixin) { |
| 68 | required string name = 1 [db_index = False, max_length = 32, null = False, blank = False]; |
| 69 | required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False]; |
| 70 | required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True]; |
| 71 | required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True]; |
| 72 | required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True]; |
| 73 | optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 74 | optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 75 | required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False]; |
| 76 | required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False]; |
| 77 | required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True]; |
| 78 | optional string topology_parameters = 11 [db_index = False, null = True, blank = True]; |
| 79 | optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 80 | optional string controller_parameters = 13 [db_index = False, null = True, blank = True]; |
| 81 | optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True]; |
| 82 | optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True]; |
| 83 | optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True]; |
| 84 | 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]; |
| 85 | required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True]; |
| 86 | required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True]; |
| 87 | required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True]; |
| 88 | } |
| 89 | |
| 90 | message Slice (PlCoreBase){ |
| 91 | required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False]; |
| 92 | required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True]; |
| 93 | required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True]; |
| 94 | 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]; |
| 95 | required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True]; |
| 96 | required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False]; |
| 97 | required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False]; |
| 98 | optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True]; |
| 99 | 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'))"]; |
| 100 | optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True]; |
| 101 | optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True]; |
| 102 | optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True]; |
| 103 | optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True]; |
| 104 | optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True]; |
| 105 | optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True]; |
| 106 | optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False]; |
| 107 | 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]; |
| 108 | required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True]; |
| 109 | } |
| 110 | """ |
| 111 | |
| 112 | # Update this to the contents of modeldefs.xtarget |
| 113 | target = \ |
| 114 | """ |
| 115 | items: |
| 116 | {%- for m in proto.messages | sort(attribute='name') %} |
| 117 | {%- if m.name != 'XOSBase' %} |
| 118 | - app: {{ xproto_unquote(xproto_first_non_empty([m.options.app_name, context.app_name, options.app_name])) }} |
| 119 | fields: |
| 120 | {%- set id_field = {'type':'int32', 'name':'id', 'options':{}} %} |
| 121 | {% for f in (xproto_base_fields(m, proto.message_table) + m.fields + [id_field]) | sort(attribute='name') -%} |
| 122 | {% if not f.link or f.options.link_type != 'manytomany' -%} |
| 123 | - hint: {% if f.options.help_text %}{{ xproto_unquote(f.options.help_text) }}{% else %}''{% endif %} |
| 124 | {% if not f.link -%} |
| 125 | name: {{ f.name }} |
| 126 | {%- else -%} |
| 127 | name: {{ f.name }}_id |
| 128 | relation: {model: {{ f.options.model }}, type: {{ f.options.link_type }}} |
| 129 | {% endif %} |
| 130 | type: {{ xproto_type_to_ui_type(f) }} |
| 131 | {% set validators = xproto_validators(f) -%} |
| 132 | {% if validators -%} |
| 133 | validators: |
| 134 | {% for v in validators | sort(attribute='name',reverse=True) -%} |
| 135 | - {{ v | yaml }} |
| 136 | {% endfor %} |
| 137 | {% else -%} |
| 138 | validators: [] |
| 139 | {% endif %} |
| 140 | {% endif -%} |
| 141 | {% endfor %} |
| 142 | name: {{ m.name }} |
| 143 | {%- set goodlinks = xproto_links_to_modeldef_relations( xproto_base_links(m, proto.message_table) + m.links ) %} |
| 144 | {% if goodlinks %} |
| 145 | relations: |
| 146 | {{ goodlinks | join('\n') | indent(width=2)}} |
| 147 | {%- else %} |
| 148 | relations: [] |
| 149 | {%- endif %} |
| 150 | {%- endif %} |
| 151 | {% endfor -%} |
| 152 | """ |
| 153 | |
| 154 | self.generate(xproto = xproto, target = target) |
| 155 | output = self.get_output() |
| 156 | yaml_ir = yaml.load(output) |
| 157 | self.assertEqual(len(yaml_ir['items']), 4) |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | unittest.main() |
| 161 | |
| 162 | |