cmp.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ovn
  15. import (
  16. "yunion.io/x/ovsdb/schema/ovn_nb"
  17. "yunion.io/x/ovsdb/types"
  18. "yunion.io/x/onecloud/pkg/vpcagent/ovnutil"
  19. )
  20. // cmp scans the database for irows. For those present, mark them with ocver.
  21. // If all rows are found, return true to indicate this. Otherwise return as
  22. // 2nd value the args to destroy these found records
  23. func cmp(db *ovn_nb.OVNNorthbound, ocver string, irows ...types.IRow) (bool, []string) {
  24. irowsFound := make([]types.IRow, 0, len(irows))
  25. irowsDiff := make([]types.IRow, 0)
  26. for _, irow := range irows {
  27. irowFound := db.FindOneMatchNonZeros(irow)
  28. if irowFound != nil {
  29. irowsFound = append(irowsFound, irowFound)
  30. } else {
  31. if irowDiff := db.FindOneMatchByAnyIndex(irow); irowDiff != nil {
  32. irowsDiff = append(irowsDiff, irowDiff)
  33. }
  34. }
  35. }
  36. // mark them anyway even if not all found, to avoid the destroy
  37. // call at sweep stage
  38. for _, irowFound := range irowsFound {
  39. irowFound.SetExternalId(externalKeyOcVersion, ocver)
  40. }
  41. if len(irowsFound) == len(irows) {
  42. return true, nil
  43. }
  44. irowsCleanup := append(irowsFound, irowsDiff...)
  45. args := ovnutil.OvnNbctlArgsDestroy(irowsCleanup)
  46. return false, args
  47. }