blob: 029b6edbbf0965a032d275b1e6ff31978e43fbc1 [file] [log] [blame]
Matteo Scandolo9ce18252017-06-22 10:48:25 -07001from toscaparser.tosca_template import ToscaTemplate
2from 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 Scandolo9ce18252017-06-22 10:48:25 -07005
6class TOSCA_Parser:
7
Matteo Scandolo485b7132017-06-30 11:46:47 -07008 def compute_dependencies(self, template, models_by_name):
9 """
10 NOTE this method is augmenting self.template, isn't there a more explicit way to achieve it?
11 """
12 for nodetemplate in template.nodetemplates:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070013 nodetemplate.dependencies = []
14 nodetemplate.dependencies_names = []
15 for reqs in nodetemplate.requirements:
16 for (k,v) in reqs.items():
17 name = v["node"]
Matteo Scandolo485b7132017-06-30 11:46:47 -070018 if (name in models_by_name):
19 nodetemplate.dependencies.append(models_by_name[name])
Matteo Scandolo9ce18252017-06-22 10:48:25 -070020 nodetemplate.dependencies_names.append(name)
21
22 # go another level deep, as our requirements can have requirements...
Matteo Scandolodf2600b2017-07-05 17:01:29 -070023 # NOTE do we still need to go deep?
Matteo Scandolo9ce18252017-06-22 10:48:25 -070024 for sd_req in v.get("requirements",[]):
25 for (sd_req_k, sd_req_v) in sd_req.items():
26 name = sd_req_v["node"]
Matteo Scandolo485b7132017-06-30 11:46:47 -070027 if (name in models_by_name):
28 nodetemplate.dependencies.append(models_by_name[name])
Matteo Scandolo9ce18252017-06-22 10:48:25 -070029 nodetemplate.dependencies_names.append(name)
30
Matteo Scandolo485b7132017-06-30 11:46:47 -070031 @staticmethod
32 def topsort_dependencies(g):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070033
34 # Get set of all nodes, including those without outgoing edges
35 keys = set(g.keys())
36 values = set({})
37 for v in g.values():
38 values = values | set(v.dependencies_names)
39
40 all_nodes = list(keys | values)
41 steps = all_nodes
42
43
44 # Final order
45 order = []
46
47 # DFS stack, not using recursion
48 stack = []
49
50 # Unmarked set
51 unmarked = all_nodes
52
53 # visiting = [] - skip, don't expect 1000s of nodes, |E|/|V| is small
54
55 while unmarked:
56 stack.insert(0, unmarked[0]) # push first unmarked
57
58 while (stack):
59 n = stack[0]
60 add = True
61 try:
62 for m in g[n].dependencies_names:
63 if (m in unmarked):
64 add = False
65 stack.insert(0, m)
66 except KeyError:
67 pass
68 if (add):
69 if (n in steps and n not in order):
70 order.append(n)
71 item = stack.pop(0)
72 try:
73 unmarked.remove(item)
74 except ValueError:
75 pass
76
77 noorder = list(set(steps) - set(order))
78 return order + noorder
79
Matteo Scandolo9ce18252017-06-22 10:48:25 -070080 @staticmethod
81 def populate_model(model, data):
82 for k,v in data.iteritems():
83 setattr(model, k, v)
84 return model
85
86 @staticmethod
Matteo Scandolo9ce18252017-06-22 10:48:25 -070087 def _translate_exception(msg):
88 readable = []
89 for line in msg.splitlines():
Matteo Scandolo9ce18252017-06-22 10:48:25 -070090 if line.strip().startswith('MissingRequiredFieldError'):
91 readable.append(line)
92
93 if len(readable) > 0:
94 return '/n'.join(readable)
95 else:
96 return msg
97
Matteo Scandolo485b7132017-06-30 11:46:47 -070098 def save_recipe_to_tmp_file(self, recipe):
99 tmp_file = open(self.recipe_file, 'w')
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700100 tmp_file.write(recipe)
101 tmp_file.close()
102
Matteo Scandolo485b7132017-06-30 11:46:47 -0700103 @staticmethod
104 def get_tosca_models_by_name(template):
105 models_by_name = {}
106 for node in template.nodetemplates:
107 models_by_name[node.name] = node
108 return models_by_name
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700109
Matteo Scandolo485b7132017-06-30 11:46:47 -0700110 @staticmethod
111 def get_ordered_models_template(ordered_models_name, templates_by_model_name):
112 ordered_models_templates = []
113 for name in ordered_models_name:
114 if name in templates_by_model_name:
115 ordered_models_templates.append(templates_by_model_name[name])
116 return ordered_models_templates
117
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700118 @staticmethod
119 def populate_dependencies(model, requirements, saved_models):
120 for dep in requirements:
121 class_name = dep.keys()[0]
122 related_model = saved_models[dep[class_name]['node']]
123 setattr(model, "%s_id" % class_name, related_model.id)
124 return model
Matteo Scandolo485b7132017-06-30 11:46:47 -0700125
126 def __init__(self, recipe):
127
128 # the template returned by TOSCA-Parser
129 self.template = None
130 # dictionary containing the models in the recipe and their template
131 self.templates_by_model_name = None
132 # list of models ordered by requirements
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700133 self.ordered_models_name = []
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700134 # dictionary containing the saved model
135 self.saved_model_by_name = {}
Matteo Scandolo485b7132017-06-30 11:46:47 -0700136
137 self.ordered_models_template = []
138 self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml'
139
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700140 self.recipe = recipe
141
142 def execute(self):
Matteo Scandolo485b7132017-06-30 11:46:47 -0700143 try:
144 # [] save the recipe to a tmp file
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700145 self.save_recipe_to_tmp_file(self.recipe)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700146 # [] parse the recipe with TOSCA Parse
147 self.template = ToscaTemplate(self.recipe_file)
148 # [] get all models in the recipe
149 self.templates_by_model_name = self.get_tosca_models_by_name(self.template)
150 # [] compute requirements
151 self.compute_dependencies(self.template, self.templates_by_model_name)
152 # [] topsort requirements
153 self.ordered_models_name = self.topsort_dependencies(self.templates_by_model_name)
154 # [] topsort templates
155 self.ordered_models_template = self.get_ordered_models_template(self.ordered_models_name, self.templates_by_model_name)
156
157 for recipe in self.ordered_models_template:
158 # get properties from tosca
159 data = recipe.templates[recipe.name]['properties']
160 # [] get model by class name
161 class_name = recipe.type.replace("tosca.nodes.", "")
162 if class_name not in RESOURCES:
163 raise Exception("Nodetemplate %s's type %s is not a known resource" % (recipe.name, class_name))
164 model = GRPCModelsAccessor.get_model_from_classname(class_name, data)
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700165 # [] populate model with data[[
Matteo Scandolo485b7132017-06-30 11:46:47 -0700166 model = self.populate_model(model, data)
167 # [] check if the model has requirements
168 # [] if it has populate them
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700169 model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700170 # [] save or update
171 model.save()
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700172
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -0700173 self.saved_model_by_name[recipe.name] = model
174
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700175 except Exception as e:
176 print e
Matteo Scandolo485b7132017-06-30 11:46:47 -0700177 if e.message:
178 exception_msg = e.message
179 else:
180 exception_msg = str(e)
181 raise Exception(exception_msg)
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700182
183