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: |
Matteo Scandolo | 24197a1 | 2017-12-13 12:21:59 -0800 | [diff] [blame] | 44 | if isinstance(k, str): |
| 45 | try: |
| 46 | filter[k] = data[k] |
| 47 | except KeyError, e: |
| 48 | raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" % (class_name, e)) |
| 49 | elif isinstance(k, list): |
| 50 | # one of they keys in this list has to be set |
| 51 | one_of_key = None |
| 52 | for i in k: |
| 53 | if i in data: |
| 54 | one_of_key = i |
| 55 | one_of_key_val = data[i] |
| 56 | if not one_of_key: |
| 57 | raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key_one_of (%s)" % (class_name, k)) |
| 58 | else: |
| 59 | filter[one_of_key] = one_of_key_val |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 61 | key = "%s~%s" % (username, password) |
| 62 | if not key in RESOURCES: |
| 63 | raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username) |
| 64 | if class_name not in RESOURCES[key]: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 65 | 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] | 66 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 67 | cls = RESOURCES[key][class_name] |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 68 | |
| 69 | models = cls.objects.filter(**filter) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 70 | |
| 71 | if len(models) == 1: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 72 | 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] | 73 | model = models[0] |
| 74 | elif len(models) == 0: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 75 | |
| 76 | if 'must-exist' in data and data['must-exist']: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 77 | 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] | 78 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 79 | model = cls.objects.new() |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 80 | print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 81 | else: |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 82 | 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] | 83 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 84 | return model |