-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
406 lines (353 loc) · 11.4 KB
/
main.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"reflect"
"strings"
"github.com/abiosoft/ishell"
log "github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"opennos-mgmt/gnmi"
"opennos-mgmt/gnmi/modeldata"
"opennos-mgmt/gnmi/modeldata/oc"
vlan "opennos-mgmt/management/vlan"
"opennos-mgmt/utils/credentials"
pb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ygot/ygot"
cfg "opennos-mgmt/config"
)
var validIfaces = [...]string{
"eth-1", "eth-1/1", "eth-1/2", "eth-1/3", "eth-1/4",
"eth-2", "eth-2/1", "eth-2/2", "eth-2/3", "eth-2/4",
"eth-3", "eth-3/1", "eth-3/2", "eth-3/3", "eth-3/4",
"eth-4", "eth-4/1", "eth-4/2", "eth-4/3", "eth-4/4",
"eth-5", "eth-5/1", "eth-5/2", "eth-5/3", "eth-5/4",
"eth-6", "eth-6/1", "eth-6/2", "eth-6/3", "eth-6/4",
"eth-7", "eth-7/1", "eth-7/2", "eth-7/3", "eth-7/4",
"eth-8", "eth-8/1", "eth-8/2", "eth-8/3", "eth-8/4",
"eth-9", "eth-9/1", "eth-9/2", "eth-9/3", "eth-9/4",
"eth-10", "eth-10/1", "eth-10/2", "eth-10/3", "eth-10/4",
"eth-11", "eth-11/1", "eth-11/2", "eth-11/3", "eth-11/4",
"eth-12", "eth-12/1", "eth-12/2", "eth-12/3", "eth-12/4",
"eth-13", "eth-13/1", "eth-13/2", "eth-13/3", "eth-13/4",
"eth-14", "eth-14/1", "eth-14/2", "eth-14/3", "eth-14/4",
"eth-15", "eth-15/1", "eth-15/2", "eth-15/3", "eth-15/4",
"eth-16", "eth-16/1", "eth-16/2", "eth-16/3", "eth-16/4",
"eth-17", "eth-17/1", "eth-17/2", "eth-17/3", "eth-17/4",
"eth-18", "eth-18/1", "eth-18/2", "eth-18/3", "eth-18/4",
"eth-19", "eth-19/1", "eth-19/2", "eth-19/3", "eth-19/4",
"eth-20", "eth-20/1", "eth-20/2", "eth-20/3", "eth-20/4",
"eth-21", "eth-21/1", "eth-21/2", "eth-21/3", "eth-21/4",
"eth-22", "eth-22/1", "eth-22/2", "eth-22/3", "eth-22/4",
"eth-32", "eth-23/1", "eth-23/2", "eth-23/3", "eth-23/4",
"eth-24", "eth-24/1", "eth-24/2", "eth-24/3", "eth-24/4",
"eth-25", "eth-25/1", "eth-25/2", "eth-25/3", "eth-25/4",
"eth-26", "eth-26/1", "eth-26/2", "eth-26/3", "eth-26/4",
"eth-27", "eth-27/1", "eth-27/2", "eth-27/3", "eth-27/4",
"eth-28", "eth-28/1", "eth-28/2", "eth-28/3", "eth-28/4",
"eth-29", "eth-29/1", "eth-29/2", "eth-29/3", "eth-29/4",
"eth-30", "eth-30/1", "eth-30/2", "eth-30/3", "eth-30/4",
"eth-31", "eth-31/1", "eth-31/2", "eth-31/3", "eth-31/4",
"eth-32", "eth-32/1", "eth-32/2", "eth-32/3", "eth-32/4",
}
var gEditIfaceCmdCompleterInvoked bool = false
type Iface struct {
speed uint32
}
func NewIface() *Iface {
return &Iface{}
}
type EditIfaceCmdCtx struct {
completerInvoked bool
}
func NewEditIfaceCmdCtx() *EditIfaceCmdCtx {
return &EditIfaceCmdCtx{
completerInvoked: false,
}
}
var editIfaceCmdCtx *EditIfaceCmdCtx = NewEditIfaceCmdCtx()
var (
bindAddr = flag.String("bind_address", ":10161", "Bind to address:port or just :port")
configFile = flag.String("config", "", "IETF JSON file for target startup config")
)
type server struct {
*gnmi.Server
}
var gnmiCallback gnmi.ConfigCallback = func(newConfig ygot.ValidatedGoStruct, cbUserData interface{}) error {
configMngr := cbUserData.(*cfg.ConfigMngrT)
changelog, err := configMngr.GetDiffRunningConfigWithCandidateConfig(&newConfig)
if err != nil {
log.Errorf("Failed to get diff of two config objects: %s", err)
return err
}
log.Infof("Number of changes: %d", len(changelog))
jsonDump, err := json.MarshalIndent(changelog, "", " ")
if err != nil {
log.Errorf("Failed to JSON dump: %s", err)
} else {
log.Infof("Dump JSON: %s", string(jsonDump))
}
if len(changelog) == 0 {
return nil
}
return configMngr.CommitChangelog(&changelog, &newConfig)
}
func newServer(model *gnmi.Model, config []byte) (*server, error) {
configMngr := cfg.NewConfigMngrT()
err := configMngr.LoadConfig(model, config)
if err != nil {
return nil, err
}
s, err := gnmi.NewServer(model, config, gnmiCallback, configMngr)
if err != nil {
return nil, err
}
return &server{Server: s}, nil
}
// Get overrides the Get func of gnmi.Target to provide user auth.
func (s *server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) {
msg, ok := credentials.AuthorizeUser(ctx)
if !ok {
log.Infof("denied a Get request: %v", msg)
return nil, status.Error(codes.PermissionDenied, msg)
}
log.Infof("allowed a Get request: %v", msg)
return s.Server.Get(ctx, req)
}
// Set overrides the Set func of gnmi.Target to provide user auth.
func (s *server) Set(ctx context.Context, req *pb.SetRequest) (*pb.SetResponse, error) {
msg, ok := credentials.AuthorizeUser(ctx)
if !ok {
log.Infof("denied a Set request: %v", msg)
return nil, status.Error(codes.PermissionDenied, msg)
}
log.Infof("allowed a Set request: %v", msg)
return s.Server.Set(ctx, req)
}
func gNMIServerRun() {
model := gnmi.NewModel(modeldata.ModelData,
reflect.TypeOf((*oc.Device)(nil)),
oc.SchemaTree["Device"],
oc.Unmarshal,
oc.ΛEnum)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Supported models:\n")
for _, m := range model.SupportedModels() {
fmt.Fprintf(os.Stderr, " %s\n", m)
}
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
opts := credentials.ServerCredentials()
g := grpc.NewServer(opts...)
var configData []byte
if *configFile != "" {
var err error
configData, err = ioutil.ReadFile(*configFile)
if err != nil {
log.Exitf("error in reading config file: %v", err)
}
}
s, err := newServer(model, configData)
if err != nil {
log.Exitf("error in creating gnmi target: %v", err)
}
pb.RegisterGNMIServer(g, s)
reflection.Register(g)
log.Infof("starting to listen on %s", *bindAddr)
listen, err := net.Listen("tcp", *bindAddr)
if err != nil {
log.Exitf("failed to listen: %v", err)
}
log.Info("starting to serve")
if err := g.Serve(listen); err != nil {
log.Exitf("failed to serve: %v", err)
}
}
func main() {
go gNMIServerRun()
shell := ishell.New()
// display info.
shell.Println("Welcome to OpenNOS CLI")
//Consider the unicode characters supported by the users font
//shell.SetMultiChoicePrompt(" >>"," - ")
//shell.SetChecklistOptions("[ ] ","[X] ")
// var vlans []string = make([]string, 1)
// var editableIfaces = map[string]*Iface{}
var editableIfaces = map[string]*Iface{
"eth-1": NewIface(), "eth-2": NewIface(), "eth-3": NewIface(), "eth-4": NewIface(), "eth-5": NewIface(),
"eth-6": NewIface(), "eth-7": NewIface(), "eth-8": NewIface(), "eth-9": NewIface(), "eth-10": NewIface(),
"eth-11": NewIface(), "eth-12": NewIface(), "eth-13": NewIface(), "eth-14": NewIface(), "eth-15": NewIface(),
"eth-16": NewIface(), "eth-17": NewIface(), "eth-18": NewIface(), "eth-19": NewIface(), "eth-20": NewIface(),
"eth-21": NewIface(), "eth-22": NewIface(), "eth-23": NewIface(), "eth-24": NewIface(), "eth-25": NewIface(),
"eth-26": NewIface(), "eth-27": NewIface(), "eth-28": NewIface(), "eth-29": NewIface(), "eth-30": NewIface(),
"eth-31": NewIface(), "eth-32": NewIface(),
}
// var editableIfaces []string = []string{
// "eth-1", "eth-2", "eth-3", "eth-4", "eth-5", "eth-6", "eth-7", "eth-8", "eth-9", "eth-10",
// "eth-11", "eth-12", "eth-13", "eth-14", "eth-15", "eth-16", "eth-17", "eth-18", "eth-19", "eth-20",
// "eth-21", "eth-22", "eth-23", "eth-24", "eth-25", "eth-26", "eth-27", "eth-28", "eth-29", "eth-30",
// "eth-31", "eth-32",
// }
editCmd := &ishell.Cmd{
Name: "edit",
Help: "edit <interface> | <aggregate> | <vlan>",
LongHelp: `Edit`,
}
editIfaceCmd := &ishell.Cmd{
Name: "interface",
Help: "edit interface <interface name>",
Completer: func(args []string) []string {
if editIfaceCmdCtx.completerInvoked {
if len(args) > 1 {
return nil
}
if len(args) > 0 {
return []string{"help"}
// return nil
}
// log.Println(args)
// return nil
// return []string{"\nPress enter to edit..."}
}
ifnames := make([]string, len(editableIfaces))
i := 0
for ifname := range editableIfaces {
ifnames[i] = ifname
i++
}
editIfaceCmdCtx.completerInvoked = true
return ifnames
},
Func: func(c *ishell.Context) {
log.Infof("Choosed interface %s", c.Args[0])
if len(c.Args) == 0 {
c.Err(errors.New("Missing interface name"))
return
}
if len(c.Args) == 2 {
if c.Args[1] != "help" {
c.Err(errors.New("Invalid argument"))
return
}
log.Infof("Enter to edit interface %s", c.Args[0])
return
}
if len(c.Args) > 1 {
c.Err(errors.New("Too many arguments"))
return
}
foundIface := false
for _, iface := range validIfaces {
if c.Args[0] == iface {
foundIface = true
break
}
}
if !foundIface {
c.Err(errors.New("Invalid argument"))
return
}
// editableIfaces[c.Args[0]] = NewIface()
editIfaceCmdCtx.completerInvoked = false
c.SetPrompt(fmt.Sprintf("[edit interface %s]# ", c.Args[0]))
// editableIfaces[c.Args[2]] = NewIface()
// vlans = append(vlans, c.Args...)
port := make([]string, 1)
port[0] = c.Args[0]
if err := vlan.SetNativeVlan(port, 2); err != nil {
c.Err(errors.New("Failed to set native VLAN"))
return
}
},
}
executePromptCmd := &ishell.Cmd{
Name: "execute_prompt",
Help: "Press Enter to execute command",
// Func: func(c *ishell.Context) {
// log.Println("Press Enter to execute command")
// },
}
// editIfaceCmd.AddCmd(&ishell.Cmd{
// Name: "add_face_to_vlan",
// Help: "add_face_to_vlan",
// Func: func(c *ishell.Context) {
// if len(c.Args) == 0 {
// c.Err(errors.New("missing interface name"))
// return
// }
// vlans = append(vlans, c.Args...)
// },
// })
editIfaceCmd.AddCmd(executePromptCmd)
editCmd.AddCmd(editIfaceCmd)
// editCmd.AddCmd(executePromptCmd)
addCmd := &ishell.Cmd{
Name: "add",
Help: "add",
LongHelp: `Try dynamic autocomplete by adding and removing words.
Then view the autocomplete by tabbing after "words" subcommand.
This is an example of a long help.`,
}
addIfaceCmd := &ishell.Cmd{
Name: "interfaces",
Help: "Specify network interfaces to add into a VLAN",
LongHelp: `Try dynamic autocomplete by adding and removing words.
Then view the autocomplete by tabbing after "words" subcommand.
This is an example of a long help.`,
}
addIfaceToCmd := &ishell.Cmd{
Name: "to",
Help: "to",
LongHelp: `Try dynamic autocomplete by adding and removing words.
Then view the autocomplete by tabbing after "words" subcommand.
This is an example of a long help.`,
}
addIfaceToVlanCmd := &ishell.Cmd{
Name: "vlan",
Help: "<VLAN-ID> <IFACE-ID> [<IFACE-ID>...]",
LongHelp: `add interfaces to vlan <VLAN ID> <PORT NAME>`,
Func: func(c *ishell.Context) {
c.ShowPrompt(false)
defer c.ShowPrompt(true)
defaultInput := "vlan-1 eth-1 eth-2"
if len(c.Args) > 0 {
defaultInput = strings.Join(c.Args, " ")
}
c.Print("input: ")
read := c.ReadLineWithDefault(defaultInput)
if read == defaultInput {
c.Println("you left the default input intact")
} else {
c.Printf("you modified input to '%s'", read)
c.Println()
}
},
}
addIfaceToCmd.AddCmd(addIfaceToVlanCmd)
addIfaceCmd.AddCmd(addIfaceToCmd)
addIfaceCmd.AddCmd(addIfaceCmd)
addCmd.AddCmd(addIfaceCmd)
shell.AddCmd(addCmd)
shell.AddCmd(editCmd)
// when started with "exit" as first argument, assume non-interactive execution
if len(os.Args) > 1 && os.Args[1] == "exit" {
shell.Process(os.Args[2:]...)
} else {
// start shell
shell.Run()
// teardown
shell.Close()
}
}