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
package nucleus
import (
"encoding/json"
"io/ioutil"
"sync"
cpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/csbi"
"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/errors"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/filesystem"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// FilesystemPndStore provides a filesystem implementation for a pnd store.
type FilesystemPndStore struct {
pendingChannels map[uuid.UUID]chan device.Details
csbiClient cpb.CsbiServiceClient
pathToPndFile string
fileMutex sync.Mutex
}
// NewFilesystemPndStore returns a filesystem implementation for a pnd store.
func NewFilesystemPndStore() FilesystemPndStore {
store.EnsureFilesystemStorePathExists(filesystem.PndFilename)
return FilesystemPndStore{
pendingChannels: make(map[uuid.UUID]chan device.Details),
pathToPndFile: store.GetCompletePathToFileStore(filesystem.PndFilename),
fileMutex: sync.Mutex{},
}
}
func (t *FilesystemPndStore) readAllPndsFromFile() ([]networkdomain.NetworkDomain, error) {
var loadedPnds []LoadedPnd
content, err := ioutil.ReadFile(t.pathToPndFile)
if err != nil {
return nil, err
}
err = json.Unmarshal(content, &loadedPnds)
if err != nil {
return nil, err
}
pnds := make([]networkdomain.NetworkDomain, len(loadedPnds))
csbiClient, err := t.getCsbiClient()
if err != nil {
return nil, err
}
for i, loadedPND := range loadedPnds {
newPnd, err := NewPND(
loadedPND.Name,
loadedPND.Description,
uuid.MustParse(loadedPND.ID),
csbiClient,
t.callback,
)
if err != nil {
return nil, err
}
pnds[i] = newPnd
}
return pnds, nil
}
func (t *FilesystemPndStore) writeAllPndsToFile(pnds []networkdomain.NetworkDomain) error {
serializedData, err := json.Marshal(pnds)
if err != nil {
return err
}
err = ioutil.WriteFile(t.pathToPndFile, serializedData, 0600)
if err != nil {
return err
}
return nil
}
// Add a pnd to the store.
func (t *FilesystemPndStore) Add(item networkdomain.NetworkDomain) error {
t.fileMutex.Lock()
defer t.fileMutex.Unlock()
pnds, err := t.readAllPndsFromFile()
if err != nil {
return err
}
pnds = append(pnds, item)
err = t.writeAllPndsToFile(pnds)
if err != nil {
return err
}
return nil
}
// Delete deletes a pnd from the store.
func (t *FilesystemPndStore) Delete(item networkdomain.NetworkDomain) error {
t.fileMutex.Lock()
defer t.fileMutex.Unlock()
pnds, err := t.readAllPndsFromFile()
if err != nil {
return err
}
for i, pnd := range pnds {
if pnd.ID() == item.ID() {
//remove item from slice
pnds[i] = pnds[len(pnds)-1]
pnds = pnds[:len(pnds)-1]
err = t.writeAllPndsToFile(pnds)
if err != nil {
return err
}
return nil
}
}
return &errors.ErrNotFound{ID: item.ID, Name: item.GetName()}
}
// Get provides a the query interface to find a stored pnd.
func (t *FilesystemPndStore) Get(query store.Query) (networkdomain.NetworkDomain, error) {
t.fileMutex.Lock()
defer t.fileMutex.Unlock()
pnds, err := t.readAllPndsFromFile()
if err != nil {
return nil, err
}
for _, pnd := range pnds {
if pnd.ID() == query.ID || pnd.GetName() == query.Name {
return pnd, nil
}
}
return nil, &errors.ErrNotFound{ID: query.ID, Name: query.Name}
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
}
// GetAll returns all pnds currently on the store.
func (t *FilesystemPndStore) GetAll() ([]networkdomain.NetworkDomain, error) {
t.fileMutex.Lock()
defer t.fileMutex.Unlock()
pnds, err := t.readAllPndsFromFile()
return pnds, err
}
// PendingChannels holds channels used communicate with pending
// cSBI deployments
func (t *FilesystemPndStore) PendingChannels(id uuid.UUID, parseErrors ...error) (chan device.Details, error) {
ch, ok := t.pendingChannels[id]
if !ok {
return nil, &errors.ErrNotFound{ID: id}
}
return ch, nil
}
// AddPendingChannel adds a pending channel to the map
func (t *FilesystemPndStore) AddPendingChannel(id uuid.UUID, ch chan device.Details) {
t.pendingChannels[id] = ch
}
// RemovePendingChannel removes a pending channel from the map
func (t *FilesystemPndStore) RemovePendingChannel(id uuid.UUID) {
delete(t.pendingChannels, id)
}
func (t *FilesystemPndStore) callback(id uuid.UUID, ch chan device.Details) {
if ch != nil {
t.AddPendingChannel(id, ch)
log.Infof("pending channel %v added", id)
} else {
t.RemovePendingChannel(id)
log.Infof("pending channel %v removed", id)
}
}
func (t *FilesystemPndStore) getCsbiClient() (cpb.CsbiServiceClient, error) {
if t.csbiClient == nil {
orchestrator := viper.GetString("csbi-orchestrator")
conn, err := grpc.Dial(orchestrator, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
t.csbiClient = cpb.NewCsbiServiceClient(conn)
}
return t.csbiClient, nil
}