/usr/share/gocode/src/github.com/socketplane/libovsdb/example/play_with_ovs.go is in golang-github-socketplane-libovsdb-dev 0.1+git20160503.9.d4b9e7a53548-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 154 155 156 157 158 159 160 161 162 163 164 165 | package main
import (
"fmt"
"os"
"reflect"
"github.com/socketplane/libovsdb"
)
// Silly game that detects creation of Bridge named "stop" and exits
// Just a demonstration of how an app can use libovsdb library to configure and manage OVS
var quit chan bool
var update chan *libovsdb.TableUpdates
var cache map[string]map[string]libovsdb.Row
func play(ovs *libovsdb.OvsdbClient) {
go processInput(ovs)
for {
select {
case currUpdate := <-update:
for table, tableUpdate := range currUpdate.Updates {
if table == "Bridge" {
for uuid, row := range tableUpdate.Rows {
newRow := row.New
if _, ok := newRow.Fields["name"]; ok {
name := newRow.Fields["name"].(string)
if name == "stop" {
fmt.Println("Bridge stop detected : ", uuid)
ovs.Disconnect()
quit <- true
}
}
}
}
}
}
}
}
func createBridge(ovs *libovsdb.OvsdbClient, bridgeName string) {
namedUUID := "gopher"
// bridge row to insert
bridge := make(map[string]interface{})
bridge["name"] = bridgeName
// simple insert operation
insertOp := libovsdb.Operation{
Op: "insert",
Table: "Bridge",
Row: bridge,
UUIDName: namedUUID,
}
// Inserting a Bridge row in Bridge table requires mutating the open_vswitch table.
mutateUUID := []libovsdb.UUID{{namedUUID}}
mutateSet, _ := libovsdb.NewOvsSet(mutateUUID)
mutation := libovsdb.NewMutation("bridges", "insert", mutateSet)
condition := libovsdb.NewCondition("_uuid", "==", libovsdb.UUID{getRootUUID()})
// simple mutate operation
mutateOp := libovsdb.Operation{
Op: "mutate",
Table: "Open_vSwitch",
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{insertOp, mutateOp}
reply, _ := ovs.Transact("Open_vSwitch", operations...)
if len(reply) < len(operations) {
fmt.Println("Number of Replies should be atleast equal to number of Operations")
}
ok := true
for i, o := range reply {
if o.Error != "" && i < len(operations) {
fmt.Println("Transaction Failed due to an error :", o.Error, " details:", o.Details, " in ", operations[i])
ok = false
} else if o.Error != "" {
fmt.Println("Transaction Failed due to an error :", o.Error)
ok = false
}
}
if ok {
fmt.Println("Bridge Addition Successful : ", reply[0].UUID.GoUUID)
}
}
func processInput(ovs *libovsdb.OvsdbClient) {
for {
fmt.Printf("\n Enter a Bridge Name : ")
var bridgeName string
fmt.Scanf("%s", &bridgeName)
createBridge(ovs, bridgeName)
}
}
func getRootUUID() string {
for uuid := range cache["Open_vSwitch"] {
return uuid
}
return ""
}
func populateCache(updates libovsdb.TableUpdates) {
for table, tableUpdate := range updates.Updates {
if _, ok := cache[table]; !ok {
cache[table] = make(map[string]libovsdb.Row)
}
for uuid, row := range tableUpdate.Rows {
empty := libovsdb.Row{}
if !reflect.DeepEqual(row.New, empty) {
cache[table][uuid] = row.New
} else {
delete(cache[table], uuid)
}
}
}
}
func main() {
quit = make(chan bool)
update = make(chan *libovsdb.TableUpdates)
cache = make(map[string]map[string]libovsdb.Row)
// By default libovsdb connects to 127.0.0.0:6400.
ovs, err := libovsdb.Connect("", 0)
// If you prefer to connect to OVS in a specific location :
// ovs, err := libovsdb.Connect("192.168.56.101", 6640)
if err != nil {
fmt.Println("Unable to Connect ", err)
os.Exit(1)
}
var notifier myNotifier
ovs.Register(notifier)
initial, _ := ovs.MonitorAll("Open_vSwitch", "")
populateCache(*initial)
fmt.Println(`Silly game of stopping this app when a Bridge with name "stop" is monitored !`)
go play(ovs)
<-quit
}
type myNotifier struct {
}
func (n myNotifier) Update(context interface{}, tableUpdates libovsdb.TableUpdates) {
populateCache(tableUpdates)
update <- &tableUpdates
}
func (n myNotifier) Locked([]interface{}) {
}
func (n myNotifier) Stolen([]interface{}) {
}
func (n myNotifier) Echo([]interface{}) {
}
func (n myNotifier) Disconnected(client *libovsdb.OvsdbClient) {
}
|