This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifplugin.go
107 lines (88 loc) · 3.72 KB
/
ifplugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Generate golang code from the protobuf model of our mock interfaces:
//go:generate protoc --proto_path=model --proto_path=$GOPATH/src --gogo_out=model model/interface.proto
// Generate adapter for the descriptor of our mock interfaces:
//go:generate descriptor-adapter --descriptor-name Interface --value-type *mock_interfaces.Interface --meta-type *idxvpp.OnlyIndex --import "model" --import "github.com/ligato/vpp-agent/pkg/idxvpp" --output-dir "descriptor"
package ifplugin
import (
"github.com/pkg/errors"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/vpp-agent/examples/kvscheduler/mock_plugins/ifplugin/descriptor"
"github.com/ligato/vpp-agent/examples/kvscheduler/mock_plugins/ifplugin/mockcalls"
"github.com/ligato/vpp-agent/pkg/idxvpp"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
)
// IfPlugin configures mock interfaces.
type IfPlugin struct {
Deps
// access to our mock SB
// - it is recommended to put implementation of every SB call needed for
// your descriptor into a separate package `<southband-name>calls/` and
// expose them via interface. This will allow to replace access to SB with
// mocks and make unit testing easier (in our example these calls are
// already mocks)
ifaceHandler mockcalls.MockIfaceAPI
// descriptor for interfaces
ifaceDescriptor *descriptor.InterfaceDescriptor
// metadata index map (exposed read-only for other plugins)
intfIndex idxvpp.NameToIndex
}
// Deps lists dependencies of the mock interface plugin.
type Deps struct {
infra.PluginDeps
// the plugin depends on KVScheduler because it needs to register the
// descriptor for interfaces.
KVScheduler kvs.KVScheduler
}
// Init of a real (not-mock) plugin usually:
// - loads configuration from a file (if any)
// - registers descriptors for all objects the plugin implements
// - potentially starts go routine to watch for some asynchronous events
// (from which usually sends notifications to KVScheduler via PushSBNotification)
// - etc.
//
// In this mock ifplugin, we only only create mock SB handlers and register the descriptor.
func (p *IfPlugin) Init() error {
var err error
// init handler
p.ifaceHandler = mockcalls.NewMockIfaceHandler(p.Log)
// init & register descriptors
ifaceDescriptor := descriptor.NewInterfaceDescriptor(p.ifaceHandler, p.Log)
err = p.KVScheduler.RegisterKVDescriptor(ifaceDescriptor)
if err != nil {
return err
}
// obtain read-only reference to index map with interface metadata
var withIndex bool
metadataMap := p.KVScheduler.GetMetadataMap(ifaceDescriptor.Name)
p.intfIndex, withIndex = metadataMap.(idxvpp.NameToIndex)
if !withIndex {
return errors.New("missing index with interface metadata")
}
return nil
}
// Close of a real (not-mock) plugin usually:
// - stops all the associated go routines (if any)
// - closes channels, registrations, etc..
//
// In this example we do nothing (no need to un-register descriptor).
func (p *IfPlugin) Close() error {
return nil
}
// GetInterfaceIndex gives read-only access to map with metadata of all configured
// mock interfaces.
func (p *IfPlugin) GetInterfaceIndex() idxvpp.NameToIndex {
return p.intfIndex
}