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 | from __future__ import print_function |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 17 | from xossynchronizer.dependency_walker_new import * |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 18 | from xossynchronizer.model_policies.policy import Policy |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 19 | |
| 20 | import imp |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 21 | import inspect |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 22 | import time |
| 23 | import traceback |
| 24 | |
| 25 | |
| 26 | class XOSPolicyEngine(object): |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 27 | def __init__(self, policies_dir, model_accessor, log): |
| 28 | self.model_accessor = model_accessor |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 29 | self.model_policies = self.load_model_policies(policies_dir) |
| 30 | self.policies_by_name = {} |
| 31 | self.policies_by_class = {} |
| 32 | self.log = log |
| 33 | |
| 34 | for policy in self.model_policies: |
| 35 | if policy.model_name not in self.policies_by_name: |
| 36 | self.policies_by_name[policy.model_name] = [] |
| 37 | self.policies_by_name[policy.model_name].append(policy) |
| 38 | |
| 39 | if policy.model not in self.policies_by_class: |
| 40 | self.policies_by_class[policy.model] = [] |
| 41 | self.policies_by_class[policy.model].append(policy) |
| 42 | |
| 43 | def update_wp(self, d, o): |
| 44 | try: |
| 45 | save_fields = [] |
| 46 | if d.write_protect != o.write_protect: |
| 47 | d.write_protect = o.write_protect |
| 48 | save_fields.append("write_protect") |
| 49 | if save_fields: |
| 50 | d.save(update_fields=save_fields) |
| 51 | except AttributeError as e: |
| 52 | raise e |
| 53 | |
| 54 | def update_dep(self, d, o): |
| 55 | try: |
| 56 | print("Trying to update %s" % d) |
| 57 | save_fields = [] |
| 58 | if d.updated < o.updated: |
| 59 | save_fields = ["updated"] |
| 60 | |
| 61 | if save_fields: |
| 62 | d.save(update_fields=save_fields) |
| 63 | except AttributeError as e: |
| 64 | log.exception("AttributeError in update_dep", e=e) |
| 65 | raise e |
| 66 | except Exception as e: |
| 67 | log.exception("Exception in update_dep", e=e) |
| 68 | |
| 69 | def delete_if_inactive(self, d, o): |
| 70 | try: |
| 71 | d.delete() |
| 72 | print("Deleted %s (%s)" % (d, d.__class__.__name__)) |
| 73 | except BaseException: |
| 74 | pass |
| 75 | return |
| 76 | |
| 77 | def load_model_policies(self, policies_dir): |
| 78 | policies = [] |
| 79 | for fn in os.listdir(policies_dir): |
| 80 | if fn.startswith("test"): |
| 81 | # don't try to import unit tests! |
| 82 | continue |
| 83 | pathname = os.path.join(policies_dir, fn) |
| 84 | if ( |
| 85 | os.path.isfile(pathname) |
| 86 | and fn.endswith(".py") |
| 87 | and (fn != "__init__.py") |
| 88 | ): |
| 89 | module = imp.load_source(fn[:-3], pathname) |
| 90 | for classname in dir(module): |
| 91 | c = getattr(module, classname, None) |
| 92 | |
| 93 | # make sure 'c' is a descendent of Policy and has a |
| 94 | # provides field (this eliminates the abstract base classes |
| 95 | # since they don't have a provides) |
| 96 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 97 | if inspect.isclass(c): |
| 98 | base_names = [b.__name__ for b in c.__bases__] |
| 99 | |
| 100 | if ( |
| 101 | "Policy" in base_names |
| 102 | and hasattr(c, "model_name") |
| 103 | and (c not in policies) |
| 104 | ): |
| 105 | if not c.model_name: |
| 106 | log.info( |
| 107 | "load_model_policies: skipping model policy", |
| 108 | classname=classname, |
| 109 | ) |
| 110 | continue |
| 111 | if not self.model_accessor.has_model_class(c.model_name): |
| 112 | log.error( |
| 113 | "load_model_policies: unable to find model policy", |
| 114 | classname=classname, |
| 115 | model=c.model_name, |
| 116 | ) |
| 117 | c.model = self.model_accessor.get_model_class(c.model_name) |
| 118 | policies.append(c) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 119 | |
| 120 | log.info("Loaded model policies", policies=policies) |
| 121 | return policies |
| 122 | |
| 123 | def execute_model_policy(self, instance, action): |
| 124 | # These are the models whose children get deleted when they are |
| 125 | delete_policy_models = ["Slice", "Instance", "Network"] |
| 126 | sender_name = getattr(instance, "model_name", instance.__class__.__name__) |
| 127 | |
| 128 | # if (action != "deleted"): |
| 129 | # walk_inv_deps(self.update_dep, instance) |
| 130 | # walk_deps(self.update_wp, instance) |
| 131 | # elif (sender_name in delete_policy_models): |
| 132 | # walk_inv_deps(self.delete_if_inactive, instance) |
| 133 | |
| 134 | policies_failed = False |
| 135 | for policy in self.policies_by_name.get(sender_name, None): |
| 136 | method_name = "handle_%s" % action |
| 137 | if hasattr(policy, method_name): |
| 138 | try: |
| 139 | log.debug( |
| 140 | "MODEL POLICY: calling handler", |
| 141 | sender_name=sender_name, |
| 142 | instance=instance, |
| 143 | policy=policy.__name__, |
| 144 | method=method_name, |
| 145 | ) |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 146 | getattr(policy(model_accessor=self.model_accessor), method_name)(instance) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 147 | log.debug( |
| 148 | "MODEL POLICY: completed handler", |
| 149 | sender_name=sender_name, |
| 150 | instance=instance, |
| 151 | policy_name=policy.__name__, |
| 152 | method=method_name, |
| 153 | ) |
| 154 | except Exception as e: |
| 155 | log.exception("MODEL POLICY: Exception when running handler", e=e) |
| 156 | policies_failed = True |
| 157 | |
| 158 | try: |
| 159 | instance.policy_status = "%s" % traceback.format_exc(limit=1) |
| 160 | instance.policy_code = 2 |
| 161 | instance.save(update_fields=["policy_status", "policy_code"]) |
| 162 | except Exception as e: |
| 163 | log.exception( |
| 164 | "MODEL_POLICY: Exception when storing policy_status", e=e |
| 165 | ) |
| 166 | |
| 167 | if not policies_failed: |
| 168 | try: |
| 169 | instance.policed = max(instance.updated, instance.changed_by_step) |
| 170 | instance.policy_status = "done" |
| 171 | instance.policy_code = 1 |
| 172 | |
| 173 | instance.save(update_fields=["policed", "policy_status", "policy_code"]) |
| 174 | |
| 175 | if hasattr(policy, "after_policy_save"): |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 176 | policy(model_accessor=self.model_accessor).after_policy_save(instance) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 177 | |
| 178 | log.info("MODEL_POLICY: Saved", o=instance) |
| 179 | except BaseException: |
| 180 | log.exception( |
| 181 | "MODEL POLICY: Object failed to update policed timestamp", |
| 182 | instance=instance, |
| 183 | ) |
| 184 | |
| 185 | def noop(self, o, p): |
| 186 | pass |
| 187 | |
| 188 | def run(self): |
| 189 | while True: |
| 190 | start = time.time() |
| 191 | try: |
| 192 | self.run_policy_once() |
| 193 | except Exception as e: |
| 194 | log.exception("MODEL_POLICY: Exception in run()", e=e) |
| 195 | if time.time() - start < 5: |
| 196 | time.sleep(5) |
| 197 | |
| 198 | # TODO: This loop is different from the synchronizer event_loop, but they both do mostly the same thing. Look for |
| 199 | # ways to combine them. |
| 200 | |
| 201 | def run_policy_once(self): |
| 202 | models = self.policies_by_class.keys() |
| 203 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 204 | self.model_accessor.check_db_connection_okay() |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 205 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 206 | objects = self.model_accessor.fetch_policies(models, False) |
| 207 | deleted_objects = self.model_accessor.fetch_policies(models, True) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 208 | |
| 209 | for o in objects: |
| 210 | if o.deleted: |
| 211 | # This shouldn't happen, but previous code was examining o.deleted. Verify. |
| 212 | continue |
| 213 | if not o.policed: |
| 214 | self.execute_model_policy(o, "create") |
| 215 | else: |
| 216 | self.execute_model_policy(o, "update") |
| 217 | |
| 218 | for o in deleted_objects: |
| 219 | self.execute_model_policy(o, "delete") |
| 220 | |
| 221 | try: |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 222 | self.model_accessor.reset_queries() |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 223 | except Exception as e: |
| 224 | # this shouldn't happen, but in case it does, catch it... |
| 225 | log.exception("MODEL POLICY: exception in reset_queries", e) |