forked from davrodpin/ovsdbviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (125 loc) · 3.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"github.com/halfcrazy/ovsdbviz/graphviz"
"github.com/halfcrazy/ovsdbviz/ovsdb"
"github.com/jessevdk/go-flags"
"os"
"strings"
)
const (
tableAttrRow = `<tr><td port="f%d" border="1" bgcolor="%s">%s</td></tr>`
tableBackgroundColor = "turquoise1"
)
func CreateLabel(table ovsdb.TableSchema, columns []string) string {
var labels []string
for index, columnName := range columns {
tableBgColor := "transparent"
label := columnName
if index == 0 {
tableBgColor = tableBackgroundColor
if table.IsRoot {
label = fmt.Sprintf("%s (root)", label)
}
}
if table.IsIndex(columnName) {
label = fmt.Sprintf("%s (index)", label)
}
labels = append(labels, fmt.Sprintf(tableAttrRow, index, tableBgColor, label))
}
return strings.Join(labels, "")
}
func GetPortIndex(columns []string, column string) int {
portIndex := 0 // pointing to the table name by default
for i, columnName := range columns {
if columnName == column {
portIndex = i
break
}
}
return portIndex
}
type RPCOptions struct {
DBName string `long:"db" description:"ovs db name"`
Address string `long:"address" description:"ovs db server address, eg 192.168.1.1:6641"`
}
type LocalOptions struct {
SchemaPath string `long:"schema" description:"ovs schema file path"`
}
type CliOptions struct {
Out string `long:"out" description:"dot output file path" required:"true"`
Local LocalOptions `group:"local"`
RPC RPCOptions `group:"rpc"`
}
var cliOptions CliOptions
var parser = flags.NewParser(&cliOptions, flags.Default)
func init() {
if _, err := parser.Parse(); err != nil {
fmt.Println(parser.Usage)
os.Exit(1)
}
if cliOptions.Local.SchemaPath != "" && (cliOptions.RPC.DBName != "" || cliOptions.RPC.Address != "") {
fmt.Println("cannot specify local and rpc in the same time")
os.Exit(1)
}
if cliOptions.Local.SchemaPath == "" && (cliOptions.RPC.DBName == "" || cliOptions.RPC.Address == "") {
fmt.Println("you must specify local or rpc")
os.Exit(1)
}
}
func main() {
schema, err := ovsdb.NewDatabaseSchema(ovsdb.SchemaOption{
Address: cliOptions.RPC.Address,
DB: cliOptions.RPC.DBName,
SchemaPath: cliOptions.Local.SchemaPath,
})
if err != nil {
panic(err)
}
// Need to always iterate all column for a given table following the same order
// in order to build and reference graphviz node ports
tableColumnOrder := schema.OrderedColumns()
graph := graphviz.NewGraph()
// NODES
for tableName, columnOrder := range tableColumnOrder {
label := CreateLabel(schema.Tables[tableName], columnOrder)
nodeAttrs := make(map[string]string)
nodeAttrs["shape"] = "none"
nodeAttrs["label"] = fmt.Sprintf(`<<table border="0" cellspacing="0">%s</table>>`, label)
graph.AddNode(tableName, nodeAttrs)
}
// EDGES
for tableName, table := range schema.Tables {
for cn, column := range table.Columns {
references := column.RefersTo()
if len(references) > 0 {
portIndex := GetPortIndex(tableColumnOrder[tableName], cn)
for refAttribute, reference := range references {
src := tableName
srcPort := fmt.Sprintf("f%d", portIndex)
dst := reference
dstPort := "f0"
edgeAttrs := make(map[string]string)
edgeAttrs["label"] = refAttribute
edgeAttrs["splines"] = "polyline"
switch refAttribute {
case "key":
edgeAttrs["color"] = "red"
case "value":
edgeAttrs["color"] = "blue"
}
graph.AddEdge(src, srcPort, dst, dstPort, edgeAttrs)
}
}
}
}
output, err := os.Create(cliOptions.Out)
if err != nil {
panic(err)
}
defer output.Close()
_, err = output.WriteString(graph.String())
if err != nil {
panic(fmt.Sprintf("Error while writing output to %s: %v", cliOptions.Out, err))
}
}