1- /***
1+ /*
22Copyright 2014 Cisco Systems Inc. All rights reserved.
33
44Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +18,7 @@ package ofctrl
1818
1919import (
2020 "errors"
21+ "fmt"
2122
2223 "antrea.io/libOpenflow/openflow15"
2324)
@@ -96,31 +97,39 @@ func (self *OFSwitch) DefaultTable() *Table {
9697 return self .tableDb [0 ]
9798}
9899
99- // Create a new group. return an error if it already exists
100- func (self * OFSwitch ) NewGroup (groupId uint32 , groupType GroupType ) (* Group , error ) {
101- // check if the group already exists
102- if self .groupDb [groupId ] != nil {
103- return nil , errors .New ("group already exists" )
100+ // NewGroup creates a new group; when using cache, returns an error if the group ID already exists, otherwise returns the
101+ // group object; when not using cache, returns the new group object, and the caller should ensure the groupID is not
102+ // duplicated.
103+ func (self * OFSwitch ) NewGroup (groupID uint32 , groupType GroupType , useCache bool ) (* Group , error ) {
104+ // Check if the group already exists.
105+ if useCache {
106+ if self .groupDb [groupID ] != nil {
107+ return nil , errors .New ("group already exists" )
108+ }
104109 }
105110
106- // Create a new group
107- group := newGroup (groupId , groupType , self )
108- // Save it in the DB
109- self .groupDb [groupId ] = group
111+ // Create a new group.
112+ group := newGroup (groupID , groupType , self )
113+ if useCache {
114+ // Save it in cache.
115+ self .groupDb [groupID ] = group
116+ }
110117
111118 return group , nil
112119}
113120
114- // Delete a group.
115- // Return an error if there are flows refer pointing at it
121+ // DeleteGroup deletes a group in cache.
116122func (self * OFSwitch ) DeleteGroup (groupId uint32 ) error {
117123 delete (self .groupDb , groupId )
118124 return nil
119125}
120126
121- // GetGroup Returns a group
122- func (self * OFSwitch ) GetGroup (groupId uint32 ) * Group {
123- return self .groupDb [groupId ]
127+ // GetGroup returns a group if it is cached.
128+ func (self * OFSwitch ) GetGroup (groupId uint32 ) (* Group , error ) {
129+ if _ , exists := self .groupDb [groupId ]; ! exists {
130+ return nil , fmt .Errorf ("group %d does not exist in cache" , groupId )
131+ }
132+ return self .groupDb [groupId ], nil
124133}
125134
126135// Create a new meter. return an error if it already exists
0 commit comments