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
package nucleus
import (
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/device"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkdomain"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/database"
errors "code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
pndStoreName = "pnd"
)
// LoadedPnd represents a Principal Network Domain that was loaeded by using
// the Load() method of the PndStore.
type LoadedPnd struct {
ID string `json:"id" bson:"_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
// PndStore is used to store PrincipalNetworkDomains
type PndStore struct {
pndStoreName string
pendingChannels map[uuid.UUID]chan device.Details
}
// NewPndStore returns a PndStore
func NewPndStore() networkdomain.PndStore {
storeMode := store.GetStoreMode()
log.Debugf("StoreMode: %s", storeMode)
switch storeMode {
case store.Filesystem:
store := NewMemoryPndStore()
return &store
case store.Database:
return &PndStore{
pendingChannels: make(map[uuid.UUID]chan device.Details),
pndStoreName: "pnd-store.json"}
case store.Memory:
store := NewMemoryPndStore()
return &store
default:
return nil
}
}
// Get takes a PrincipalNetworkDomain's UUID or name and returns the PrincipalNetworkDomain. If the requested
// PrincipalNetworkDomain does not exist an error is returned.
func (s *PndStore) Get(query store.Query) (networkdomain.NetworkDomain, error) {
var loadedPND LoadedPnd
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.pndStoreName)
result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: query.ID.String()}})
if result == nil {
return nil, nil
}
err := result.Decode(&loadedPND)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, errors.ErrCouldNotMarshall{StoreName: pndStoreName}
}
newPnd, err := NewPND(
loadedPND.Name,
loadedPND.Description,
uuid.MustParse(loadedPND.ID),
nil,
s.callback,
)
if err != nil {
return nil, err
}
return newPnd, nil
}
// GetAll returns all stored pnds.
func (s *PndStore) GetAll() ([]networkdomain.NetworkDomain, error) {
var loadedPnds []LoadedPnd
var pnds []networkdomain.NetworkDomain
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.pndStoreName)
cursor, err := collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
err = cursor.All(ctx, &loadedPnds)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, errors.ErrCouldNotMarshall{StoreName: pndStoreName}
}
for _, loadedPND := range loadedPnds {
newPnd, err := NewPND(
loadedPND.Name,
loadedPND.Description,
uuid.MustParse(loadedPND.ID),
nil,
nil,
)
if err != nil {
return nil, err
}
pnds = append(pnds, newPnd)
}
return pnds, nil
}
// Add adds a pnd to the pnd store.
func (s *PndStore) Add(pnd networkdomain.NetworkDomain) error {
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database(database.DatabaseName).
Collection(s.pndStoreName).
InsertOne(ctx, pnd)
if err != nil {
return errors.ErrCouldNotCreate{StoreName: pndStoreName}
}
return nil
}
// Delete deletes a pnd.
// It also deletes all assosicated devices and sbis.
func (s *PndStore) Delete(pnd networkdomain.NetworkDomain) error {
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.pndStoreName)
_, err := collection.DeleteOne(ctx, bson.D{primitive.E{Key: pnd.ID().String()}})
if err != nil {
return err
}
// TODO: Delete all assosicated devices + SBIs
return nil
}
// PendingChannels holds channels used communicate with pending
// cSBI deployments
func (s *PndStore) PendingChannels(id uuid.UUID, parseErrors ...error) (chan device.Details, error) {
ch, ok := s.pendingChannels[id]
if !ok {
return nil, &errors.ErrNotFound{ID: id}
}
return ch, nil
}
// AddPendingChannel adds a pending channel to the map
func (s *PndStore) AddPendingChannel(id uuid.UUID, ch chan device.Details) {
s.pendingChannels[id] = ch
}
// RemovePendingChannel removes a pending channel from the map
func (s *PndStore) RemovePendingChannel(id uuid.UUID) {
delete(s.pendingChannels, id)
}
func (s *PndStore) callback(id uuid.UUID, ch chan device.Details) {
if ch != nil {
s.AddPendingChannel(id, ch)
log.Infof("pending channel %v added", id)
} else {
s.RemovePendingChannel(id)
log.Infof("pending channel %v removed", id)
}
}