Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 17 | from toscaparser.tosca_template import ToscaTemplate, ValidationError |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 18 | from default import TOSCA_RECIPES_DIR |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 19 | from grpc_client.resources import RESOURCES |
| 20 | from grpc_client.models_accessor import GRPCModelsAccessor |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 21 | from grpc._channel import _Rendezvous |
| 22 | import json |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 23 | |
| 24 | class TOSCA_Parser: |
| 25 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 26 | 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 31 | nodetemplate.dependencies = [] |
| 32 | nodetemplate.dependencies_names = [] |
| 33 | for reqs in nodetemplate.requirements: |
| 34 | for (k,v) in reqs.items(): |
| 35 | name = v["node"] |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 36 | if (name in models_by_name): |
| 37 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 38 | nodetemplate.dependencies_names.append(name) |
| 39 | |
| 40 | # go another level deep, as our requirements can have requirements... |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 41 | # NOTE do we still need to go deep? |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 42 | 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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 45 | if (name in models_by_name): |
| 46 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 47 | nodetemplate.dependencies_names.append(name) |
| 48 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 49 | @staticmethod |
| 50 | def topsort_dependencies(g): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 51 | |
| 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 98 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 105 | def _translate_exception(msg): |
| 106 | readable = [] |
| 107 | for line in msg.splitlines(): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 108 | if line.strip().startswith('MissingRequiredFieldError'): |
| 109 | readable.append(line) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 110 | if line.strip().startswith('UnknownFieldError'): |
| 111 | readable.append(line) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 112 | |
| 113 | if len(readable) > 0: |
| 114 | return '/n'.join(readable) |
| 115 | else: |
| 116 | return msg |
| 117 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 118 | def save_recipe_to_tmp_file(self, recipe): |
| 119 | tmp_file = open(self.recipe_file, 'w') |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 120 | tmp_file.write(recipe) |
| 121 | tmp_file.close() |
| 122 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 123 | @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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 129 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 130 | @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 Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 138 | @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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 145 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 146 | def __init__(self, recipe, username, password, **kwargs): |
| 147 | |
| 148 | self.delete = False |
| 149 | if 'delete' in kwargs: |
| 150 | self.delete = True |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 151 | |
| 152 | # store username/password combination to read resources |
| 153 | self.username = username |
| 154 | self.password = password |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 155 | |
| 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 Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 161 | self.ordered_models_name = [] |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 162 | # dictionary containing the saved model |
| 163 | self.saved_model_by_name = {} |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 164 | |
| 165 | self.ordered_models_template = [] |
| 166 | self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml' |
| 167 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 168 | self.recipe = recipe |
| 169 | |
| 170 | def execute(self): |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 171 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 172 | try: |
| 173 | # [] save the recipe to a tmp file |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 174 | self.save_recipe_to_tmp_file(self.recipe) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 175 | # [] 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 Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 191 | 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] | 192 | # [] populate model with data |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 193 | model = self.populate_model(model, data) |
| 194 | # [] check if the model has requirements |
| 195 | # [] if it has populate them |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 196 | model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 197 | # [] 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 202 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 203 | self.saved_model_by_name[recipe.name] = model |
| 204 | |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 205 | except ValidationError as e: |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 206 | if e.message: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 207 | exception_msg = TOSCA_Parser._translate_exception(e.message) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 208 | else: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 209 | exception_msg = TOSCA_Parser._translate_exception(str(e)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 210 | raise Exception(exception_msg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 211 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 212 | 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 221 | |