This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
/
model.go
89 lines (73 loc) · 2 KB
/
model.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
package client
import (
"context"
"fmt"
"sort"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/sirupsen/logrus"
"github.com/ligato/vpp-agent/api/genericmanager"
"github.com/ligato/vpp-agent/api/types"
"github.com/ligato/vpp-agent/pkg/debug"
)
func (c *Client) ModelList(ctx context.Context, opts types.ModelListOptions) ([]types.Model, error) {
cfgClient, err := c.ConfigClient()
if err != nil {
return nil, err
}
knownModels, err := cfgClient.KnownModels()
if err != nil {
return nil, err
}
logrus.Debugf("retrieved %d known models", len(knownModels))
if debug.IsEnabledFor("models") {
for _, m := range knownModels {
logrus.Debug(proto.CompactTextString(&m))
}
}
allModels := convertModels(knownModels)
sort.Sort(modelsByName(allModels))
return allModels, nil
}
func convertModels(knownModels []genericmanager.ModelInfo) []types.Model {
allModels := make([]types.Model, len(knownModels))
for i, m := range knownModels {
module := strings.Split(m.Model.Module, ".")
typ := m.Model.Type
version := m.Model.Version
name := fmt.Sprintf("%s.%s", m.Model.Module, typ)
alias := fmt.Sprintf("%s.%s", module[0], typ)
if alias == name {
alias = ""
}
protoName := m.Info["protoName"]
keyPrefix := m.Info["keyPrefix"]
nameTemplate := m.Info["nameTemplate"]
//p := reflect.New(proto.MessageType(protoName)).Elem().Interface().(descriptor.Message)
//fd, _ := descriptor.ForMessage(p)
model := types.Model{
Name: name,
Module: m.Model.Module,
Version: version,
Type: typ,
Alias: alias,
KeyPrefix: keyPrefix,
ProtoName: protoName,
NameTemplate: nameTemplate,
//Proto: proto.MarshalTextString(fd),
//ProtoFile: fd.GetName(),
}
allModels[i] = model
}
return allModels
}
type modelsByName []types.Model
func (s modelsByName) Len() int {
return len(s)
}
func (s modelsByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func (s modelsByName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}