added mock adapter for new VPP stats
[govpp.git] / adapter / stats_api.go
1 // Copyright (c) 2019 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package adapter
16
17 // StatsAPI provides connection to VPP stats API.
18 type StatsAPI interface {
19         // Connect establishes client connection to the stats API.
20         Connect() error
21
22         // Disconnect terminates client connection.
23         Disconnect() error
24
25         // ListStats lists names for all stats.
26         ListStats(patterns ...string) (statNames []string, err error)
27
28         // DumpStats dumps all stat entries.
29         DumpStats(patterns ...string) ([]*StatEntry, error)
30 }
31
32 // StatType represents type of stat directory and simply
33 // defines what type of stat data is stored in the stat entry.
34 type StatType int
35
36 const (
37         _ StatType = iota
38         ScalarIndex
39         SimpleCounterVector
40         CombinedCounterVector
41         ErrorIndex
42 )
43
44 func (d StatType) String() string {
45         switch d {
46         case ScalarIndex:
47                 return "ScalarIndex"
48         case SimpleCounterVector:
49                 return "SimpleCounterVector"
50         case CombinedCounterVector:
51                 return "CombinedCounterVector"
52         case ErrorIndex:
53                 return "ErrorIndex"
54         }
55         return "UnknownStatType"
56 }
57
58 // StatEntry represents single stat entry. The type of stat stored in Data
59 // is defined by Type.
60 type StatEntry struct {
61         Name string
62         Type StatType
63         Data Stat
64 }
65
66 // Counter represents simple counter with single value.
67 type Counter uint64
68
69 // CombinedCounter represents counter with two values, for packet count and bytes count.
70 type CombinedCounter struct {
71         Packets Counter
72         Bytes   Counter
73 }
74
75 // ScalarStat represents stat for ScalarIndex.
76 type ScalarStat float64
77
78 // ErrorStat represents stat for ErrorIndex.
79 type ErrorStat uint64
80
81 // SimpleCounterStat represents stat for SimpleCounterVector.
82 // The outer array represents workers and the inner array represents sw_if_index.
83 // Values should be aggregated per interface for every worker.
84 type SimpleCounterStat [][]Counter
85
86 // CombinedCounterStat represents stat for CombinedCounterVector.
87 // The outer array represents workers and the inner array represents sw_if_index.
88 // Values should be aggregated per interface for every worker.
89 type CombinedCounterStat [][]CombinedCounter
90
91 // Data represents some type of stat which is usually defined by StatType.
92 type Stat interface {
93         // isStat is unexported to limit implementations of Data interface to this package,
94         isStat()
95 }
96
97 func (ScalarStat) isStat()          {}
98 func (ErrorStat) isStat()           {}
99 func (SimpleCounterStat) isStat()   {}
100 func (CombinedCounterStat) isStat() {}