blob: c3a25570066ee2ab138d573f5d2f82abb8264bf2 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# 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 Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
16
Scott Bakerbba67b62019-01-28 17:38:21 -080017import imp
18import inspect
19import os
20import threading
21import time
Zack Williams5c2ea232019-01-30 15:23:01 -070022
Scott Bakerbba67b62019-01-28 17:38:21 -080023from multistructlog import create_logger
Zack Williams5c2ea232019-01-30 15:23:01 -070024from xosconfig import Config
Scott Bakerbba67b62019-01-28 17:38:21 -080025
26log = create_logger(Config().get("logging"))
27
28
29class 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 Bakerc2fddaa2019-01-30 15:45:03 -080036 def __init__(self, steps, model_accessor, *args, **kwargs):
Scott Bakerbba67b62019-01-28 17:38:21 -080037 self.steps = steps
Scott Bakerc2fddaa2019-01-30 15:45:03 -080038 self.model_accessor = model_accessor
Scott Bakerbba67b62019-01-28 17:38:21 -080039
40 def run(self):
41 while True:
42 time.sleep(5)
43 self.run_once()
44
45 def run_once(self):
Zack Williamsda69db22019-01-29 16:44:52 -070046 log.debug("Starting pull steps", steps=self.steps)
Scott Bakerbba67b62019-01-28 17:38:21 -080047
48 threads = []
49 for step in self.steps:
Zack Williams5c2ea232019-01-30 15:23:01 -070050 thread = threading.Thread(
51 target=step(model_accessor=self.model_accessor).pull_records,
52 name="pull_step",
53 )
Scott Bakerbba67b62019-01-28 17:38:21 -080054 threads.append(thread)
55
56 for t in threads:
57 t.start()
58
59 for t in threads:
60 t.join()
61
Zack Williamsda69db22019-01-29 16:44:52 -070062 log.debug("Done with pull steps", steps=self.steps)
Scott Bakerbba67b62019-01-28 17:38:21 -080063
64
65class 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 Bakerc2fddaa2019-01-30 15:45:03 -080077 def __init__(self, model_accessor):
78 self.model_accessor = model_accessor
Scott Bakerbba67b62019-01-28 17:38:21 -080079 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 Williams5c2ea232019-01-30 15:23:01 -0700109 sched = XOSPullStepScheduler(
110 steps=self.pull_steps, model_accessor=self.model_accessor
111 )
Scott Bakerbba67b62019-01-28 17:38:21 -0800112 sched.run()