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
package server
import (
"context"
"time"
dpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/device"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/networkdomain"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// DeviceServer represents a deviceServer.
type DeviceServer struct {
dpb.UnimplementedDeviceServiceServer
networkDomain networkdomain.NetworkDomain
}
// NewDeviceServer returns a new DeviceServer.
func NewDeviceServer(networkDomain networkdomain.NetworkDomain) *DeviceServer {
return &DeviceServer{
networkDomain: networkDomain,
}
}
// Add adds a new device.
func (d *DeviceServer) Add(ctx context.Context, request *dpb.AddDeviceRequest) (*dpb.AddDeviceResponse, error) {
sbiID, err := uuid.Parse(request.Device.Sbi.Id)
if err != nil {
return nil, status.Errorf(codes.Aborted, "%v", err)
}
id, err := d.networkDomain.AddDevice(
request.Device.DeviceName,
request.Device.TransportOption,
sbiID,
)
if err != nil {
return nil, status.Errorf(codes.Aborted, "%v", err)
}
return &dpb.AddDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
DeviceId: id.String(),
}, nil
}
// GetAll returns all stored devices.
func (d *DeviceServer) GetAll(ctx context.Context, request *dpb.GetAllDeviceRequest) (*dpb.GetAllDeviceResponse, error) {
devices := d.networkDomain.Devices()
onds := []*dpb.Device{}
for _, device := range devices {
ygotStructAsJSON, err := device.GetModelAsString()
if err != nil {
log.Error(err)
return nil, status.Errorf(codes.Aborted, "%v", err)
}
onds = append(onds, &dpb.Device{
Id: device.ID().String(),
Name: device.Name(),
Model: ygotStructAsJSON,
})
}
return &dpb.GetAllDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
Device: onds,
}, nil
}
// Get returns a device.
func (d *DeviceServer) Get(ctx context.Context, request *dpb.GetDeviceRequest) (*dpb.GetDeviceResponse, error) {
device, err := d.networkDomain.GetDevice(request.DeviceID)
if err != nil {
return nil, status.Errorf(codes.Aborted, "%v", err)
}
ygotStructAsJSON, err := device.GetModelAsString()
if err != nil {
log.Error(err)
return nil, status.Errorf(codes.Aborted, "%v", err)
}
ond := &dpb.Device{
Id: device.ID().String(),
Name: device.Name(),
Model: ygotStructAsJSON,
}
return &dpb.GetDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
Device: ond,
}, nil
}
// Update updates a device.
func (d *DeviceServer) Update(ctx context.Context, request *dpb.UpdateDeviceRequest) (*dpb.UpdateDeviceResponse, error) {
deviceID, err := uuid.Parse(request.Device.Id)
if err != nil {
return &dpb.UpdateDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
}, err
}
device, err := d.networkDomain.GetDevice(deviceID.String())
if err != nil {
return &dpb.UpdateDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
}, err
}
err = d.networkDomain.UpdateDevice(device, request.Device.Model)
if err != nil {
return &dpb.UpdateDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
}, err
}
return &dpb.UpdateDeviceResponse{
Timestamp: time.Now().UnixNano(),
Status: dpb.Status_STATUS_OK,
}, nil
}