Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 15 | from __future__ import absolute_import |
Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 16 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 17 | from multistructlog import create_logger |
| 18 | from xosconfig import Config |
| 19 | |
| 20 | from .resources import RESOURCES |
| 21 | |
| 22 | log = create_logger(Config().get("logging")) |
| 23 | |
| 24 | |
| 25 | class GRPCModelsException(Exception): |
| 26 | """ |
| 27 | Differentiates between exceptions created by GRPCModelsAccessor and other exceptions |
| 28 | """ |
| 29 | pass |
| 30 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 31 | |
| 32 | class GRPCModelsAccessor: |
| 33 | """ |
| 34 | This class provide the glue between the models managed by TOSCA and the ones living in xos-core |
| 35 | """ |
| 36 | |
| 37 | @staticmethod |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 38 | def get_model_from_classname(class_name, data, username, password): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 39 | """ |
| 40 | Give a Model Class Name and some data, check if that exits or instantiate a new one |
| 41 | """ |
| 42 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 43 | # NOTE: we need to import this later as it's generated by main.py |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 44 | from .KEYS import TOSCA_KEYS |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 45 | |
| 46 | # get the key for this model |
| 47 | try: |
| 48 | filter_keys = TOSCA_KEYS[class_name] |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 49 | except KeyError: |
| 50 | raise GRPCModelsException( |
| 51 | "[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name) |
| 52 | ) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 53 | |
| 54 | if len(filter_keys) == 0: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 55 | raise GRPCModelsException( |
| 56 | "[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name) |
| 57 | ) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 58 | |
| 59 | filter = {} |
| 60 | for k in filter_keys: |
Matteo Scandolo | 24197a1 | 2017-12-13 12:21:59 -0800 | [diff] [blame] | 61 | if isinstance(k, str): |
| 62 | try: |
| 63 | filter[k] = data[k] |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 64 | except KeyError as e: |
| 65 | raise GRPCModelsException( |
| 66 | "[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" |
| 67 | % (class_name, e) |
| 68 | ) |
Matteo Scandolo | 24197a1 | 2017-12-13 12:21:59 -0800 | [diff] [blame] | 69 | elif isinstance(k, list): |
| 70 | # one of they keys in this list has to be set |
| 71 | one_of_key = None |
| 72 | for i in k: |
| 73 | if i in data: |
| 74 | one_of_key = i |
| 75 | one_of_key_val = data[i] |
| 76 | if not one_of_key: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 77 | raise GRPCModelsException( |
| 78 | "[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key_one_of (%s)" |
| 79 | % (class_name, k) |
| 80 | ) |
Matteo Scandolo | 24197a1 | 2017-12-13 12:21:59 -0800 | [diff] [blame] | 81 | else: |
| 82 | filter[one_of_key] = one_of_key_val |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 83 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 84 | key = "%s~%s" % (username, password) |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 85 | if key not in RESOURCES: |
| 86 | raise GRPCModelsException( |
| 87 | "[XOS-TOSCA] User '%s' does not have ready resources" % username |
| 88 | ) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 89 | if class_name not in RESOURCES[key]: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 90 | raise GRPCModelsException( |
| 91 | "[XOS-TOSCA] The model you are trying to create (class: %s, properties, %s) is not know by xos-core" |
| 92 | % (class_name, str(filter)) |
| 93 | ) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 94 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 95 | cls = RESOURCES[key][class_name] |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 96 | |
| 97 | models = cls.objects.filter(**filter) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 98 | |
| 99 | if len(models) == 1: |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 100 | model = models[0] |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 101 | log.info( |
| 102 | "[XOS-TOSCA] Model of class %s and properties %s already exist, retrieving instance..." |
| 103 | % (class_name, str(filter)), |
| 104 | model=model, |
| 105 | ) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 106 | elif len(models) == 0: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 107 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 108 | if "must-exist" in data and data["must-exist"]: |
| 109 | raise GRPCModelsException( |
| 110 | "[XOS-TOSCA] Model of class %s and properties %s has property 'must-exist' but cannot be found" |
| 111 | % (class_name, str(filter)) |
| 112 | ) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 113 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 114 | model = cls.objects.new() |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 115 | log.info( |
| 116 | "[XOS-TOSCA] Model (%s) is new, creating new instance..." % str(filter) |
| 117 | ) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 118 | else: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 119 | raise GRPCModelsException( |
| 120 | "[XOS-TOSCA] Model of class %s and properties %s has multiple instances, I can't handle it" |
| 121 | % (class_name, str(filter)) |
| 122 | ) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 123 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame^] | 124 | return model |