blob: 2c96c48d9bd5afeea82f454fff258220635b20af [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001
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 Scandolo5a07a2c2018-06-05 18:04:00 -070016from xosconfig import Config
17from multistructlog import create_logger
18log = create_logger(Config().get('logging'))
Matteo Scandolo920e8fd2017-08-08 13:05:24 -070019
Matteo Scandolo485b7132017-06-30 11:46:47 -070020from resources import RESOURCES
21
22class 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 Scandolo21dde412017-07-11 18:54:12 -070028 def get_model_from_classname(class_name, data, username, password):
Matteo Scandolo485b7132017-06-30 11:46:47 -070029 """
30 Give a Model Class Name and some data, check if that exits or instantiate a new one
31 """
32
Matteo Scandolo1bd10762017-10-18 09:53:14 +020033 # 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 Scandolo24197a12017-12-13 12:21:59 -080047 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 Scandolo485b7132017-06-30 11:46:47 -070063
Matteo Scandolo21dde412017-07-11 18:54:12 -070064 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 Scandolo1bd10762017-10-18 09:53:14 +020068 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 Scandolo485b7132017-06-30 11:46:47 -070069
Matteo Scandolo21dde412017-07-11 18:54:12 -070070 cls = RESOURCES[key][class_name]
Matteo Scandolo1bd10762017-10-18 09:53:14 +020071
72 models = cls.objects.filter(**filter)
Matteo Scandolo485b7132017-06-30 11:46:47 -070073
74 if len(models) == 1:
Matteo Scandolo485b7132017-06-30 11:46:47 -070075 model = models[0]
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070076 log.info("[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter)), model=model)
Matteo Scandolo485b7132017-06-30 11:46:47 -070077 elif len(models) == 0:
Matteo Scandolod12be212017-07-07 10:44:34 -070078
79 if 'must-exist' in data and data['must-exist']:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020080 raise Exception("[XOS-TOSCA] Model of class %s and properties %s has property 'must-exist' but cannot be found" % (class_name, str(filter)))
Matteo Scandolod12be212017-07-07 10:44:34 -070081
Matteo Scandolo485b7132017-06-30 11:46:47 -070082 model = cls.objects.new()
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070083 log.info("[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter))
Matteo Scandolo485b7132017-06-30 11:46:47 -070084 else:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020085 raise Exception("[XOS-Tosca] Model of class %s and properties %s has multiple instances, I can't handle it" % (class_name, str(filter)))
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070086
Matteo Scandolo485b7132017-06-30 11:46:47 -070087 return model