Skip to content

Commit

Permalink
Added port tag update
Browse files Browse the repository at this point in the history
Signed-off-by: Zhenfang Wei <kopkop@gmail.com>
  • Loading branch information
kopwei committed Mar 12, 2016
1 parent 2dc2714 commit 5f1e853
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func main() {
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("Successfully created the veth port %s\n", "vethA")

err = client.UpdatePortTagByName(brName, "vethA", 10)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("Successfully updated the veth port %s's tag value to %d\n", "vethA", 10)

ports, err := client.FindAllPortUUIDsOnBridge(brName)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions ovsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
insertOperation = "insert"
mutateOperation = "mutate"
selectOperation = "select"
updateOperation = "update"
)

// OvsClient is the interface towards outside user
Expand All @@ -42,6 +43,8 @@ type OvsClient interface {
CreateVethPort(brname, portname string, vlantag int) error
CreatePatchPort(brname, portname, peername string) error
DeletePort(brname, porname string) error
UpdatePortTagByName(brname, portname string, vlantag int) error
UpdatePortTagByUUID(portUUID string, vlantag int) error
FindAllPortUUIDsOnBridge(brname string) ([]string, error)
PortExistsOnBridge(portname, brname string) (bool, error)
RemoveInterfaceFromPort(portname, interfaceUUID string) error
Expand Down
58 changes: 58 additions & 0 deletions ovsport.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,61 @@ func (client *ovsClient) getPortUUIDByName(portname string) (string, error) {
answer := reply[0].Rows[0]["_uuid"].([]interface{})[1].(string)
return answer, nil
}

func (client *ovsClient) UpdatePortTagByName(brname, portname string, vlantag int) error {
if vlantag < 0 || vlantag > 4095 {
return fmt.Errorf("The vlan tag value is not in valid range")
}
portExist, err := client.PortExistsOnBridge(portname, brname)
if err != nil {
return err
}
if !portExist {
return fmt.Errorf("The port %s doesn't exists on bridge %s", portname, brname)
}
portUUID, err := client.getPortUUIDByName(portname)
if err != nil {
return err
}
return client.UpdatePortTagByUUID(portUUID, vlantag)
}

func (client *ovsClient) UpdatePortTagByUUID(portUUID string, vlantag int) error {
if vlantag < 0 || vlantag > 4095 {
return fmt.Errorf("The vlan tag value is not in valid range")
}
portExist, err := client.portExistsByUUID(portUUID)
if err != nil {
return err
}
if !portExist {
return fmt.Errorf("The port with UUID %s doesn't exists", portUUID)
}
updateCondition := libovsdb.NewCondition("_uuid", "==", []string{"uuid", portUUID})
// port row to update
port := make(map[string]interface{})
port["tag"] = vlantag

updateOp := libovsdb.Operation{
Op: updateOperation,
Table: portTableName,
Where: []interface{}{updateCondition},
}

// Inserting a Port row in Port table requires mutating the Bridge table
mutateUUID := []libovsdb.UUID{libovsdb.UUID{GoUuid: portUUID}}
mutateSet, _ := libovsdb.NewOvsSet(mutateUUID)
mutation := libovsdb.NewMutation("ports", updateOperation, mutateSet)
condition := libovsdb.NewCondition("_uuid", "==", []string{"uuid", portUUID})

// simple mutate operation
mutateOp := libovsdb.Operation{
Op: mutateOperation,
Table: bridgeTableName,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}

operations := []libovsdb.Operation{updateOp, mutateOp}
return client.transact(operations, "update port")
}

0 comments on commit 5f1e853

Please sign in to comment.