|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +"""Retrieve data from a Routing WS (or Arclink server) to be used locally |
| 4 | +
|
| 5 | +This program is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU General Public License as published by |
| 7 | +the Free Software Foundation, either version 3 of the License, or |
| 8 | +any later version. |
| 9 | +
|
| 10 | + :Copyright: |
| 11 | + 2014-2020 Javier Quinteros, Deutsches GFZ Potsdam <[email protected]> |
| 12 | + :License: |
| 13 | + GPLv3 |
| 14 | + :Platform: |
| 15 | + Linux |
| 16 | +
|
| 17 | +.. moduleauthor:: Javier Quinteros <[email protected]>, GEOFON, GFZ Potsdam |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import sys |
| 22 | +import argparse |
| 23 | +import logging |
| 24 | +from urllib.parse import urlparse |
| 25 | + |
| 26 | +try: |
| 27 | + import cPickle as pickle |
| 28 | +except ImportError: |
| 29 | + import pickle |
| 30 | + |
| 31 | +try: |
| 32 | + import configparser |
| 33 | +except ImportError: |
| 34 | + import ConfigParser as configparser |
| 35 | + |
| 36 | +sys.path.append('..') |
| 37 | + |
| 38 | +try: |
| 39 | + from routeutils.utils import addRemote |
| 40 | + from routeutils.utils import addRoutes |
| 41 | + from routeutils.utils import addVirtualNets |
| 42 | + from routeutils.utils import cacheStations |
| 43 | + from routeutils.utils import Route |
| 44 | + from routeutils.utils import RoutingCache |
| 45 | +except: |
| 46 | + raise |
| 47 | + |
| 48 | + |
| 49 | +def mergeRoutes(fileRoutes, synchroList, allowOverlaps=False): |
| 50 | + """Retrieve routes from different sources and merge them with the local |
| 51 | +ones in the routing tables. The configuration file is checked to see whether |
| 52 | +overlapping routes are allowed or not. A pickled version of the the routing |
| 53 | +table is saved under the same filename plus ``.bin`` (e.g. routing.xml.bin). |
| 54 | +
|
| 55 | +:param fileRoutes: File containing the local routing table |
| 56 | +:type fileRoutes: str |
| 57 | +:param synchroList: List of data centres where routes should be imported from |
| 58 | +:type synchroList: str |
| 59 | +:param allowOverlaps: Specify if overlapping streams should be allowed or not |
| 60 | +:type allowOverlaps: boolean |
| 61 | +
|
| 62 | +""" |
| 63 | + |
| 64 | + logs = logging.getLogger('mergeRoutes') |
| 65 | + logs.info('Synchronizing with: %s' % synchroList) |
| 66 | + |
| 67 | + ptRT = addRoutes(fileRoutes, allowOverlaps=allowOverlaps) |
| 68 | + ptVN = addVirtualNets(fileRoutes) |
| 69 | + |
| 70 | + for line in synchroList.splitlines(): |
| 71 | + if not len(line): |
| 72 | + break |
| 73 | + logs.debug(str(line.split(','))) |
| 74 | + dcid, url = line.split(',') |
| 75 | + url = url.strip() |
| 76 | + parts = urlparse(url) |
| 77 | + |
| 78 | + if parts.scheme in ('file', ''): |
| 79 | + if parts.path != 'routing-%s.xml' % dcid: |
| 80 | + msg = 'Routes from %s must be in a file called "routing-%s.xml' \ |
| 81 | + % (dcid, dcid) |
| 82 | + logs.error(msg) |
| 83 | + raise Exception('File must be called "routing-%s.xml"' % dcid) |
| 84 | + else: |
| 85 | + try: |
| 86 | + addRemote('./routing-%s.xml' % dcid.strip(), url.strip()) |
| 87 | + except: |
| 88 | + msg = 'Failure updating routing information from %s (%s)' % \ |
| 89 | + (dcid, url) |
| 90 | + logs.error(msg) |
| 91 | + |
| 92 | + if os.path.exists('./routing-%s.xml' % dcid.strip()): |
| 93 | + # FIXME addRoutes should return no Exception ever and skip a |
| 94 | + # problematic file returning a coherent version of the routes |
| 95 | + print('Adding REMOTE %s' % dcid) |
| 96 | + ptRT = addRoutes('./routing-%s.xml' % dcid.strip(), |
| 97 | + routingTable=ptRT, allowOverlaps=allowOverlaps) |
| 98 | + ptVN = addVirtualNets('./routing-%s.xml' % dcid.strip(), |
| 99 | + vnTable=ptVN) |
| 100 | + |
| 101 | + try: |
| 102 | + os.remove('./%s.bin' % fileRoutes) |
| 103 | + except: |
| 104 | + pass |
| 105 | + |
| 106 | + stationTable = dict() |
| 107 | + cacheStations(ptRT, stationTable) |
| 108 | + |
| 109 | + with open('./%s.bin' % fileRoutes, 'wb') as finalRoutes: |
| 110 | + pickle.dump((ptRT, stationTable, ptVN), finalRoutes) |
| 111 | + logs.info('Routes in main Routing Table: %s\n' % len(ptRT)) |
| 112 | + logs.info('Stations cached: %s\n' % |
| 113 | + sum([len(stationTable[dc][st]) for dc in stationTable |
| 114 | + for st in stationTable[dc]])) |
| 115 | + logs.info('Virtual Networks defined: %s\n' % len(ptVN)) |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + # FIXME logLevel must be used via argparser |
| 120 | + # Check verbosity in the output |
| 121 | + msg = 'Get EIDA routing configuration and export it to the FDSN-WS style.' |
| 122 | + parser = argparse.ArgumentParser(description=msg) |
| 123 | + parser.add_argument('-l', '--loglevel', |
| 124 | + help='Verbosity in the output.', |
| 125 | + choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', |
| 126 | + 'DEBUG']) |
| 127 | + # TODO Add type=argparse.FileType |
| 128 | + parser.add_argument('-c', '--config', |
| 129 | + help='Config file to use.', |
| 130 | + default='../routing.cfg') |
| 131 | + args = parser.parse_args() |
| 132 | + |
| 133 | + config = configparser.RawConfigParser() |
| 134 | + # Command line parameter has priority |
| 135 | + try: |
| 136 | + verbo = getattr(logging, args.loglevel) |
| 137 | + except: |
| 138 | + # If no command-line parameter then read from config file |
| 139 | + try: |
| 140 | + verbo = config.get('Service', 'verbosity') |
| 141 | + verbo = getattr(logging, verbo) |
| 142 | + except: |
| 143 | + # Otherwise, default value |
| 144 | + verbo = logging.INFO |
| 145 | + |
| 146 | + # INFO is the default value |
| 147 | + logging.basicConfig(level=verbo) |
| 148 | + logs = logging.getLogger('getEIDAconfig') |
| 149 | + logs.setLevel(verbo) |
| 150 | + |
| 151 | + if not len(config.read(args.config)): |
| 152 | + logs.error('Configuration file %s could not be read' % args.config) |
| 153 | + |
| 154 | + print('Skipping routing information. Config file does not allow to ' |
| 155 | + + 'overwrite the information. (%s)' % args.config) |
| 156 | + |
| 157 | + try: |
| 158 | + os.remove('routing-tmp.xml.bin') |
| 159 | + except: |
| 160 | + pass |
| 161 | + |
| 162 | + try: |
| 163 | + if 'synchronize' in config.options('Service'): |
| 164 | + synchroList = config.get('Service', 'synchronize') |
| 165 | + except: |
| 166 | + # Otherwise, default value |
| 167 | + synchroList = '' |
| 168 | + |
| 169 | + mergeRoutes('routing.xml', synchroList) |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == '__main__': |
| 173 | + main() |
0 commit comments