BW3-Core/boswatch/router/routerManager.py

121 lines
4.8 KiB
Python
Raw Normal View History

2019-03-01 20:41:13 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: routerManager.py
@date: 04.03.2019
2019-03-01 20:41:13 +01:00
@author: Bastian Schroll
@description: Class for the BOSWatch packet router manager class
2019-03-01 20:41:13 +01:00
"""
2019-03-01 21:36:38 +01:00
# todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!)
2019-03-01 20:41:13 +01:00
import logging
import importlib
2019-03-02 09:17:20 +01:00
from boswatch.configYaml import ConfigYAML
from boswatch.router.router import Router
from boswatch.router.route import Route
2019-03-01 20:41:13 +01:00
logging.debug("- %s loaded", __name__)
class RouterManager:
2019-03-04 20:10:49 +01:00
"""!Class to manage all routers"""
2019-03-01 20:41:13 +01:00
def __init__(self):
2019-03-04 20:10:49 +01:00
"""!Create new router"""
2019-03-01 22:12:29 +01:00
self._routerDict = {}
2019-03-01 20:41:13 +01:00
def __del__(self):
2019-03-04 20:10:49 +01:00
"""!Destroy the internal routerDict
All routers and route point instances will be destroyed too
Also destroys all instances from modules or plugins"""
2019-03-01 22:12:29 +01:00
# destroy all routers (also destroys all instances of modules/plugins)
del self._routerDict
2019-03-01 21:36:38 +01:00
2019-03-02 09:17:20 +01:00
# if there is an error, router list would be empty (see tmp variable)
def buildRouter(self, config):
2019-03-04 20:10:49 +01:00
"""!Initialize Routers from given config file
@param config: instance of ConfigYaml class
@return True or False"""
2019-03-01 22:12:29 +01:00
self._routerDict = {} # all routers and instances of modules/plugins would be destroyed
routerDict_tmp = {}
2019-03-01 20:41:13 +01:00
logging.debug("build routers")
# first we have to init all routers
# because a router can be a valid target and we need his reference
for router in config.get("router"):
2019-03-02 09:17:20 +01:00
if router.get("name") is None or router.get("route") is None:
logging.error("name or route not found in router: %s", router)
return False
if router.get("name") in self._routerDict:
logging.error("duplicated router name: %s", router.get("name"))
return False
routerDict_tmp[router.get("name")] = Router(router.get("name"))
2019-03-01 21:36:38 +01:00
2019-03-01 20:41:13 +01:00
for router in config.get("router"):
2019-03-02 09:17:20 +01:00
routerName = router.get("name")
2019-03-01 20:41:13 +01:00
for route in router.get("route"):
2019-03-01 21:36:38 +01:00
routeType = route.get("type")
routeName = route.get("name")
2019-03-02 09:17:20 +01:00
routeConfig = route.get("Config", default=ConfigYAML()) # if no config - build a empty
2019-03-01 21:36:38 +01:00
if routeType is None or routeName is None:
logging.error("type or name not found in route: %s", route)
return False
2019-03-01 21:36:38 +01:00
try:
if routeType == "plugin":
importedFile = importlib.import_module(routeType + "." + routeName)
2019-03-02 09:17:20 +01:00
loadedClass = importedFile.BoswatchPlugin(routeConfig)
routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run))
2019-03-01 21:36:38 +01:00
elif routeType == "module":
importedFile = importlib.import_module(routeType + "." + routeName)
2019-03-02 09:17:20 +01:00
loadedClass = importedFile.BoswatchModule(routeConfig)
routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run))
2019-03-01 21:36:38 +01:00
elif routeType == "router":
routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeName].runRouter))
2019-03-01 21:36:38 +01:00
else:
logging.error("unknown type '%s' in %s", routeType, route)
return False
2019-03-01 21:36:38 +01:00
except ModuleNotFoundError:
logging.error("%s not found: %s", route.get("type"), route.get("name"))
return False
2019-03-01 22:12:29 +01:00
2019-03-01 21:36:38 +01:00
logging.debug("finished building routers")
self._routerDict = routerDict_tmp
2019-03-01 21:36:38 +01:00
self._showRouterRoute()
return True
2019-03-01 20:41:13 +01:00
2019-03-02 09:17:20 +01:00
def runRouter(self, routerRunList, bwPacket):
2019-03-04 20:10:49 +01:00
"""!Run given Routers
@param routerRunList: string or list of router names in string form
@param bwPacket: instance of Packet class"""
2019-03-02 09:17:20 +01:00
if type(routerRunList) is str: # convert single string name to list
routerRunList = [routerRunList]
2019-03-01 22:12:29 +01:00
2019-03-02 09:17:20 +01:00
for routerName in routerRunList:
2019-03-01 22:12:29 +01:00
if routerName in self._routerDict:
self._routerDict[routerName].runRouter(bwPacket)
2019-03-01 20:41:13 +01:00
2019-03-01 21:36:38 +01:00
def _showRouterRoute(self):
2019-03-04 20:10:49 +01:00
"""!Show the routes of all routers"""
2019-03-02 09:17:20 +01:00
for name, routerObject in self._routerDict.items():
2019-03-01 20:41:13 +01:00
logging.debug("Route for %s", name)
2019-03-02 09:17:20 +01:00
counter = 0
for routePoint in routerObject.routeList:
counter += 1
logging.debug(" %d. %s", counter, routePoint.name)