Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame^] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
| 16 | #!/usr/bin/env python |
| 17 | |
| 18 | # TODO: Moved this into the synchronizer, as it appeared to require model |
| 19 | # access. Verify whether or not that's true and reconcile with |
| 20 | # generate/dependency_walker.py |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | import os |
| 24 | import imp |
| 25 | import inspect |
| 26 | import time |
| 27 | import traceback |
| 28 | import commands |
| 29 | import threading |
| 30 | from xosconfig import Config |
| 31 | import json |
| 32 | |
| 33 | from xosconfig import Config |
| 34 | from multistructlog import create_logger |
| 35 | |
| 36 | log = create_logger(Config().get("logging")) |
| 37 | |
| 38 | missing_links = {} |
| 39 | |
| 40 | if Config.get("dependency_graph"): |
| 41 | dep_data = open(Config.get("dependency_graph")).read() |
| 42 | else: |
| 43 | dep_data = "{}" |
| 44 | |
| 45 | dependencies = json.loads(dep_data) |
| 46 | dependencies = {k: [item[0] for item in items] for k, items in dependencies.items()} |
| 47 | |
| 48 | inv_dependencies = {} |
| 49 | for k, lst in dependencies.items(): |
| 50 | for v in lst: |
| 51 | try: |
| 52 | inv_dependencies[v].append(k) |
| 53 | except KeyError: |
| 54 | inv_dependencies[v] = [k] |
| 55 | |
| 56 | |
| 57 | def plural(name): |
| 58 | if name.endswith("s"): |
| 59 | return name + "es" |
| 60 | else: |
| 61 | return name + "s" |
| 62 | |
| 63 | |
| 64 | def walk_deps(fn, object): |
| 65 | model = object.__class__.__name__ |
| 66 | try: |
| 67 | deps = dependencies[model] |
| 68 | except BaseException: |
| 69 | deps = [] |
| 70 | return __walk_deps(fn, object, deps) |
| 71 | |
| 72 | |
| 73 | def walk_inv_deps(fn, object): |
| 74 | model = object.__class__.__name__ |
| 75 | try: |
| 76 | deps = inv_dependencies[model] |
| 77 | except BaseException: |
| 78 | deps = [] |
| 79 | return __walk_deps(fn, object, deps) |
| 80 | |
| 81 | |
| 82 | def __walk_deps(fn, object, deps): |
| 83 | model = object.__class__.__name__ |
| 84 | ret = [] |
| 85 | for dep in deps: |
| 86 | # print "Checking dep %s"%dep |
| 87 | peer = None |
| 88 | link = dep.lower() |
| 89 | try: |
| 90 | peer = getattr(object, link) |
| 91 | except AttributeError: |
| 92 | link = plural(link) |
| 93 | try: |
| 94 | peer = getattr(object, link) |
| 95 | except AttributeError: |
| 96 | if model + "." + link not in missing_links: |
| 97 | print("Model %s missing link for dependency %s" % (model, link)) |
| 98 | log.exception( |
| 99 | "WARNING: Model missing link for dependency.", |
| 100 | model=model, |
| 101 | link=link, |
| 102 | ) |
| 103 | missing_links[model + "." + link] = True |
| 104 | |
| 105 | if peer: |
| 106 | try: |
| 107 | peer_objects = peer.all() |
| 108 | except AttributeError: |
| 109 | peer_objects = [peer] |
| 110 | except BaseException: |
| 111 | peer_objects = [] |
| 112 | |
| 113 | for o in peer_objects: |
| 114 | if hasattr(o, "updated"): |
| 115 | fn(o, object) |
| 116 | ret.append(o) |
| 117 | # Uncomment the following line to enable recursion |
| 118 | # walk_inv_deps(fn, o) |
| 119 | return ret |