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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
package test
import (
"encoding/json"
"fmt"
"github.com/ovn-org/libovsdb/model"
"github.com/ovn-org/libovsdb/ovsdb"
)
// Note that this schema is not strictly a subset of the real OVS schema. It has
// some small variations that allow to effectively test some OVSDB RFC features
const schema = `
{
"name": "Open_vSwitch",
"version": "0.0.1",
"tables": {
"Open_vSwitch": {
"columns": {
"manager_options": {
"type": {
"key": {
"type": "uuid",
"refTable": "Manager"
},
"min": 0,
"max": "unlimited"
}
},
"bridges": {
"type": {
"key": {
"type": "uuid"
},
"min": 0,
"max": "unlimited"
}
}
},
"isRoot": true,
"maxRows": 1
},
"Bridge": {
"columns": {
"name": {
"type": "string",
"mutable": false
},
"datapath_type": {
"type": "string"
},
"datapath_id": {
"type": {
"key": "string",
"min": 0,
"max": 1
},
"ephemeral": true
},
"ports": {
"type": {
"key": {
"type": "uuid"
},
"min": 0,
"max": "unlimited"
}
},
"mirrors": {
"type": {
"key": {
"type": "uuid",
"refTable": "Mirror"
},
"min": 0,
"max": "unlimited"
}
},
"status": {
"type": {
"key": "string",
"value": "string",
"min": 0,
"max": "unlimited"
},
"ephemeral": true
},
"other_config": {
"type": {
"key": "string",
"value": "string",
"min": 0,
"max": "unlimited"
}
},
"external_ids": {
"type": {
"key": "string",
"value": "string",
"min": 0,
"max": "unlimited"
}
}
},
"isRoot": true,
"indexes": [
[
"name"
]
]
},
"Flow_Sample_Collector_Set": {
"columns": {
"id": {
"type": {
"key": {
"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295
},
"min": 1,
"max": 1
}
},
"bridge": {
"type": {
"key": {
"type": "uuid"
},
"min": 1,
"max": 1
}
},
"external_ids": {
"type": {
"key": "string",
"value": "string",
"min": 0,
"max": "unlimited"
}
}
},
"isRoot": true,
"indexes": [
[
"id",
"bridge"
]
]
},
"Manager": {
"columns": {
"target": {
"type": "string"
}
},
"indexes": [["target"]]
},
"Mirror": {
"columns": {
"name": {
"type": "string"
},
"select_src_port": {
"type": {
"key": {
"type": "uuid",
"refTable": "Port",
"refType": "weak"
},
"min": 1,
"max": "unlimited"
}
}
}
},
"Port": {
"columns": {
"name": {
"type": "string",
"mutable": false
}
},
"isRoot": true,
"indexes": [["name"]]
}
}
}
`
// BridgeType is the simplified ORM model of the Bridge table
type BridgeType struct {
UUID string `ovsdb:"_uuid"`
Name string `ovsdb:"name"`
DatapathType string `ovsdb:"datapath_type"`
DatapathID *string `ovsdb:"datapath_id"`
OtherConfig map[string]string `ovsdb:"other_config"`
ExternalIds map[string]string `ovsdb:"external_ids"`
Ports []string `ovsdb:"ports"`
Status map[string]string `ovsdb:"status"`
Mirrors []string `ovsdb:"mirrors"`
}
// OvsType is the simplified ORM model of the Bridge table
type OvsType struct {
UUID string `ovsdb:"_uuid"`
Bridges []string `ovsdb:"bridges"`
ManagerOptions []string `ovsdb:"manager_options"`
}
type FlowSampleCollectorSetType struct {
UUID string `ovsdb:"_uuid"`
Bridge string `ovsdb:"bridge"`
ExternalIDs map[string]string `ovsdb:"external_ids"`
ID int `ovsdb:"id"`
IPFIX *string // `ovsdb:"ipfix"`
}
type ManagerType struct {
UUID string `ovsdb:"_uuid"`
Target string `ovsdb:"target"`
}
type PortType struct {
UUID string `ovsdb:"_uuid"`
Name string `ovsdb:"name"`
}
type MirrorType struct {
UUID string `ovsdb:"_uuid"`
Name string `ovsdb:"name"`
SelectSrcPort []string `ovsdb:"select_src_port"`
}
func GetModel() (model.DatabaseModel, error) {
client, err := model.NewClientDBModel(
"Open_vSwitch",
map[string]model.Model{
"Open_vSwitch": &OvsType{},
"Bridge": &BridgeType{},
"Flow_Sample_Collector_Set": &FlowSampleCollectorSetType{},
"Manager": &ManagerType{},
"Mirror": &MirrorType{},
"Port": &PortType{},
},
)
if err != nil {
return model.DatabaseModel{}, err
}
schema, err := GetSchema()
if err != nil {
return model.DatabaseModel{}, err
}
dbModel, errs := model.NewDatabaseModel(schema, client)
if len(errs) > 0 {
return model.DatabaseModel{}, fmt.Errorf("errors build model: %v", errs)
}
return dbModel, nil
}
func GetSchema() (ovsdb.DatabaseSchema, error) {
var dbSchema ovsdb.DatabaseSchema
err := json.Unmarshal([]byte(schema), &dbSchema)
return dbSchema, err
}
|