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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package nucleus
import (
"encoding/json"
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/customerrs"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkelement"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/transport"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/google/uuid"
"github.com/openconfig/ygot/ygot"
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/protobuf/proto"
)
// NewNetworkElement creates a network element.
func NewNetworkElement(name string, uuidInput uuid.UUID, opt *tpb.TransportOption, sbi southbound.SouthboundInterface) (networkelement.NetworkElement, error) {
t, err := NewTransport(opt, sbi)
if err != nil {
return nil, err
}
// TODO: this needs to check the case that the uuidInput is set, as the
// same uuid may be already stored.
if uuidInput == uuid.Nil {
uuidInput = uuid.New()
}
if name == "" {
name = namesgenerator.GetRandomName(0)
}
// We want a representation of the MNE's config (the SBI-schema's root created through ygot),
// but do not want to work on the sbi.Schema() directly.
// So the root of sbi.Schema() is never changed when a set or get on a network element will be called.
root, err := ygot.DeepCopy(sbi.Schema().Root)
if err != nil {
return nil, err
}
ygotDeepCopy, ok := root.(ygot.GoStruct)
if !ok {
return nil, &customerrs.InvalidTypeAssertionError{
Value: root,
Type: (*ygot.ValidatedGoStruct)(nil),
}
}
if opt.Type == spb.Type_TYPE_CONTAINERISED {
return &CsbiNetworkElement{
CommonNetworkElement: CommonNetworkElement{
UUID: uuidInput,
Model: ygotDeepCopy,
sbi: sbi,
transport: t,
name: name,
transportOptions: opt,
},
}, nil
}
return &CommonNetworkElement{
UUID: uuidInput,
Model: ygotDeepCopy,
sbi: sbi,
transport: t,
name: name,
transportOptions: opt,
}, nil
}
// CommonNetworkElement represents an MNE.
type CommonNetworkElement struct {
// UUID represents the Network Elements UUID
UUID uuid.UUID
// Network Element embeds a ygot.GoStruct containing the network element details
Model ygot.GoStruct
// SBI is the network element's southbound interface implementation
sbi southbound.SouthboundInterface
// Transport is the network element's Transport implementation
transport transport.Transport
// Name is the network element's human readable name
name string
transportOptions *tpb.TransportOption
}
// ID returns the UUID of the Network Element.
func (n *CommonNetworkElement) ID() uuid.UUID {
return n.UUID
}
// GetModel returns the ygot representation of the Network Element.
func (n *CommonNetworkElement) GetModel() ygot.GoStruct {
return n.Model
}
// CreateModelCopy returns a copy of the ygot representation of the Network Element.
func (n *CommonNetworkElement) CreateModelCopy() (ygot.ValidatedGoStruct, error) {
return createValidatedCopy(n)
}
// Transport returns the Transport of the network element.
func (n *CommonNetworkElement) Transport() transport.Transport {
return n.transport
}
// TransportAddress returns the TransportAddress of the network element.
func (n *CommonNetworkElement) TransportAddress() string {
return n.transportOptions.Address
}
// Name returns the name of the network element.
func (n *CommonNetworkElement) Name() string {
return n.name
}
// SBI returns the sbi of the Network Element.
func (n *CommonNetworkElement) SBI() southbound.SouthboundInterface {
return n.sbi
}
// SetTransport sets the Network Element's Transport.
func (n *CommonNetworkElement) SetTransport(t transport.Transport) {
n.transport = t
}
// SetName sets the Network Element's name.
func (n *CommonNetworkElement) SetName(name string) {
n.name = name
}
// SetSBI sets the Network Element's SBI.
func (n *CommonNetworkElement) SetSBI(sbi southbound.SouthboundInterface) {
n.sbi = sbi
}
// ProcessResponse processes a response for the Network Element.
func (n *CommonNetworkElement) ProcessResponse(resp proto.Message) error {
return n.transport.ProcessResponse(resp, n.Model, n.sbi.Schema())
}
// IsTransportValid returns a boolean if the transport of a network element is valid.
func (n *CommonNetworkElement) IsTransportValid() bool {
if n.transportOptions != nil && n.transportOptions.Address != "" {
return true
}
return false
}
// CsbiNetworkElement is used for the cSBI functionality.
type CsbiNetworkElement struct {
CommonNetworkElement
}
// ID returns the UUID of the Network Element.
func (n *CsbiNetworkElement) ID() uuid.UUID {
return n.UUID
}
// GetModel returns the ygot representation of the Network Element.
func (n *CsbiNetworkElement) GetModel() ygot.GoStruct {
return n.Model
}
// CreateModelCopy returns a copy of the ygot representation of the Network Element.
func (n *CsbiNetworkElement) CreateModelCopy() (ygot.ValidatedGoStruct, error) {
return createValidatedCopy(n)
}
// Transport returns the Transport of the network element.
func (n *CsbiNetworkElement) Transport() transport.Transport {
return n.transport
}
// Name returns the name of the network element.
func (n *CsbiNetworkElement) Name() string {
return n.name
}
// SBI returns the sbi of the Network Element.
func (n *CsbiNetworkElement) SBI() southbound.SouthboundInterface {
return n.sbi
}
// ProcessResponse processes a response for the Network Element.
func (n *CsbiNetworkElement) ProcessResponse(resp proto.Message) error {
// TODO: callback to send response to caller
return n.transport.ProcessResponse(resp, n.Model, n.sbi.Schema())
}
func createValidatedCopy(n networkelement.NetworkElement) (ygot.ValidatedGoStruct, error) {
cpy, err := ygot.DeepCopy(n.GetModel())
ygot.BuildEmptyTree(cpy)
if err != nil {
return nil, err
}
validatedCpy, ok := cpy.(ygot.ValidatedGoStruct)
if !ok {
return nil, customerrs.InvalidTypeAssertionError{
Value: validatedCpy,
Type: (*ygot.ValidatedGoStruct)(nil),
}
}
return validatedCpy, nil
}
// IsTransportValid returns a boolean if the transport of a network element is valid.
func (n *CsbiNetworkElement) IsTransportValid() bool {
if n.transportOptions != nil && n.transportOptions.Address != "" {
return true
}
return false
}
// MarshalJSON implements the MarshalJSON interface to store a network element as JSON.
func (n *CommonNetworkElement) MarshalJSON() ([]byte, error) {
var transportType string
var transportAddress string
var transportUsername string
var transportPassword string
var transportOptionType spb.Type
// Handling of these cases is necessary as we use partial network elements for testing.
// eg. in most tests no transport or sbi is defined.
// The marshaller will crash if we want to access a nil field.
if n.transport == nil || n.transportOptions == nil {
transportType = "testing"
transportAddress = "testing"
transportUsername = "testing"
transportPassword = "testing"
transportOptionType = spb.Type_TYPE_OPENCONFIG
} else {
transportType = n.transport.Type()
transportAddress = n.transportOptions.Address
transportUsername = n.transportOptions.Username
transportPassword = n.transportOptions.Password
transportOptionType = n.transportOptions.Type
}
var sbiUUID uuid.UUID
if n.sbi == nil {
sbiUUID = uuid.UUID{}
} else {
sbiUUID = n.sbi.ID()
}
modelAsString, err := ygot.EmitJSON(n.Model, n.getYgotEmitJSONConfig())
if err != nil {
return []byte{}, err
}
return json.Marshal(&struct {
ID uuid.UUID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
TransportType string `json:"transport_type,omitempty"`
TransportAddress string `json:"transport_address,omitempty"`
TransportUsername string `json:"transport_username,omitempty"`
TransportPassword string `json:"transport_password,omitempty"`
TransportOptionType spb.Type `json:"transport_option"`
SBI uuid.UUID `json:"sbi,omitempty"`
Model string `bson:"model,omitempty"`
}{
ID: n.ID(),
Name: n.Name(),
TransportType: transportType,
TransportAddress: transportAddress,
TransportUsername: transportUsername,
TransportPassword: transportPassword,
TransportOptionType: transportOptionType,
SBI: sbiUUID,
Model: modelAsString,
})
}
// MarshalBSON implements the MarshalBSON interface to store a network element as BSON.
func (n *CommonNetworkElement) MarshalBSON() ([]byte, error) {
var transportType string
var transportAddress string
var transportUsername string
var transportPassword string
var transportOptionType spb.Type
// Handling of these cases is necessary as we use partial network elements for testing.
// eg. in most tests no transport or sbi is defined.
// The marshaller will crash if we want to access a nil field.
if n.transport == nil || n.transportOptions == nil {
transportType = "testing"
transportAddress = "testing"
transportUsername = "testing"
transportPassword = "testing"
transportOptionType = spb.Type_TYPE_OPENCONFIG
} else {
transportType = n.transport.Type()
transportAddress = n.transportOptions.Address
transportUsername = n.transportOptions.Username
transportPassword = n.transportOptions.Password
transportOptionType = n.transportOptions.Type
}
modelAsString, err := ygot.EmitJSON(n.Model, n.getYgotEmitJSONConfig())
if err != nil {
return []byte{}, err
}
return bson.Marshal(&struct {
ID string `bson:"_id,omitempty"`
Name string `bson:"name,omitempty"`
TransportType string `bson:"transport_type,omitempty"`
TransportAddress string `bson:"transport_address,omitempty"`
TransportUsername string `bson:"transport_username,omitempty"`
TransportPassword string `bson:"transport_password,omitempty"`
TransportOptionType spb.Type `bson:"transport_option"`
SBI string `bson:"sbi,omitempty"`
Model string `bson:"model,omitempty"`
}{
ID: n.ID().String(),
Name: n.Name(),
TransportType: transportType,
TransportAddress: transportAddress,
TransportUsername: transportUsername,
TransportPassword: transportPassword,
TransportOptionType: transportOptionType,
SBI: n.sbi.ID().String(),
Model: modelAsString,
})
}
// GetModelAsString returns the YANG model of a network element as string.
func (n *CommonNetworkElement) GetModelAsString() (string, error) {
modelAsString, err := ygot.EmitJSON(n.Model, n.getYgotEmitJSONConfig())
if err != nil {
return "", err
}
return modelAsString, nil
}
func (n *CommonNetworkElement) getYgotEmitJSONConfig() *ygot.EmitJSONConfig {
return &ygot.EmitJSONConfig{
Format: ygot.RFC7951,
Indent: "",
SkipValidation: true,
RFC7951Config: &ygot.RFC7951JSONConfig{
AppendModuleName: true,
}}
}