Newer
Older
pb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/core"
ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport"
nbi "code.fbi.h-da.de/danet/gosdn/controller/northbound/client"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ygot"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
var dialOptions []grpc.DialOption
func init() {
dialOptions = []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
}
// Init initialises the CLI client.
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)
// }
}
// GetIds requests all UUID information from the controller
func GetIds(addr string) ([]*ppb.PrincipalNetworkDomain, error) {
ctx := context.Background()
resp, err := GetAllCore(ctx, addr)
if err != nil {
return nil, err
}
return resp.Pnd, nil
}
func GetAllCore(ctx context.Context, addr string) (*pb.GetPndListResponse, error) {
coreClient, err := nbi.CoreClient(addr, dialOptions...)
if err != nil {
return nil, err
}
}
// AddPnd takes a name, description and SBI UUID to create a new
// PrincipalNetworkDomain on the controller
func AddPnd(addr, name, description, sbi string) (*pb.CreatePndListResponse, error) {
coreClient, err := nbi.CoreClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
{
Name: name,
Description: description,
Sbi: sbi,
},
},
}
// GetPnd requests one PrincipalNetworkDomain from the
// controller.
func GetPnd(addr string, args ...string) (*pb.GetPndResponse, error) {
coreClient, err := nbi.CoreClient(addr, dialOptions...)
if err != nil {
return nil, err
}
if len(args) <= 0 {
return nil, errors.New("not enough arguments")
}
ctx := context.Background()
Timestamp: time.Now().UnixNano(),
Pid: args,
}
return coreClient.GetPnd(ctx, req)
}
// GetPnds requests all PrincipalNetworkDomains from the
// controller.
func GetPnds(addr string, args ...string) (*pb.GetPndListResponse, error) {
coreClient, err := nbi.CoreClient(addr, dialOptions...)
if err != nil {
return nil, err
}
if len(args) <= 0 {
return nil, errors.New("not enough arguments")
}
ctx := context.Background()
req := &pb.GetPndListRequest{
Timestamp: time.Now().UnixNano(),
}
return coreClient.GetPndList(ctx, req)
// DeletePnd requests a deletion of the provided PND.
func DeletePnd(addr string, pid string) (*pb.DeletePndResponse, error) {
coreClient, err := nbi.CoreClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
req := &pb.DeletePndRequest{
Timestamp: time.Now().UnixNano(),
Pid: pid,
}
return coreClient.DeletePnd(ctx, req)
}
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
// GetSbi requests one or more to the provided PND belonging SBIs from the
// controller.
func GetSbi(addr string, pid string, sid ...string) (*ppb.GetSbiResponse, error) {
client, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
req := &ppb.GetSbiRequest{
Timestamp: time.Now().UnixNano(),
Pid: pid,
Sid: sid,
}
return client.GetSbi(ctx, req)
}
//GetSBIs requests all to the provided PND belonging SBIs from the controller.
func GetSBIs(addr string, pid string) (*ppb.GetSbiListResponse, error) {
client, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
req := &ppb.GetSbiListRequest{
Timestamp: time.Now().UnixNano(),
Pid: pid,
}
return client.GetSbiList(ctx, req)
}
// GetChanges requests all pending and unconfirmed changes from the controller
func GetChanges(addr, pnd string) (*ppb.GetChangeListResponse, error) {
ctx := context.Background()
client, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
// Commit sends a Commit request for one or multiple changes to the
func Commit(addr, pnd string, cuids ...string) (*ppb.SetChangeListResponse, error) {
changes := make([]*ppb.SetChange, len(cuids))
for i, arg := range cuids {
changes[i] = &ppb.SetChange{
Cuid: arg,
return CommitConfirm(addr, pnd, changes)
// Confirm sends a Confirm request for one or multiple changes to the
func Confirm(addr, pnd string, cuids ...string) (*ppb.SetChangeListResponse, error) {
changes := make([]*ppb.SetChange, len(cuids))
for i, arg := range cuids {
changes[i] = &ppb.SetChange{
Cuid: arg,
return CommitConfirm(addr, pnd, changes)
func CommitConfirm(addr, pnd string, changes []*ppb.SetChange) (*ppb.SetChangeListResponse, error) {
ctx := context.Background()
client, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
Timestamp: time.Now().UnixNano(),
Change: changes,
Pid: pnd,
}
// AddDevice adds a new device to the controller. The device name is optional.
// If no name is provided a name will be generated upon device creation.
func AddDevice(addr, deviceName string, opt *tpb.TransportOption, sid, pid uuid.UUID) (*ppb.SetOndListResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
Timestamp: time.Now().UnixNano(),
Ond: []*ppb.SetOnd{
{
case spb.Type_TYPE_CONTAINERISED, spb.Type_TYPE_PLUGIN:
req.Ond[0].Sbi.Type = t
req.Ond[0].TransportOption.Type = t
default:
// GetDevice requests one device belonging to a given
// PrincipalNetworkDomain from the controller. If no device identifier
func GetDevice(addr, pid string, did ...string) (*ppb.GetOndResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
if len(did) == 0 {
// GetSbiSchemaTree gets the sbi tree for a sbi
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
func GetSbiSchemaTree(addr string, pid, sid uuid.UUID) (map[string]*yang.Entry, error) {
sbiClient, err := nbi.SbiClient(addr, dialOptions...)
if err != nil {
return map[string]*yang.Entry{}, err
}
req := &spb.GetSchemaRequest{
Timestamp: time.Now().UnixNano(),
Pid: pid.String(),
Sid: sid.String(),
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
defer cancel()
sClient, err := sbiClient.GetSchema(ctx, req)
if err != nil {
return map[string]*yang.Entry{}, err
}
sTreeBytes := []byte{}
for {
payload, err := sClient.Recv()
if err != nil {
if err == io.EOF {
break
}
log.Error(err)
sClient.CloseSend()
return map[string]*yang.Entry{}, err
}
sTreeBytes = append(sTreeBytes, payload.Chunk...)
}
sTreeMap, err := ygot.GzipToSchema(sTreeBytes)
if err != nil {
return map[string]*yang.Entry{}, err
}
return sTreeMap, nil
}
// GetDevices requests all devices belonging to a given
func GetDevices(addr, pid string) (*ppb.GetOndListResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
Pid: pid,
}
ctx := context.Background()
return pndClient.GetOndList(ctx, req)
}
func GetPath(addr, pid, did, path string) (*ppb.GetPathResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
req := &ppb.GetPathRequest{
Timestamp: time.Now().UnixNano(),
Did: did,
Pid: pid,
Path: path,
func DeleteDevice(addr, pid, did string) (*ppb.DeleteOndResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
// ChangeRequest change creates a ChangeRequest for the specified OND. ApiOperations are
// used to specify the type of the change (update, replace, delete as specified
// in https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-specification.md#34-modifying-state)
// For delete operations the value field needs to contain an empty string.
func ChangeRequest(addr, did, pid, path, value string, op ppb.ApiOperation) (*ppb.SetPathListResponse, error) {
return SendChangeRequest(addr, pid, req)
func SendChangeRequest(addr, pid string, req *ppb.ChangeRequest) (*ppb.SetPathListResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
Timestamp: time.Now().UnixNano(),
ChangeRequest: []*ppb.ChangeRequest{req},
Pid: pid,
}
// Login logs a user in
func Login(addr, username, pwd string) (*apb.LoginResponse, error) {
authClient, err := nbi.AuthClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
r := &apb.LoginRequest{
Timestamp: time.Now().UnixNano(),
Username: username,
Pwd: pwd,
}
return authClient.Login(ctx, r)
}
// Logout logs a user out
func Logout(addr, username string) (*apb.LogoutResponse, error) {
authClient, err := nbi.AuthClient(addr, dialOptions...)
if err != nil {
return nil, err
}
ctx := context.Background()
r := &apb.LogoutRequest{
Timestamp: time.Now().UnixNano(),
Username: username,
}
return authClient.Logout(ctx, r)
}