forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_restapi.go
359 lines (332 loc) · 11.8 KB
/
plugin_restapi.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// 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.
package restapi
import (
"fmt"
"net/http"
"sync"
"go.ligato.io/cn-infra/v2/infra"
"go.ligato.io/cn-infra/v2/rpc/rest"
access "go.ligato.io/cn-infra/v2/rpc/rest/security/model/access-security"
"go.ligato.io/cn-infra/v2/servicelabel"
"go.ligato.io/vpp-agent/v3/plugins/govppmux"
vpevppcalls "go.ligato.io/vpp-agent/v3/plugins/govppmux/vppcalls"
kvscheduler "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
linuxifplugin "go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin"
iflinuxcalls "go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/linuxcalls"
l3linuxcalls "go.ligato.io/vpp-agent/v3/plugins/linux/l3plugin/linuxcalls"
"go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin"
"go.ligato.io/vpp-agent/v3/plugins/netalloc"
"go.ligato.io/vpp-agent/v3/plugins/orchestrator"
"go.ligato.io/vpp-agent/v3/plugins/restapi/resturl"
telemetryvppcalls "go.ligato.io/vpp-agent/v3/plugins/telemetry/vppcalls"
abfvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/abfplugin/vppcalls"
"go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin"
aclvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls"
"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin"
ifvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
ipsecvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ipsecplugin/vppcalls"
"go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin"
l2vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls"
"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin"
l3vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
natvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/vppcalls"
puntvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/puntplugin/vppcalls"
wireguardvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/wireguardplugin/vppcalls"
)
// REST api methods
const (
GET = http.MethodGet
POST = http.MethodPost
PUT = http.MethodPut
)
// Default Go routine count used to retrieve linux configuration
const defaultGoRoutineCount = 10
// Plugin registers Rest Plugin
type Plugin struct {
Deps
// Index page
index *index
// Handlers
vpeHandler vpevppcalls.VppCoreAPI
teleHandler telemetryvppcalls.TelemetryVppAPI
// VPP Handlers
abfHandler abfvppcalls.ABFVppRead
aclHandler aclvppcalls.ACLVppRead
ifHandler ifvppcalls.InterfaceVppRead
natHandler natvppcalls.NatVppRead
l2Handler l2vppcalls.L2VppAPI
l3Handler l3vppcalls.L3VppAPI
ipSecHandler ipsecvppcalls.IPSecVPPRead
puntHandler puntvppcalls.PuntVPPRead
wireguardHandler wireguardvppcalls.WgVppRead
// Linux handlers
linuxIfHandler iflinuxcalls.NetlinkAPIRead
linuxL3Handler l3linuxcalls.NetlinkAPIRead
govppmux sync.Mutex
}
// Deps represents dependencies of Rest Plugin
type Deps struct {
infra.PluginDeps
HTTPHandlers rest.HTTPHandlers
VPP govppmux.API
ServiceLabel servicelabel.ReaderAPI
AddrAlloc netalloc.AddressAllocator
VPPACLPlugin aclplugin.API
VPPIfPlugin ifplugin.API
VPPL2Plugin *l2plugin.L2Plugin
VPPL3Plugin *l3plugin.L3Plugin
LinuxIfPlugin linuxifplugin.API
NsPlugin nsplugin.API
Dispatcher orchestrator.Dispatcher
KVScheduler kvscheduler.KVScheduler
}
// index defines map of main index page entries
type index struct {
ItemMap map[string][]indexItem
}
// indexItem is single index page entry
type indexItem struct {
Name string
Path string
}
// Init initializes the Rest Plugin
func (p *Plugin) Init() (err error) {
// VPP Indexes
ifIndexes := p.VPPIfPlugin.GetInterfaceIndex()
bdIndexes := p.VPPL2Plugin.GetBDIndex()
dhcpIndexes := p.VPPIfPlugin.GetDHCPIndex()
aclIndexes := p.VPPACLPlugin.GetACLIndex() // TODO: make ACL optional
vrfIndexes := p.VPPL3Plugin.GetVRFIndex()
// Linux Indexes
linuxIfIndexes := p.LinuxIfPlugin.GetInterfaceIndex()
// Initialize VPP handlers
p.vpeHandler, err = vpevppcalls.NewHandler(p.VPP)
if err != nil {
return fmt.Errorf("VPP core handler error: %w", err)
} else if p.vpeHandler == nil {
p.Log.Info("VPP core handler is not available, it will be skipped")
}
p.teleHandler = telemetryvppcalls.CompatibleTelemetryHandler(p.VPP)
if p.teleHandler == nil {
p.Log.Info("VPP Telemetry handler is not available, it will be skipped")
}
// core
p.ifHandler = ifvppcalls.CompatibleInterfaceVppHandler(p.VPP, p.Log)
if p.ifHandler == nil {
p.Log.Info("VPP Interface handler is not available, it will be skipped")
}
p.l2Handler = l2vppcalls.CompatibleL2VppHandler(p.VPP, ifIndexes, bdIndexes, p.Log)
if p.l2Handler == nil {
p.Log.Info("VPP L2 handler is not available, it will be skipped")
}
p.l3Handler = l3vppcalls.CompatibleL3VppHandler(p.VPP, ifIndexes, vrfIndexes, p.AddrAlloc, p.Log)
if p.l3Handler == nil {
p.Log.Info("VPP L3 handler is not available, it will be skipped")
}
p.ipSecHandler = ipsecvppcalls.CompatibleIPSecVppHandler(p.VPP, ifIndexes, p.Log)
if p.ipSecHandler == nil {
p.Log.Info("VPP IPSec handler is not available, it will be skipped")
}
// plugins (might not be available - disabled)
p.abfHandler = abfvppcalls.CompatibleABFHandler(p.VPP, aclIndexes, ifIndexes, p.Log)
if p.abfHandler == nil {
p.Log.Infof("ABF handler is not available, it will be skipped")
}
p.aclHandler = aclvppcalls.CompatibleACLHandler(p.VPP, ifIndexes)
if p.aclHandler == nil {
p.Log.Infof("ACL handler is not available, it will be skipped")
}
p.natHandler = natvppcalls.CompatibleNatVppHandler(p.VPP, ifIndexes, dhcpIndexes, p.Log)
if p.natHandler == nil {
p.Log.Infof("NAT handler is not available, it will be skipped")
}
p.puntHandler = puntvppcalls.CompatiblePuntVppHandler(p.VPP, ifIndexes, p.Log)
if p.puntHandler == nil {
p.Log.Infof("Punt handler is not available, it will be skipped")
}
p.wireguardHandler = wireguardvppcalls.CompatibleWgVppHandler(p.VPP, ifIndexes, p.Log)
if p.wireguardHandler == nil {
p.Log.Info("Wireguard handler is not available, it will be skipped")
}
// Linux handlers
p.linuxIfHandler = iflinuxcalls.NewNetLinkHandler(p.NsPlugin, linuxIfIndexes, p.ServiceLabel.GetAgentPrefix(),
defaultGoRoutineCount, p.Log)
p.linuxL3Handler = l3linuxcalls.NewNetLinkHandler(p.NsPlugin, linuxIfIndexes, defaultGoRoutineCount, p.Log)
p.index = &index{
ItemMap: getIndexPageItems(),
}
// Register permission groups, used if REST security is enabled
p.HTTPHandlers.RegisterPermissionGroup(getPermissionsGroups()...)
return nil
}
// AfterInit is used to register HTTP handlers
func (p *Plugin) AfterInit() (err error) {
// Info handlers.
p.registerInfoHandlers()
// NB configuration handlers.
p.registerNBConfigurationHandlers()
// VPP handlers
p.registerTelemetryHandlers()
// core
p.registerInterfaceHandlers()
p.registerL2Handlers()
p.registerL3Handlers()
p.registerIPSecHandlers()
// plugins
p.registerABFHandler()
p.registerACLHandlers()
p.registerNATHandlers()
p.registerPuntHandlers()
// Linux handlers
p.registerLinuxInterfaceHandlers()
p.registerLinuxL3Handlers()
// Index and stats handlers
p.registerIndexHandlers()
p.registerStatsHandler()
return nil
}
// Close is used to clean up resources used by Plugin
func (p *Plugin) Close() error {
return nil
}
// Fill index item lists
func getIndexPageItems() map[string][]indexItem {
idxMap := map[string][]indexItem{
"Info": {
{Name: "Version", Path: resturl.Version},
{Name: "JSONSchema", Path: resturl.JSONSchema},
},
"NB configuration": {
{Name: "Get or Put NB configuration", Path: resturl.Configuration},
{Name: "Validation", Path: resturl.Validate},
},
"ACL plugin": {
{Name: "IP-type access lists", Path: resturl.ACLIP},
{Name: "MACIP-type access lists", Path: resturl.ACLMACIP},
},
"Interface plugin": {
{Name: "All interfaces", Path: resturl.Interface},
{Name: "Loopbacks", Path: resturl.Loopback},
{Name: "Ethernets", Path: resturl.Ethernet},
{Name: "Memifs", Path: resturl.Memif},
{Name: "Taps", Path: resturl.Tap},
{Name: "VxLANs", Path: resturl.VxLan},
{Name: "Af-packets", Path: resturl.AfPacket},
},
"L2 plugin": {
{Name: "Bridge domains", Path: resturl.Bd},
{Name: "L2Fibs", Path: resturl.Fib},
{Name: "Cross connects", Path: resturl.Xc},
},
"L3 plugin": {
{Name: "Routes", Path: resturl.Routes},
{Name: "ARPs", Path: resturl.Arps},
{Name: "Proxy ARP interfaces", Path: resturl.PArpIfs},
{Name: "Proxy ARP ranges", Path: resturl.PArpRngs},
},
"Telemetry": {
{Name: "All data", Path: resturl.Telemetry},
{Name: "Memory", Path: resturl.TMemory},
{Name: "Runtime", Path: resturl.TRuntime},
{Name: "Node count", Path: resturl.TNodeCount},
},
"Stats": {
{Name: "Configurator Stats", Path: resturl.ConfiguratorStats},
},
}
return idxMap
}
// Create permission groups (tracer, telemetry, dump - optionally add more in the future). Used only if
// REST security is enabled in plugin
func getPermissionsGroups() []*access.PermissionGroup {
infoPg := &access.PermissionGroup{
Name: "info",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", GET),
newPermission(resturl.Version, GET),
newPermission(resturl.JSONSchema, GET),
},
}
nbConfigValidationPg := &access.PermissionGroup{
Name: "nbConfigValidation",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", GET),
newPermission(resturl.Validate, GET),
},
}
nbConfigReadPg := &access.PermissionGroup{
Name: "nbConfigRead",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", GET),
newPermission(resturl.Configuration, GET),
},
}
nbConfigWritePg := &access.PermissionGroup{
Name: "nbConfigWrite",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", PUT),
newPermission(resturl.Configuration, PUT),
},
}
tracerPg := &access.PermissionGroup{
Name: "stats",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", GET),
newPermission(resturl.ConfiguratorStats, GET),
},
}
telemetryPg := &access.PermissionGroup{
Name: "telemetry",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", GET),
newPermission(resturl.Telemetry, GET),
newPermission(resturl.TMemory, GET),
newPermission(resturl.TRuntime, GET),
newPermission(resturl.TNodeCount, GET),
},
}
dumpPg := &access.PermissionGroup{
Name: "dump",
Permissions: []*access.PermissionGroup_Permissions{
newPermission("/", GET),
newPermission(resturl.ABF, GET),
newPermission(resturl.ACLIP, GET),
newPermission(resturl.ACLMACIP, GET),
newPermission(resturl.Interface, GET),
newPermission(resturl.Loopback, GET),
newPermission(resturl.Ethernet, GET),
newPermission(resturl.Memif, GET),
newPermission(resturl.Tap, GET),
newPermission(resturl.VxLan, GET),
newPermission(resturl.AfPacket, GET),
newPermission(resturl.Bd, GET),
newPermission(resturl.Fib, GET),
newPermission(resturl.Xc, GET),
newPermission(resturl.Arps, GET),
newPermission(resturl.Routes, GET),
newPermission(resturl.PArpIfs, GET),
newPermission(resturl.PArpRngs, GET),
},
}
return []*access.PermissionGroup{infoPg, tracerPg, telemetryPg, dumpPg,
nbConfigValidationPg, nbConfigReadPg, nbConfigWritePg}
}
// Returns permission object with url and provided methods
func newPermission(url string, methods ...string) *access.PermissionGroup_Permissions {
return &access.PermissionGroup_Permissions{
Url: url,
AllowedMethods: methods,
}
}