[CORD-2080] Adding 'tosca_key' support in xproto
Change-Id: Ib01c1cdcb81a51819c9e08703e6c76a4ffa09f17
diff --git a/src/grpc_client/KEYS.reference.py b/src/grpc_client/KEYS.reference.py
new file mode 100644
index 0000000..fd8c776
--- /dev/null
+++ b/src/grpc_client/KEYS.reference.py
@@ -0,0 +1,75 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#########################################################################
+# #
+# This file is here for reference, the used one is generate by xos-genx #
+# #
+#########################################################################
+
+TOSCA_KEYS = {
+ 'XOSBase': [],
+ 'User': ['email'],
+ 'Privilege': [],
+ 'AddressPool': ['name'],
+ 'Controller': ['name'],
+ 'ControllerImages': [],
+ 'ControllerNetwork': [],
+ 'ControllerRole': [],
+ 'ControllerSite': [],
+ 'ControllerPrivilege': [],
+ 'ControllerSitePrivilege': [],
+ 'ControllerSlice': [],
+ 'ControllerSlicePrivilege': [],
+ 'ControllerUser': [],
+ 'Deployment': ['name'],
+ 'DeploymentPrivilege': [],
+ 'DeploymentRole': [],
+ 'Diag': ['name'],
+ 'Flavor': ['name'],
+ 'Image': ['name'],
+ 'ImageDeployments': [],
+ 'Instance': ['name'],
+ 'Network': ['name'],
+ 'NetworkParameter': [],
+ 'NetworkParameterType': ['name'],
+ 'NetworkSlice': ['network', 'slice'],
+ 'NetworkTemplate': ['name'],
+ 'Node': ['name'],
+ 'NodeLabel': ['name'],
+ 'Port': [],
+ 'Role': [],
+ 'Service': ['name'],
+ 'ServiceAttribute': ['name'],
+ 'ServiceDependency': ['provider_service'],
+ 'ServiceMonitoringAgentInfo': ['name'],
+ 'ServicePrivilege': [],
+ 'ServiceRole': [],
+ 'Site': ['name'],
+ 'SiteDeployment': ['site', 'deployment'],
+ 'SitePrivilege': ['site', 'role'],
+ 'SiteRole': [],
+ 'Slice': ['name'],
+ 'SlicePrivilege': [],
+ 'SliceRole': [],
+ 'Tag': ['name'],
+ 'InterfaceType': ['name'],
+ 'ServiceInterface': ['service', 'interface_type'],
+ 'ServiceInstance': ['name'],
+ 'ServiceInstanceLink': ['provider_service_instance'],
+ 'ServiceInstanceAttribute': ['name'],
+ 'TenantWithContainer': ['name'],
+ 'XOS': ['name'],
+ 'XOSGuiExtension': ['name'],
+}
\ No newline at end of file
diff --git a/src/grpc_client/models_accessor.py b/src/grpc_client/models_accessor.py
index b51ee90..918c04d 100644
--- a/src/grpc_client/models_accessor.py
+++ b/src/grpc_client/models_accessor.py
@@ -27,39 +27,46 @@
Give a Model Class Name and some data, check if that exits or instantiate a new one
"""
- if data.get('name'):
- used_key = 'name'
- else:
- if len(data.keys()) > 0:
- # FIXME apparently we're not matching model without a name field
- used_key = data.keys()[0]
- else:
- used_key = None
+ # NOTE: we need to import this later as it's generated by main.py
+ from KEYS import TOSCA_KEYS
+
+ # get the key for this model
+ try:
+ filter_keys = TOSCA_KEYS[class_name]
+ except KeyError, e:
+ raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name))
+
+ if len(filter_keys) == 0:
+ raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name))
+
+ filter = {}
+ for k in filter_keys:
+ try:
+ filter[k] = data[k]
+ except KeyError, e:
+ raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" % (class_name, e))
key = "%s~%s" % (username, password)
if not key in RESOURCES:
raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username)
if class_name not in RESOURCES[key]:
- raise Exception('[XOS-TOSCA] The model you are trying to create (%s: %s, class: %s) is not know by xos-core' % (used_key, data[used_key], class_name))
+ raise Exception('[XOS-TOSCA] The model you are trying to create (class: %s, properties, %s) is not know by xos-core' % (class_name, str(filter)))
cls = RESOURCES[key][class_name]
- if used_key:
- models = cls.objects.filter(**{used_key: data[used_key]})
- else:
- # NOTE if we don't have a way to track the model, create a new one
- models = []
+
+ models = cls.objects.filter(**filter)
if len(models) == 1:
- print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key]
+ print "[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter))
model = models[0]
elif len(models) == 0:
if 'must-exist' in data and data['must-exist']:
- raise Exception("[XOS-TOSCA] Model %s:%s has property 'must-exist' but cannot be found" % (class_name, data[used_key]))
+ raise Exception("[XOS-TOSCA] Model of class %s and properties %s has property 'must-exist' but cannot be found" % (class_name, str(filter)))
model = cls.objects.new()
- print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key] if used_key else class_name
+ print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter)
else:
- raise Exception("[XOS-Tosca] Model %s has multiple instances, I can't handle it" % data[used_key])
+ raise Exception("[XOS-Tosca] Model of class %s and properties %s has multiple instances, I can't handle it" % (class_name, str(filter)))
return model
\ No newline at end of file
diff --git a/src/tosca/default.py b/src/tosca/default.py
index 6bb789c..4de0975 100644
--- a/src/tosca/default.py
+++ b/src/tosca/default.py
@@ -17,4 +17,5 @@
import os
TOSCA_DEFS_DIR = os.path.dirname(os.path.realpath(__file__)) + "/custom_types"
-TOSCA_RECIPES_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"
\ No newline at end of file
+TOSCA_RECIPES_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"
+TOSCA_KEYS_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/../grpc_client")
\ No newline at end of file
diff --git a/src/tosca/generator.py b/src/tosca/generator.py
index 12e2442..6aa309c 100644
--- a/src/tosca/generator.py
+++ b/src/tosca/generator.py
@@ -15,7 +15,7 @@
import os
-from default import TOSCA_DEFS_DIR
+from default import TOSCA_DEFS_DIR, TOSCA_KEYS_DIR
from xosgenx.generator import XOSGenerator
from xosapi.xos_grpc_client import Empty
@@ -36,7 +36,6 @@
print "[XOS-TOSCA] Generating TOSCA"
self._clean()
-
try:
xproto = client.utility.GetXproto(Empty())
args = Args()
@@ -50,3 +49,17 @@
print "[XOS-TOSCA] Failed to generate TOSCA"
print e
+ try:
+ xproto = client.utility.GetXproto(Empty())
+ args = Args()
+ args.output = TOSCA_KEYS_DIR
+ args.inputs = str(xproto.xproto)
+ args.target = os.path.join(current_dir, 'xtarget/tosca_keys.xtarget')
+ args.write_to_file = 'single'
+ args.dest_file = 'KEYS.py'
+ XOSGenerator.generate(args)
+ print "[XOS-TOSCA] TOSCA Keys generated in %s" % args.output
+ except Exception as e:
+ print "[XOS-TOSCA] Failed to generate TOSCA Keys"
+ print e
+
diff --git a/src/tosca/parser.py b/src/tosca/parser.py
index acbd5f1..b30f716 100644
--- a/src/tosca/parser.py
+++ b/src/tosca/parser.py
@@ -147,6 +147,14 @@
setattr(model, "%s_id" % class_name, related_model.id)
return model
+ @staticmethod
+ def add_dependencies(data, requirements, saved_models):
+ for dep in requirements:
+ class_name = dep.keys()[0]
+ related_model = saved_models[dep[class_name]['node']]
+ data["%s_id" % class_name] = related_model.id
+ return data
+
def __init__(self, recipe, username, password, **kwargs):
self.delete = False
@@ -198,6 +206,10 @@
data = {}
# [] get model by class name
class_name = recipe.type.replace("tosca.nodes.", "")
+
+ # augemnt data with relations
+ data = self.add_dependencies(data, recipe.requirements, self.saved_model_by_name)
+
model = GRPCModelsAccessor.get_model_from_classname(class_name, data, self.username, self.password)
# [] populate model with data
model = self.populate_model(model, data)
diff --git a/src/tosca/xtarget/tosca_keys.xtarget b/src/tosca/xtarget/tosca_keys.xtarget
new file mode 100644
index 0000000..ad235e5
--- /dev/null
+++ b/src/tosca/xtarget/tosca_keys.xtarget
@@ -0,0 +1,5 @@
+TOSCA_KEYS = {
+{%- for m in proto.messages %}
+ '{{ m.name }}': {{ xproto_fields_to_tosca_keys(m.fields + xproto_base_fields(m, proto.message_table)) }},
+{%- endfor %}
+}
\ No newline at end of file