Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 17 | from resources import RESOURCES |
| 18 | |
| 19 | class GRPCModelsAccessor: |
| 20 | """ |
| 21 | This class provide the glue between the models managed by TOSCA and the ones living in xos-core |
| 22 | """ |
| 23 | |
| 24 | @staticmethod |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 25 | def get_model_from_classname(class_name, data, username, password): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 26 | """ |
| 27 | Give a Model Class Name and some data, check if that exits or instantiate a new one |
| 28 | """ |
| 29 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 30 | # NOTE: we need to import this later as it's generated by main.py |
| 31 | from KEYS import TOSCA_KEYS |
| 32 | |
| 33 | # get the key for this model |
| 34 | try: |
| 35 | filter_keys = TOSCA_KEYS[class_name] |
| 36 | except KeyError, e: |
| 37 | raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name)) |
| 38 | |
| 39 | if len(filter_keys) == 0: |
| 40 | raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name)) |
| 41 | |
| 42 | filter = {} |
| 43 | for k in filter_keys: |
| 44 | try: |
| 45 | filter[k] = data[k] |
| 46 | except KeyError, e: |
| 47 | raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" % (class_name, e)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 48 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 49 | key = "%s~%s" % (username, password) |
| 50 | if not key in RESOURCES: |
| 51 | raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username) |
| 52 | if class_name not in RESOURCES[key]: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 53 | 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))) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 54 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 55 | cls = RESOURCES[key][class_name] |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 56 | |
| 57 | models = cls.objects.filter(**filter) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 58 | |
| 59 | if len(models) == 1: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 60 | print "[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 61 | model = models[0] |
| 62 | elif len(models) == 0: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 63 | |
| 64 | if 'must-exist' in data and data['must-exist']: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 65 | raise Exception("[XOS-TOSCA] Model of class %s and properties %s has property 'must-exist' but cannot be found" % (class_name, str(filter))) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 66 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 67 | model = cls.objects.new() |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 68 | print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 69 | else: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 70 | raise Exception("[XOS-Tosca] Model of class %s and properties %s has multiple instances, I can't handle it" % (class_name, str(filter))) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 71 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 72 | return model |