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
package nucleus
import (
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/southbound"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/database"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
"code.fbi.h-da.de/danet/gosdn/controller/store"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
// DatabaseSbiStore is used to store SouthboundInterfaces
type DatabaseSbiStore struct {
sbiStoreName string
}
// Add adds a SBI.
func (s *DatabaseSbiStore) Add(item southbound.SouthboundInterface) error {
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database(database.DatabaseName).
Collection(s.sbiStoreName).
InsertOne(ctx, item)
if err != nil {
if mongo.IsDuplicateKeyError(err) {
return nil
}
return errors.ErrCouldNotCreate{StoreName: sbiStoreName}
}
return nil
}
// Delete deletes an SBI.
func (s *DatabaseSbiStore) Delete(item southbound.SouthboundInterface) error {
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
_, err := client.Database(database.DatabaseName).
Collection(s.sbiStoreName).
DeleteOne(ctx, bson.D{primitive.E{Key: "_id", Value: item.ID().String()}})
if err != nil {
return errors.ErrCouldNotCreate{StoreName: sbiStoreName}
}
return nil
}
// Get takes a SouthboundInterface's UUID or name and returns the SouthboundInterface. If the requested
// SouthboundInterface does not exist an error is returned.
func (s *DatabaseSbiStore) Get(query store.Query) (southbound.SouthboundInterface, error) {
var loadedSbi *LoadedSbi
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
log.Debugf("SBI-Search-ID: %+v\n", query.ID.String())
db := client.Database(database.DatabaseName)
collection := db.Collection(s.sbiStoreName)
result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: query.ID.String()}})
if result == nil {
return nil, nil
}
err := result.Decode(&loadedSbi)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, errors.ErrCouldNotMarshall{StoreName: sbiStoreName}
}
newSbi, _ := NewSBI(loadedSbi.Type, uuid.MustParse(loadedSbi.ID))
return newSbi, nil
}
// GetAll returns all SBIs
func (s *DatabaseSbiStore) GetAll() ([]southbound.SouthboundInterface, error) {
var loadedSbis []LoadedSbi
var sbis []southbound.SouthboundInterface
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.sbiStoreName)
cursor, err := collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
err = cursor.All(ctx, &loadedSbis)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, errors.ErrCouldNotMarshall{StoreName: sbiStoreName}
}
for _, sbi := range loadedSbis {
newSbi, _ := NewSBI(sbi.Type, uuid.MustParse(sbi.ID))
sbis = append(sbis, newSbi)
}
return sbis, nil
}
// GetSBI takes a SouthboundInterface's UUID or name and returns the SouthboundInterface. If the requested
// SouthboundInterface does not exist an error is returned.
func (s *DatabaseSbiStore) GetSBI(id uuid.UUID) (southbound.SouthboundInterface, error) {
var loadedSbi LoadedSbi
client, ctx, cancel := database.GetMongoConnection()
defer cancel()
defer client.Disconnect(ctx)
db := client.Database(database.DatabaseName)
collection := db.Collection(s.sbiStoreName)
result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: id.String()}})
if result == nil {
return nil, nil
}
err := result.Decode(&loadedSbi)
if err != nil {
log.Printf("Failed marshalling %v", err)
return nil, errors.ErrCouldNotMarshall{StoreName: sbiStoreName}
}
newSbi, err := NewSBI(loadedSbi.Type, uuid.MustParse(loadedSbi.ID))
return newSbi, nil
}
func getSbiTypeFromString(sbiTypeAsString string) spb.Type {
sbiTypeInt := spb.Type_value[sbiTypeAsString]
return spb.Type(sbiTypeInt)
}