From cce68e8da6ae7efe6a05bfe93d9e596ff563f1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BCllhorst?= Date: Mon, 24 Apr 2017 11:13:00 +0200 Subject: [PATCH] Wokring on support for hopglass server (raw.json). --- NodeHierarchy.py | 16 +++------ cloud/Link.py | 82 ++++++++++++--------------------------------- cloud/LocalGraph.py | 7 ++-- parser/Hopglass.py | 6 ++-- 4 files changed, 34 insertions(+), 77 deletions(-) diff --git a/NodeHierarchy.py b/NodeHierarchy.py index 8e2b574..7ae922f 100755 --- a/NodeHierarchy.py +++ b/NodeHierarchy.py @@ -50,18 +50,10 @@ class NodeHierarchy(object): def __createLinkObjects__(self): links = [] - for link in self.__hopglass.links: - try: - srcNode = self.nodes[link['source']['node_id']] - except: - srcNode = None - try: - dstNode = self.nodes[link['target']['node_id']] - except: - dstNode = None - - print('Create Link object #',len(links), '\r',end = '') - links.append(Link(link, srcNode, dstNode)) + for linkParID, linkPar in self.__hopglass.links.items(): + for linkID, link in linkPar.items(): + print('Create Link object #',len(links), '\r',end = '') + links.append(Link(link, (self.nodes[linkParID[0]], self.nodes[linkParID[1]]))) print('') return links diff --git a/cloud/Link.py b/cloud/Link.py index 214da49..c9915c1 100644 --- a/cloud/Link.py +++ b/cloud/Link.py @@ -1,69 +1,31 @@ class Link(object): - def __init__(self, LinkJsonObject, srcNode, dstNode): - self.__jsonObject__ = LinkJsonObject - self.__srcNode__ = srcNode - self.__dstNode__ = dstNode - self.linkType = self.__getLinkType__() - self.isVpn = self.__getLinkVpnState__() - - + def __init__(self, LinkJsonObject, nodes): + self.__jsonObject = LinkJsonObject + self.linkType, self.isVpn = self.__getLinkType__() + self.__nodes = nodes + def __getLinkType__(self): - type_src = None - type_dst = None - if self.__srcNode__ != None: - for k, v in self.__srcNode__.interfaces.items(): - if self.__jsonObject__['source']['interface_mac'] in v: - type_src = k - if self.__dstNode__ != None: - for k, v in self.__dstNode__.interfaces.items(): - if self.__jsonObject__['target']['interface_mac'] in v: - type_dst = k - - if type_src == type_dst: - if type_src == None: - return 'unknown' - return type_src - else: - if type_src == None: - return type_dst - elif type_dst == None: - return type_src - else: - #print(self.__srcNode__.hostname, type_src, '<-->', self.__dstNode__.hostname, type_dst) - if type_src == 'wireless': - return type_dst - else: - return type_src - - def __getLinkVpnState__(self): - if self.__jsonObject__['vpn'] == True: - return True - for node in self.getEndpointNodes(getGateways = True): - if node.isGateway == True: - return True - return False - + types = [x['type'] for x in self.__jsonObject] + ltype = types[0] + lvpn = False + for x in types: + if x != 'unknown' and x != 'other': + if x == 'l2tp' or x == 'tunnel': + lvpn = True + val = x + return ltype, lvpn + def getEndpointNodes(self, getGateways = False): - nodes = [] - if self.__srcNode__ != None: - if getGateways == True or self.__srcNode__.isGateway == False: - nodes.append(self.__srcNode__) - if self.__dstNode__ != None: - if getGateways == True or self.__dstNode__.isGateway == False: - nodes.append(self.__dstNode__) - return nodes - + return self.__nodes + def getEndpointNodeIDs(self, getGateways = True): - nodeIDs = [] - for node in self.getEndpointNodes(getGateways): - nodeIDs.append(node.nodeID) - return nodeIDs - + return [x.nodeID for x in self.__nodes] + def isNodeIDinLink(self, nodeID): - for endpoint in self.getEndpointNodes(): - if endpoint.nodeID == nodeID: + for x in self.__nodes: + if nodeID == x.nodeID: return True return False - + def isNodeInLink(self, node): return self.isNodeIDinLink(node.nodeID) diff --git a/cloud/LocalGraph.py b/cloud/LocalGraph.py index e81eaf8..29ff1f4 100644 --- a/cloud/LocalGraph.py +++ b/cloud/LocalGraph.py @@ -125,6 +125,9 @@ class LocalGraph(Graph): print('BranchesThatExistsInCloud:', self.getBranchesThatExistsInCloud()) print('lan links in cloud:') for link in self.getLanLinksInCloud(): - if link.__srcNode__ != None and link.__dstNode__ != None: - print(' ', link.__srcNode__.hostname, '<--->', link.__dstNode__.hostname) + hosts = link.getEndpointNodes() + if len(hosts) == 1: + print(' ', hosts.hostname, 'has unknown neighbour.') + else: + print(' ', hosts[0].hostname, '<--->', hosts[1].hostname) print('=====') diff --git a/parser/Hopglass.py b/parser/Hopglass.py index 8bca9f2..25b1736 100644 --- a/parser/Hopglass.py +++ b/parser/Hopglass.py @@ -11,9 +11,9 @@ class Hopglass(JsonParser): self.gatewayMacs = [] self.gateways = [] self.__aggregateData__() - #print(self.ifIDs) - for k, v in self.links.items(): - print(k,v,'\n') + # print(self.ifIDs) + # for k, v in self.links.items(): + # print(k,v,'\n') def __aggregateData__(self): for nodeID, nodeData in self.__jsonData__.items():