-
Notifications
You must be signed in to change notification settings - Fork 1
/
ovs_client.go
112 lines (88 loc) · 2.94 KB
/
ovs_client.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
/* Copyright 2019 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Contains code from source: https://github.com/socketplane/libovsdb/tree/4de3618546deba09d8875d719752db32bd4652c0
package main
import (
"fmt"
"github.com/socketplane/libovsdb"
)
const (
DefaultDatabase = "Open_vSwitch"
ControllerTable = "Controller"
)
type OpenFlowControllerTarget struct {
Address string
Protocol string
Port uint16
}
type OpenFlowController struct {
uuid string
Name string
Connected bool
Target *OpenFlowControllerTarget
}
type Client struct {
Address string
Protocol string
Port string
Connection *libovsdb.OvsdbClient
Database string
}
func NewClient(address, protocol, port, privateKeyPath, publicKeyPath, caPath string) (*Client, error) {
var err error
o := Client{Address: address, Protocol: protocol, Port: port, Database: DefaultDatabase}
o.Connection, err = libovsdb.ConnectUsingProtocolWithTLS(o.Protocol, fmt.Sprintf("%v:%v", o.Address, o.Port), privateKeyPath, publicKeyPath, caPath)
if err != nil {
log.Errorf("failed to dial: %v", err)
return nil, err
}
return &o, nil
}
func (o *Client) Get(param, table string) (map[string]interface{}, error) {
// TODO(dherkel@google.com): This needs to be more generic if anything other than Controller needs to be tested.
switch table {
case ControllerTable:
return o.GetOpenFlowControllerTarget(param)
}
return nil, fmt.Errorf("unimplemented OVS Get Type")
}
func (o *Client) GetOpenFlowControllerTarget(target string) (map[string]interface{}, error) {
condition := libovsdb.NewCondition("target", "==", target)
selectOp := libovsdb.Operation{
Op: "select",
Table: ControllerTable,
Where: []interface{}{condition},
Columns: []string{"target"},
}
operations := []libovsdb.Operation{selectOp}
reply, _ := o.Connection.Transact(o.Database, operations...)
if len(reply) < len(operations) {
log.Error("number of Replies should be at least equal to number of Operations")
}
ok := true
for i, o := range reply {
if o.Error != "" && i < len(operations) {
log.Errorf("transaction failed due to an error :", o.Error, " details:", o.Details, " in ", operations[i])
ok = false
} else if o.Error != "" {
log.Errorf("transaction failed due to an error :", o.Error)
ok = false
}
}
if ok {
if len(reply[0].Rows) > 0 {
return reply[0].Rows[0], nil
}
return map[string]interface{}{}, nil
}
return nil, fmt.Errorf("unable to set system information")
}