seedlink = route.routeSeedlink(k) if ('S', route.networkCode(), route.stationCode(), route.locationCode(), route.streamCode(), seedlink.address(), None) not in existingRoutes: route.removeRouteSeedlink(k) logd("removing %s.%s.%s.%s %s" % (route.networkCode(), route.stationCode(), route.locationCode(), route.streamCode(), seedlink.address())) continue k += 1 if ( j + k ) == 0: routing.removeRoute(i) continue i += 1 for ((networkCode, stationCode, locationCode, streamCode, user, start), (end,)) in existingAccess.iteritems(): access = routing.access(DataModel.AccessIndex(networkCode, stationCode, locationCode, streamCode, user, Core.Time.FromString(start, "%Y-%m-%d %H:%M:%S"))) if not access: access = DataModel.Access() access.setNetworkCode(networkCode) access.setStationCode(stationCode) access.setLocationCode(locationCode) access.setStreamCode(streamCode) access.setUser(user) access.setStart(Core.Time.FromString(start, "%Y-%m-%d %H:%M:%S")) access.setEnd(end) routing.add(access) else: update = False try: cend = access.end() if (not end) or (end and cend != end):
def run(self): ''' Reimplements the main loop of the application. This methods collects all bindings and updates the database. It searches for already existing objects and updates them or creates new objects. Objects that is didn't touched are removed. This tool is the only one that should writes the configuration into the database and thus manages the content. ''' # Initialize the basic directories filebase = System.Environment.Instance().installDir() descdir = os.path.join(filebase, "etc", "descriptions") keydir = os.path.join(filebase, "etc", "key", self.name()) # Load definitions of the configuration schema defs = System.SchemaDefinitions() if defs.load(descdir) == False: log("could not read descriptions") return False if defs.moduleCount() == 0: log("no modules defined, nothing to do") return False # Create a model from the schema and read its configuration including # all bindings. model = System.Model() model.create(defs) model.readConfig() mod = model.module("arclink") mod_access = model.module("arclink-access") existingRoutes = {} existingAccess = {} routing = self.query().loadRouting() inventory = self.query().loadInventory() iResolver = InventoryResolver(inventory) DataModel.Notifier.Enable() DataModel.Notifier.SetCheckEnabled(False) if mod: log("Working on arclink bindings") for staid in mod.bindings.keys(): binding = mod.getBinding(staid) if not binding: log("no route to station %s %s" % (staid.networkCode, staid.stationCode)) continue params = {} for i in range(binding.sectionCount()): params.update(collectParams(binding.section(i))) if 'routes' not in params: log("no routes definition to station %s %s" % (staid.networkCode, staid.stationCode)) continue a_def_priority = 1 s_def_priority = 1 for r in params['routes'].split(','): emptyLabel = True ## Default is None route_streams = params.get('routes.%s.streams' % r) ## DEFAULT is True route_netonly = params.get('routes.%s.disableStationCode' % r) if route_netonly is None or route_netonly == "false": route_netonly = False else: route_netonly = True # Arclink route_address = params.get('routes.%s.arclink.address' % r) if route_address: emptyLabel = False route_start = params.get('routes.%s.arclink.start' % r) route_end = params.get('routes.%s.arclink.end' % r) route_priority = params.get( 'routes.%s.arclink.priority' % r) networkCode = staid.networkCode stationCode = staid.stationCode if route_priority is None: route_priority = a_def_priority else: route_priority = int(route_priority) if route_end: route_end = Core.Time.FromString( route_end, "%Y-%m-%d %H:%M:%S") if route_start: route_start = Core.Time.FromString( route_start, "%Y-%m-%d %H:%M:%S") if route_netonly: stationCode = "" ## Resolve Inventory try: networkList = iResolver.expandNetworkStation( networkCode, stationCode, route_start, route_end) except Exception as e: log("Arclink routing issue, cannot find network object for %s %s (label: %s)::\n\t %s" % (staid.networkCode, staid.stationCode, r, str(e))) continue ## Generate routes for each network found for (network, stations) in networkList: ## Resolve start date / end date of routing to be generated rStart = iResolver.findStartDate( network, route_start, route_end) rEnd = iResolver.findEndDate( network, route_start, route_end) if not route_streams: existingRoutes[( 'A', networkCode, stationCode, "", "", route_address, rStart.toString("%Y-%m-%d %H:%M:%S"))] = ( rEnd, route_priority) logd("Adding %s.%s.%s.%s" % (networkCode, stationCode, "", "")) continue ## Add the route or routes for this net for (locationCode, streamCode) in iResolver.expandStream( stations, route_streams, route_start, route_end): existingRoutes[( 'A', networkCode, stationCode, locationCode, streamCode, route_address, rStart.toString("%Y-%m-%d %H:%M:%S"))] = ( rEnd, route_priority) logd("Adding %s.%s.%s.%s" % (networkCode, stationCode, locationCode, streamCode)) a_def_priority += 1 else: logd( "binding for %s %s is missing routes.%s.arclink.address" % (staid.networkCode, staid.stationCode, r)) ## Seedlink route_address = params.get('routes.%s.seedlink.address' % r) if route_address: emptyLabel = False route_priority = params.get( 'routes.%s.seedlink.priority' % r) networkCode = staid.networkCode stationCode = staid.stationCode if route_priority is None: route_priority = s_def_priority else: route_priority = int(route_priority) if route_netonly: stationCode = "" ## Resolve Inventory try: networkList = iResolver.expandNetworkStation( networkCode, stationCode, Core.Time.GMT(), None) except Exception as e: log("Seedlink routing issue, cannot find network object for %s %s (label: %s)::\n\t %s" % (staid.networkCode, staid.stationCode, r, str(e))) continue ## Generate routes for each network found for (network, stations) in networkList: if not route_streams: existingRoutes[('S', networkCode, stationCode, "", "", route_address, None)] = (None, route_priority) continue ## Add the route or routes for this net for (locationCode, streamCode) in iResolver.expandStream( stations, route_streams, Core.Time.GMT(), None): existingRoutes[('S', networkCode, stationCode, locationCode, streamCode, route_address, None)] = (None, route_priority) s_def_priority += 1 else: logd( "seedlink binding of station %s is missing routes.%s.seedlink.address" % (staid.networkCode + '_' + staid.stationCode, r)) if emptyLabel: log("routes label %s is empty or not found for station %s.%s" % (r, staid.networkCode, staid.stationCode)) # Update access on basis of access module if mod_access: logd("Working on arclink-access bindings") for staid in mod_access.bindings.keys(): binding = mod_access.getBinding(staid) if not binding: continue params = {} for i in range(binding.sectionCount()): params.update(collectParams(binding.section(i))) access_users = params.get('access.users') access_start = params.get('access.start') access_end = params.get('access.end') access_netonly = params.get('access.disableStationCode') access_streams = params.get('access.streams') if access_netonly is None or access_netonly == "false": access_netonly = False else: access_netonly = True if not access_users: continue networkCode = staid.networkCode stationCode = staid.stationCode if access_start: access_start = Core.Time.FromString( access_start, "%Y-%m-%d %H:%M:%S") if access_end: access_end = Core.Time.FromString(access_end, "%Y-%m-%d %H:%M:%S") if access_netonly: stationCode = "" ## Resolve Inventory try: networkList = iResolver.expandNetworkStation( networkCode, stationCode, access_start, access_end) except Exception as e: #log("Access issue, cannot find network object for %s %s::\n\t %s" % (staid.networkCode, staid.stationCode, str(e))) for user in access_users.split(','): existingAccess[(networkCode, "", "", "", user, "1980-01-01 00:00:00")] = (None, ) continue ## Generate routes for each network found for (network, stations) in networkList: ## Resolve start date / end date of routing to be generated aStart = iResolver.findStartDate(network, access_start, access_end) aEnd = iResolver.findEndDate(network, access_start, access_end) if not access_streams: for user in access_users.split(','): existingAccess[( networkCode, stationCode, "", "", user, aStart.toString("%Y-%m-%d %H:%M:%S"))] = ( aEnd, ) continue ## Add the route or routes for this net for (locationCode, streamCode) in iResolver.expandStream( stations, access_streams, access_start, access_end): for user in access_users.split(','): existingAccess[( networkCode, stationCode, locationCode, streamCode, user, aStart.toString("%Y-%m-%d %H:%M:%S"))] = ( aEnd, ) for ((routeType, networkCode, stationCode, locationCode, streamCode, address, start), (end, priority)) in existingRoutes.iteritems(): if routeType != 'A' and routeType != 'S': logd("Invalid route type %s " % routeType) continue route = routing.route( DataModel.RouteIndex(networkCode, stationCode, locationCode, streamCode)) if not route: route = DataModel.Route.Create() route.setNetworkCode(networkCode) route.setStationCode(stationCode) route.setLocationCode(locationCode) route.setStreamCode(streamCode) routing.add(route) if routeType == 'A': arclink = route.routeArclink( DataModel.RouteArclinkIndex( address, Core.Time.FromString(start, "%Y-%m-%d %H:%M:%S"))) if not arclink: arclink = DataModel.RouteArclink() arclink.setAddress(address) arclink.setStart( Core.Time.FromString(start, "%Y-%m-%d %H:%M:%S")) arclink.setEnd(end) arclink.setPriority(priority) logd("inserting: %s %s.%s.%s.%s %s %s %s %s" % (routeType, networkCode, stationCode, locationCode, streamCode, address, start, end, priority)) route.add(arclink) else: logd("checking for update: %s %s.%s.%s.%s %s %s %s %s" % (routeType, networkCode, stationCode, locationCode, streamCode, address, start, end, priority)) update = False try: cpriority = arclink.priority() if cpriority != priority: arclink.setPriority(priority) update = True except ValueError as e: if priority: arclink.setPriority(priority) update = True try: cend = arclink.end() if (not end) or (end and cend != end): arclink.setEnd(end) update = True except ValueError as e: if end: arclink.setEnd(end) update = True if update: arclink.update() elif routeType == 'S': seedlink = route.routeSeedlink( DataModel.RouteSeedlinkIndex(address)) if not seedlink: seedlink = DataModel.RouteSeedlink() seedlink.setAddress(address) seedlink.setPriority(priority) logd("inserting: %s %s.%s.%s.%s %s %s %s %s" % (routeType, networkCode, stationCode, locationCode, streamCode, address, start, end, priority)) route.add(seedlink) else: logd("checking for update: %s %s.%s.%s.%s %s %s %s %s" % (routeType, networkCode, stationCode, locationCode, streamCode, address, start, end, priority)) update = False try: cpriority = seedlink.priority() if cpriority != priority: seedlink.setPriority(priority) update = True except ValueError as e: if priority: seedlink.setPriority(priority) update = True if update: seedlink.update() else: log("route (%s.%s.%s.%s %s,%s) not inserted because is of an invalid type." % (networkCode, stationCode, locationCode, streamCode, address, priority)) i = 0 while i < routing.routeCount(): route = routing.route(i) j = 0 while j < route.routeArclinkCount(): arclink = route.routeArclink(j) if ('A', route.networkCode(), route.stationCode(), route.locationCode(), route.streamCode(), arclink.address(), arclink.start().toString("%Y-%m-%d %H:%M:%S") ) not in existingRoutes: route.removeRouteArclink(j) logd("removing %s.%s.%s.%s %s" % (route.networkCode(), route.stationCode(), route.locationCode(), route.streamCode(), arclink.address())) continue j += 1 k = 0 while k < route.routeSeedlinkCount(): seedlink = route.routeSeedlink(k) if ('S', route.networkCode(), route.stationCode(), route.locationCode(), route.streamCode(), seedlink.address(), None) not in existingRoutes: route.removeRouteSeedlink(k) logd("removing %s.%s.%s.%s %s" % (route.networkCode(), route.stationCode(), route.locationCode(), route.streamCode(), seedlink.address())) continue k += 1 if (j + k) == 0: routing.removeRoute(i) continue i += 1 for ((networkCode, stationCode, locationCode, streamCode, user, start), (end, )) in existingAccess.iteritems(): access = routing.access( DataModel.AccessIndex( networkCode, stationCode, locationCode, streamCode, user, Core.Time.FromString(start, "%Y-%m-%d %H:%M:%S"))) if not access: access = DataModel.Access() access.setNetworkCode(networkCode) access.setStationCode(stationCode) access.setLocationCode(locationCode) access.setStreamCode(streamCode) access.setUser(user) access.setStart( Core.Time.FromString(start, "%Y-%m-%d %H:%M:%S")) access.setEnd(end) routing.add(access) else: update = False try: cend = access.end() if (not end) or (end and cend != end): access.setEnd(end) update = True except ValueError as e: if end: access.setEnd(end) update = True if update: access.update() i = 0 while i < routing.accessCount(): access = routing.access(i) if (access.networkCode(), access.stationCode(), access.locationCode(), access.streamCode(), access.user(), access.start().toString("%Y-%m-%d %H:%M:%S") ) not in existingAccess: routing.remove(access) continue i += 1 self.sendNotifiers("ROUTING") return True