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 | 5ccdb13 | 2018-01-17 14:02:12 -0800 | [diff] [blame] | 23 | import traceback |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 24 | |
| 25 | class TOSCA_Parser: |
| 26 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 27 | def compute_dependencies(self, template, models_by_name): |
| 28 | """ |
| 29 | NOTE this method is augmenting self.template, isn't there a more explicit way to achieve it? |
| 30 | """ |
| 31 | for nodetemplate in template.nodetemplates: |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 32 | nodetemplate.dependencies = [] |
| 33 | nodetemplate.dependencies_names = [] |
| 34 | for reqs in nodetemplate.requirements: |
| 35 | for (k,v) in reqs.items(): |
| 36 | name = v["node"] |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 37 | if (name in models_by_name): |
| 38 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 39 | nodetemplate.dependencies_names.append(name) |
| 40 | |
| 41 | # go another level deep, as our requirements can have requirements... |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 42 | # NOTE do we still need to go deep? |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 43 | for sd_req in v.get("requirements",[]): |
| 44 | for (sd_req_k, sd_req_v) in sd_req.items(): |
| 45 | name = sd_req_v["node"] |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 46 | if (name in models_by_name): |
| 47 | nodetemplate.dependencies.append(models_by_name[name]) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 48 | nodetemplate.dependencies_names.append(name) |
| 49 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 50 | @staticmethod |
| 51 | def topsort_dependencies(g): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 52 | |
| 53 | # Get set of all nodes, including those without outgoing edges |
| 54 | keys = set(g.keys()) |
| 55 | values = set({}) |
| 56 | for v in g.values(): |
| 57 | values = values | set(v.dependencies_names) |
| 58 | |
| 59 | all_nodes = list(keys | values) |
| 60 | steps = all_nodes |
| 61 | |
| 62 | |
| 63 | # Final order |
| 64 | order = [] |
| 65 | |
| 66 | # DFS stack, not using recursion |
| 67 | stack = [] |
| 68 | |
| 69 | # Unmarked set |
| 70 | unmarked = all_nodes |
| 71 | |
| 72 | # visiting = [] - skip, don't expect 1000s of nodes, |E|/|V| is small |
| 73 | |
| 74 | while unmarked: |
| 75 | stack.insert(0, unmarked[0]) # push first unmarked |
| 76 | |
| 77 | while (stack): |
| 78 | n = stack[0] |
| 79 | add = True |
| 80 | try: |
| 81 | for m in g[n].dependencies_names: |
| 82 | if (m in unmarked): |
| 83 | add = False |
| 84 | stack.insert(0, m) |
| 85 | except KeyError: |
| 86 | pass |
| 87 | if (add): |
| 88 | if (n in steps and n not in order): |
| 89 | order.append(n) |
| 90 | item = stack.pop(0) |
| 91 | try: |
| 92 | unmarked.remove(item) |
| 93 | except ValueError: |
| 94 | pass |
| 95 | |
| 96 | noorder = list(set(steps) - set(order)) |
| 97 | return order + noorder |
| 98 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 99 | @staticmethod |
| 100 | def populate_model(model, data): |
| 101 | for k,v in data.iteritems(): |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 102 | # NOTE must-exists is a TOSCA implementation choice, remove it before saving the model |
| 103 | if k != "must-exist": |
Matteo Scandolo | 5ccdb13 | 2018-01-17 14:02:12 -0800 | [diff] [blame] | 104 | try: |
| 105 | setattr(model, k, v) |
| 106 | except TypeError, e: |
| 107 | raise Exception('Failed to set %s on field %s for class %s, Exception was: "%s"' % (v, k, model.model_name, e)) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 108 | return model |
| 109 | |
| 110 | @staticmethod |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 111 | def _translate_exception(msg): |
| 112 | readable = [] |
| 113 | for line in msg.splitlines(): |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 114 | if line.strip().startswith('MissingRequiredFieldError') or \ |
| 115 | line.strip().startswith('UnknownFieldError') or \ |
| 116 | line.strip().startswith('ImportError') or \ |
| 117 | line.strip().startswith('InvalidTypeError') or \ |
| 118 | line.strip().startswith('TypeMismatchError'): |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 119 | readable.append(line) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 120 | |
| 121 | if len(readable) > 0: |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 122 | return '\n'.join(readable) + '\n' |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 123 | else: |
| 124 | return msg |
| 125 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 126 | def save_recipe_to_tmp_file(self, recipe): |
| 127 | tmp_file = open(self.recipe_file, 'w') |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 128 | tmp_file.write(recipe) |
| 129 | tmp_file.close() |
| 130 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 131 | @staticmethod |
| 132 | def get_tosca_models_by_name(template): |
| 133 | models_by_name = {} |
| 134 | for node in template.nodetemplates: |
| 135 | models_by_name[node.name] = node |
| 136 | return models_by_name |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 137 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 138 | @staticmethod |
| 139 | def get_ordered_models_template(ordered_models_name, templates_by_model_name): |
| 140 | ordered_models_templates = [] |
| 141 | for name in ordered_models_name: |
| 142 | if name in templates_by_model_name: |
| 143 | ordered_models_templates.append(templates_by_model_name[name]) |
| 144 | return ordered_models_templates |
| 145 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 146 | @staticmethod |
| 147 | def populate_dependencies(model, requirements, saved_models): |
| 148 | for dep in requirements: |
| 149 | class_name = dep.keys()[0] |
| 150 | related_model = saved_models[dep[class_name]['node']] |
| 151 | setattr(model, "%s_id" % class_name, related_model.id) |
| 152 | return model |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 153 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 154 | @staticmethod |
| 155 | def add_dependencies(data, requirements, saved_models): |
| 156 | for dep in requirements: |
| 157 | class_name = dep.keys()[0] |
| 158 | related_model = saved_models[dep[class_name]['node']] |
| 159 | data["%s_id" % class_name] = related_model.id |
| 160 | return data |
| 161 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 162 | def __init__(self, recipe, username, password, **kwargs): |
| 163 | |
| 164 | self.delete = False |
| 165 | if 'delete' in kwargs: |
| 166 | self.delete = True |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 167 | |
| 168 | # store username/password combination to read resources |
| 169 | self.username = username |
| 170 | self.password = password |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 171 | |
| 172 | # the template returned by TOSCA-Parser |
| 173 | self.template = None |
| 174 | # dictionary containing the models in the recipe and their template |
| 175 | self.templates_by_model_name = None |
| 176 | # list of models ordered by requirements |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 177 | self.ordered_models_name = [] |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 178 | # dictionary containing the saved model |
| 179 | self.saved_model_by_name = {} |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 180 | |
| 181 | self.ordered_models_template = [] |
| 182 | self.recipe_file = TOSCA_RECIPES_DIR + '/tmp.yaml' |
| 183 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 184 | self.recipe = recipe |
| 185 | |
| 186 | def execute(self): |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 187 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 188 | try: |
| 189 | # [] save the recipe to a tmp file |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 190 | self.save_recipe_to_tmp_file(self.recipe) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 191 | # [] parse the recipe with TOSCA Parse |
| 192 | self.template = ToscaTemplate(self.recipe_file) |
| 193 | # [] get all models in the recipe |
| 194 | self.templates_by_model_name = self.get_tosca_models_by_name(self.template) |
| 195 | # [] compute requirements |
| 196 | self.compute_dependencies(self.template, self.templates_by_model_name) |
| 197 | # [] topsort requirements |
| 198 | self.ordered_models_name = self.topsort_dependencies(self.templates_by_model_name) |
| 199 | # [] topsort templates |
| 200 | self.ordered_models_template = self.get_ordered_models_template(self.ordered_models_name, self.templates_by_model_name) |
| 201 | |
| 202 | for recipe in self.ordered_models_template: |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 203 | try: |
| 204 | # get properties from tosca |
| 205 | if not 'properties' in recipe.templates[recipe.name]: |
| 206 | data = {} |
| 207 | else: |
| 208 | data = recipe.templates[recipe.name]['properties'] |
| 209 | if data == None: |
| 210 | data = {} |
| 211 | # [] get model by class name |
| 212 | class_name = recipe.type.replace("tosca.nodes.", "") |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 213 | |
| 214 | # augemnt data with relations |
| 215 | data = self.add_dependencies(data, recipe.requirements, self.saved_model_by_name) |
| 216 | |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 217 | model = GRPCModelsAccessor.get_model_from_classname(class_name, data, self.username, self.password) |
| 218 | # [] populate model with data |
| 219 | model = self.populate_model(model, data) |
| 220 | # [] check if the model has requirements |
| 221 | # [] if it has populate them |
| 222 | model = self.populate_dependencies(model, recipe.requirements, self.saved_model_by_name) |
| 223 | # [] save, update or delete |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 224 | |
Matteo Scandolo | 91d7aeb | 2018-02-09 14:54:09 -0800 | [diff] [blame] | 225 | reference_only = False |
| 226 | if 'must-exist' in data: |
| 227 | reference_only = True |
| 228 | |
| 229 | if self.delete and not model.is_new and not reference_only: |
| 230 | print "[XOS-Tosca] Deleting model %s[%s]" % (class_name, model.id) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 231 | model.delete() |
| 232 | elif not self.delete: |
Matteo Scandolo | 91d7aeb | 2018-02-09 14:54:09 -0800 | [diff] [blame] | 233 | print "[XOS-Tosca] Saving model %s[%s]" % (class_name, model.id) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 234 | model.save() |
| 235 | |
Matteo Scandolo | c468075 | 2018-05-17 12:16:30 -0700 | [diff] [blame] | 236 | |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 237 | self.saved_model_by_name[recipe.name] = model |
| 238 | except Exception, e: |
| 239 | print "[XOS-TOSCA] Failed to save model: %s [%s]" % (class_name, recipe.name) |
Matteo Scandolo | 5ccdb13 | 2018-01-17 14:02:12 -0800 | [diff] [blame] | 240 | traceback.print_exc() |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 241 | raise e |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 242 | |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 243 | except ValidationError as e: |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 244 | if e.message: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 245 | exception_msg = TOSCA_Parser._translate_exception(e.message) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 246 | else: |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 247 | exception_msg = TOSCA_Parser._translate_exception(str(e)) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 248 | raise Exception(exception_msg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 249 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 250 | except _Rendezvous, e: |
| 251 | try: |
Matteo Scandolo | c468075 | 2018-05-17 12:16:30 -0700 | [diff] [blame] | 252 | details = json.loads(e._state.details) |
| 253 | exception_msg = details["error"] |
| 254 | if "specific_error" in details: |
| 255 | exception_msg = "%s: %s" % (exception_msg, details["specific_error"]) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 256 | except Exception: |
| 257 | exception_msg = e._state.details |
| 258 | raise Exception(exception_msg) |
| 259 | except Exception, e: |
Matteo Scandolo | 91d7aeb | 2018-02-09 14:54:09 -0800 | [diff] [blame] | 260 | print e |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 261 | raise Exception(e) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 262 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 263 | |