[CORD-2349][CORD-2391] Adding TOSCA keys to support migration to the new TOSCA engine
[CORD-2429] Moving ip generation logic to the core

Change-Id: I818c7fd2974f2f8b95d2214490ae7e898e09601c
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py b/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
index a46770d..51cd2bb 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
@@ -12,6 +12,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from xosgenx.jinja2_extensions import xproto_field_graph_components
+
 def xproto_tosca_required(null, blank, default=None):
 
     if null == 'True' or blank == 'True' or default != 'False':
@@ -32,14 +34,32 @@
         return type
 
 def xproto_fields_to_tosca_keys(fields):
-	keys = []
-	# look for explicit keys
-	for f in fields:
-		if 'tosca_key' in f['options'] and f['options']['tosca_key'] and 'link' not in f:
-			keys.append(f['name'])
-		if 'tosca_key' in f['options'] and f['options']['tosca_key'] and ('link' in f and f['link']):
-			keys.append("%s_id" % f['name'])
-	# if not keys are specified and there is a name field, use that as key.
-	if len(keys) == 0 and 'name' in map(lambda f: f['name'], fields):
-		keys.append('name')
-	return keys
\ No newline at end of file
+    keys = []
+
+    # look for one_of keys
+    _one_of = xproto_field_graph_components(fields, 'tosca_key_one_of')
+    one_of = [list(i) for i in _one_of]
+
+    # look for explicit keys
+    for f in fields:
+        if 'tosca_key' in f['options'] and f['options']['tosca_key'] and 'link' not in f:
+            keys.append(f['name'])
+        if 'tosca_key' in f['options'] and f['options']['tosca_key'] and ('link' in f and f['link']):
+            keys.append("%s_id" % f['name'])
+    # if not keys are specified and there is a name field, use that as key.
+    if len(keys) == 0 and 'name' in map(lambda f: f['name'], fields):
+        keys.append('name')
+
+    for of in one_of:
+        # check if the field is a link, and in case add _id
+        for index, f in enumerate(of):
+            try:
+                field = [x for x in fields if x['name'] == f and ('link' in x and x['link'])][0]
+                of[index] = "%s_id" % f
+            except IndexError, e:
+                # the field is not a link
+                pass
+
+        keys.append(of)
+
+    return keys
\ No newline at end of file