Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 1 | from toscaparser.tosca_template import ToscaTemplate |
| 2 | from default import TOSCA_RECIPES_DIR |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 3 | from grpc_client.resources import RESOURCES |
| 4 | from grpc_client.models_accessor import GRPCModelsAccessor |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 5 | |
| 6 | class TOSCA_Parser: |
| 7 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 8 | 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 13 | nodetemplate.dependencies = [] |
| 14 | nodetemplate.dependencies_names = [] |
| 15 | for reqs in nodetemplate.requirements: |
| 16 | for (k,v) in reqs.items(): |
| 17 | name = v["node"] |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 18 | if (name in models_by_name): |
| 19 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 20 | nodetemplate.dependencies_names.append(name) |
| 21 | |
| 22 | # go another level deep, as our requirements can have requirements... |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame^] | 23 | # NOTE do we still need to go deep? |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 24 | 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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 27 | if (name in models_by_name): |
| 28 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 29 | nodetemplate.dependencies_names.append(name) |
| 30 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 31 | @staticmethod |
| 32 | def topsort_dependencies(g): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 33 | |
| 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 80 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 87 | def _translate_exception(msg): |
| 88 | readable = [] |
| 89 | for line in msg.splitlines(): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 90 | 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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 98 | def save_recipe_to_tmp_file(self, recipe): |
| 99 | tmp_file = open(self.recipe_file, 'w') |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 100 | tmp_file.write(recipe) |
| 101 | tmp_file.close() |
| 102 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 103 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 109 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 110 | @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 Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 118 | @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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 125 | |
| 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 Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame^] | 133 | self.ordered_models_name = [] |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 134 | # dictionary containing the saved model |
| 135 | self.saved_model_by_name = {} |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 136 | |
| 137 | self.ordered_models_template = [] |
| 138 | self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml' |
| 139 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame^] | 140 | self.recipe = recipe |
| 141 | |
| 142 | def execute(self): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 143 | try: |
| 144 | # [] save the recipe to a tmp file |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame^] | 145 | self.save_recipe_to_tmp_file(self.recipe) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 146 | # [] 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 Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 165 | # [] populate model with data[[ |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 166 | model = self.populate_model(model, data) |
| 167 | # [] check if the model has requirements |
| 168 | # [] if it has populate them |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 169 | model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 170 | # [] save or update |
| 171 | model.save() |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 172 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 173 | self.saved_model_by_name[recipe.name] = model |
| 174 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 175 | except Exception as e: |
| 176 | print e |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 177 | if e.message: |
| 178 | exception_msg = e.message |
| 179 | else: |
| 180 | exception_msg = str(e) |
| 181 | raise Exception(exception_msg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 182 | |
| 183 | |