go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/ifplugin/ifplugin.go (about) 1 // Copyright (c) 2018 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 // Generate golang code from the protobuf model of our mock interfaces: 16 //go:generate protoc --proto_path=. --go_out=paths=source_relative:. model/interface.proto 17 18 // Generate adapter for the descriptor of our mock interfaces: 19 //go:generate descriptor-adapter --descriptor-name Interface --value-type *mock_interfaces.Interface --meta-type *idxvpp.OnlyIndex --import "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin/model" --import "go.ligato.io/vpp-agent/v3/pkg/idxvpp" --output-dir "descriptor" 20 21 package ifplugin 22 23 import ( 24 "github.com/pkg/errors" 25 "go.ligato.io/cn-infra/v2/infra" 26 27 "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin/descriptor" 28 "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin/mockcalls" 29 "go.ligato.io/vpp-agent/v3/pkg/idxvpp" 30 kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 31 ) 32 33 // IfPlugin configures mock interfaces. 34 type IfPlugin struct { 35 Deps 36 37 // access to our mock SB 38 // - it is recommended to put implementation of every SB call needed for 39 // your descriptor into a separate package `<southband-name>calls/` and 40 // expose them via interface. This will allow to replace access to SB with 41 // mocks and make unit testing easier (in our example these calls are 42 // already mocks) 43 ifaceHandler mockcalls.MockIfaceAPI 44 45 // descriptor for interfaces 46 ifaceDescriptor *descriptor.InterfaceDescriptor 47 48 // metadata index map (exposed read-only for other plugins) 49 intfIndex idxvpp.NameToIndex 50 } 51 52 // Deps lists dependencies of the mock interface plugin. 53 type Deps struct { 54 infra.PluginDeps 55 56 // the plugin depends on KVScheduler because it needs to register the 57 // descriptor for interfaces. 58 KVScheduler kvs.KVScheduler 59 } 60 61 // Init of a real (not-mock) plugin usually: 62 // - loads configuration from a file (if any) 63 // - registers descriptors for all objects the plugin implements 64 // - potentially starts go routine to watch for some asynchronous events 65 // (from which usually sends notifications to KVScheduler via PushSBNotification) 66 // - etc. 67 // 68 // In this mock ifplugin, we only only create mock SB handlers and register the descriptor. 69 func (p *IfPlugin) Init() error { 70 var err error 71 72 // init handler 73 p.ifaceHandler = mockcalls.NewMockIfaceHandler(p.Log) 74 75 // init & register descriptors 76 ifaceDescriptor := descriptor.NewInterfaceDescriptor(p.ifaceHandler, p.Log) 77 err = p.KVScheduler.RegisterKVDescriptor(ifaceDescriptor) 78 if err != nil { 79 return err 80 } 81 82 // obtain read-only reference to index map with interface metadata 83 var withIndex bool 84 metadataMap := p.KVScheduler.GetMetadataMap(ifaceDescriptor.Name) 85 p.intfIndex, withIndex = metadataMap.(idxvpp.NameToIndex) 86 if !withIndex { 87 return errors.New("missing index with interface metadata") 88 } 89 90 return nil 91 } 92 93 // Close of a real (not-mock) plugin usually: 94 // - stops all the associated go routines (if any) 95 // - closes channels, registrations, etc.. 96 // 97 // In this example we do nothing (no need to un-register descriptor). 98 func (p *IfPlugin) Close() error { 99 return nil 100 } 101 102 // GetInterfaceIndex gives read-only access to map with metadata of all configured 103 // mock interfaces. 104 func (p *IfPlugin) GetInterfaceIndex() idxvpp.NameToIndex { 105 return p.intfIndex 106 }