Example #1
0
func TestTableServer(t *testing.T) {
	ts := NewTestStorage()

	aa := helper.NewAgentAnalyzerWithConfig(t, confAgentAnalyzer, ts)
	fclient := flow.NewTableClient(aa.Analyzer.WSServer)

	aa.Start()
	defer aa.Stop()

	client, err := api.NewCrudClientFromConfig(&http.AuthenticationOpts{})
	if err != nil {
		t.Fatal(err.Error())
	}

	capture := api.NewCapture("G.V().Has('Name', 'br-sflow', 'Type', 'ovsbridge')", "")
	if err := client.Create("capture", capture); err != nil {
		t.Fatal(err.Error())
	}

	time.Sleep(1 * time.Second)
	setupCmds := []helper.Cmd{
		{"ovs-vsctl add-br br-sflow", true},
		{"ovs-vsctl add-port br-sflow sflow-intf1 -- set interface sflow-intf1 type=internal", true},
		{"ip address add 169.254.33.33/24 dev sflow-intf1", true},
		{"ip link set sflow-intf1 up", true},
		{"ping -c 15 -I sflow-intf1 169.254.33.34", false},
	}

	tearDownCmds := []helper.Cmd{
		{"ovs-vsctl del-br br-sflow", true},
	}

	helper.ExecCmds(t, setupCmds...)
	defer helper.ExecCmds(t, tearDownCmds...)

	gh := helper.NewGremlinQueryHelper(&http.AuthenticationOpts{})

	node := gh.GetNodeFromGremlinReply(t, `g.V().Has("Name", "br-sflow", "Type", "ovsbridge")`)

	hnmap := make(topology.HostNodeTIDMap)
	hnmap[node.Host()] = append(hnmap[node.Host()], node.Metadata()["TID"].(string))

	fsq := flow.FlowSearchQuery{}
	flowset, err := fclient.LookupFlowsByNodes(hnmap, fsq)
	if err != nil {
		t.Fatal(err.Error())
	}

	aa.Flush()

	if len(ts.GetFlows()) != len(flowset.Flows) {
		t.Fatalf("Should return the same number of flows than in the database, got: %v", flowset)
	}

	for _, f := range flowset.Flows {
		if f.NodeTID != node.Metadata()["TID"].(string) {
			t.Fatalf("Returned a non expected flow: %v", f)
		}
	}

	client.Delete("capture", capture.ID())
}
Example #2
0
func NewServerFromConfig() (*Server, error) {
	embedEtcd := config.GetConfig().GetBool("etcd.embedded")

	backend, err := graph.BackendFromConfig()
	if err != nil {
		return nil, err
	}

	g, err := graph.NewGraphFromConfig(backend)
	if err != nil {
		return nil, err
	}

	httpServer, err := shttp.NewServerFromConfig("analyzer")
	if err != nil {
		return nil, err
	}

	wsServer := shttp.NewWSServerFromConfig(httpServer, "/ws")

	var etcdServer *etcd.EmbeddedEtcd
	if embedEtcd {
		if etcdServer, err = etcd.NewEmbeddedEtcdFromConfig(); err != nil {
			return nil, err
		}
	}

	etcdClient, err := etcd.NewEtcdClientFromConfig()
	if err != nil {
		return nil, err
	}

	analyzerUpdate := config.GetConfig().GetInt("analyzer.flowtable_update")
	analyzerExpire := config.GetConfig().GetInt("analyzer.flowtable_expire")
	agentRatio := config.GetConfig().GetFloat64("analyzer.flowtable_agent_ratio")
	if agentRatio == 0.0 {
		agentRatio = 0.5
	}

	agentUpdate := int64(float64(analyzerUpdate) * agentRatio)
	agentExpire := int64(float64(analyzerExpire) * agentRatio)

	if err := etcdClient.SetInt64("/agent/config/flowtable_update", agentUpdate); err != nil {
		return nil, err
	}

	if err := etcdClient.SetInt64("/agent/config/flowtable_expire", agentExpire); err != nil {
		return nil, err
	}

	apiServer, err := api.NewApi(httpServer, etcdClient.KeysApi)
	if err != nil {
		return nil, err
	}

	captureApiHandler := &api.CaptureApiHandler{
		BasicApiHandler: api.BasicApiHandler{
			ResourceHandler: &api.CaptureResourceHandler{},
			EtcdKeyAPI:      etcdClient.KeysApi,
		},
		Graph: g,
	}
	err = apiServer.RegisterApiHandler(captureApiHandler)
	if err != nil {
		return nil, err
	}

	alertApiHandler := &api.AlertApiHandler{
		BasicApiHandler: api.BasicApiHandler{
			ResourceHandler: &api.AlertResourceHandler{},
			EtcdKeyAPI:      etcdClient.KeysApi,
		},
	}
	err = apiServer.RegisterApiHandler(alertApiHandler)
	if err != nil {
		return nil, err
	}

	alertManager := alert.NewAlertManager(g, alertApiHandler)

	aserver := alert.NewServer(alertManager, wsServer)
	gserver := graph.NewServer(g, wsServer)

	gfe := mappings.NewGraphFlowEnhancer(g)
	ofe := mappings.NewOvsFlowEnhancer(g)

	pipeline := mappings.NewFlowMappingPipeline(gfe, ofe)

	tableClient := flow.NewTableClient(wsServer)

	server := &Server{
		HTTPServer:          httpServer,
		WSServer:            wsServer,
		GraphServer:         gserver,
		AlertServer:         aserver,
		FlowMappingPipeline: pipeline,
		TableClient:         tableClient,
		EmbeddedEtcd:        etcdServer,
		EtcdClient:          etcdClient,
	}
	server.SetStorageFromConfig()

	updateHandler := flow.NewFlowHandler(server.flowExpireUpdate, time.Second*time.Duration(analyzerUpdate))
	expireHandler := flow.NewFlowHandler(server.flowExpireUpdate, time.Second*time.Duration(analyzerExpire))
	flowtable := flow.NewTable(updateHandler, expireHandler)
	server.FlowTable = flowtable

	api.RegisterTopologyApi("analyzer", g, httpServer, tableClient, server.Storage)

	api.RegisterFlowApi("analyzer", flowtable, server.Storage, httpServer)

	return server, nil
}