From 666cd20c04c38c265179a5ff9c10e0027954afcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BCllhorst?= Date: Sun, 23 Apr 2017 22:57:07 +0200 Subject: [PATCH] Wokring on support for hopglass server (raw.json). --- NodeHierarchy.py | 14 +++++---- parser/Hopglass.py | 74 ++++++++++++++++++++++++++++++++++++++++++++ parser/JsonParser.py | 4 +-- requirements.txt | 1 + 4 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 parser/Hopglass.py create mode 100644 requirements.txt diff --git a/NodeHierarchy.py b/NodeHierarchy.py index 27b1331..bd31eb9 100755 --- a/NodeHierarchy.py +++ b/NodeHierarchy.py @@ -1,7 +1,8 @@ #!/usr/bin/python3 import argparse -from parser.NodesParser import NodesParser -from parser.GraphParser import GraphParser +# from parser.NodesParser import NodesParser +# from parser.GraphParser import GraphParser +from parser.Hopglass import Hopglass from cloud.Node import Node from cloud.Link import Link from cloud.GlobalGraph import GlobalGraph @@ -13,8 +14,9 @@ from info.Info import Info class NodeHierarchy(object): def __init__(self): self.__args__ = self.__parseArguments__() - self.__nodesJson__ = NodesParser(self.__args__.json_path) - self.__graphJson__ = GraphParser(self.__args__.json_path) + self.__hopglass = Hopglass(self.__args__.raw_json) + # self.__nodesJson__ = NodesParser(self.__args__.json_path) + # self.__graphJson__ = GraphParser(self.__args__.json_path) self.__shapesJson__ = self.__parseShapes__() self.nodes = self.__createNodeObjects__() self.links = self.__createLinkObjects__() @@ -48,7 +50,7 @@ class NodeHierarchy(object): def __createLinkObjects__(self): links = [] - for link in self.__graphJson__.links: + for link in self.__hopglass.links: try: srcNode = self.nodes[link['source']['node_id']] except: @@ -68,7 +70,7 @@ class NodeHierarchy(object): def __parseArguments__(self): parser = argparse.ArgumentParser(description='This Script generates a hierarchical nodes list for node migration using nginx geo feature.') - parser.add_argument('-j', '--json-path', required=False, default='https://service.freifunk-muensterland.de/maps/data/', help='Path of nodes.json and graph.json (can be local folder or remote URL).') + parser.add_argument('-r', '--raw-json', required=False, default='https://karte.freifunk-muensterland.de/data/raw.json', help='Location of raw.json file (can be local folder or remote URL).') parser.add_argument('-s', '--shapes-path', required=False, default='https://freifunk-muensterland.de/md-fw-dl/shapes/', help='Path of shapefiles (can be local folder or remote URL).') parser.add_argument('-t', '--targets', nargs='+', required=True, help='List of targets which should be proceeded. Example: -t citya cityb ...') parser.add_argument('-o', '--out-file', default='./webserver-configuration', required=False, help='Filename where the generated Output should stored.') diff --git a/parser/Hopglass.py b/parser/Hopglass.py new file mode 100644 index 0000000..aa11eef --- /dev/null +++ b/parser/Hopglass.py @@ -0,0 +1,74 @@ +from parser.JsonParser import JsonParser +import collections +import json + +class Hopglass(JsonParser): + def __init__(self, filePath): + super().__init__(filePath) + self.ifIDs = {} + self.links = collections.defaultdict(dict) + self.nodes = {} + self.__aggregateData__() + #print(self.ifIDs) + for k, v in self.links.items(): + print(k,v,'\n') + + def __aggregateData__(self): + for nodeID, nodeData in self.__jsonData__.items(): + + # let pass nodes that provide all required informations only + if not set(('nodeinfo', 'neighbours')) <= set(nodeData): + continue + + nodeInfo = nodeData['nodeinfo'] + neighbours = nodeData['neighbours'] + + if not 'batadv' in neighbours: + continue + + if not 'mesh' in nodeInfo.get('network', {}): + continue + + for batID, batVal in nodeInfo['network']['mesh'].items(): + if not 'interfaces' in batVal: + continue + for ifType, ifVal in batVal['interfaces'].items(): + for mac in ifVal: + self.ifIDs[mac] = { + 'type' : ifType, + 'nodeID' : nodeID + } + + self.nodes[nodeID] = nodeData + + for nodeID, nodeData in self.nodes.items(): + for iname, ivalue in nodeData['neighbours']['batadv'].items(): + if 'neighbours' not in ivalue: + continue + if not iname in self.ifIDs: + continue + for nname, nvalue in ivalue['neighbours'].items(): + if nname not in self.ifIDs: + continue + nifID = self.ifIDs[nname]['nodeID'] + partID = (nodeID, nifID) if nodeID > nifID else (nifID, nodeID) + linkID = (iname, nname) if iname > nname else (nname, iname) + + linkNode = { + 'nodeID' : nodeID, + 'type' : self.ifIDs[iname]['type'], + 'tq' : nvalue['tq'] + } + + if linkID in self.links[partID]: + self.links[partID][linkID].append(linkNode) + else: + self.links[partID][linkID] = [linkNode] + + + def getLinksForNodeID(self, nodeID): + links = [] + for link in self.links: + if link['target']['node_id'] == nodeID or link['source']['node_id'] == nodeID: + links.append(link) + return links diff --git a/parser/JsonParser.py b/parser/JsonParser.py index 96f914f..d9bed53 100644 --- a/parser/JsonParser.py +++ b/parser/JsonParser.py @@ -9,7 +9,7 @@ class JsonParser(object): def __getFile__(self, fileName): if fileName.startswith('https://') or fileName.startswith('http://'): if self.printStatus: - print('Download', fileName.rsplit('/', 1)[1] , 'from URL:', fileName) + print('Download', fileName, 'from URL:', fileName) resource = urllib.request.urlopen(fileName) try: data = json.loads(resource.read().decode('utf-8')) @@ -19,7 +19,7 @@ class JsonParser(object): resource.close() else: if self.printStatus: - print('Open', fileName.rsplit('/', 1)[1] , 'from file:', fileName) + print('Open', fileName, 'from file:', fileName) with open(fileName) as data_file: try: data = json.load(data_file) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..231726c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +shapely \ No newline at end of file