Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 1 | from toscaparser.tosca_template import ToscaTemplate, ValidationError |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 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 | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 5 | from grpc._channel import _Rendezvous |
| 6 | import json |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 7 | |
| 8 | class TOSCA_Parser: |
| 9 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 10 | 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 15 | nodetemplate.dependencies = [] |
| 16 | nodetemplate.dependencies_names = [] |
| 17 | for reqs in nodetemplate.requirements: |
| 18 | for (k,v) in reqs.items(): |
| 19 | name = v["node"] |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 20 | if (name in models_by_name): |
| 21 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 22 | nodetemplate.dependencies_names.append(name) |
| 23 | |
| 24 | # go another level deep, as our requirements can have requirements... |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 25 | # NOTE do we still need to go deep? |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 26 | 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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 29 | if (name in models_by_name): |
| 30 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 31 | nodetemplate.dependencies_names.append(name) |
| 32 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 33 | @staticmethod |
| 34 | def topsort_dependencies(g): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 35 | |
| 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 82 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 89 | def _translate_exception(msg): |
| 90 | readable = [] |
| 91 | for line in msg.splitlines(): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 92 | if line.strip().startswith('MissingRequiredFieldError'): |
| 93 | readable.append(line) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 94 | if line.strip().startswith('UnknownFieldError'): |
| 95 | readable.append(line) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 96 | |
| 97 | if len(readable) > 0: |
| 98 | return '/n'.join(readable) |
| 99 | else: |
| 100 | return msg |
| 101 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 102 | def save_recipe_to_tmp_file(self, recipe): |
| 103 | tmp_file = open(self.recipe_file, 'w') |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 104 | tmp_file.write(recipe) |
| 105 | tmp_file.close() |
| 106 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 107 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 113 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 114 | @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 Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 122 | @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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 129 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 130 | def __init__(self, recipe, username, password): |
| 131 | |
| 132 | # store username/password combination to read resources |
| 133 | self.username = username |
| 134 | self.password = password |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 135 | |
| 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 Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 141 | self.ordered_models_name = [] |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 142 | # dictionary containing the saved model |
| 143 | self.saved_model_by_name = {} |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 144 | |
| 145 | self.ordered_models_template = [] |
| 146 | self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml' |
| 147 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 148 | self.recipe = recipe |
| 149 | |
| 150 | def execute(self): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 151 | try: |
| 152 | # [] save the recipe to a tmp file |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 153 | self.save_recipe_to_tmp_file(self.recipe) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 154 | # [] 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 Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 170 | model = GRPCModelsAccessor.get_model_from_classname(class_name, data, self.username, self.password) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 171 | # [] populate model with data |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 172 | model = self.populate_model(model, data) |
| 173 | # [] check if the model has requirements |
| 174 | # [] if it has populate them |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 175 | model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 176 | # [] save or update |
| 177 | model.save() |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 178 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 179 | self.saved_model_by_name[recipe.name] = model |
| 180 | |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 181 | except ValidationError as e: |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 182 | if e.message: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 183 | exception_msg = TOSCA_Parser._translate_exception(e.message) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 184 | else: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 185 | exception_msg = TOSCA_Parser._translate_exception(str(e)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 186 | raise Exception(exception_msg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 187 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 188 | 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 197 | |