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
package cli
import (
ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd"
nbi "code.fbi.h-da.de/cocsn/gosdn/northbound/client"
"context"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"time"
pb "code.fbi.h-da.de/cocsn/api/go/gosdn/core"
"google.golang.org/grpc"
)
// TODO: Delete once proper certs are set up
var grpcWithInsecure = grpc.WithInsecure()
func Init(addr string) error {
ctx := context.Background()
resp, err := getAllCore(ctx, addr)
if err != nil {
return err
}
if len(resp.Pnd) > 0 {
pid := resp.Pnd[0].Id
viper.Set("CLI_PND", pid)
log.Infof("PND: %v", pid)
if len(resp.Pnd[0].Sbi) != 0 {
sbi := resp.Pnd[0].Sbi[0].Id
viper.Set("CLI_SBI", sbi)
log.Infof("SBI: %v", sbi)
}
}
return nil
}
func GetIds(addr string) error {
ctx := context.Background()
resp, err := getAllCore(ctx, addr)
if err != nil {
return err
}
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
log.Infof("\tSBI %v: %v", j+1, sbi.Id)
}
}
return nil
}
func getAllCore(ctx context.Context, addr string) (*pb.GetResponse, error) {
coreClient, err := nbi.CoreClient(addr, grpcWithInsecure)
if err != nil {
return nil, err
}
req := &pb.GetRequest{
Timestamp: time.Now().UnixNano(),
All: true,
}
return coreClient.Get(ctx, req)
}
func AddDevice(addr, username, password, sbi, pnd, deviceAddress string) error {
pndClient, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return err
}
req := &ppb.SetRequest{
Timestamp: time.Now().UnixNano(),
Ond: []*ppb.SetOnd{
{
Address: deviceAddress,
Username: username,
Password: password,
Sbi: &ppb.SouthboundInterface{
Type: 0,
},
},
},
}
ctx := context.Background()
resp, err := pndClient.Set(ctx, req)
if err != nil {
return err
}
log.Info(resp.String())
return nil
}
func GetDevice(addr, pid, path string, did ...string) error {
pndClient, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return err
}
var all bool
if len(did) != 0 {
all = true
}
req := &ppb.GetRequest{
Timestamp: time.Now().UnixNano(),
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
}
ctx := context.Background()
resp, err := pndClient.Get(ctx, req)
if err != nil {
return err
}
log.Info(resp.String())
return nil
}
func Update(addr, did, pid, path, value string) error {
req := &ppb.ChangeRequest{
Id: did,
Path: path,
Value: value,
ApiOp: ppb.ChangeRequest_UPDATE,
}
return sendChangeRequest(addr, pid, req)
}
func Replace(addr, did, pid, path, value string) error {
req := &ppb.ChangeRequest{
Id: did,
Path: path,
Value: value,
ApiOp: ppb.ChangeRequest_REPLACE,
}
return sendChangeRequest(addr, pid, req)
}
func Delete(addr, did, pid, path string) error {
req := &ppb.ChangeRequest{
Id: did,
Path: path,
ApiOp: ppb.ChangeRequest_DELETE,
}
return sendChangeRequest(addr, pid, req)
}
func sendChangeRequest(addr, pid string, req *ppb.ChangeRequest) error {
pndClient, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return err
}
ctx := context.Background()
r := &ppb.SetRequest{
Timestamp: time.Now().UnixNano(),
ChangeRequest: []*ppb.ChangeRequest{req},
}
resp, err := pndClient.Set(ctx, r)
if err != nil {
return err
}
log.Info(resp.String())
return nil