Initialize Routers from given config file.
38 def buildRouters(self, config):
39 r"""!Initialize Routers from given config file
40
41 @param config: instance of ConfigYaml class
42 @return True or False"""
43 self._routerDict = {}
44 routerDict_tmp = {}
45 logging.debug("build routers")
46
47
48
49 for router in config.get("router"):
50 if router.get("name") is None or router.get("route") is None:
51 logging.error("name or route not found in router: %s", router)
52 return False
53 if router.get("name") in self._routerDict:
54 logging.error("duplicated router name: %s", router.get("name"))
55 return False
56 routerDict_tmp[router.get("name")] = Router(router.get("name"))
57
58 for router in config.get("router"):
59 routerName = router.get("name")
60
61 for route in router.get("route"):
62 routeType = route.get("type")
63 routeRes = route.get("res")
64 routeName = route.get("name", default=routeRes)
65
66 routeConfig = route.get("config", default=ConfigYAML())
67
68 if routeType is None or routeRes is None:
69 logging.error("type or name not found in route: %s", route)
70 return False
71
72 try:
73 if routeType == "plugin":
74 importedFile = importlib.import_module(routeType + "." + routeRes)
75 loadedClass = importedFile.BoswatchPlugin(routeConfig)
76 routerDict_tmp[routerName].addRoute(Route(routeName,
77 loadedClass._run,
78 loadedClass._getStatistics,
79 loadedClass._cleanup))
80
81 elif routeType == "module":
82 importedFile = importlib.import_module(routeType + "." + routeRes)
83 loadedClass = importedFile.BoswatchModule(routeConfig)
84 routerDict_tmp[routerName].addRoute(Route(routeName,
85 loadedClass._run,
86 loadedClass._getStatistics,
87 loadedClass._cleanup))
88
89 elif routeType == "router":
90 routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeRes].runRouter))
91
92 else:
93 logging.error("unknown type '%s' in %s", routeType, route)
94 return False
95
96 except ModuleNotFoundError:
97 logging.exception("%s not found: %s", route.get("type"), route.get("res"))
98 return False
99
100 logging.debug("finished building routers")
101 self._routerDict = routerDict_tmp
102 self._showRouterRoute()
103 return True
104