blob: 918c04d2d20aa180f09851d657fbec07364db83d [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:
44 try:
45 filter[k] = data[k]
46 except KeyError, e:
47 raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" % (class_name, e))
Matteo Scandolo485b7132017-06-30 11:46:47 -070048
Matteo Scandolo21dde412017-07-11 18:54:12 -070049 key = "%s~%s" % (username, password)
50 if not key in RESOURCES:
51 raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username)
52 if class_name not in RESOURCES[key]:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020053 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 -070054
Matteo Scandolo21dde412017-07-11 18:54:12 -070055 cls = RESOURCES[key][class_name]
Matteo Scandolo1bd10762017-10-18 09:53:14 +020056
57 models = cls.objects.filter(**filter)
Matteo Scandolo485b7132017-06-30 11:46:47 -070058
59 if len(models) == 1:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020060 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 -070061 model = models[0]
62 elif len(models) == 0:
Matteo Scandolod12be212017-07-07 10:44:34 -070063
64 if 'must-exist' in data and data['must-exist']:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020065 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 -070066
Matteo Scandolo485b7132017-06-30 11:46:47 -070067 model = cls.objects.new()
Matteo Scandolo1bd10762017-10-18 09:53:14 +020068 print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter)
Matteo Scandolo485b7132017-06-30 11:46:47 -070069 else:
Matteo Scandolo1bd10762017-10-18 09:53:14 +020070 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 -070071
Matteo Scandolo485b7132017-06-30 11:46:47 -070072 return model