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