/
dump.go
144 lines (122 loc) · 3.63 KB
/
dump.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
// Copyright (c) 2019 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 commands
import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/ligato/vpp-agent/api/types"
agentcli "github.com/ligato/vpp-agent/cmd/agentctl/cli"
"github.com/ligato/vpp-agent/plugins/kvscheduler/api"
)
func NewDumpCommand(cli agentcli.Cli) *cobra.Command {
var opts DumpOptions
cmd := &cobra.Command{
Use: "dump MODEL",
Short: "Dump running state",
Example: `
To dump VPP interfaces run:
$ {{.CommandPath}} vpp.interfaces
To use different dump view use --view flag:
$ {{.CommandPath}} --view=NB vpp.interfaces`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Models = args
return runDump(cli, opts)
},
}
cmd.Flags().StringVar(&opts.View, "view", "cached", "Dump view type: cached, NB, SB")
return cmd
}
type DumpOptions struct {
Models []string
View string
}
func runDump(cli agentcli.Cli, opts DumpOptions) error {
dumpView := opts.View
model := opts.Models[0]
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
allModels, err := cli.Client().ModelList(ctx, types.ModelListOptions{})
if err != nil {
return err
}
var modelKeyPrefix string
for _, m := range allModels {
if (m.Alias != "" && model == m.Alias) || model == m.Name {
modelKeyPrefix = m.KeyPrefix
break
}
}
if modelKeyPrefix == "" {
return fmt.Errorf("no such model: %q", model)
}
dump, err := cli.Client().SchedulerDump(ctx, types.SchedulerDumpOptions{
KeyPrefix: modelKeyPrefix,
View: dumpView,
})
if err != nil {
return err
}
sort.Sort(dumpByKey(dump))
printDumpTable(cli.Out(), dump)
return nil
}
// printDumpTable prints dump data using table format
//
// KEY VALUE ORIGIN METADATA
// config/vpp/v2/interfaces/UNTAGGED-local0 [vpp.interfaces.Interface] from-SB map[IPAddresses:<nil> SwIfIndex:0 TAPHostIfName: Vrf:0]
// name: "UNTAGGED-local0"
// type: SOFTWARE_LOOPBACK
//
// config/vpp/v2/interfaces/loop1 [vpp.interfaces.Interface] from-NB map[IPAddresses:<nil> SwIfIndex:1 TAPHostIfName: Vrf:0]
// name: "loop1"
// type: SOFTWARE_LOOPBACK
//
func printDumpTable(out io.Writer, dump []api.KVWithMetadata) {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)
fmt.Fprintf(w, "KEY\tVALUE\tORIGIN\tMETADATA\t\n")
for _, d := range dump {
val := proto.MarshalTextString(d.Value)
val = strings.ReplaceAll(val, "\n", "\t\t\t\n\t")
var meta string
if d.Metadata != nil {
meta = fmt.Sprintf("%+v", d.Metadata)
}
fmt.Fprintf(w, "%s\t[%s]\t%s\t%s\t\n",
d.Key, proto.MessageName(d.Value), d.Origin, meta)
fmt.Fprintf(w, "\t%s\t\t\n", val)
}
if err := w.Flush(); err != nil {
return
}
fmt.Fprint(out, buf.String())
}
type dumpByKey []api.KVWithMetadata
func (s dumpByKey) Len() int {
return len(s)
}
func (s dumpByKey) Less(i, j int) bool {
return s[i].Key < s[j].Key
}
func (s dumpByKey) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}