blob: b51ee90c506e7c9cd56f3fc4f2af45cc1b1b708b [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 Scandolo485b7132017-06-30 11:46:47 -070030 if data.get('name'):
31 used_key = 'name'
32 else:
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070033 if len(data.keys()) > 0:
34 # FIXME apparently we're not matching model without a name field
35 used_key = data.keys()[0]
36 else:
37 used_key = None
Matteo Scandolo485b7132017-06-30 11:46:47 -070038
Matteo Scandolo21dde412017-07-11 18:54:12 -070039 key = "%s~%s" % (username, password)
40 if not key in RESOURCES:
41 raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username)
42 if class_name not in RESOURCES[key]:
Matteo Scandolod12be212017-07-07 10:44:34 -070043 raise Exception('[XOS-TOSCA] The model you are trying to create (%s: %s, class: %s) is not know by xos-core' % (used_key, data[used_key], class_name))
Matteo Scandolo485b7132017-06-30 11:46:47 -070044
Matteo Scandolo21dde412017-07-11 18:54:12 -070045 cls = RESOURCES[key][class_name]
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070046 if used_key:
47 models = cls.objects.filter(**{used_key: data[used_key]})
48 else:
49 # NOTE if we don't have a way to track the model, create a new one
50 models = []
Matteo Scandolo485b7132017-06-30 11:46:47 -070051
52 if len(models) == 1:
53 print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key]
54 model = models[0]
55 elif len(models) == 0:
Matteo Scandolod12be212017-07-07 10:44:34 -070056
57 if 'must-exist' in data and data['must-exist']:
58 raise Exception("[XOS-TOSCA] Model %s:%s has property 'must-exist' but cannot be found" % (class_name, data[used_key]))
59
Matteo Scandolo485b7132017-06-30 11:46:47 -070060 model = cls.objects.new()
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070061 print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key] if used_key else class_name
Matteo Scandolo485b7132017-06-30 11:46:47 -070062 else:
63 raise Exception("[XOS-Tosca] Model %s has multiple instances, I can't handle it" % data[used_key])
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070064
Matteo Scandolo485b7132017-06-30 11:46:47 -070065 return model