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(): |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 101 | # NOTE must-exists is a TOSCA implementation choice, remove it before saving the model |
| 102 | if k != "must-exist": |
| 103 | setattr(model, k, v) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 104 | return model |
| 105 | |
| 106 | @staticmethod |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 107 | def _translate_exception(msg): |
| 108 | readable = [] |
| 109 | for line in msg.splitlines(): |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 110 | if line.strip().startswith('MissingRequiredFieldError') or \ |
| 111 | line.strip().startswith('UnknownFieldError') or \ |
| 112 | line.strip().startswith('ImportError') or \ |
| 113 | line.strip().startswith('InvalidTypeError') or \ |
| 114 | line.strip().startswith('TypeMismatchError'): |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 115 | readable.append(line) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 116 | |
| 117 | if len(readable) > 0: |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 118 | return '\n'.join(readable) + '\n' |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 119 | else: |
| 120 | return msg |
| 121 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 122 | def save_recipe_to_tmp_file(self, recipe): |
| 123 | tmp_file = open(self.recipe_file, 'w') |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 124 | tmp_file.write(recipe) |
| 125 | tmp_file.close() |
| 126 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 127 | @staticmethod |
| 128 | def get_tosca_models_by_name(template): |
| 129 | models_by_name = {} |
| 130 | for node in template.nodetemplates: |
| 131 | models_by_name[node.name] = node |
| 132 | return models_by_name |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 133 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 134 | @staticmethod |
| 135 | def get_ordered_models_template(ordered_models_name, templates_by_model_name): |
| 136 | ordered_models_templates = [] |
| 137 | for name in ordered_models_name: |
| 138 | if name in templates_by_model_name: |
| 139 | ordered_models_templates.append(templates_by_model_name[name]) |
| 140 | return ordered_models_templates |
| 141 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 142 | @staticmethod |
| 143 | def populate_dependencies(model, requirements, saved_models): |
| 144 | for dep in requirements: |
| 145 | class_name = dep.keys()[0] |
| 146 | related_model = saved_models[dep[class_name]['node']] |
| 147 | setattr(model, "%s_id" % class_name, related_model.id) |
| 148 | return model |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 149 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame^] | 150 | @staticmethod |
| 151 | def add_dependencies(data, requirements, saved_models): |
| 152 | for dep in requirements: |
| 153 | class_name = dep.keys()[0] |
| 154 | related_model = saved_models[dep[class_name]['node']] |
| 155 | data["%s_id" % class_name] = related_model.id |
| 156 | return data |
| 157 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 158 | def __init__(self, recipe, username, password, **kwargs): |
| 159 | |
| 160 | self.delete = False |
| 161 | if 'delete' in kwargs: |
| 162 | self.delete = True |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 163 | |
| 164 | # store username/password combination to read resources |
| 165 | self.username = username |
| 166 | self.password = password |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 167 | |
| 168 | # the template returned by TOSCA-Parser |
| 169 | self.template = None |
| 170 | # dictionary containing the models in the recipe and their template |
| 171 | self.templates_by_model_name = None |
| 172 | # list of models ordered by requirements |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 173 | self.ordered_models_name = [] |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 174 | # dictionary containing the saved model |
| 175 | self.saved_model_by_name = {} |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 176 | |
| 177 | self.ordered_models_template = [] |
| 178 | self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml' |
| 179 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 180 | self.recipe = recipe |
| 181 | |
| 182 | def execute(self): |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 183 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 184 | try: |
| 185 | # [] save the recipe to a tmp file |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 186 | self.save_recipe_to_tmp_file(self.recipe) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 187 | # [] parse the recipe with TOSCA Parse |
| 188 | self.template = ToscaTemplate(self.recipe_file) |
| 189 | # [] get all models in the recipe |
| 190 | self.templates_by_model_name = self.get_tosca_models_by_name(self.template) |
| 191 | # [] compute requirements |
| 192 | self.compute_dependencies(self.template, self.templates_by_model_name) |
| 193 | # [] topsort requirements |
| 194 | self.ordered_models_name = self.topsort_dependencies(self.templates_by_model_name) |
| 195 | # [] topsort templates |
| 196 | self.ordered_models_template = self.get_ordered_models_template(self.ordered_models_name, self.templates_by_model_name) |
| 197 | |
| 198 | for recipe in self.ordered_models_template: |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 199 | try: |
| 200 | # get properties from tosca |
| 201 | if not 'properties' in recipe.templates[recipe.name]: |
| 202 | data = {} |
| 203 | else: |
| 204 | data = recipe.templates[recipe.name]['properties'] |
| 205 | if data == None: |
| 206 | data = {} |
| 207 | # [] get model by class name |
| 208 | class_name = recipe.type.replace("tosca.nodes.", "") |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame^] | 209 | |
| 210 | # augemnt data with relations |
| 211 | data = self.add_dependencies(data, recipe.requirements, self.saved_model_by_name) |
| 212 | |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 213 | model = GRPCModelsAccessor.get_model_from_classname(class_name, data, self.username, self.password) |
| 214 | # [] populate model with data |
| 215 | model = self.populate_model(model, data) |
| 216 | # [] check if the model has requirements |
| 217 | # [] if it has populate them |
| 218 | model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name) |
| 219 | # [] save, update or delete |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 220 | |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 221 | if self.delete and not model.is_new: |
| 222 | model.delete() |
| 223 | elif not self.delete: |
| 224 | model.save() |
| 225 | |
| 226 | self.saved_model_by_name[recipe.name] = model |
| 227 | except Exception, e: |
| 228 | print "[XOS-TOSCA] Failed to save model: %s [%s]" % (class_name, recipe.name) |
| 229 | raise e |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 230 | |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 231 | except ValidationError as e: |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 232 | if e.message: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 233 | exception_msg = TOSCA_Parser._translate_exception(e.message) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 234 | else: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 235 | exception_msg = TOSCA_Parser._translate_exception(str(e)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 236 | raise Exception(exception_msg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 237 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 238 | except _Rendezvous, e: |
| 239 | try: |
| 240 | exception_msg = json.loads(e._state.details)["error"] |
| 241 | except Exception: |
| 242 | exception_msg = e._state.details |
| 243 | raise Exception(exception_msg) |
| 244 | except Exception, e: |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 245 | raise Exception(e) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 246 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 247 | |