def test_topology_rules(self): restclient = RESTClient("localhost:8082", scheme=self.schemeHTTP, username=self.username, password=self.password, insecure=True) noderule1 = restclient.noderule_create( "create", metadata={"Name": "node1", "Type": "fabric"}) noderule2 = restclient.noderule_create( "create", metadata={"Name": "node2", "Type": "fabric"}) time.sleep(1) edgerule = restclient.edgerule_create( "G.V().Has('Name', 'node1')", "G.V().Has('Name', 'node2')", {"RelationType": "layer2", "EdgeName": "my_edge"}) time.sleep(1) node1 = restclient.lookup_nodes("G.V().Has('Name', 'node1')") self.assertEqual(len(node1), 1, "should find only one node as node1") node2 = restclient.lookup_nodes("G.V().Has('Name', 'node2')") self.assertEqual(len(node2), 1, "should find only one node as node2") edge = restclient.lookup_edges( "G.E().Has('RelationType', 'layer2', 'EdgeName', 'my_edge')") self.assertEqual(len(edge), 1, "should find only one edge") restclient.edgerule_delete(edgerule.uuid) restclient.noderule_delete(noderule1.uuid) restclient.noderule_delete(noderule2.uuid)
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()
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()
def test_injection(self): 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) self.wsclient = WSClient("host-test2", self.schemeWS + "://localhost:8082/ws/publisher", protocol=WSTestClient, test=create_node, username=self.username, password=self.password, insecure=True) self.wsclient.connect() self.wsclient.start() time.sleep(1) restclient = RESTClient("localhost:8082", scheme=self.schemeHTTP, username=self.username, password=self.password, insecure=True) 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")
def clear_dragonflow_items(self): """Delete all the items created by DragonFlow""" restclient = RESTClient(cfg.CONF.df_skydive.analyzer_endpoint, username=cfg.CONF.df_skydive.user, password=cfg.CONF.df_skydive.password) items = restclient.lookup_edges("G.E().Has('source', 'dragonflow')") for edge in items: edge_del_msg = skydive_client.WSMessage( "Graph", skydive_client.EdgeDeletedMsgType, edge) self.protocol.sendWSMessage(edge_del_msg) items = restclient.lookup_nodes("G.V().Has('source', 'dragonflow')") for node in items: node_del_msg = skydive_client.WSMessage( "Graph", skydive_client.NodeDeletedMsgType, node) self.protocol.sendWSMessage(node_del_msg)
def make_query(params): """Performs the query with the rest client :param params: the parameters of the ansible module implemented. :returns: a list of nodes coded as dictionary (JSON representation of skydive node). """ scheme = "https" if params["ssl"] else "http" restclient = RESTClient(params["analyzer"], scheme=scheme, insecure=params["insecure"], username=params["username"], password=params["password"]) nodes = restclient.lookup_nodes(params["query"]) return [node.repr_json() for node in nodes]
def test_injection(self): 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) self.wsclient = WSClient("host-test2", self.schemeWS + "://localhost:8082/ws/publisher", protocol=WSTestClient, test=create_node, username=self.username, password=self.password, insecure=True) self.wsclient.connect() self.wsclient.start() time.sleep(1) restclient = RESTClient("localhost:8082", scheme=self.schemeHTTP, username=self.username, password=self.password, insecure=True) 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")
def get_node_id(module, node_selector): scheme = "http" if module.params["ssl"]: scheme = "https" if node_selector.startswith("G.") or node_selector.startswith("g."): restclient = RESTClient(module.params["analyzer"], scheme=scheme, insecure=module.params["insecure"], username=module.params["username"], password=module.params["password"]) nodes = restclient.lookup_nodes(node_selector) if len(nodes) == 0: raise Exception("Node not found: %s" % node_selector) elif len(nodes) > 1: raise Exception("Node selection should return only one node: %s" % node_selector) return str(nodes[0].id) return node_selector
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")