go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/l2plugin/mockcalls/bd_mockcalls.go (about) 1 // Copyright (c) 2017 Cisco and/or its affiliates. 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 15 package mockcalls 16 17 import ( 18 "fmt" 19 20 l2 "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/model" 21 ) 22 23 // CreateBridgeDomain creates new bridge domain in the mock SB. 24 func (h *MockBDHandler) CreateBridgeDomain(bdName string) (sbBDHandle uint32, err error) { 25 sbBDHandle = h.nextBDHandle 26 h.nextBDHandle++ 27 h.mockBDs[sbBDHandle] = &l2.BridgeDomain{ 28 Name: bdName, 29 } 30 h.log.Debugf("Created bridge domain: %s", bdName) 31 return sbBDHandle, nil 32 } 33 34 // DeleteBridgeDomain removes existing bridge domain from the mock SB. 35 func (h *MockBDHandler) DeleteBridgeDomain(sbBDHandle uint32) error { 36 bd, err := h.getBridgeDomain(sbBDHandle) 37 if err != nil { 38 return err 39 } 40 delete(h.mockBDs, sbBDHandle) 41 h.log.Debugf("Deleted bridge domain: %s", bd.Name) 42 return nil 43 } 44 45 // AddInterfaceToBridgeDomain puts interface into bridge domain. 46 func (h *MockBDHandler) AddInterfaceToBridgeDomain(sbBDHandle uint32, ifaceName string, isBVI bool) error { 47 bd, err := h.getBridgeDomain(sbBDHandle) 48 if err != nil { 49 return err 50 } 51 for _, iface := range bd.Interfaces { 52 if iface.Name == ifaceName { 53 return fmt.Errorf("interface '%s' is already in the bridge domain '%s'", 54 ifaceName, bd.Name) 55 } 56 } 57 bd.Interfaces = append(bd.Interfaces, &l2.BridgeDomain_Interface{ 58 Name: ifaceName, 59 BridgedVirtualInterface: isBVI, 60 }) 61 h.log.Debugf("Added interface '%s' into the bridge domain '%s'", 62 ifaceName, bd.Name) 63 return nil 64 } 65 66 // DeleteInterfaceFromBridgeDomain removes interface from bridge domain. 67 func (h *MockBDHandler) DeleteInterfaceFromBridgeDomain(sbBDHandle uint32, ifaceName string) error { 68 bd, err := h.getBridgeDomain(sbBDHandle) 69 if err != nil { 70 return err 71 } 72 var idx int 73 for ; idx < len(bd.Interfaces); idx++ { 74 if bd.Interfaces[idx].Name == ifaceName { 75 break 76 } 77 } 78 if idx == len(bd.Interfaces) { 79 return fmt.Errorf("interface '%s' is not in the bridge domain '%s'", 80 ifaceName, bd.Name) 81 } 82 bd.Interfaces = append(bd.Interfaces[:idx], bd.Interfaces[idx+1:]...) 83 h.log.Debugf("Removed interface '%s' from the bridge domain '%s'", 84 ifaceName, bd.Name) 85 return nil 86 } 87 88 // DumpBridgeDomains dumps bridge domains "configured" in the mock SB. 89 func (h *MockBDHandler) DumpBridgeDomains() (mockBDs, error) { 90 h.log.Debugf("Dumped mock bridge domains: %+v", h.mockBDs) 91 return h.mockBDs, nil 92 } 93 94 // getInterface returns configuration of bridge domain represented in the mock SB 95 // with the given integer handle. 96 func (h *MockBDHandler) getBridgeDomain(sbBDHandle uint32) (*l2.BridgeDomain, error) { 97 bd, exists := h.mockBDs[sbBDHandle] 98 if !exists { 99 return nil, fmt.Errorf("cannot find bridge domain with index: %d", sbBDHandle) 100 } 101 return bd, nil 102 }