forked from openconfig/ondatra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.go
137 lines (118 loc) · 4.09 KB
/
topology.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
// Copyright 2020 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 (
"golang.org/x/net/context"
"fmt"
"testing"
"github.com/openconfig/ondatra/internal/ate"
"github.com/openconfig/ondatra/internal/reservation"
opb "github.com/openconfig/ondatra/proto"
)
var lagCount uint32
// Topology is an ATE topology API.
type Topology struct {
ate *reservation.ATE
}
func (tp *Topology) String() string {
return fmt.Sprintf("{ate: %s}", tp.ate)
}
// New returns a new ATE topology.
func (tp *Topology) New() *ATETopology {
return &ATETopology{ate: tp.ate, top: &opb.Topology{}}
}
// ATETopology is an ATE topology.
type ATETopology struct {
ate *reservation.ATE
top *opb.Topology
}
func (at *ATETopology) String() string {
return fmt.Sprintf("{ate: %s, topology: %s}", at.ate, at.top)
}
// AddInterface adds and returns an interface with the specified name.
// The returned interface has an Ethernet configuration with an MTU of 1500.
func (at *ATETopology) AddInterface(name string) *Interface {
ipb := &opb.InterfaceConfig{
Name: name,
Ethernet: &opb.EthernetConfig{Mtu: 1500},
}
at.top.Interfaces = append(at.top.Interfaces, ipb)
return &Interface{pb: ipb}
}
// ClearInterfaces clear interfaces from the ATE topology.
func (at *ATETopology) ClearInterfaces() *ATETopology {
at.top.Interfaces = nil
return at
}
// Interfaces returns a map of interface names to interfaces.
func (at *ATETopology) Interfaces() map[string]*Interface {
intfs := make(map[string]*Interface)
for _, pb := range at.top.Interfaces {
intfs[pb.GetName()] = &Interface{pb}
}
return intfs
}
// AddLAG adds and returns a new LAG.
func (at *ATETopology) AddLAG() *LAG {
lagCount = lagCount + 1
lpb := &opb.Lag{Name: fmt.Sprintf("lag%d", lagCount)}
at.top.Lags = append(at.top.Lags, lpb)
return &LAG{pb: lpb}
}
// Push replaces the topology to the ATE with this one.
// Currently running protocols will stop.
func (at *ATETopology) Push(t testing.TB) *ATETopology {
t.Helper()
logAction(t, "Pushing topology to %s", at.ate)
if err := ate.PushTopology(context.Background(), at.ate, at.top); err != nil {
t.Fatalf("Push(t) on %s: %v", at, err)
}
return at
}
// Update updates the topology on the ATE to this one.
// Currently running protocols will continue running.
func (at *ATETopology) Update(t testing.TB) {
t.Helper()
logAction(t, "Updating topology to %s", at.ate)
if err := ate.UpdateTopology(context.Background(), at.ate, at.top, false); err != nil {
t.Fatalf("Update(t) on %s: %v", at, err)
}
}
// UpdateBGPPeerStates is equivalent to Update() but only updates the BGP peer state.
// This is provided as a temporary workaround for the high overhead of Update().
// TODO: Remove this method once new Ixia config binding is used.
func (at *ATETopology) UpdateBGPPeerStates(t testing.TB) {
t.Helper()
if err := ate.UpdateTopology(context.Background(), at.ate, at.top, true); err != nil {
t.Fatalf("UpdateBGPPeerState(t) on %s: %v", at, err)
}
}
// StartProtocols starts the control plane protocols on the ATE.
func (at *ATETopology) StartProtocols(t testing.TB) *ATETopology {
t.Helper()
logAction(t, "Starting protocols on %s", at.ate)
if err := ate.StartProtocols(context.Background(), at.ate); err != nil {
t.Fatalf("StartProtocols(t) on %s: %v", at, err)
}
return at
}
// StopProtocols stops the control plane protocols on the ATE.
func (at *ATETopology) StopProtocols(t testing.TB) *ATETopology {
t.Helper()
logAction(t, "Stopping protocols to %s", at.ate)
if err := ate.StopProtocols(context.Background(), at.ate); err != nil {
t.Fatalf("StopProtocols(t) on %s: %v", at, err)
}
return at
}