forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_restapi.go
266 lines (235 loc) · 8.04 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
// 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 (
"net/http"
"sync"
"git.fd.io/govpp.git/api"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/rpc/rest"
access "github.com/ligato/cn-infra/rpc/rest/security/model/access-security"
"github.com/ligato/cn-infra/utils/safeclose"
"github.com/ligato/vpp-agent/plugins/govppmux"
iflinuxcalls "github.com/ligato/vpp-agent/plugins/linux/ifplugin/linuxcalls"
l3linuxcalls "github.com/ligato/vpp-agent/plugins/linux/l3plugin/linuxcalls"
"github.com/ligato/vpp-agent/plugins/restapi/resturl"
aclvppcalls "github.com/ligato/vpp-agent/plugins/vpp/aclplugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/vpp/ifplugin"
ifvppcalls "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/vppcalls"
"github.com/ligato/vpp-agent/plugins/vpp/l2plugin"
l2vppcalls "github.com/ligato/vpp-agent/plugins/vpp/l2plugin/vppcalls"
l3vppcalls "github.com/ligato/vpp-agent/plugins/vpp/l3plugin/vppcalls"
natvppcalls "github.com/ligato/vpp-agent/plugins/vpp/natplugin/vppcalls"
)
// REST api methods
const (
GET = http.MethodGet
POST = http.MethodPost
)
// Plugin registers Rest Plugin
type Plugin struct {
Deps
// Index page
index *index
// Channels
vppChan api.Channel
dumpChan api.Channel
// VPP Handlers
aclHandler aclvppcalls.ACLVppRead
ifHandler ifvppcalls.IfVppRead
natHandler natvppcalls.NatVppRead
bdHandler l2vppcalls.BridgeDomainVppRead
fibHandler l2vppcalls.FIBVppRead
xcHandler l2vppcalls.XConnectVppRead
arpHandler l3vppcalls.ArpVppRead
pArpHandler l3vppcalls.ProxyArpVppRead
rtHandler l3vppcalls.RouteVppRead
// 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
GoVppmux govppmux.TraceAPI
VPPIfPlugin ifplugin.API
VPPL2Plugin *l2plugin.L2Plugin
}
// 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) {
// Check VPP dependency
/*if p.VPP == nil {
return fmt.Errorf("REST plugin requires VPP plugin API")
}*/
// VPP channels
if p.vppChan, err = p.GoVppmux.NewAPIChannel(); err != nil {
return err
}
if p.dumpChan, err = p.GoVppmux.NewAPIChannel(); err != nil {
return err
}
// VPP Indexes
ifIndexes := p.VPPIfPlugin.GetInterfaceIndex()
bdIndexes := p.VPPL2Plugin.GetBDIndex()
dhcpIndexes := p.VPPIfPlugin.GetDHCPIndex()
// Initialize VPP handlers
p.aclHandler = aclvppcalls.NewACLVppHandler(p.vppChan, p.dumpChan, ifIndexes)
p.ifHandler = ifvppcalls.NewIfVppHandler(p.vppChan, p.Log)
p.natHandler = natvppcalls.NewNatVppHandler(p.vppChan, ifIndexes, dhcpIndexes, p.Log)
p.bdHandler = l2vppcalls.NewBridgeDomainVppHandler(p.vppChan, ifIndexes, p.Log)
p.fibHandler = l2vppcalls.NewFIBVppHandler(p.vppChan, ifIndexes, bdIndexes, p.Log)
p.xcHandler = l2vppcalls.NewXConnectVppHandler(p.vppChan, ifIndexes, p.Log)
p.arpHandler = l3vppcalls.NewArpVppHandler(p.vppChan, ifIndexes, p.Log)
p.pArpHandler = l3vppcalls.NewProxyArpVppHandler(p.vppChan, ifIndexes, p.Log)
p.rtHandler = l3vppcalls.NewRouteVppHandler(p.vppChan, ifIndexes, p.Log)
// Linux indexes and handlers
//if p.Linux != nil {
p.linuxIfHandler = iflinuxcalls.NewNetLinkHandler()
p.linuxL3Handler = l3linuxcalls.NewNetLinkHandler()
//}
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) {
p.Log.Debug("REST API Plugin is up and running")
// VPP handlers
p.registerAccessListHandlers()
p.registerInterfaceHandlers()
p.registerNatHandlers()
p.registerL2Handlers()
p.registerL3Handlers()
// Linux handlers
//if p.Linux != nil {
p.registerLinuxInterfaceHandlers()
p.registerLinuxL3Handlers()
//}
// Telemetry, command, index, tracer
p.registerTracerHandler()
p.registerTelemetryHandlers()
p.registerCommandHandler()
p.registerIndexHandlers()
return nil
}
// Close is used to clean up resources used by Plugin
func (p *Plugin) Close() (err error) {
return safeclose.Close(p.vppChan, p.dumpChan)
}
// Fill index item lists
func getIndexPageItems() map[string][]indexItem {
idxMap := map[string][]indexItem{
"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: "Bridge domain IDs", Path: resturl.BdID},
{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},
},
"Tracer": {
{Name: "VPP Binary API", Path: resturl.Tracer},
},
}
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 {
tracerPg := &access.PermissionGroup{
Name: "tracer",
Permissions: []*access.PermissionGroup_Permissions{
newPermission(resturl.Index, GET),
newPermission(resturl.Tracer, GET),
},
}
telemetryPg := &access.PermissionGroup{
Name: "telemetry",
Permissions: []*access.PermissionGroup_Permissions{
newPermission(resturl.Index, 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(resturl.Index, 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.BdID, 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{tracerPg, telemetryPg, dumpPg}
}
// 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,
}
}