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 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
| 16 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 17 | import imp |
| 18 | import inspect |
| 19 | import os |
| 20 | import threading |
| 21 | import time |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 22 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 23 | from multistructlog import create_logger |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 24 | from xosconfig import Config |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 25 | |
| 26 | log = create_logger(Config().get("logging")) |
| 27 | |
| 28 | |
| 29 | class XOSPullStepScheduler: |
| 30 | """ XOSPullStepThread |
| 31 | |
| 32 | A Thread for servicing pull steps. There is one event_step associated with one XOSPullStepThread. |
| 33 | The thread's pull_records() function is called for every five seconds. |
| 34 | """ |
| 35 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 36 | def __init__(self, steps, model_accessor, *args, **kwargs): |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 37 | self.steps = steps |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 38 | self.model_accessor = model_accessor |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 39 | |
| 40 | def run(self): |
| 41 | while True: |
| 42 | time.sleep(5) |
| 43 | self.run_once() |
| 44 | |
| 45 | def run_once(self): |
Zack Williams | da69db2 | 2019-01-29 16:44:52 -0700 | [diff] [blame] | 46 | log.debug("Starting pull steps", steps=self.steps) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 47 | |
| 48 | threads = [] |
| 49 | for step in self.steps: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 50 | thread = threading.Thread( |
| 51 | target=step(model_accessor=self.model_accessor).pull_records, |
| 52 | name="pull_step", |
| 53 | ) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 54 | threads.append(thread) |
| 55 | |
| 56 | for t in threads: |
| 57 | t.start() |
| 58 | |
| 59 | for t in threads: |
| 60 | t.join() |
| 61 | |
Zack Williams | da69db2 | 2019-01-29 16:44:52 -0700 | [diff] [blame] | 62 | log.debug("Done with pull steps", steps=self.steps) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 63 | |
| 64 | |
| 65 | class XOSPullStepEngine: |
| 66 | """ XOSPullStepEngine |
| 67 | |
| 68 | Load pull step modules. Two methods are defined: |
| 69 | |
| 70 | load_pull_step_modules(dir) ... look for step modules in the given directory, and load objects that are |
| 71 | descendant from PullStep. |
| 72 | |
| 73 | start() ... Launch threads to handle processing of the PullSteps. It's expected that load_step_modules() |
| 74 | will be called before start(). |
| 75 | """ |
| 76 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 77 | def __init__(self, model_accessor): |
| 78 | self.model_accessor = model_accessor |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 79 | self.pull_steps = [] |
| 80 | |
| 81 | def load_pull_step_modules(self, pull_step_dir): |
| 82 | self.pull_steps = [] |
| 83 | log.info("Loading pull steps", pull_step_dir=pull_step_dir) |
| 84 | |
| 85 | # NOTE we'll load all the classes that inherit from PullStep |
| 86 | for fn in os.listdir(pull_step_dir): |
| 87 | pathname = os.path.join(pull_step_dir, fn) |
| 88 | if ( |
| 89 | os.path.isfile(pathname) |
| 90 | and fn.endswith(".py") |
| 91 | and (fn != "__init__.py") |
| 92 | and ("test" not in fn) |
| 93 | ): |
| 94 | event_module = imp.load_source(fn[:-3], pathname) |
| 95 | |
| 96 | for classname in dir(event_module): |
| 97 | c = getattr(event_module, classname, None) |
| 98 | |
| 99 | if inspect.isclass(c): |
| 100 | base_names = [b.__name__ for b in c.__bases__] |
| 101 | if "PullStep" in base_names: |
| 102 | self.pull_steps.append(c) |
| 103 | log.info("Loaded pull steps", steps=self.pull_steps) |
| 104 | |
| 105 | def start(self): |
| 106 | log.info("Starting pull steps engine", steps=self.pull_steps) |
| 107 | |
| 108 | for step in self.pull_steps: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 109 | sched = XOSPullStepScheduler( |
| 110 | steps=self.pull_steps, model_accessor=self.model_accessor |
| 111 | ) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 112 | sched.run() |