2019-03-01 20:41:13 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
2019-03-04 14:48:25 +01:00
|
|
|
@file: routerManager.py
|
|
|
|
|
@date: 04.03.2019
|
2019-03-01 20:41:13 +01:00
|
|
|
@author: Bastian Schroll
|
2019-03-04 14:48:25 +01:00
|
|
|
@description: Class for the BOSWatch packet router manager class
|
2019-03-01 20:41:13 +01:00
|
|
|
"""
|
2019-03-04 14:48:25 +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
|
2019-03-04 14:48:25 +01:00
|
|
|
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:
|
|
|
|
|
def __init__(self):
|
2019-03-01 22:12:29 +01:00
|
|
|
self._routerDict = {}
|
2019-03-01 20:41:13 +01:00
|
|
|
|
|
|
|
|
def __del__(self):
|
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-01 22:12:29 +01:00
|
|
|
self._routerDict = {} # all routers and instances of modules/plugins would be destroyed
|
2019-03-01 22:46:28 +01:00
|
|
|
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
|
2019-03-01 22:46:28 +01:00
|
|
|
if router.get("name") in self._routerDict:
|
|
|
|
|
logging.error("duplicated router name: %s", router.get("name"))
|
|
|
|
|
return False
|
2019-03-04 14:48:25 +01:00
|
|
|
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 22:46:28 +01:00
|
|
|
|
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:
|
2019-03-01 22:46:28 +01:00
|
|
|
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)
|
2019-03-04 14:48:25 +01:00
|
|
|
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)
|
2019-03-04 14:48:25 +01:00
|
|
|
routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run))
|
2019-03-01 21:36:38 +01:00
|
|
|
|
|
|
|
|
elif routeType == "router":
|
2019-03-04 14:48:25 +01:00
|
|
|
routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeName].runRouter))
|
2019-03-01 21:36:38 +01:00
|
|
|
|
|
|
|
|
else:
|
2019-03-01 22:46:28 +01:00
|
|
|
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"))
|
2019-03-01 22:46:28 +01:00
|
|
|
return False
|
2019-03-01 22:12:29 +01:00
|
|
|
|
2019-03-01 21:36:38 +01:00
|
|
|
logging.debug("finished building routers")
|
2019-03-01 22:46:28 +01:00
|
|
|
self._routerDict = routerDict_tmp
|
2019-03-01 21:36:38 +01:00
|
|
|
self._showRouterRoute()
|
2019-03-01 22:46:28 +01:00
|
|
|
return True
|
2019-03-01 20:41:13 +01:00
|
|
|
|
2019-03-02 09:17:20 +01:00
|
|
|
def runRouter(self, routerRunList, bwPacket):
|
|
|
|
|
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-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)
|