Exemplo n.º 1
0
        def create_node(protocol):
            node = Node("TOR_TEST",
                        "",
                        metadata={
                            "Name": "Test TOR",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            node = Node("PORT_TEST",
                        "",
                        metadata={
                            "Name": "Test port",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            edge = Edge("TOR_L2LINK",
                        "",
                        "TOR_TEST",
                        "PORT_TEST",
                        metadata={"RelationType": "layer2"})
            msg = WSMessage("Graph", EdgeAddedMsgType, edge)
            protocol.sendWSMessage(msg)
Exemplo n.º 2
0
    def onOpen(self):
        print("WebSocket connection open.")

        # create a fabric port
        node = Node("PORT_TEST", "host-test",
                    metadata={"Name": "Test port !", "Type": "fabric"})
        msg = WSMessage("Graph", NodeAddedMsgType, node)
        self.sendWSMessage(msg)

        # get the TOR id
        restclient = RESTClient("localhost:8082")
        nodes = restclient.lookup_nodes("G.V().Has('Name', 'TOR2')")
        if len(nodes) != 1:
            print("More than one node found")
            sys.exit(-1)
        tor_id = nodes[0].id

        # create a ownership + layer2 link
        edge = Edge(tor_id + node.id + "ownership", "host-test",
                    tor_id, node.id,
                    metadata={"RelationType": "ownership"})
        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
        self.sendWSMessage(msg)

        edge = Edge(tor_id + node.id + "layer2", "",
                    tor_id, node.id,
                    metadata={"RelationType": "layer2", "Type": "fabric"})
        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
        self.sendWSMessage(msg)

        print("Success!")
        self.sendClose()
Exemplo n.º 3
0
    def onOpen(self):
        result = self.factory.kwargs["result"]

        endpoints = {}
        for entry in result:
            if entry.service_host in endpoints:
                endpoints[entry.service_host].add(entry.service_name)
            else:
                endpoints[entry.service_host] = set([entry.service_name])

            if entry.client_host in endpoints:
                endpoints[entry.client_host].add(entry.client_name)
            else:
                endpoints[entry.client_host] = set([entry.client_name])

        for k, v in endpoints.iteritems():
            """
            hid = uuid.uuid5(uuid.NAMESPACE_DNS, str(k))
            node = Node(str(hid), k, metadata={"Name": k, "Type": "host"})
            msg = WSMessage("Graph", NodeAddedMsgType, node)

            self.sendWSMessage(msg)
            """
            for name in v:
                nid = uuid.uuid5(uuid.NAMESPACE_DNS, str(k + name))
                node = Node(str(nid),
                            k,
                            metadata={
                                "Name": name,
                                "Type": "service"
                            })
                msg = WSMessage("Graph", NodeAddedMsgType, node)
                self.sendWSMessage(msg)
                """
                edge = Edge(str(uuid.uuid1()), k, str(hid), str(nid),
                            metadata={"RelationType": "ownership"})
                msg = WSMessage("Graph", EdgeAddedMsgType, edge)
                self.sendWSMessage(msg)
                """

        for entry in result:
            try:
                nid1 = uuid.uuid5(uuid.NAMESPACE_DNS,
                                  str(entry.client_host + entry.client_name))
                nid2 = uuid.uuid5(uuid.NAMESPACE_DNS,
                                  str(entry.service_host + entry.service_name))
                edge = Edge(str(uuid.uuid1()),
                            k,
                            str(nid1),
                            str(nid2),
                            metadata={
                                "RelationType": "layer2",
                                "Type": "Connection"
                            })
                msg = WSMessage("Graph", EdgeAddedMsgType, edge)
                self.sendWSMessage(msg)
            except Exception as e:
                print(e)
Exemplo n.º 4
0
    def onOpen(self):
        module = self.factory.kwargs["module"]
        params = self.factory.kwargs["params"]
        result = self.factory.kwargs["result"]

        node1 = self.factory.kwargs["node1"]
        node2 = self.factory.kwargs["node2"]

        if module.check_mode:
            self.stop()
            return

        try:
            host = params["host"]

            metadata = params["metadata"]
            metadata["RelationType"] = params["relation_type"]

            uid = uuid.uuid5(
                uuid.NAMESPACE_OID,
                "%s:%s:%s" % (node1, node2, params["relation_type"]))

            edge = Edge(str(uid), host, node1, node2, metadata=metadata)

            msg = WSMessage("Graph", EdgeAddedMsgType, edge)
            self.sendWSMessage(msg)

            result["UUID"] = str(uid)
        except Exception as e:
            module.fail_json(msg='Error during topology update %s' % e,
                             **result)
        finally:
            self.stop()
Exemplo n.º 5
0
    def onOpen(self):
        module = self.factory.kwargs["module"]
        params = self.factory.kwargs["params"]
        result = self.factory.kwargs["result"]
        edges = self.factory.kwargs["edges"]

        if module.check_mode:
            self.stop()
            return

        try:
            host = params["host"]
            uuids = []
            for edge in edges:
                metadata = edge.get("metadata", dict())
                metadata["RelationType"] = edge["relation_type"]
                node1 = edge["node1"]
                node2 = edge["node2"]
                uid = uuid.uuid5(
                    uuid.NAMESPACE_OID,
                    "%s:%s:%s" % (node1, node2, edge["relation_type"]))
                edge = Edge(str(uid), host, node1, node2, metadata=metadata)
                msg = WSMessage("Graph", EdgeAddedMsgType, edge)
                self.sendWSMessage(msg)
                uuids.append(uid)
            result["UUID"] = str(uuids)
        except Exception as e:
            module.fail_json(msg='Error during topology update %s' % e,
                             **result)
        finally:
            self.factory.client.loop.call_soon(self.stop)
Exemplo n.º 6
0
    def onOpen(self):
        module = self.factory.kwargs["module"]
        params = self.factory.kwargs["params"]
        result = self.factory.kwargs["result"]

        if module.check_mode:
            self.stop()
            return

        try:
            host = params["host"]

            metadata = params["metadata"]
            metadata["Name"] = params["name"]
            metadata["Type"] = params["type"]

            seed = params["seed"]
            if len(seed) == 0:
                seed = "%s:%s" % (params["name"], params["type"])
            uid = str(uuid.uuid5(uuid.NAMESPACE_OID, seed))

            node = Node(str(uid), host, metadata=metadata)

            msg = WSMessage("Graph", NodeAddedMsgType, node)
            self.sendWSMessage(msg)

            result["UUID"] = uid
        except Exception as e:
            module.fail_json(msg='Error during topology update %s' % e,
                             **result)
        finally:
            self.stop()
Exemplo n.º 7
0
    def onOpen(self):
        module = self.factory.kwargs["module"]
        params = self.factory.kwargs["params"]
        result = self.factory.kwargs["result"]

        if module.check_mode:
            self.stop()
            return

        try:
            host = params["host"]
            nodes = params["nodes"]
            uuids = []
            for node in nodes:
                metadata = dict()
                md = node.get("metadata", dict())
                for (k, v) in md.items():
                    metadata[k] = v
                metadata["Name"] = node["name"]
                metadata["Type"] = node["type"]
                seed = node.get("seed", "")
                if len(seed) == 0:
                    seed = "%s:%s" % (node["name"], node["type"])
                uid = str(uuid.uuid5(uuid.NAMESPACE_OID, seed))

                node = Node(str(uid), host, metadata=metadata)
                msg = WSMessage("Graph", NodeAddedMsgType, node)
                self.sendWSMessage(msg)
                uuids.append(uid)
            result["UUID"] = uuids
        except Exception as e:
            module.fail_json(msg='Error during topology update %s' % e,
                             **result)
        finally:
            self.factory.client.loop.call_soon(self.stop)
Exemplo n.º 8
0
    def addNode(self, id, metadata):
        node = Node(hashlib.md5(id.encode()).hexdigest(),
                    "python-client",
                    metadata=metadata)
        msg = WSMessage("Graph", NodeAddedMsgType, node)
        self.sendWSMessage(msg)

        return node
Exemplo n.º 9
0
 def addEdge(self, id1, id2, metadata):
     id = id1 + id2
     edge = Edge(hashlib.md5(id.encode()).hexdigest(),
                 "python-client",
                 id1,
                 id2,
                 metadata=metadata)
     msg = WSMessage("Graph", EdgeAddedMsgType, edge)
     self.sendWSMessage(msg)
Exemplo n.º 10
0
        def create_node(protocol):
            node = Node("TOR_TEST",
                        "",
                        metadata={
                            "Name": "Test TOR",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            node = Node("PORT_TEST",
                        "",
                        metadata={
                            "Name": "Test port",
                            "Type": "fabric"
                        })
            msg = WSMessage("Graph", NodeAddedMsgType, node)
            protocol.sendWSMessage(msg)

            edge = Edge("TOR_L2LINK",
                        "",
                        "TOR_TEST",
                        "PORT_TEST",
                        metadata={"RelationType": "layer2"})
            msg = WSMessage("Graph", EdgeAddedMsgType, edge)
            protocol.sendWSMessage(msg)

            time.sleep(1)

            restclient = RESTClient("localhost:8082")
            nodes = restclient.lookup_nodes("G.V().Has('Name', 'Test port')")
            self.assertEqual(len(nodes), 1, "should find one an only one node")

            tor_id = nodes[0].id
            self.assertEqual(tor_id, nodes[0].id, "wrong id for node")

            edges = restclient.lookup_edges(
                "G.E().Has('RelationType', 'layer2')")
            self.assertEqual(len(edges), 1, "should find one an only one edge")
Exemplo n.º 11
0
    def onOpen(self):
        LOG.info("WebSocket connection open.")

        mode = self.factory.kwargs["mode"]
        file = self.factory.kwargs["file"]

        if mode and mode[-1] == 'e':
            LOG.info(mode[:-1] + "ing: " + file)
        else:
            LOG.info(mode + "ing: " + file)

        with open(file) as json_file:
            data = json.load(json_file)

            if "Nodes" in data:
                for node in data["Nodes"]:
                    if mode == 'add':
                        msg = WSMessage("Graph", NodeAddedMsgType, node)
                    elif mode == 'delete':
                        msg = WSMessage("Graph", NodeDeletedMsgType, node)
                    elif mode == 'update':
                        msg = WSMessage("Graph", NodeUpdatedMsgType, node)

                    self.sendWSMessage(msg)

            if "Edges" in data:
                for edge in data["Edges"]:
                    if mode == 'add':
                        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
                    elif mode == 'delete':
                        msg = WSMessage("Graph", EdgeDeletedMsgType, edge)
                    elif mode == 'update':
                        msg = WSMessage("Graph", EdgeUpdatedMsgType, edge)

                    self.sendWSMessage(msg)

        self.stop()
Exemplo n.º 12
0
    def onOpen(self):
        module = self.factory.kwargs["module"]
        params = self.factory.kwargs["params"]
        result = self.factory.kwargs["result"]
        if "node1" and "node2" in self.factory.kwargs:
            node1 = self.factory.kwargs["node1"]
            node2 = self.factory.kwargs["node2"]

        if module.check_mode:
            self.stop()
            return
        try:
            host = params["host"]
            if params["metadata"]:
                metadata = module._check_type_dict(params["metadata"])
            else:
                metadata = {}
            if "node_type" in params:
                metadata["Name"] = params["name"]
                metadata["Type"] = params["node_type"]
                seed = params["seed"]
                if not seed:
                    seed = "%s:%s" % (params["name"], params["node_type"])
                if (module.params["state"] == "present"
                        or module.params["state"] == "update"):
                    uid = str(uuid.uuid5(uuid.NAMESPACE_OID, seed))
                    node = Node(uid, host, metadata=metadata)
                    if module.params["state"] == "present":
                        msg = WSMessage("Graph", NodeAddedMsgType, node)
                    else:
                        msg = WSMessage("Graph", NodeUpdatedMsgType, node)
                else:
                    uid = params["id"]
                    node = Node(uid, host, metadata=metadata)
                    msg = WSMessage("Graph", NodeDeletedMsgType, node)
            elif "relation_type" in params:
                metadata["RelationType"] = params["relation_type"]
                if (module.params["state"] == "present"
                        or module.params["state"] == "update"):
                    uid = str(
                        uuid.uuid5(
                            uuid.NAMESPACE_OID,
                            "%s:%s:%s" %
                            (node1, node2, params["relation_type"]),
                        ))
                    edge = Edge(uid, host, node1, node2, metadata=metadata)
                    if module.params["state"] == "present":
                        msg = WSMessage("Graph", EdgeAddedMsgType, edge)
                    else:
                        msg = WSMessage("Graph", EdgeUpdatedMsgType, edge)
                else:
                    uid = module.params["id"]
                    edge = Edge(uid, host, node1, node2, metadata=metadata)
                    msg = WSMessage("Graph", EdgeDeletedMsgType, edge)

            self.sendWSMessage(msg)
            if uid:
                result["UUID"] = uid
            result["changed"] = True
        except Exception as e:
            module.fail_json(msg="Error during topology update %s" % e,
                             **result)
        finally:
            self.stop()