/
onosuenib.go
114 lines (90 loc) · 2.47 KB
/
onosuenib.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
// SPDX-FileCopyrightText: 2021-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
package collect
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"time"
"github.com/onosproject/onos-api/go/onos/uenib"
"github.com/onosproject/onos-exporter/pkg/kpis"
"google.golang.org/grpc"
)
// onosUenibCollector is the onos uenib collector.
// It extracts all the uenib related kpis using the Collect method.
type onosUenibCollector struct {
collector
}
// Collect implements the Collector interface behavior for
// onosUenibCollector, returning a list of kpis.KPI.
func (col *onosUenibCollector) Collect() ([]kpis.KPI, error) {
kpis := []kpis.KPI{}
if len(col.config.getAddress()) == 0 {
return kpis, fmt.Errorf("onosUenibCollector Collect missing service address")
}
conn, err := GetConnection(
col.config.getAddress(),
col.config.getCertPath(),
col.config.getKeyPath(),
col.config.noTLS(),
)
if err != nil {
return kpis, err
}
defer conn.Close()
uenibKPI, err := listUEs(conn)
if err != nil {
return kpis, err
}
kpis = append(kpis, uenibKPI)
return kpis, err
}
// listUEs receives a connection to a onos uenib service
// to retrieve the uenib UEs Aspects and store them according to the
// data structure of the kpis.OnosUenibUEs KPI.
func listUEs(conn *grpc.ClientConn) (kpis.KPI, error) {
uenibKPI := kpis.OnosUenibUEs()
uenibKPI.UEs = make(map[string]kpis.UE)
aspectTypes := []string{}
client := uenib.CreateUEServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
response, err := client.ListUEs(ctx, &uenib.ListUERequest{AspectTypes: aspectTypes})
if err != nil {
return uenibKPI, err
}
if err != nil {
return uenibKPI, err
}
for {
resp, err := response.Recv()
if err == io.EOF {
break
} else if err != nil {
return uenibKPI, err
} else {
ue := parseObjectUE(resp.UE, false)
uenibKPI.UEs[ue.ID] = ue
}
}
return uenibKPI, nil
}
func parseObjectUE(ue uenib.UE, verbose bool) kpis.UE {
aspectsList := []string{}
for aspectType, aspect := range ue.Aspects {
if verbose {
aspectTypeValue := strings.Join([]string{aspectType, bytes.NewBuffer(aspect.Value).String()}, "=")
aspectsList = append(aspectsList, aspectTypeValue)
} else {
aspectsList = append(aspectsList, aspectType)
}
}
aspects := strings.Join(aspectsList, ",")
return kpis.UE{
ID: string(ue.ID),
Aspects: aspects,
}
}