blob: 7c142431c038c0cc1006d70c57664ec8aacf8369 [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 Scandolod12be212017-07-07 10:44:34 -070017from toscaparser.tosca_template import ToscaTemplate, ValidationError
Matteo Scandolo9ce18252017-06-22 10:48:25 -070018from default import TOSCA_RECIPES_DIR
Matteo Scandolo485b7132017-06-30 11:46:47 -070019from grpc_client.resources import RESOURCES
20from grpc_client.models_accessor import GRPCModelsAccessor
Matteo Scandolo21dde412017-07-11 18:54:12 -070021from grpc._channel import _Rendezvous
22import json
Matteo Scandolo9ce18252017-06-22 10:48:25 -070023
24class TOSCA_Parser:
25
Matteo Scandolo485b7132017-06-30 11:46:47 -070026 def compute_dependencies(self, template, models_by_name):
27 """
28 NOTE this method is augmenting self.template, isn't there a more explicit way to achieve it?
29 """
30 for nodetemplate in template.nodetemplates:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070031 nodetemplate.dependencies = []
32 nodetemplate.dependencies_names = []
33 for reqs in nodetemplate.requirements:
34 for (k,v) in reqs.items():
35 name = v["node"]
Matteo Scandolo485b7132017-06-30 11:46:47 -070036 if (name in models_by_name):
37 nodetemplate.dependencies.append(models_by_name[name])
Matteo Scandolo9ce18252017-06-22 10:48:25 -070038 nodetemplate.dependencies_names.append(name)
39
40 # go another level deep, as our requirements can have requirements...
Matteo Scandolodf2600b2017-07-05 17:01:29 -070041 # NOTE do we still need to go deep?
Matteo Scandolo9ce18252017-06-22 10:48:25 -070042 for sd_req in v.get("requirements",[]):
43 for (sd_req_k, sd_req_v) in sd_req.items():
44 name = sd_req_v["node"]
Matteo Scandolo485b7132017-06-30 11:46:47 -070045 if (name in models_by_name):
46 nodetemplate.dependencies.append(models_by_name[name])
Matteo Scandolo9ce18252017-06-22 10:48:25 -070047 nodetemplate.dependencies_names.append(name)
48
Matteo Scandolo485b7132017-06-30 11:46:47 -070049 @staticmethod
50 def topsort_dependencies(g):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070051
52 # Get set of all nodes, including those without outgoing edges
53 keys = set(g.keys())
54 values = set({})
55 for v in g.values():
56 values = values | set(v.dependencies_names)
57
58 all_nodes = list(keys | values)
59 steps = all_nodes
60
61
62 # Final order
63 order = []
64
65 # DFS stack, not using recursion
66 stack = []
67
68 # Unmarked set
69 unmarked = all_nodes
70
71 # visiting = [] - skip, don't expect 1000s of nodes, |E|/|V| is small
72
73 while unmarked:
74 stack.insert(0, unmarked[0]) # push first unmarked
75
76 while (stack):
77 n = stack[0]
78 add = True
79 try:
80 for m in g[n].dependencies_names:
81 if (m in unmarked):
82 add = False
83 stack.insert(0, m)
84 except KeyError:
85 pass
86 if (add):
87 if (n in steps and n not in order):
88 order.append(n)
89 item = stack.pop(0)
90 try:
91 unmarked.remove(item)
92 except ValueError:
93 pass
94
95 noorder = list(set(steps) - set(order))
96 return order + noorder
97
Matteo Scandolo9ce18252017-06-22 10:48:25 -070098 @staticmethod
99 def populate_model(model, data):
100 for k,v in data.iteritems():
101 setattr(model, k, v)
102 return model
103
104 @staticmethod
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700105 def _translate_exception(msg):
106 readable = []
107 for line in msg.splitlines():
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700108 if line.strip().startswith('MissingRequiredFieldError'):
109 readable.append(line)
Matteo Scandolod12be212017-07-07 10:44:34 -0700110 if line.strip().startswith('UnknownFieldError'):
111 readable.append(line)
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700112
113 if len(readable) > 0:
114 return '/n'.join(readable)
115 else:
116 return msg
117
Matteo Scandolo485b7132017-06-30 11:46:47 -0700118 def save_recipe_to_tmp_file(self, recipe):
119 tmp_file = open(self.recipe_file, 'w')
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700120 tmp_file.write(recipe)
121 tmp_file.close()
122
Matteo Scandolo485b7132017-06-30 11:46:47 -0700123 @staticmethod
124 def get_tosca_models_by_name(template):
125 models_by_name = {}
126 for node in template.nodetemplates:
127 models_by_name[node.name] = node
128 return models_by_name
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700129
Matteo Scandolo485b7132017-06-30 11:46:47 -0700130 @staticmethod
131 def get_ordered_models_template(ordered_models_name, templates_by_model_name):
132 ordered_models_templates = []
133 for name in ordered_models_name:
134 if name in templates_by_model_name:
135 ordered_models_templates.append(templates_by_model_name[name])
136 return ordered_models_templates
137
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700138 @staticmethod
139 def populate_dependencies(model, requirements, saved_models):
140 for dep in requirements:
141 class_name = dep.keys()[0]
142 related_model = saved_models[dep[class_name]['node']]
143 setattr(model, "%s_id" % class_name, related_model.id)
144 return model
Matteo Scandolo485b7132017-06-30 11:46:47 -0700145
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -0700146 def __init__(self, recipe, username, password, **kwargs):
147
148 self.delete = False
149 if 'delete' in kwargs:
150 self.delete = True
Matteo Scandolo21dde412017-07-11 18:54:12 -0700151
152 # store username/password combination to read resources
153 self.username = username
154 self.password = password
Matteo Scandolo485b7132017-06-30 11:46:47 -0700155
156 # the template returned by TOSCA-Parser
157 self.template = None
158 # dictionary containing the models in the recipe and their template
159 self.templates_by_model_name = None
160 # list of models ordered by requirements
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700161 self.ordered_models_name = []
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700162 # dictionary containing the saved model
163 self.saved_model_by_name = {}
Matteo Scandolo485b7132017-06-30 11:46:47 -0700164
165 self.ordered_models_template = []
166 self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml'
167
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700168 self.recipe = recipe
169
170 def execute(self):
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -0700171
Matteo Scandolo485b7132017-06-30 11:46:47 -0700172 try:
173 # [] save the recipe to a tmp file
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700174 self.save_recipe_to_tmp_file(self.recipe)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700175 # [] parse the recipe with TOSCA Parse
176 self.template = ToscaTemplate(self.recipe_file)
177 # [] get all models in the recipe
178 self.templates_by_model_name = self.get_tosca_models_by_name(self.template)
179 # [] compute requirements
180 self.compute_dependencies(self.template, self.templates_by_model_name)
181 # [] topsort requirements
182 self.ordered_models_name = self.topsort_dependencies(self.templates_by_model_name)
183 # [] topsort templates
184 self.ordered_models_template = self.get_ordered_models_template(self.ordered_models_name, self.templates_by_model_name)
185
186 for recipe in self.ordered_models_template:
187 # get properties from tosca
188 data = recipe.templates[recipe.name]['properties']
189 # [] get model by class name
190 class_name = recipe.type.replace("tosca.nodes.", "")
Matteo Scandolo21dde412017-07-11 18:54:12 -0700191 model = GRPCModelsAccessor.get_model_from_classname(class_name, data, self.username, self.password)
Matteo Scandolod12be212017-07-07 10:44:34 -0700192 # [] populate model with data
Matteo Scandolo485b7132017-06-30 11:46:47 -0700193 model = self.populate_model(model, data)
194 # [] check if the model has requirements
195 # [] if it has populate them
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700196 model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name)
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -0700197 # [] save, update or delete
198 if self.delete and not model.is_new:
199 model.delete()
200 elif not self.delete:
201 model.save()
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700202
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700203 self.saved_model_by_name[recipe.name] = model
204
Matteo Scandolod12be212017-07-07 10:44:34 -0700205 except ValidationError as e:
Matteo Scandolo485b7132017-06-30 11:46:47 -0700206 if e.message:
Matteo Scandolod12be212017-07-07 10:44:34 -0700207 exception_msg = TOSCA_Parser._translate_exception(e.message)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700208 else:
Matteo Scandolod12be212017-07-07 10:44:34 -0700209 exception_msg = TOSCA_Parser._translate_exception(str(e))
Matteo Scandolo485b7132017-06-30 11:46:47 -0700210 raise Exception(exception_msg)
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700211
Matteo Scandolo21dde412017-07-11 18:54:12 -0700212 except _Rendezvous, e:
213 try:
214 exception_msg = json.loads(e._state.details)["error"]
215 except Exception:
216 exception_msg = e._state.details
217 raise Exception(exception_msg)
218 except Exception, e:
219 raise e
220
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700221