[CORD-1338] Adding onetomany relations to modeldefs

Change-Id: I213ebf807984adcc69edee91252ccd53f6ab1c25
diff --git a/group_vars/all b/group_vars/all
index f2e1b3b..2f3c749 100644
--- a/group_vars/all
+++ b/group_vars/all
@@ -106,4 +106,4 @@
 
 frontend_only: False
 
-xos_services: []
+xos_services: []
\ No newline at end of file
diff --git a/lib/xos-genx/tests/optimize_test.py b/lib/xos-genx/tests/optimize_test.py
index af40575..0d2c1b1 100644
--- a/lib/xos-genx/tests/optimize_test.py
+++ b/lib/xos-genx/tests/optimize_test.py
@@ -1,6 +1,5 @@
 import unittest
 from xosgenx.jinja2_extensions.fol2 import FOL2Python
-import vimpdb
 
 class XProtoOptimizeTest(unittest.TestCase):
     def setUp(self):
diff --git a/lib/xos-genx/tests/translator_test.py b/lib/xos-genx/tests/translator_test.py
index e82af6d..fad4dba 100644
--- a/lib/xos-genx/tests/translator_test.py
+++ b/lib/xos-genx/tests/translator_test.py
@@ -228,6 +228,33 @@
         output = XOSGenerator.generate(args)
         self.assertNotIn('default:', output)
 
+    def test_one_to_many_in_modeldef(self):
+        xproto = \
+"""
+option app_label = "test";
+
+message ServiceDependency {
+    required manytoone provider_service->Service:provided_dependencies = 1;
+    required manytoone subscriber_service->Service:subscribed_dependencies = 2;
+}
+
+message Service {
+    required string name = 1;
+}
+"""
+
+        args = FakeArgs()
+        args.inputs = xproto
+        args.target = 'modeldefs.xtarget'
+        output = XOSGenerator.generate(args)
+        # Service deps model
+        self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
+        self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
+
+        # Service model
+        self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
+        self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
+
 if __name__ == '__main__':
     unittest.main()
 
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/base.py b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
index 3cb9886..40afa48 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/base.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
@@ -49,22 +49,6 @@
 
     return plural
 
-def xproto_links_to_modeldef_relations(llst):
-    outlist = []
-    seen = []
-    for l in llst:
-        try:
-            t = l['link_type']
-        except KeyError, e:
-            raise e
-
-        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
-
-
 def xproto_base_def(model_name, base, suffix='', suffix_list=[]):
     if (model_name=='XOSBase'):
         return '(models.Model, PlModelMixIn)'
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/gui.py b/lib/xos-genx/xosgenx/jinja2_extensions/gui.py
index bd23fdb..25342d2 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/gui.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/gui.py
@@ -87,4 +87,25 @@
         val = 'null'
     else:
         val = str(default)
-    return val
\ No newline at end of file
+    return val
+
+
+def xproto_links_to_modeldef_relations(llst):
+    outlist = []
+    seen = []
+    for l in llst:
+        try:
+            t = l['link_type']
+        except KeyError, e:
+            raise e
+
+        if l['peer']['fqn'] not in seen and t != 'manytomany':
+            on_field = 'null'
+            if l['link_type'] == 'manytoone':
+                on_field = l['src_port']
+            elif l['link_type'] == 'onetomany':
+                on_field = l['dst_port']
+            outlist.append('- {model: %s, type: %s, on_field: %s}\n' % (l['peer']['name'], l['link_type'], on_field))
+        seen.append(l['peer'])
+
+    return outlist
\ No newline at end of file
diff --git a/lib/xos-genx/xosgenx/targets/modeldefs.xtarget b/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
index 366f2ee..ec8df1c 100644
--- a/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
+++ b/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
@@ -35,7 +35,7 @@
   {% endif -%}
   {% endfor %}
   name: {{ m.name }}
-  {%- set goodlinks = xproto_links_to_modeldef_relations( xproto_base_links(m, proto.message_table) + m.links ) %}
+  {%- set goodlinks = xproto_links_to_modeldef_relations( xproto_base_links(m, proto.message_table) + m.links ) + xproto_links_to_modeldef_relations( xproto_base_rlinks(m, proto.message_table) + m.rlinks )%}
   {% if goodlinks %}
   relations:
   {{ goodlinks | join('\n') | indent(width=2)}}
diff --git a/xos/coreapi/protos/modeldefs.proto b/xos/coreapi/protos/modeldefs.proto
index d0d95fc..91d2652 100644
--- a/xos/coreapi/protos/modeldefs.proto
+++ b/xos/coreapi/protos/modeldefs.proto
@@ -25,6 +25,7 @@
 message FieldRelation {
     string model = 1;
     string type = 2;
+    string on_field = 3;
 };
 
 message ModelField {