forked from openconfig/ondatra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
165 lines (137 loc) · 3.75 KB
/
device.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
// Copyright 2019 Google LLC
//
// 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
//
// https://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 ondatra
import (
"fmt"
"testing"
"github.com/openconfig/ondatra/internal/reservation"
"github.com/openconfig/ondatra/telemetry"
opb "github.com/openconfig/ondatra/proto"
)
// Device represents a network device.
type Device struct {
id string
res reservation.Device
}
// String returns a string repesentation of the device.
func (d *Device) String() string {
return fmt.Sprintf("%s(%s)", d.ID(), d.Name())
}
// ID returns the device id.
func (d *Device) ID() string {
return d.id
}
// Name returns the device name.
func (d *Device) Name() string {
return d.res.Dimensions().Name
}
// Vendor is a DUT vendor.
type Vendor int
const (
// ARISTA vendor.
ARISTA = Vendor(opb.Device_ARISTA)
// CISCO vendor.
CISCO = Vendor(opb.Device_CISCO)
// JUNIPER vendor.
JUNIPER = Vendor(opb.Device_JUNIPER)
// IXIA vendor.
IXIA = Vendor(opb.Device_IXIA)
// CIENA vendor.
CIENA = Vendor(opb.Device_CIENA)
)
// String returns the name of the vendor.
func (v Vendor) String() string {
return opb.Device_Vendor(v).String()
}
// Vendor returns the device vendor.
func (d *Device) Vendor() Vendor {
return Vendor(d.res.Dimensions().Vendor)
}
// Model returns the device hardware model.
func (d *Device) Model() string {
return d.res.Dimensions().HardwareModel
}
// Version returns the device software version.
func (d *Device) Version() string {
return d.res.Dimensions().SoftwareVersion
}
// Port returns a port with a given id.
func (d *Device) Port(t testing.TB, ID string) *Port {
t.Helper()
p, err := d.port(ID)
if err != nil {
t.Fatalf("Port(t, %s) on %s: %v", ID, d, err)
}
return p
}
// Ports returns a slice of all ports configured on the device.
func (d *Device) Ports() []*Port {
var ports []*Port
for id, p := range d.res.Dimensions().Ports {
ports = append(ports, d.newPort(id, p))
}
return ports
}
func (d *Device) port(id string) (*Port, error) {
rp, err := d.res.Dimensions().Port(id)
if err != nil {
return nil, err
}
return d.newPort(id, rp), nil
}
func (d *Device) newPort(id string, res *reservation.Port) *Port {
return &Port{dev: d, id: id, res: res}
}
// Operations returns a handle to the device operations API.
func (d *Device) Operations() *Operations {
return &Operations{d.res}
}
// Telemetry returns a telemetry path root for the device.
func (d *Device) Telemetry() *telemetry.DevicePath {
return telemetry.DeviceRoot(d.ID())
}
// Port represents a port.
type Port struct {
dev *Device
id string
res *reservation.Port
}
func (p *Port) String() string {
return fmt.Sprintf("%s:%s(%s:%s)", p.dev.ID(), p.ID(), p.dev.Name(), p.Name())
}
// ID returns the port ID.
func (p *Port) ID() string {
return p.id
}
// Name returns the port name.
func (p *Port) Name() string {
return p.res.Name
}
// Device returns the device that owns the port.
func (p *Port) Device() *Device {
return p.dev
}
// Speed is a port speed.
type Speed int
const (
// Speed10Gb is a port speed of 10Gbps.
Speed10Gb = Speed(opb.Port_S_10GB)
// Speed100Gb is a port speed of 100Gbps.
Speed100Gb = Speed(opb.Port_S_100GB)
)
// TODO: Restore when speed is consistently propagated back.
// Speed returns the port speed.
// func (p *Port) Speed() Speed {
// return Speed(p.pb.GetSpeed())
//}