go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/ifplugin/mockcalls/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 "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin/model" 21 ) 22 23 // SimulateFailedTapCreation allows to simulate failure of the next Create 24 // operation for a TAP interface. 25 var SimulateFailedTapCreation bool 26 27 // CreateLoopbackInterface creates loopback in the mock SB. 28 func (h *MockIfaceHandler) CreateLoopbackInterface(ifaceName string) (sbIfaceHandle uint32, err error) { 29 sbIfaceHandle = h.nextIfaceHandle 30 h.nextIfaceHandle++ 31 h.mockIfaces[sbIfaceHandle] = &mock_interfaces.Interface{ 32 Name: ifaceName, 33 Type: mock_interfaces.Interface_LOOPBACK, 34 } 35 h.log.Debugf("Created Loopback interface: %s", ifaceName) 36 return sbIfaceHandle, nil 37 } 38 39 // DeleteLoopbackInterface deletes loopback in the mock SB. 40 func (h *MockIfaceHandler) DeleteLoopbackInterface(sbIfaceHandle uint32) error { 41 iface, err := h.getInterface(sbIfaceHandle) 42 if err != nil { 43 return err 44 } 45 delete(h.mockIfaces, sbIfaceHandle) 46 h.log.Debugf("Deleted Loopback interface: %s", iface.Name) 47 return nil 48 } 49 50 // CreateTapInterface creates TAP interface in the mock SB. 51 func (h *MockIfaceHandler) CreateTapInterface(ifaceName string) (sbIfaceHandle uint32, err error) { 52 if SimulateFailedTapCreation { 53 SimulateFailedTapCreation = false // next attempt will succeed 54 return 0, fmt.Errorf("mock error") 55 } 56 sbIfaceHandle = h.nextIfaceHandle 57 h.nextIfaceHandle++ 58 h.mockIfaces[sbIfaceHandle] = &mock_interfaces.Interface{ 59 Name: ifaceName, 60 Type: mock_interfaces.Interface_TAP, 61 } 62 h.log.Debugf("Created TAP interface: %s", ifaceName) 63 return sbIfaceHandle, nil 64 } 65 66 // CreateTapInterface deletes TAP interface in the mock SB. 67 func (h *MockIfaceHandler) DeleteTapInterface(sbIfaceHandle uint32) error { 68 iface, err := h.getInterface(sbIfaceHandle) 69 if err != nil { 70 return err 71 } 72 delete(h.mockIfaces, sbIfaceHandle) 73 h.log.Debugf("Deleted TAP interface: %s", iface.Name) 74 return nil 75 } 76 77 // InterfaceAdminDown puts the given interface DOWN. 78 func (h *MockIfaceHandler) InterfaceAdminDown(sbIfaceHandle uint32) error { 79 iface, err := h.getInterface(sbIfaceHandle) 80 if err != nil { 81 return err 82 } 83 iface.Enabled = false 84 h.log.Debugf("Set interface '%s' DOWN", iface.Name) 85 return nil 86 } 87 88 // InterfaceAdminUp puts the given interface UP. 89 func (h *MockIfaceHandler) InterfaceAdminUp(sbIfaceHandle uint32) error { 90 iface, err := h.getInterface(sbIfaceHandle) 91 if err != nil { 92 return err 93 } 94 iface.Enabled = true 95 h.log.Debugf("Set interface '%s' UP", iface.Name) 96 return nil 97 } 98 99 // SetInterfaceMac changes MAC address of the given interface. 100 func (h *MockIfaceHandler) SetInterfaceMac(sbIfaceHandle uint32, macAddress string) error { 101 iface, err := h.getInterface(sbIfaceHandle) 102 if err != nil { 103 return err 104 } 105 iface.PhysAddress = macAddress 106 h.log.Debugf("Set interface '%s' MAC address: %s", iface.Name, macAddress) 107 return nil 108 } 109 110 // DumpInterfaces returns interfaces "configured" in the mock SB. 111 func (h *MockIfaceHandler) DumpInterfaces() (mockIfaces, error) { 112 h.log.Debugf("Dumped mock interfaces: %+v", h.mockIfaces) 113 return h.mockIfaces, nil 114 } 115 116 // getInterface returns configuration of interface represented in the mock SB 117 // with the given integer handle. 118 func (h *MockIfaceHandler) getInterface(sbIfaceHandle uint32) (*mock_interfaces.Interface, error) { 119 iface, exists := h.mockIfaces[sbIfaceHandle] 120 if !exists { 121 return nil, fmt.Errorf("cannot find interface with index: %d", sbIfaceHandle) 122 } 123 return iface, nil 124 }