CORD-1427: Support packaging and separate namespaces for models

Change-Id: I541524f61f1a12244f4cced738c0d1fac1359a2f
diff --git a/xos/genx/targets/django-split.xtarget b/xos/genx/targets/django-split.xtarget
index 4053d72..ccb4068 100644
--- a/xos/genx/targets/django-split.xtarget
+++ b/xos/genx/targets/django-split.xtarget
@@ -5,14 +5,14 @@
 
 {%- for l in m.links %}
 
-{% if l.peer != m.name %}
-from core.models.{{ l.peer | lower }} import {{ l.peer }} 
+{% if l.peer.name != m.name %}
+from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }} 
 {% endif %}
 
 {%- endfor %}
 {% for b in m.bases %}
-{% if b!='XOSBase' and 'Mixin' not in b%}
-from core.models.{{b | lower}} import {{ b }}
+{% if b.name!='XOSBase' and 'Mixin' not in b.name %}
+from core.models.{{b.name | lower}} import {{ b.name }}
 {% endif %}
 {% endfor %}
 
@@ -26,8 +26,8 @@
   {%- endfor %}
 
   # Relations
-  {% for l in m.links %}
-  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer==m.name -%}'self'{%- else -%}{{ l.peer }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
+  {% for l in m.links %}{% set peer_name=l.peer.name %}
+  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
   {%- endfor %}
 
   {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
diff --git a/xos/genx/targets/django.xtarget b/xos/genx/targets/django.xtarget
index 8ebd2a7..5635e95 100644
--- a/xos/genx/targets/django.xtarget
+++ b/xos/genx/targets/django.xtarget
@@ -1,13 +1,12 @@
 
-from header import *
 {% for m in proto.messages %}
-{% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{% endif %}
+{% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{%- else -%}from header import *{% endif %}
 {% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %}
 
 {%- for l in m.links %}
 
 {% if l.peer != m.name %}
-from core.models.{{ l.peer | lower }} import {{ l.peer }} 
+from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }} 
 {% endif %}
 
 {%- endfor %}
@@ -27,8 +26,8 @@
   {%- endfor %}
 
   # Relations
-  {% for l in m.links %}
-  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer==m.name -%}'self'{%- else -%}{{ l.peer }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
+  {% for l in m.links %}{% set peer_name=l.peer.name %}
+  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
   {%- endfor %}
 
   {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
diff --git a/xos/genx/targets/dot.xtarget b/xos/genx/targets/dot.xtarget
index a51ccb8..96aceec 100644
--- a/xos/genx/targets/dot.xtarget
+++ b/xos/genx/targets/dot.xtarget
@@ -1,7 +1,7 @@
 digraph {
 {% for m in proto.messages %}
   {%- for l in m.links %}
-  {{ m.name }} -> {{ l.peer }};
+  {{ m.fqn }} -> {{ l.peer.name }};
   {%- endfor %}
 {% endfor %}
 }
diff --git a/xos/genx/targets/proto.xtarget b/xos/genx/targets/proto.xtarget
index e310ad4..27b9490 100644
--- a/xos/genx/targets/proto.xtarget
+++ b/xos/genx/targets/proto.xtarget
@@ -1,6 +1,6 @@
 {% for m in proto.messages %}
 message {{ m.name }} {
-  option bases = "{{ m.bases | join(",") }}";
+  option bases = "{{ m.bases | map(attribute='name') | join(",") }}";
   {%- for f in m.fields %}
   {{ f.modifier }} {{f.type}} {{f.name}} = {{ f.id }}{% if f.options %} [{% for k,v in f.options.iteritems() %} {{ k }} = "{{ v}}"{% if not loop.last %},{% endif %} {% endfor %}]{% endif %};
   {%- endfor %}
diff --git a/xos/genx/targets/service.xtarget b/xos/genx/targets/service.xtarget
index e5bf019..87a0654 100644
--- a/xos/genx/targets/service.xtarget
+++ b/xos/genx/targets/service.xtarget
@@ -4,15 +4,14 @@
 {% if file_exists(m.name|lower+'_header.py') -%}from {{m.name|lower }}_header import *{% endif %}
 {% if file_exists(m.name|lower+'_top.py') -%}{{ include_file(m.name|lower+'_top.py') }} {% endif %}
 
-{%- for l in m.links -%}
-{% if l.peer not in proto.message_names -%}
-from core.models import {{ l.peer }} 
+{%- for l in m.links -%}{% set peer_name=l.peer.name %}
+{% if peer_name not in proto.message_names -%}
+from core.models import {{ peer_name }} 
 {%- endif -%}
 {%- endfor -%}
 {%- for b in m.bases -%}
-{%- if b!='XOSBase' and 'Mixin' not in b -%}
-#from core.models.{{b | lower}} import {{ b }}
-from core.models import {{ b }}
+{%- if b.name!='XOSBase' and 'Mixin' not in b.name %}
+from core.models import {{ b.name }}
 {%- endif -%}
 {% endfor %}
 
@@ -37,8 +36,8 @@
   {%- endfor %}
 
   # Relations
-  {% for l in m.links %}
-  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer==m.name -%}'self'{%- else -%}{{ l.peer }} {%- endif -%}, {{ xproto_django_options_str(l, l.dst_port ) }} )
+  {% for l in m.links %}{% set peer_name=l.peer.name %}
+  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }} {%- endif -%}, {{ xproto_django_options_str(l, l.dst_port ) }} )
   {%- endfor %}
 
   {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
diff --git a/xos/genx/targets/xproto.xtarget b/xos/genx/targets/xproto.xtarget
index 843d741..513d7a7 100644
--- a/xos/genx/targets/xproto.xtarget
+++ b/xos/genx/targets/xproto.xtarget
@@ -10,9 +10,9 @@
   {% endif %}
   {%- endfor %}
   
-  {%- for l in m.links %}
+  {%- for l in m.links %}{% set peer_name=l.peer.name %}
   {{ l }}
-  {{ l.modifier }} {{ l.link_type }} {{ l.name }} -> {{ l.peer }}:{{ l.dst_port }} = 1 {% if l.options %} [{% for k,v in l.options.iteritems() %} {{ k }} = {{ v }}{% if not loop.last %},{% endif %} {% endfor %}]{% endif %};
+  {{ l.modifier }} {{ l.link_type }} {{ l.name }} -> {{ l.peer.name }}:{{ l.dst_port }} = 1 {% if l.options %} [{% for k,v in l.options.iteritems() %} {{ k }} = {{ v }}{% if not loop.last %},{% endif %} {% endfor %}]{% endif %};
   {%- endfor -%}
 }
 {% endfor %}
diff --git a/xos/genx/tool/generator.py b/xos/genx/tool/generator.py
index e6a6f2f..e76bf87 100755
--- a/xos/genx/tool/generator.py
+++ b/xos/genx/tool/generator.py
@@ -1,6 +1,6 @@
-import plyproto.model as m
+import plyxproto.model as m
 import pdb
-import plyproto.parser as plyproto
+import plyxproto.parser as plyxproto
 import traceback
 import sys
 import jinja2
@@ -40,7 +40,7 @@
 
     def generate(self):
         try:
-            parser = plyproto.ProtobufAnalyzer()
+            parser = plyxproto.ProtobufAnalyzer()
             input = self.input
             ast = parser.parse_string(input,debug=0)
 
diff --git a/xos/genx/tool/lib.py b/xos/genx/tool/lib.py
index 82de5b1..e9acbe5 100644
--- a/xos/genx/tool/lib.py
+++ b/xos/genx/tool/lib.py
@@ -43,8 +43,8 @@
         except KeyError, e:
             raise e
 
-        if l['peer'] not in seen and t!='manytomany':
-            outlist.append('- {model: %s, type: %s}\n'%(l['peer'], l['link_type']))
+        if l['peer']['fqn'] not in seen and t!='manytomany':
+            outlist.append('- {model: %s, type: %s}\n'%(l['peer']['name'], l['link_type']))
         seen.append(l['peer'])
     
     return outlist
@@ -88,6 +88,7 @@
     elif (not base):
         return ''
     else:
+        base = map(lambda s:s['name'], base)
         return '(' + ','.join(base) + ')'
 
 def xproto_first_non_empty(lst):
@@ -182,8 +183,21 @@
 
     try:
         if field['through']:
-            if not field['through'].endswith('_'+field['name']):
-                output_dict['through'] = '%r'%field['through']
+            d = {}
+            if isinstance(field['through'], str):
+                split = field['through'].rsplit('.',1)
+                d['name'] = split[-1]
+                if len(split)==2:
+                    d['package'] = split[0]
+                    d['fqn'] = 'package' + '.' + d['name']
+                else:
+                    d['fqn'] = d['name']
+                    d['package'] = ''
+            else:
+                d = field['through']
+
+            if not d['name'].endswith('_'+field['name']):
+                output_dict['through'] = '%r'%d['fqn']
     except KeyError:
         pass
 
@@ -218,10 +232,20 @@
     fields = []
 
     for b in m['bases']:
-        if b in table:
-            base_fields = xproto_base_fields(table[b], table)
+        option1 = b['fqn']
+        try:
+            option2 = m['package'] + '.' + b['name']
+        except TypeError:
+            option2 = option1
 
-            model_fields = table[b]['fields']
+        accessor = None
+        if option1 in table: accessor = option1
+        elif option2 in table: accessor = option2
+
+        if accessor:
+            base_fields = xproto_base_fields(table[accessor], table)
+
+            model_fields = table[accessor]['fields']
             fields.extend(base_fields)
             fields.extend(model_fields)
 
@@ -230,7 +254,8 @@
 def xproto_base_links(m, table):
     links = []
 
-    for b in m['bases']:
+    for base in m['bases']:
+        b = base['name']
         if b in table:
             base_links = xproto_base_links(table[b], table)
 
diff --git a/xos/genx/tool/proto2xproto.py b/xos/genx/tool/proto2xproto.py
index ad6a21e..c0ee565 100644
--- a/xos/genx/tool/proto2xproto.py
+++ b/xos/genx/tool/proto2xproto.py
@@ -1,7 +1,7 @@
-import plyproto.model as m
+import plyxproto.model as m
 import pdb
 import argparse
-import plyproto.parser as plyproto
+import plyxproto.parser as plyxproto
 import traceback
 import sys
 import jinja2
@@ -11,6 +11,17 @@
     def push(self,x):
         self.append(x)
 
+def str_to_dict(s):
+    lst = s.rsplit('.',1)
+    name = lst[-1]
+
+    if len(lst)==2:
+        package = lst[0]
+    else:
+        package = ''
+
+    return {'name': name, 'package': package, 'fqn': s}
+
 def replace_link(obj):
         try:
             link = obj.link
@@ -24,7 +35,14 @@
             except TypeError:
                 through_str = None
 
-            ls = m.LinkSpec(obj, m.LinkDefinition(link['link'][1:-1],obj.name,link['model'][1:-1],link['port'][1:-1],through_str))
+            if through_str:
+                through_dict = str_to_dict(through_str)
+            else:
+                through_dict = {}
+
+            model_dict = str_to_dict(link['model'][1:-1])
+
+            ls = m.LinkSpec(obj, m.LinkDefinition(link['link'][1:-1],obj.name,model_dict,link['port'][1:-1],through_dict))
             return ls
         except:
             return obj
@@ -60,7 +78,7 @@
     def proto_to_xproto_message(self, obj):
         try:
             bases = self.message_options['bases'].split(',')
-            bases = map(lambda x:x[1:-1], bases)
+            bases = map(lambda x:str_to_dict(x[1:-1]), bases)
             obj.bases = bases
         except KeyError:
             raise
diff --git a/xos/genx/tool/tests/django_generator_test.py b/xos/genx/tool/tests/django_generator_test.py
index 052a48f..9a8296d 100644
--- a/xos/genx/tool/tests/django_generator_test.py
+++ b/xos/genx/tool/tests/django_generator_test.py
@@ -5,7 +5,7 @@
     def test_proto_generator(self):
         xproto = \
 """
-message VRouterPort (XOSBase){
+message VRouterPort (xos.core.XOSBase){
      optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True];
      required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
      required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False];
@@ -21,8 +21,8 @@
 
 {%- for l in m.links %}
 
-{% if l.peer != m.name %}
-from core.models.{{ l.peer | lower }} import {{ l.peer }}
+{% if l.peer.name != m.name %}
+from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
 {% endif %}
 
 {%- endfor %}
@@ -33,7 +33,7 @@
 {% endfor %}
 
 
-class {{ m.name }}{{ xproto_base_def(m.bases) }}:
+class {{ m.name }}{{ xproto_base_def(m.name, m.bases) }}:
   # Primitive Fields (Not Relations)
   {% for f in m.fields %}
   {%- if not f.link -%}
@@ -43,7 +43,7 @@
 
   # Relations
   {% for l in m.links %}
-  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer==m.name -%}'self'{%- else -%}{{ l.peer }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
+  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer.name==m.name -%}'self'{%- else -%}{{ l.peer.name }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
   {%- endfor %}
 
   {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
diff --git a/xos/genx/tool/tests/graph_test.py b/xos/genx/tool/tests/graph_test.py
index 44b35ed..645549e 100644
--- a/xos/genx/tool/tests/graph_test.py
+++ b/xos/genx/tool/tests/graph_test.py
@@ -7,8 +7,8 @@
   {% for m in proto.messages %}
   {{ m.name }} {
   {%- for l in m.links %}
-     {%- if proto.message_table[l.peer] -%}
-     {%- set model = proto.message_table[l.peer] %}
+     {%- if proto.message_table[l.peer.fqn] -%}
+     {%- set model = proto.message_table[l.peer.fqn] %}
         {% for f in model.fields %}
         {{ f.type }} {{ f.name }};
         {% endfor %}
@@ -104,8 +104,8 @@
   {% for m in proto.messages %}
   {{ m.name }} {
   {%- for l in m.links %}
-     {%- if proto.message_table[l.peer] -%}
-     {%- set model = proto.message_table[l.peer] %}
+     {%- if proto.message_table[l.peer.fqn] -%}
+     {%- set model = proto.message_table[l.peer.fqn] %}
         {% for f in model.fields %}
         {{ f.type }} {{ f.name }};
         {% endfor %}
@@ -193,7 +193,7 @@
         self.generate(xproto=proto, target=target)
         output = self.get_output()
         num_semis = output.count(';')
-        self.assertGreater(num_semis, 3) # 3 is the number of links, each of which contains at least one field
+        self.assertGreater(num_semis, 3)
 
     def test_from_base(self):
         target = \
diff --git a/xos/genx/tool/tests/package_test.py b/xos/genx/tool/tests/package_test.py
new file mode 100644
index 0000000..b6841b1
--- /dev/null
+++ b/xos/genx/tool/tests/package_test.py
@@ -0,0 +1,405 @@
+from xproto_test_base import *
+
+class XProtoPackageTest(XProtoTest):
+    def test_package_fqn(self):
+        target = \
+"""
+  {% for m in proto.messages %}
+  {{ m.name }},{{ m.package }},{{ m.fqn }}
+  {% endfor %}
+"""
+
+        proto = \
+"""
+package xos.core;
+
+message Port (PlCoreBase,ParameterMixin) {
+     required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
+     optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
+     optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
+     required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
+}
+"""
+        self.generate(xproto=proto, target=target)
+        output = self.get_output()
+        self.assertIn('Port,xos.core,xos.core.Port', output)
+
+    def test_cross_model(self):
+        target = \
+"""
+  {% for m in proto.messages %}
+  {{ m.fqn }} {
+  {%- for l in m.links %}
+     {%- if proto.message_table[l.peer.fqn] %}
+     {{ l.peer.name }} {
+     {%- set model = proto.message_table[l.peer.fqn] %}
+        {% for f in model.fields %}
+        {{ f.type }} {{ f.name }};
+        {% endfor %}
+     }
+    {%- endif -%}
+    {%- if proto.message_table[m.package + '.' + l.peer.name] %}
+     {{ l.peer.name }} {
+     {%- set model = proto.message_table[m.package + '.' + l.peer.name] %}
+        {% for f in model.fields %}
+        {{ f.type }} {{ f.name }};
+        {% endfor %}
+     }
+     {%- endif -%}
+  {% endfor %}
+  }
+  {% endfor %}
+"""
+
+        proto = \
+"""
+package xos.network;
+
+message Port (PlCoreBase,ParameterMixin){
+     required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
+     optional manytoone instance->xos.core.Instance:ports = 2 [db_index = True, null = True, blank = True];
+     optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
+     required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
+}
+
+
+package xos.core;
+
+message Instance (PlCoreBase){
+     optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
+     optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
+     required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
+     optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
+     optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
+     optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
+     required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
+     required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
+     required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
+     required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
+     required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
+     optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
+     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];
+     optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
+     optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
+     required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
+}
+
+package xos.network;
+
+message Network (PlCoreBase,ParameterMixin) {
+     required string name = 1 [db_index = False, max_length = 32, null = False, blank = False];
+     required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False];
+     required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True];
+     required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True];
+     required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True];
+     optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True];
+     optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True];
+     required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False];
+     required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False];
+     required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True];
+     optional string topology_parameters = 11 [db_index = False, null = True, blank = True];
+     optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True];
+     optional string controller_parameters = 13 [db_index = False, null = True, blank = True];
+     optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True];
+     optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True];
+     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];
+     required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True];
+     required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True];
+     required manytomany instances->xos.core.Instance/xos.network.Port:networks = 20 [db_index = False, null = False, blank = True];
+}
+
+message Slice (PlCoreBase){
+     required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False];
+     required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True];
+     required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True];
+     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];
+     required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True];
+     required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False];
+     required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False];
+     optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True];
+     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'))"];
+     optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True];
+     optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True];
+     optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True];
+     optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True];
+     optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True];
+     optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True];
+     optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False];
+     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];
+     required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True];
+}
+"""
+        self.generate(xproto=proto, target=target)
+        output = self.get_output()
+        self.assertIn('numberCores', output) # Instance showed up via cross-package call
+        self.assertIn('ip;', output) # Network showed up via cross-package call
+        self.assertIn('max_instances', output) # Slice showed up via implicit in-package call
+
+    def test_base_class_fields(self):
+        target = \
+"""
+  {% for m in proto.messages %}
+  {{ m.name }} {
+  {%- for b in m.bases %}
+     {%- if proto.message_table[b.fqn] -%}
+     {%- set model = proto.message_table[b.fqn] %}
+        {% for f in model.fields %}
+        {{ f.type }} {{ f.name }};
+        {% endfor %}
+     {%- endif -%}
+  {% endfor %}
+  }
+  {% endfor %}
+"""
+
+        proto = \
+"""
+package xos.network;
+
+message Port (PlCoreBase,ParameterMixin){
+     required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
+     optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
+     optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
+     required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
+}
+
+package xos.someotherpackage;
+
+message Instance (xos.network.Port){
+     optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
+     optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
+     required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
+     optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
+     optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
+     optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
+     required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
+     required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
+     required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
+     required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
+     required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
+     optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
+     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];
+     optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
+     optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
+     required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
+}
+"""
+        self.generate(xproto=proto, target=target)
+        output = self.get_output()
+        self.assertIn('xos_created', output)
+
+    def test_from_base(self):
+        target = \
+"""
+  {% for f in xproto_base_fields(proto.messages.3, proto.message_table) %}
+        {{ f.type }} {{ f.name }};
+  {% endfor %}
+"""
+
+        proto = \
+"""
+option app_name = "firstapp";
+
+message Port (PlCoreBase,ParameterMixin){
+     required string easter_egg = 1;
+     required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
+     optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
+     optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
+     required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
+}
+
+package A;
+
+message Instance (Port){
+     optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
+     optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
+     required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
+     optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
+     optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
+     optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
+     required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
+     required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
+     required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
+     required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
+     required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
+     optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
+     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];
+     optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
+     optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
+     required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
+}
+
+package B;
+
+option app_name="networkapp";
+
+message Network (A.Instance) {
+     required string name = 1 [db_index = False, max_length = 32, null = False, blank = False];
+     required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False];
+     required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True];
+     required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True];
+     required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True];
+     optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True];
+     optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True];
+     required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False];
+     required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False];
+     required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True];
+     optional string topology_parameters = 11 [db_index = False, null = True, blank = True];
+     optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True];
+     optional string controller_parameters = 13 [db_index = False, null = True, blank = True];
+     optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True];
+     optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True];
+     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];
+     required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True];
+     required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True];
+     required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True];
+}
+
+option app_name="sliceapp";
+
+message Slice (Network){
+     required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False];
+     required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True];
+     required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True];
+     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];
+     required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True];
+     required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False];
+     required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False];
+     optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True];
+     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'))"];
+     optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True];
+     optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True];
+     optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True];
+     optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True];
+     optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True];
+     optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True];
+     optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False];
+     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];
+     required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True];
+}
+"""
+        self.generate(xproto=proto, target=target)
+        self.assertIn('easter_egg', self.get_output())
+
+    def test_model_options(self):
+        target = \
+"""
+  Options:
+
+  {{ proto.options }}
+  {% for m in proto.messages %}
+        {{ m.options }}
+        {{ m.options.app_name }}
+  {% endfor %}
+"""
+
+        proto = \
+"""
+option app_name = "firstapp";
+
+message Port (PlCoreBase,ParameterMixin){
+     required string easter_egg = 1;
+     required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False];
+     optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True];
+     optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True];
+     required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True];
+}
+
+package A;
+
+message Instance (Port){
+     optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False];
+     optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False];
+     required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False];
+     optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False];
+     optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False];
+     required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False];
+     optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True];
+     required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False];
+     required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False];
+     required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False];
+     required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False];
+     required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False];
+     optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True];
+     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];
+     optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True];
+     optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True];
+     required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True];
+}
+
+package B;
+
+option app_name = "networkapp";
+
+message Network (A.Instance) {
+     required string name = 1 [db_index = False, max_length = 32, null = False, blank = False];
+     required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False];
+     required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True];
+     required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True];
+     required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True];
+     optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True];
+     optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True];
+     required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False];
+     required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False];
+     required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True];
+     optional string topology_parameters = 11 [db_index = False, null = True, blank = True];
+     optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True];
+     optional string controller_parameters = 13 [db_index = False, null = True, blank = True];
+     optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True];
+     optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True];
+     optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True];
+     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];
+     required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True];
+     required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True];
+     required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True];
+}
+
+
+option app_name = "networkapp";
+
+message Slice (Network){
+     required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False];
+     required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True];
+     required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True];
+     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];
+     required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True];
+     required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False];
+     required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False];
+     optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True];
+     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'))"];
+     optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True];
+     optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True];
+     optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True];
+     optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True];
+     optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True];
+     optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True];
+     optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False];
+     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];
+     required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True];
+}
+"""
+        self.generate(xproto=proto, target=target)
+        output = self.get_output()
+        self.assertEqual(output.count('firstapp'), 2)
+        self.assertEqual(output.count('networkapp'), 2)
+
+
+if __name__ == '__main__':
+    unittest.main()
+
+
diff --git a/xos/genx/tool/tests/parse_test.py b/xos/genx/tool/tests/parse_test.py
index cfba4bd..21bb181 100644
--- a/xos/genx/tool/tests/parse_test.py
+++ b/xos/genx/tool/tests/parse_test.py
@@ -82,7 +82,7 @@
     required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False];
 }
 """
-        self.generate(xproto = xproto, target = "{{ proto.messages.0.links.0.through }}")
+        self.generate(xproto = xproto, target = "{{ proto }}{{ proto.messages.0.links.0.through }}")
         self.assertIn("ServiceProxy", self.get_output())
 	
 	pass
diff --git a/xos/genx/tool/tests/pure_proto_test.py b/xos/genx/tool/tests/pure_proto_test.py
index e6153a3..ffd3701 100644
--- a/xos/genx/tool/tests/pure_proto_test.py
+++ b/xos/genx/tool/tests/pure_proto_test.py
@@ -1,4 +1,5 @@
 from xproto_test_base import *
+import pdb
 
 # Generate from xproto, then generate from equivalent proto
 class XPureProtobufGenerator(XProtoTest):
@@ -32,19 +33,19 @@
 
 {%- for l in m.links %}
 
-{% if l.peer != m.name %}
-from core.models.{{ l.peer | lower }} import {{ l.peer }}
+{% if l.peer.name != m.name %}
+from core.models.{{ l.peer.name | lower }} import {{ l.peer.name }}
 {% endif %}
 
 {%- endfor %}
 {% for b in m.bases %}
 {% if b!='XOSBase' and 'Mixin' not in b%}
-from core.models.{{b | lower}} import {{ b }}
+from core.models.{{b.name | lower}} import {{ b.name }}
 {% endif %}
 {% endfor %}
 
 
-class {{ m.name }}{{ xproto_base_def(m.bases) }}:
+class {{ m.name }}{{ xproto_base_def(m, m.bases) }}:
   # Primitive Fields (Not Relations)
   {% for f in m.fields %}
   {%- if not f.link -%}
@@ -54,7 +55,7 @@
 
   # Relations
   {% for l in m.links %}
-  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer==m.name -%}'self'{%- else -%}{{ l.peer }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
+  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if l.peer.name==m.name -%}'self'{%- else -%}{{ l.peer.name }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
   {%- endfor %}
 
   {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
diff --git a/xos/genx/tool/tests/translator_test.py b/xos/genx/tool/tests/translator_test.py
index e88e59c..0ff518c 100644
--- a/xos/genx/tool/tests/translator_test.py
+++ b/xos/genx/tool/tests/translator_test.py
@@ -17,7 +17,7 @@
 """
 {% for m in proto.messages %}
 message {{ m.name }} {
-  option bases = "{{ m.bases | join(",") }}";
+  option bases = "{{ m.bases | map(attribute='name') | join(",") }}";
   {%- for f in m.fields %}
   {{ 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 %};
   {%- endfor %}
diff --git a/xos/genx/tool/xos2jinja.py b/xos/genx/tool/xos2jinja.py
index bd7edb6..162293f 100644
--- a/xos/genx/tool/xos2jinja.py
+++ b/xos/genx/tool/xos2jinja.py
@@ -1,13 +1,28 @@
-import plyproto.model as m
+import plyxproto.model as m
 import pdb
 import argparse
-import plyproto.parser as plyproto
+import plyxproto.parser as plyxproto
 import traceback
 import sys
 import jinja2
 import os
 import copy
 
+def dotname_to_fqn(dotname):
+    b_names = [part.pval for part in dotname]
+    package = '.'.join(b_names[:-1])
+    name = b_names[-1]
+    if package:
+        fqn = package + '.' + name
+    else:
+        fqn = name
+    return {'name':name, 'fqn':fqn, 'package':package}
+
+def dotname_to_name(dotname):
+    b_names = [part.pval for part in dotname]
+    return '.'.join(b_names)
+
+
 def count_messages(body):
     count = 0
     for e in body:
@@ -35,16 +50,21 @@
     for m in messages:
         for l in m['links']:
             rlink = copy.deepcopy(l)
+
             rlink['_type'] = 'rlink' # An implicit link, not declared in the model
             rlink['src_port'] = l['dst_port']
             rlink['dst_port'] = l['src_port']
-            rlink['peer'] = m['name']
+            rlink['peer'] = {'name':m['name'], 'package': m['package'], 'fqn': m['fqn']}
             rlink['link_type'] = link_opposite[l['link_type']]
 
             try:
-                rev_links[l['peer']].append(rlink)
+                try:
+                    rev_links[l['peer']['fqn']].append(rlink)
+                except TypeError:
+                    pdb.set_trace()
+                    pass
             except KeyError:
-                rev_links[l['peer']] = [rlink]
+                rev_links[l['peer']['fqn']] = [rlink]
 
     for m in messages:
         try:
@@ -52,6 +72,17 @@
         except KeyError:
             pass
 
+def name_to_value(obj):
+    try:
+        value = obj.value.value.pval
+    except AttributeError:
+        try:
+            value = obj.value.value
+        except AttributeError:
+            value = obj.value.pval
+
+    return value
+
 class Stack(list):
     def push(self,x):
         self.append(x)
@@ -62,6 +93,7 @@
     stack = Stack()
     models = {}
     options = {}
+    package = None
     message_options = {}
     count_stack = Stack()
     content=""
@@ -79,7 +111,10 @@
         self.first_method = True
 
     def visit_PackageStatement(self, obj):
-        '''Ignore'''
+        dotlist = obj.name.value
+        dotlist2 = [f.pval for f in dotlist]
+        dotstr = '.'.join(dotlist2)
+        self.package = dotstr
         return True
 
     def visit_ImportStatement(self, obj):
@@ -111,13 +146,10 @@
         except AttributeError:
             name = obj.name.value
 
-        try:
-            value = obj.value.value.pval
-        except AttributeError:
-            try:
-                value = obj.value.value
-            except AttributeError:
-                value = obj.value.pval
+        if type(obj.value)==list:
+            value = dotname_to_name(obj.value)
+        else:
+            value = name_to_value(obj)
 
         self.stack.push([name,value])
         return True
@@ -142,15 +174,21 @@
         except AttributeError:
             s['dst_port'] = obj.dst_port
 
-        try:
-            s['through'] = obj.through.pval
-        except AttributeError:
-            s['through'] = obj.through
+        if type(obj.through)==list:
+            s['through'] = dotname_to_fqn(obj.through)
+        else:
+            try:
+                s['through'] = obj.through.pval
+            except AttributeError:
+                s['through'] = obj.through
 
-        try:
-            s['peer'] = obj.name.pval
-        except AttributeError:
-            s['peer'] = obj.name
+        if type(obj.name)==list:
+            s['peer'] = dotname_to_fqn(obj.name)
+        else:
+            try:
+                s['peer'] = obj.name.pval
+            except AttributeError:
+                s['peer'] = obj.name
 
         s['_type'] = 'link'
         s['options'] = {'modifier':'optional'}
@@ -169,6 +207,7 @@
             s['type'] = obj.ftype.value
         else:
             s['type'] = obj.ftype.name.pval
+
         s['name'] = obj.name.value.pval
         s['modifier'] = obj.field_modifier.pval
         s['id'] = obj.fieldId.pval
@@ -223,10 +262,11 @@
         links = []
         last_field = None
         try:
-            obj.bases = map(lambda x:x.pval, obj.bases)
+            obj.bases = map(dotname_to_fqn, obj.bases)
         except AttributeError:
             pass
 
+        last_field = {}
         for i in range(0,stack_num):
             f = self.stack.pop()
             if (f['_type']=='link'):
@@ -238,9 +278,23 @@
                 fields.insert(0,f)
                 last_field = f
 
-        model_def = {'name':obj.name.value.pval,'fields':fields,'links':links, 'bases':obj.bases, 'options':self.message_options}
+        if self.package:
+            model_name = '.'.join([self.package, obj.name.value.pval])
+        else:
+            model_name = obj.name.value.pval
+
+        model_def = {'name':obj.name.value.pval,'fields':fields,'links':links, 'bases':obj.bases, 'options':self.message_options, 'package':self.package, 'fqn': model_name}
         self.stack.push(model_def)
-        self.models[obj.name.value.pval] = model_def
+        self.models[model_name] = model_def
+
+        # Set message options
+        for k,v in self.options.iteritems():
+            try:
+                if k not in self.message_options.setdefault(self.current_message_name,{}):
+                    self.message_options[self.current_message_name][k] = v
+            except KeyError:
+                pass
+
         self.current_message_name = None
         return True