blob: 5d54644ef89836072d3831add73089cebb17207c [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
16
Matteo Scandolo485b7132017-06-30 11:46:47 -070017from resources import RESOURCES
18
19class 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 Scandolo21dde412017-07-11 18:54:12 -070025 def get_model_from_classname(class_name, data, username, password):
Matteo Scandolo485b7132017-06-30 11:46:47 -070026 """
27 Give a Model Class Name and some data, check if that exits or instantiate a new one
28 """
29
Matteo Scandolo1bd10762017-10-18 09:53:14 +020030 # 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 Scandolo24197a12017-12-13 12:21:59 -080044 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 Scandolo485b7132017-06-30 11:46:47 -070060
Matteo Scandolo21dde412017-07-11 18:54:12 -070061 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 Scandolo1bd10762017-10-18 09:53:14 +020065 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 -070066
Matteo Scandolo21dde412017-07-11 18:54:12 -070067 cls = RESOURCES[key][class_name]
Matteo Scandolo1bd10762017-10-18 09:53:14 +020068
69 models = cls.objects.filter(**filter)
Matteo Scandolo485b7132017-06-30 11:46:47 -070070
71 if len(models) == 1:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020072 print "[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter))
Matteo Scandolo485b7132017-06-30 11:46:47 -070073 model = models[0]
74 elif len(models) == 0:
Matteo Scandolod12be212017-07-07 10:44:34 -070075
76 if 'must-exist' in data and data['must-exist']:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020077 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 -070078
Matteo Scandolo485b7132017-06-30 11:46:47 -070079 model = cls.objects.new()
Matteo Scandolo1bd10762017-10-18 09:53:14 +020080 print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter)
Matteo Scandolo485b7132017-06-30 11:46:47 -070081 else:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020082 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 -070083
Matteo Scandolo485b7132017-06-30 11:46:47 -070084 return model