-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Using go-graphviz to generate not just the .dot but also a pn…
…g file." This reverts commit a155405.
- Loading branch information
Showing
3 changed files
with
35 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,68 @@ | ||
package clab | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
"github.com/goccy/go-graphviz" | ||
"github.com/goccy/go-graphviz/cgraph" | ||
"github.com/awalterschulze/gographviz" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var g *gographviz.Graph | ||
|
||
// GenerateGraph generates a graph for the lab topology | ||
func (c *cLab) GenerateGraph(topo string) error { | ||
h := graphviz.New() | ||
graph, err := h.Graph() | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Info("Generating lab graph ...") | ||
g = gographviz.NewGraph() | ||
if err := g.SetName(c.FileInfo.shortname); err != nil { | ||
return err | ||
} | ||
if err := g.SetDir(false); err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := graph.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
h.Close() | ||
}() | ||
|
||
graph.Attr(1, "style", "filled") // kind 1 == node defaults | ||
graph.Attr(1, "fillcolor", "red") // kind 1 == node defaults | ||
|
||
var graphNodeMap = make(map[string]*cgraph.Node) | ||
var attr map[string]string | ||
attr = make(map[string]string) | ||
attr["color"] = "red" | ||
attr["style"] = "filled" | ||
attr["fillcolor"] = "red" | ||
|
||
// Create Nodes | ||
for nodeName, node := range c.Nodes { | ||
n, err := graph.CreateNode(nodeName) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
n.SetLabel(nodeName) | ||
n.SetXLabel(node.Kind) | ||
n.SetGroup(node.Group) | ||
attr["label"] = nodeName | ||
attr["xlabel"] = node.Kind | ||
attr["group"] = node.Group | ||
|
||
if strings.Contains(node.Kind, "srl") { | ||
n.SetFillColor("green") | ||
attr["fillcolor"] = "green" | ||
} | ||
if strings.Contains(node.Group, "bb") { | ||
n.SetFillColor("blue") | ||
n.SetFontColor("white") | ||
attr["fillcolor"] = "blue" | ||
} | ||
if err := g.AddNode(c.FileInfo.shortname, node.ShortName, attr); err != nil { | ||
return err | ||
} | ||
|
||
graphNodeMap[nodeName] = n | ||
} | ||
|
||
// Create Edges between the Nodes | ||
graph.Attr(2, "fillcolor", "green") // kind 2 == edge defaults | ||
graph.Attr(2, "arrowhead", "none") // kind 2 == edge defaults | ||
graph.Attr(2, "arrowtail", "none") // kind 2 == edge defaults | ||
attr = make(map[string]string) | ||
attr["color"] = "green" | ||
|
||
for _, link := range c.Links { | ||
aEnd, ok := graphNodeMap[link.A.Node.ShortName] | ||
if !ok { | ||
log.Fatal("Node ", link.A.EndpointName, " does not exist!") | ||
} | ||
bEnd, ok := graphNodeMap[link.B.Node.ShortName] | ||
if !ok { | ||
log.Fatal("Node ", link.B.EndpointName, " does not exist!") | ||
if strings.Contains(link.B.Node.ShortName, "client") { | ||
attr["color"] = "blue" | ||
} | ||
e, err := graph.CreateEdge("", aEnd, bEnd) | ||
if err != nil { | ||
log.Fatal(err) | ||
if err := g.AddEdge(link.A.Node.ShortName, link.B.Node.ShortName, false, attr); err != nil { | ||
return err | ||
} | ||
_ = e | ||
// Print node and interface name in edge label | ||
//e.SetLabel(link.A.Node.ShortName + " " + link.A.EndpointName + "\n" + link.B.Node.ShortName + " " + link.B.EndpointName) | ||
|
||
} | ||
|
||
// create graph directory | ||
CreateDirectory(c.Dir.Lab, 0755) | ||
CreateDirectory(c.Dir.LabGraph, 0755) | ||
|
||
// write .dot file | ||
var buf bytes.Buffer | ||
if err := h.Render(graph, "dot", &buf); err != nil { | ||
log.Fatal(err) | ||
} | ||
//fmt.Println(buf.String()) | ||
dotFile := c.Dir.LabGraph + "/" + c.FileInfo.name + ".dot" | ||
createFile(dotFile, buf.String()) | ||
// create graph filename | ||
file := c.Dir.LabGraph + "/" + c.FileInfo.name + ".dot" | ||
|
||
createFile(file, g.String()) | ||
|
||
pngFile := c.Dir.LabGraph + "/" + c.FileInfo.name + ".png" | ||
if err := h.RenderFilename(graph, graphviz.PNG, pngFile); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Info("Done") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters