Beispiel #1
0
func (f *FlowTraversalStep) Nodes(s ...interface{}) *traversal.GraphTraversalV {
	var nodes []*graph.Node

	if f.Error() != nil {
		return traversal.NewGraphTraversalV(f.GraphTraversal, nodes, f.Error())
	}

	m, err := traversal.SliceToMetadata(s...)
	if err != nil {
		logging.GetLogger().Critical(err)
		return traversal.NewGraphTraversalV(f.GraphTraversal, nodes, err)
	}

	for _, flow := range f.flowset.Flows {
		if flow.NodeUUID != "" && flow.NodeUUID != "*" {
			if node := f.GraphTraversal.Graph.GetNode(graph.Identifier(flow.NodeUUID)); node != nil && node.MatchMetadata(m) {
				nodes = append(nodes, node)
			}
		}
		if flow.ANodeUUID != "" && flow.ANodeUUID != "*" {
			if node := f.GraphTraversal.Graph.GetNode(graph.Identifier(flow.ANodeUUID)); node != nil && node.MatchMetadata(m) {
				nodes = append(nodes, node)
			}
		}
		if flow.BNodeUUID != "" && flow.BNodeUUID != "*" {
			if node := f.GraphTraversal.Graph.GetNode(graph.Identifier(flow.BNodeUUID)); node != nil && node.MatchMetadata(m) {
				nodes = append(nodes, node)
			}
		}
	}
	return traversal.NewGraphTraversalV(f.GraphTraversal, nodes)
}
Beispiel #2
0
func (s *HopsGremlinTraversalStep) Exec(last traversal.GraphTraversalStep) (traversal.GraphTraversalStep, error) {
	var nodes []*graph.Node

	switch last.(type) {
	case *FlowTraversalStep:
		fs := last.(*FlowTraversalStep)
		graphTraversal := fs.GraphTraversal

		m, err := traversal.SliceToMetadata(s.context.Params...)
		if err != nil {
			return nil, traversal.ExecutionError
		}

		for _, f := range fs.flowset.Flows {
			if node := graphTraversal.Graph.GetNode(graph.Identifier(f.NodeUUID)); node != nil && node.MatchMetadata(m) {
				nodes = append(nodes, node)
			}
		}

		graphTraversalV := traversal.NewGraphTraversalV(graphTraversal, nodes)
		return graphTraversalV, nil
	}

	return nil, traversal.ExecutionError
}
Beispiel #3
0
func (f *FlowTraversalStep) MarshalJSON() ([]byte, error) {
	a := make([]interface{}, len(f.flowset.Flows))
	for i, flow := range f.flowset.Flows {
		b, err := json.Marshal(flow)
		if err != nil {
			logging.GetLogger().Errorf("Error while converting flow to JSON: %v", flow)
			continue
		}

		d := json.NewDecoder(bytes.NewReader(b))
		d.UseNumber()

		var x map[string]interface{}
		err = d.Decode(&x)
		if err != nil {
			logging.GetLogger().Errorf("Error while converting flow to JSON: %v", flow)
			continue
		}

		// substitute UUID by the node
		if flow.ANodeUUID != "" && flow.ANodeUUID != "*" {
			node := f.GraphTraversal.Graph.GetNode(graph.Identifier(flow.ANodeUUID))
			if node != nil {
				x["ANode"] = node
			}
		}

		if flow.BNodeUUID != "" && flow.BNodeUUID != "*" {
			node := f.GraphTraversal.Graph.GetNode(graph.Identifier(flow.BNodeUUID))
			if node != nil {
				x["BNode"] = node
			}
		}

		node := f.GraphTraversal.Graph.GetNode(graph.Identifier(flow.NodeUUID))
		if node != nil {
			x["Node"] = node
		}

		a[i] = x
	}
	return json.Marshal(a)
}
Beispiel #4
0
func NewGraph(t *testing.T) *graph.Graph {
	var backend graph.GraphBackend
	var err error
	switch graphBackend {
	case "gremlin-ws":
		backend, err = graph.NewGremlinBackend("ws://127.0.0.1:8182")
	case "gremlin-rest":
		backend, err = graph.NewGremlinBackend("http://127.0.0.1:8182?gremlin=")
	case "elasticsearch":
		backend, err = graph.NewElasticSearchBackend("127.0.0.1", "9200", 10, 60, 1)
		if err == nil {
			backend, err = graph.NewShadowedBackend(backend)
		}
	case "orientdb":
		password := os.Getenv("ORIENTDB_ROOT_PASSWORD")
		if password == "" {
			password = "******"
		}
		backend, err = graph.NewOrientDBBackend("http://127.0.0.1:2480", "TestSkydive", "root", password)
	default:
		backend, err = graph.NewMemoryBackend()
	}

	if err != nil {
		t.Fatal(err.Error())
	}

	t.Logf("Using %s as backend", graphBackend)

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

	hostname, err := os.Hostname()
	if err != nil {
		t.Fatal(err.Error())
	}

	root := g.LookupFirstNode(graph.Metadata{"Name": hostname, "Type": "host"})
	if root == nil {
		root = g.NewNode(graph.Identifier(hostname), graph.Metadata{"Name": hostname, "Type": "host"})
		if root == nil {
			t.Fatal("fail while adding root node")
		}
	}

	return g
}
Beispiel #5
0
func (fb *FabricProbe) addFabricNodeFromDef(nodeDef string) (*graph.Node, error) {
	nodeName, metadata, err := nodeDefToMetadata(nodeDef)
	if err != nil {
		return nil, err
	}

	u, _ := uuid.NewV5(uuid.NamespaceOID, []byte("fabric"+nodeName))
	id := graph.Identifier(u.String())

	if node := fb.Graph.GetNode(id); node != nil {
		return node, nil
	}
	metadata["Probe"] = "fabric"

	return fb.Graph.NewNode(id, metadata), nil
}
Beispiel #6
0
func CreateRootNode(g *graph.Graph) *graph.Node {
	hostID := config.GetConfig().GetString("host_id")
	m := graph.Metadata{"Name": hostID, "Type": "host"}
	if config.GetConfig().IsSet("agent.metadata") {
		subtree := config.GetConfig().Sub("agent.metadata")
		for key, value := range subtree.AllSettings() {
			m[key] = value
		}
	}
	buffer, err := ioutil.ReadFile("/var/lib/cloud/data/instance-id")
	if err == nil {
		m["InstanceID"] = strings.TrimSpace(string(buffer))
	}
	u, _ := uuid.NewV5(uuid.NamespaceOID, []byte(hostID))
	return g.NewNode(graph.Identifier(u.String()), m)
}
Beispiel #7
0
func (o *OnDemandProbeServer) OnMessage(msg shttp.WSMessage) {
	if msg.Namespace != ondemand.Namespace {
		return
	}

	var query ondemand.CaptureQuery
	if err := json.Unmarshal([]byte(*msg.Obj), &query); err != nil {
		logging.GetLogger().Errorf("Unable to decode capture %v", msg)
		return
	}

	o.Graph.Lock()
	defer o.Graph.Unlock()

	status := http.StatusOK
	n := o.Graph.GetNode(graph.Identifier(query.NodeID))
	if n == nil {
		logging.GetLogger().Errorf("Unknown node %s for new capture", query.NodeID)
		status = http.StatusNotFound
	} else {
		var ok bool
		switch msg.Type {
		case "CaptureStart":
			if ok = o.registerProbe(n, &query.Capture); ok {
				t := o.Graph.StartMetadataTransaction(n)
				t.AddMetadata("State/FlowCapture", "ON")
				t.AddMetadata("CaptureID", query.Capture.UUID)
				t.Commit()
			}

		case "CaptureStop":
			if ok = o.unregisterProbe(n); ok {
				metadata := n.Metadata()
				metadata["State/FlowCapture"] = "OFF"
				delete(metadata, "CaptureID")
				o.Graph.SetMetadata(n, metadata)
			}
		}

		if !ok {
			status = http.StatusBadRequest
		}
	}

	reply := msg.Reply(&ondemand.CaptureQuery{}, "CaptureStopReply", status)
	o.wsClient.SendWSMessage(reply)
}
Beispiel #8
0
func (t *GraphTraversal) V(s ...interface{}) *GraphTraversalV {
	var nodes []*graph.Node
	var metadata graph.Metadata
	var err error

	if t.error != nil {
		return &GraphTraversalV{error: t.error}
	}

	switch len(s) {
	case 1:
		id, ok := s[0].(string)
		if !ok {
			return &GraphTraversalV{error: fmt.Errorf("V accepts only a string when there is only one argument")}
		}
		node := t.Graph.GetNode(graph.Identifier(id))
		if node == nil {
			return &GraphTraversalV{error: fmt.Errorf("Node '%s' does not exist", id)}
		}
		nodes = []*graph.Node{node}
	default:
		if metadata, err = SliceToMetadata(s...); err != nil {
			return &GraphTraversalV{error: err}
		}
		fallthrough
	case 0:
		nodes = t.Graph.GetNodes(metadata)
	}

	if t.currentStepContext.PaginationRange != nil {
		var nodeRange []*graph.Node
		it := t.currentStepContext.PaginationRange.Iterator()
		for _, node := range nodes {
			if it.Done() {
				break
			} else if it.Next() {
				nodeRange = append(nodeRange, node)
			}
		}
		nodes = nodeRange
	}

	return &GraphTraversalV{GraphTraversal: t, nodes: nodes}
}
Beispiel #9
0
func (s *GremlinTraversalStepV) Exec(last GraphTraversalStep) (GraphTraversalStep, error) {
	g, ok := last.(*GraphTraversal)
	if !ok {
		return nil, ExecutionError
	}

	g.currentStepContext = s.StepContext

	switch len(s.Params) {
	case 1:
		if k, ok := s.Params[0].(string); ok {
			return g.V(graph.Identifier(k)), nil
		}
		return nil, ExecutionError
	case 0:
		return g.V(), nil
	default:
		return nil, ExecutionError
	}
}