[CORD-2080] Adding 'tosca_key' support in xproto
Change-Id: I2c27e4a43140ddecb38f09a33494e5b3ab694059
diff --git a/docs/dev/xproto.md b/docs/dev/xproto.md
index 2468159..68fb986 100644
--- a/docs/dev/xproto.md
+++ b/docs/dev/xproto.md
@@ -178,9 +178,20 @@
```protobuf
option validators = “port_validator:Slice is not allowed to connect to network”;
```
-
How policies (e.g., `port_validator`) are specified is described below.
+Whether a field should be shown in the GUI:
+
+```protobuf
+option gui_hidden = True;
+```
+
+Identify a field that is used as key by the TOSCA engine. A model can have multiple keys in case we need a composite key:
+
+```protobuf
+option tosca_key = True;
+```
+
### Naming Conventions
Model names should use _CamelCase_ without underscore. Model names should always
diff --git a/lib/xos-genx/tests/tosca_test.py b/lib/xos-genx/tests/tosca_test.py
new file mode 100644
index 0000000..48a4db9
--- /dev/null
+++ b/lib/xos-genx/tests/tosca_test.py
@@ -0,0 +1,91 @@
+# 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.
+
+import unittest
+from xosgenx.generator import XOSGenerator
+from helpers import FakeArgs, XProtoTestHelpers
+
+
+class XProtoToscaTest(unittest.TestCase):
+
+ def setUp(self):
+ self.target = XProtoTestHelpers.write_tmp_target(
+"""
+{%- for m in proto.messages %}
+ {{ xproto_fields_to_tosca_keys(m.fields) }}
+{% endfor -%}
+""")
+
+ def test_xproto_fields_to_tosca_keys_default(self):
+ """
+ [XOS-GenX] if no "tosca_key" is specified, and a name attribute is present in the model, use that
+ """
+ xproto = \
+"""
+option app_label = "test";
+
+message Foo {
+ required string name = 1 [ null = "False", blank="False"];
+}
+"""
+
+ args = FakeArgs()
+ args.inputs = xproto
+ args.target = self.target
+ output = XOSGenerator.generate(args)
+ self.assertIn('name', output)
+
+ def test_xproto_fields_to_tosca_keys_custom(self):
+ """
+ [XOS-GenX] if "tosca_key" is specified, use it
+ """
+ xproto = \
+ """
+ option app_label = "test";
+
+ message Foo {
+ required string name = 1 [ null = "False", blank="False"];
+ required string key_1 = 2 [ null = "False", blank="False", tosca_key=True];
+ required string key_2 = 3 [ null = "False", blank="False", tosca_key=True];
+ }
+ """
+
+ args = FakeArgs()
+ args.inputs = xproto
+ args.target = self.target
+ output = XOSGenerator.generate(args)
+ self.assertNotIn('name', output)
+ self.assertIn('key_1', output)
+ self.assertIn('key_2', output)
+
+ def test_xproto_fields_link_to_tosca_keys_custom(self):
+ """
+ [XOS-GenX] if "tosca_key" is specified, use it
+ """
+ xproto = \
+ """
+ option app_label = "test";
+
+ message Foo {
+ required string name = 1 [ null = "False", blank="False"];
+ required manytoone provider_service_instance->ServiceInstance:provided_links = 1 [db_index = True, null = False, blank = False, tosca_key=True];
+ }
+ """
+
+ args = FakeArgs()
+ args.inputs = xproto
+ args.target = self.target
+ output = XOSGenerator.generate(args)
+ self.assertNotIn('name', output)
+ self.assertIn('provider_service_instance_id', output)
\ No newline at end of file
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py b/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
index 79af1ec..a81dcb6 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/__init__.py
@@ -18,3 +18,4 @@
from .base import *
from .fol2 import *
from .gui import *
+from .tosca import *
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py b/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
new file mode 100644
index 0000000..04ac3ff
--- /dev/null
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/tosca.py
@@ -0,0 +1,26 @@
+# 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.
+
+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
diff --git a/xos/core/models/core.xproto b/xos/core/models/core.xproto
index 71aa6c0..6713c6b 100644
--- a/xos/core/models/core.xproto
+++ b/xos/core/models/core.xproto
@@ -38,7 +38,7 @@
option skip_django = True;
option description = "An XOS User";
- required string email = 1 [db_index = True, max_length = 255, null = False, blank = False];
+ required string email = 1 [db_index = True, max_length = 255, null = False, blank = False, tosca_key=True];
required string username = 2 [default = "Something", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
required string password = 3 [default = "Something", max_length = 255, blank = False, null = False, db_index = False];
optional string last_login = 4 [db_index = False, null = True, content_type = "date", blank = True];
@@ -86,7 +86,7 @@
required int32 controller_id = 3 [null = True];
required int32 object_id = 4 [null = False];
required string object_type = 5 [null = False, max_length=1024];
- required string permission = 6 [null = False, default = "all", max_length=1024];
+ required string permission = 6 [null = False, default = "all", max_length=1024, tosca_key=True];
required string granted = 7 [content_type = "date", auto_now_add = True, max_length=1024];
required string expires = 8 [content_type = "date", null = True, max_length=1024];
}
@@ -356,8 +356,8 @@
message NetworkSlice::network_slice_policy (XOSBase) {
option validators = "network_slice_validator:Slice { obj.slice.name } is not allowed to connect to networks { obj.network }";
- required manytoone network->Network:networkslices = 1 [db_index = True, null = False, blank = False, unique_with = "slice"];
- required manytoone slice->Slice:networkslices = 2 [db_index = True, null = False, blank = False];
+ required manytoone network->Network:networkslices = 1 [db_index = True, null = False, blank = False, unique_with = "slice", tosca_key=True];
+ required manytoone slice->Slice:networkslices = 2 [db_index = True, null = False, blank = False, tosca_key=True];
}
message NetworkTemplate (XOSBase) {
@@ -430,7 +430,7 @@
message ServiceDependency (XOSBase) {
- required manytoone provider_service->Service:provided_dependencies = 1 [help_text = "The service that provides this dependency", null=False, db_index = True, blank=False];
+ required manytoone provider_service->Service:provided_dependencies = 1 [help_text = "The service that provides this dependency", null=False, db_index = True, blank=False, tosca_key=True];
required manytoone subscriber_service->Service:subscribed_dependencies = 2 [help_text = "The services that subscribes to this dependency", null=False, db_index=True, blank=False];
required string connect_method = 3 [max_length = 30, help_text = "method to connect the two services", null=False, blank=False, default="none", choices = "(('none', 'None'), ('private', 'Private'), ('public', 'Public'))"];
}
@@ -472,8 +472,8 @@
message SiteDeployment (XOSBase) {
- required manytoone site->Site:sitedeployments = 1 [db_index = True, null = False, blank = False, unique_with = "deployment"];
- required manytoone deployment->Deployment:sitedeployments = 2 [db_index = True, null = False, blank = False, unique_with = "controller"];
+ required manytoone site->Site:sitedeployments = 1 [db_index = True, null = False, blank = False, unique_with = "deployment", tosca_key=True];
+ required manytoone deployment->Deployment:sitedeployments = 2 [db_index = True, null = False, blank = False, unique_with = "controller", tosca_key=True];
optional manytoone controller->Controller:sitedeployments = 3 [db_index = True, null = True, blank = True];
optional string availability_zone = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack availability zone", null = True, db_index = False];
}
@@ -481,8 +481,8 @@
message SitePrivilege (XOSBase) {
required manytoone user->User:siteprivileges = 1 [db_index = True, null = False, blank = False];
- required manytoone site->Site:siteprivileges = 2 [db_index = True, null = False, blank = False];
- required manytoone role->SiteRole:siteprivileges = 3 [db_index = True, null = False, blank = False];
+ required manytoone site->Site:siteprivileges = 2 [db_index = True, null = False, blank = False, tosca_key=True];
+ required manytoone role->SiteRole:siteprivileges = 3 [db_index = True, null = False, blank = False, tosca_key=True];
}
@@ -545,8 +545,8 @@
}
message ServiceInterface (XOSBase) {
- required manytoone service->Service:service_interfaces = 1 [db_index = True, null = False, blank = False];
- required manytoone interface_type->InterfaceType:service_interfaces = 2 [db_index = True, null = False, blank = False];
+ required manytoone service->Service:service_interfaces = 1 [db_index = True, null = False, blank = False, tosca_key=True];
+ required manytoone interface_type->InterfaceType:service_interfaces = 2 [db_index = True, null = False, blank = False, tosca_key=True];
}
message ServiceInstance (XOSBase, AttributeMixin) {
@@ -558,7 +558,7 @@
}
message ServiceInstanceLink (XOSBase) {
- required manytoone provider_service_instance->ServiceInstance:provided_links = 1 [db_index = True, null = False, blank = False];
+ required manytoone provider_service_instance->ServiceInstance:provided_links = 1 [db_index = True, null = False, blank = False, tosca_key=True];
optional manytoone provider_service_interface->ServiceInterface:provided_links = 2 [db_index = True, null = True, blank = True];
optional manytoone subscriber_service_instance->ServiceInstance:subscribed_links = 3 [db_index = True, null = True, blank = True];
optional manytoone subscriber_service->Service:subscribed_links = 4 [db_index = True, null = True, blank = True];