Newer
Older
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package adapter
import (
"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/core"
ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
"code.fbi.h-da.de/danet/gosdn/controller/api"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/change"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
"github.com/google/uuid"
"github.com/openconfig/goyang/pkg/yang"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)
// PndAdapter is an API adapter to reflect the NetworkDomain
// interface
type PndAdapter struct {
id uuid.UUID
endpoint string
}
// NewAdapter creates a PND Adapter. It requires a valid PND UUID and a reachable
// goSDN endpoint.
func NewPndAdapter(id, endpoint string) (*PndAdapter, error) {
pid, err := uuid.Parse(id)
if err != nil {
return nil, err
}
return &PndAdapter{
id: pid,
endpoint: endpoint,
}, nil
}
// AddSbi adds an SBI to the PND Adapter. Currently not implemented
func (p *PndAdapter) AddSbi(s southbound.SouthboundInterface) error {
return &errors.ErrNotYetImplemented{}
}
// RemoveSbi removes an SBI from the PND Adapter. Currently not implemented
func (p *PndAdapter) RemoveSbi(uuid.UUID) error {
return &errors.ErrNotYetImplemented{}
}
// AddDevice adds a new device to the controller. The device name is optional.
// If no name is provided a name will be generated upon device creation.
func (p *PndAdapter) AddDevice(name string, opts *tpb.TransportOption, sid uuid.UUID) (*ppb.SetOndListResponse, error) {
resp, err := api.AddDevice(p.endpoint, name, opts, sid, p.ID())
if err != nil {
return nil, err
}
return resp, nil
}
func (p *PndAdapter) GetSbiSchemaTree(sid uuid.UUID) (map[string]*yang.Entry, error) {
resp, err := api.GetSbiSchemaTree(p.Endpoint(), p.ID(), sid)
if err != nil {
return nil, err
}
return resp, nil
}
// GetDevice requests one or multiple devices belonging to a given
// PrincipalNetworkDomain from the controller.
func (p *PndAdapter) GetDevice(identifier ...string) ([]*ppb.OrchestratedNetworkingDevice, error) {
resp, err := api.GetDevice(p.endpoint, p.id.String(), identifier...)
if err != nil {
return nil, err
}
return resp.Ond, nil
}
// GetDevices requests all devices belonging to the PrincipalNetworkDomain
// attached to this adapter.
func (p *PndAdapter) GetDevices() ([]*ppb.OrchestratedNetworkingDevice, error) {
resp, err := api.GetDevices(p.endpoint, p.id.String())
if err != nil {
return nil, err
}
return resp.Ond, nil
}
// RemoveDevice removes a device from the controller
func (p *PndAdapter) RemoveDevice(did uuid.UUID) (*ppb.DeleteOndResponse, error) {
resp, err := api.DeleteDevice(p.endpoint, p.id.String(), did.String())
if err != nil {
return nil, err
}
return resp, nil
}
// RemovePnd removes a PND from the controller
func (p *PndAdapter) RemovePnd(pid uuid.UUID) (*core.DeletePndResponse, error) {
resp, err := api.DeletePnd(p.endpoint, pid.String())
if err != nil {
return nil, err
}
return resp, nil
}
// ChangeOND sends an API call to the controller requesting the creation of
// a change from the provided Operation, path and value. The Change is marked
// as Pending and times out after the specified timeout period
func (p *PndAdapter) ChangeOND(duid uuid.UUID, operation ppb.ApiOperation, path string, value ...string) (uuid.UUID, error) {
var v string
if len(value) != 0 {
v = value[0]
}
resp, err := api.ChangeRequest(p.endpoint, duid.String(), p.id.String(), path, v, operation)
if err != nil {
return uuid.Nil, err
}
log.Info(resp)
return uuid.Nil, err
}
// Request sends an API call to the controller requesting the specified path
// for the specified device
func (p *PndAdapter) Request(did uuid.UUID, path string) (proto.Message, error) {
resp, err := api.GetPath(p.endpoint, p.id.String(), did.String(), path)
if err != nil {
return nil, err
}
return resp, nil
}
// RequestAll sends an API call to the controller requesting the specified path
// for all registered devices. Not yet implemented.
func (p *PndAdapter) RequestAll(path string) ([]proto.Message, error) {
resp, err := api.GetDevices(p.Endpoint(), p.ID().String())
if err != nil {
return []proto.Message{}, err
}
reqResult := make([]proto.Message, len(resp.Ond))
g := new(errgroup.Group)
for i, ond := range resp.Ond {
i, ond := i, ond
// TODO: probably the controller should do this; this would result in a
// single request from CLI side.
g.Go(func() error {
resp, err := api.GetPath(p.endpoint, p.id.String(), ond.GetId(), path)
if err != nil {
return err
}
reqResult[i] = resp
return nil
})
}
if err := g.Wait(); err != nil {
// return the parts that succeeded, aswell as the errors that have been
// encountered
return reqResult, err
}
return reqResult, nil
}
// ContainsDevice sends an API call to the controller checking if a device
// with the given UUID is present. Not implemented, always returns false
func (p *PndAdapter) ContainsDevice(uuid.UUID) bool {
return false
}
// GetSbi sends an API call to the controller requesting the
// registered SBI with the provided ID.
func (p *PndAdapter) GetSbi(sid ...string) ([]*spb.SouthboundInterface, error) {
resp, err := api.GetSbi(p.endpoint, p.id.String(), sid...)
if err != nil {
return nil, err
}
return resp.Sbi, nil
}
// GetSBIs sends an API call to the controller requesting the
// registered SBIs. Not implemented, always returns nil
func (p *PndAdapter) GetSBIs() ([]*spb.SouthboundInterface, error) {
resp, err := api.GetSBIs(p.endpoint, p.id.String())
if err != nil {
return nil, err
}
return resp.Sbi, nil
}
// ID returns the PND Adapter's UUID
func (p *PndAdapter) ID() uuid.UUID {
return p.id
}
// Endpoint returns the PND Adapter's endpoint
func (p *PndAdapter) Endpoint() string {
return p.endpoint
}
// PendingChanges sends an API call to the controller requesting
// the UUIDs of all pending changes
func (p *PndAdapter) PendingChanges() []uuid.UUID {
resp, err := api.GetChanges(p.endpoint, p.id.String())
if err != nil {
log.Error(err)
return nil
}
return filterChanges(ppb.ChangeState_CHANGE_STATE_PENDING, resp)
}
// CommittedChanges sends an API call to the controller requesting
// the UUIDs of all committed changes
func (p *PndAdapter) CommittedChanges() []uuid.UUID {
resp, err := api.GetChanges(p.endpoint, p.id.String())
if err != nil {
log.Error(err)
return nil
}
return filterChanges(ppb.ChangeState_CHANGE_STATE_COMMITTED, resp)
}
// GetChange sends an API call to the controller requesting the specified change
func (p *PndAdapter) GetChange(uuid.UUID) (change.Change, error) {
return nil, &errors.ErrNotYetImplemented{}
}
// Commit sends an API call to the controller committing the specified change
func (p *PndAdapter) Commit(cuid uuid.UUID) error {
resp, err := api.Commit(p.endpoint, p.id.String(), cuid.String())
if err != nil {
return err
}
log.Info(resp)
return nil
}
// Confirm sends an API call to the controller confirming the specified change
func (p *PndAdapter) Confirm(cuid uuid.UUID) error {
resp, err := api.Confirm(p.endpoint, p.id.String(), cuid.String())
if err != nil {
return err
}
log.Info(resp)
return nil
}
func filterChanges(state ppb.ChangeState, resp *ppb.GetChangeListResponse) []uuid.UUID {
changes := make([]uuid.UUID, 0)
for _, ch := range resp.Change {
if ch.State == state {
id, _ := uuid.Parse(ch.Id)
changes = append(changes, id)
}
}
return changes
}