blob: 725d259ba0f4bdccbbfa8bcd5cdb0729cc516818 [file] [log] [blame]
Matteo Scandolod12be212017-07-07 10:44:34 -07001from toscaparser.tosca_template import ToscaTemplate, ValidationError
Matteo Scandolo9ce18252017-06-22 10:48:25 -07002from default import TOSCA_RECIPES_DIR
Matteo Scandolo485b7132017-06-30 11:46:47 -07003from grpc_client.resources import RESOURCES
4from grpc_client.models_accessor import GRPCModelsAccessor
Matteo Scandolo21dde412017-07-11 18:54:12 -07005from grpc._channel import _Rendezvous
6import json
Matteo Scandolo9ce18252017-06-22 10:48:25 -07007
8class TOSCA_Parser:
9
Matteo Scandolo485b7132017-06-30 11:46:47 -070010 def compute_dependencies(self, template, models_by_name):
11 """
12 NOTE this method is augmenting self.template, isn't there a more explicit way to achieve it?
13 """
14 for nodetemplate in template.nodetemplates:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070015 nodetemplate.dependencies = []
16 nodetemplate.dependencies_names = []
17 for reqs in nodetemplate.requirements:
18 for (k,v) in reqs.items():
19 name = v["node"]
Matteo Scandolo485b7132017-06-30 11:46:47 -070020 if (name in models_by_name):
21 nodetemplate.dependencies.append(models_by_name[name])
Matteo Scandolo9ce18252017-06-22 10:48:25 -070022 nodetemplate.dependencies_names.append(name)
23
24 # go another level deep, as our requirements can have requirements...
Matteo Scandolodf2600b2017-07-05 17:01:29 -070025 # NOTE do we still need to go deep?
Matteo Scandolo9ce18252017-06-22 10:48:25 -070026 for sd_req in v.get("requirements",[]):
27 for (sd_req_k, sd_req_v) in sd_req.items():
28 name = sd_req_v["node"]
Matteo Scandolo485b7132017-06-30 11:46:47 -070029 if (name in models_by_name):
30 nodetemplate.dependencies.append(models_by_name[name])
Matteo Scandolo9ce18252017-06-22 10:48:25 -070031 nodetemplate.dependencies_names.append(name)
32
Matteo Scandolo485b7132017-06-30 11:46:47 -070033 @staticmethod
34 def topsort_dependencies(g):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070035
36 # Get set of all nodes, including those without outgoing edges
37 keys = set(g.keys())
38 values = set({})
39 for v in g.values():
40 values = values | set(v.dependencies_names)
41
42 all_nodes = list(keys | values)
43 steps = all_nodes
44
45
46 # Final order
47 order = []
48
49 # DFS stack, not using recursion
50 stack = []
51
52 # Unmarked set
53 unmarked = all_nodes
54
55 # visiting = [] - skip, don't expect 1000s of nodes, |E|/|V| is small
56
57 while unmarked:
58 stack.insert(0, unmarked[0]) # push first unmarked
59
60 while (stack):
61 n = stack[0]
62 add = True
63 try:
64 for m in g[n].dependencies_names:
65 if (m in unmarked):
66 add = False
67 stack.insert(0, m)
68 except KeyError:
69 pass
70 if (add):
71 if (n in steps and n not in order):
72 order.append(n)
73 item = stack.pop(0)
74 try:
75 unmarked.remove(item)
76 except ValueError:
77 pass
78
79 noorder = list(set(steps) - set(order))
80 return order + noorder
81
Matteo Scandolo9ce18252017-06-22 10:48:25 -070082 @staticmethod
83 def populate_model(model, data):
84 for k,v in data.iteritems():
85 setattr(model, k, v)
86 return model
87
88 @staticmethod
Matteo Scandolo9ce18252017-06-22 10:48:25 -070089 def _translate_exception(msg):
90 readable = []
91 for line in msg.splitlines():
Matteo Scandolo9ce18252017-06-22 10:48:25 -070092 if line.strip().startswith('MissingRequiredFieldError'):
93 readable.append(line)
Matteo Scandolod12be212017-07-07 10:44:34 -070094 if line.strip().startswith('UnknownFieldError'):
95 readable.append(line)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070096
97 if len(readable) > 0:
98 return '/n'.join(readable)
99 else:
100 return msg
101
Matteo Scandolo485b7132017-06-30 11:46:47 -0700102 def save_recipe_to_tmp_file(self, recipe):
103 tmp_file = open(self.recipe_file, 'w')
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700104 tmp_file.write(recipe)
105 tmp_file.close()
106
Matteo Scandolo485b7132017-06-30 11:46:47 -0700107 @staticmethod
108 def get_tosca_models_by_name(template):
109 models_by_name = {}
110 for node in template.nodetemplates:
111 models_by_name[node.name] = node
112 return models_by_name
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700113
Matteo Scandolo485b7132017-06-30 11:46:47 -0700114 @staticmethod
115 def get_ordered_models_template(ordered_models_name, templates_by_model_name):
116 ordered_models_templates = []
117 for name in ordered_models_name:
118 if name in templates_by_model_name:
119 ordered_models_templates.append(templates_by_model_name[name])
120 return ordered_models_templates
121
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700122 @staticmethod
123 def populate_dependencies(model, requirements, saved_models):
124 for dep in requirements:
125 class_name = dep.keys()[0]
126 related_model = saved_models[dep[class_name]['node']]
127 setattr(model, "%s_id" % class_name, related_model.id)
128 return model
Matteo Scandolo485b7132017-06-30 11:46:47 -0700129
Matteo Scandolo21dde412017-07-11 18:54:12 -0700130 def __init__(self, recipe, username, password):
131
132 # store username/password combination to read resources
133 self.username = username
134 self.password = password
Matteo Scandolo485b7132017-06-30 11:46:47 -0700135
136 # the template returned by TOSCA-Parser
137 self.template = None
138 # dictionary containing the models in the recipe and their template
139 self.templates_by_model_name = None
140 # list of models ordered by requirements
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700141 self.ordered_models_name = []
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700142 # dictionary containing the saved model
143 self.saved_model_by_name = {}
Matteo Scandolo485b7132017-06-30 11:46:47 -0700144
145 self.ordered_models_template = []
146 self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml'
147
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700148 self.recipe = recipe
149
150 def execute(self):
Matteo Scandolo485b7132017-06-30 11:46:47 -0700151 try:
152 # [] save the recipe to a tmp file
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700153 self.save_recipe_to_tmp_file(self.recipe)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700154 # [] parse the recipe with TOSCA Parse
155 self.template = ToscaTemplate(self.recipe_file)
156 # [] get all models in the recipe
157 self.templates_by_model_name = self.get_tosca_models_by_name(self.template)
158 # [] compute requirements
159 self.compute_dependencies(self.template, self.templates_by_model_name)
160 # [] topsort requirements
161 self.ordered_models_name = self.topsort_dependencies(self.templates_by_model_name)
162 # [] topsort templates
163 self.ordered_models_template = self.get_ordered_models_template(self.ordered_models_name, self.templates_by_model_name)
164
165 for recipe in self.ordered_models_template:
166 # get properties from tosca
167 data = recipe.templates[recipe.name]['properties']
168 # [] get model by class name
169 class_name = recipe.type.replace("tosca.nodes.", "")
Matteo Scandolo21dde412017-07-11 18:54:12 -0700170 model = GRPCModelsAccessor.get_model_from_classname(class_name, data, self.username, self.password)
Matteo Scandolod12be212017-07-07 10:44:34 -0700171 # [] populate model with data
Matteo Scandolo485b7132017-06-30 11:46:47 -0700172 model = self.populate_model(model, data)
173 # [] check if the model has requirements
174 # [] if it has populate them
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700175 model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700176 # [] save or update
177 model.save()
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700178
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700179 self.saved_model_by_name[recipe.name] = model
180
Matteo Scandolod12be212017-07-07 10:44:34 -0700181 except ValidationError as e:
Matteo Scandolo485b7132017-06-30 11:46:47 -0700182 if e.message:
Matteo Scandolod12be212017-07-07 10:44:34 -0700183 exception_msg = TOSCA_Parser._translate_exception(e.message)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700184 else:
Matteo Scandolod12be212017-07-07 10:44:34 -0700185 exception_msg = TOSCA_Parser._translate_exception(str(e))
Matteo Scandolo485b7132017-06-30 11:46:47 -0700186 raise Exception(exception_msg)
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700187
Matteo Scandolo21dde412017-07-11 18:54:12 -0700188 except _Rendezvous, e:
189 try:
190 exception_msg = json.loads(e._state.details)["error"]
191 except Exception:
192 exception_msg = e._state.details
193 raise Exception(exception_msg)
194 except Exception, e:
195 raise e
196
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700197