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 | |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 16 | from xosconfig import Config |
| 17 | from multistructlog import create_logger |
| 18 | log = create_logger(Config().get('logging')) |
Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 19 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 20 | from resources import RESOURCES |
| 21 | |
| 22 | class GRPCModelsAccessor: |
| 23 | """ |
| 24 | This class provide the glue between the models managed by TOSCA and the ones living in xos-core |
| 25 | """ |
| 26 | |
| 27 | @staticmethod |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 28 | def get_model_from_classname(class_name, data, username, password): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 29 | """ |
| 30 | Give a Model Class Name and some data, check if that exits or instantiate a new one |
| 31 | """ |
| 32 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 33 | # NOTE: we need to import this later as it's generated by main.py |
| 34 | from KEYS import TOSCA_KEYS |
| 35 | |
| 36 | # get the key for this model |
| 37 | try: |
| 38 | filter_keys = TOSCA_KEYS[class_name] |
| 39 | except KeyError, e: |
| 40 | raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name)) |
| 41 | |
| 42 | if len(filter_keys) == 0: |
| 43 | raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name)) |
| 44 | |
| 45 | filter = {} |
| 46 | for k in filter_keys: |
Matteo Scandolo | 24197a1 | 2017-12-13 12:21:59 -0800 | [diff] [blame] | 47 | if isinstance(k, str): |
| 48 | try: |
| 49 | filter[k] = data[k] |
| 50 | except KeyError, e: |
| 51 | raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" % (class_name, e)) |
| 52 | elif isinstance(k, list): |
| 53 | # one of they keys in this list has to be set |
| 54 | one_of_key = None |
| 55 | for i in k: |
| 56 | if i in data: |
| 57 | one_of_key = i |
| 58 | one_of_key_val = data[i] |
| 59 | if not one_of_key: |
| 60 | raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key_one_of (%s)" % (class_name, k)) |
| 61 | else: |
| 62 | filter[one_of_key] = one_of_key_val |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 63 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 64 | key = "%s~%s" % (username, password) |
| 65 | if not key in RESOURCES: |
| 66 | raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username) |
| 67 | if class_name not in RESOURCES[key]: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 68 | 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] | 69 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 70 | cls = RESOURCES[key][class_name] |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 71 | |
| 72 | models = cls.objects.filter(**filter) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 73 | |
| 74 | if len(models) == 1: |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 75 | model = models[0] |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 76 | log.info("[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter)), model=model) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 77 | elif len(models) == 0: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 78 | |
| 79 | if 'must-exist' in data and data['must-exist']: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 80 | 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] | 81 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 82 | model = cls.objects.new() |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 83 | log.info("[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 84 | else: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 85 | 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] | 86 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 87 | return model |