Skip to content
Snippets Groups Projects

Northbound Interface

Merged Ghost User requested to merge grpc-nbi into develop
14 files
+ 1234
264
Compare changes
  • Side-by-side
  • Inline
Files
14
+ 105
28
@@ -34,19 +34,13 @@ func Init(addr string) error {
return nil
}
func GetIds(addr string) error {
func GetIds(addr string) ([]*ppb.PrincipalNetworkDomain,error) {
ctx := context.Background()
resp, err := getAllCore(ctx, addr)
if err != nil {
return err
}
for i, pnd := range resp.Pnd {
log.Infof("PND %v: %v", i+1, pnd.Id)
for j, sbi := range pnd.Sbi {
log.Infof("\tSBI %v: %v", j+1, sbi.Id)
}
return nil, err
}
return nil
return resp.Pnd, nil
}
func getAllCore(ctx context.Context, addr string) (*pb.GetResponse, error) {
@@ -61,6 +55,99 @@ func getAllCore(ctx context.Context, addr string) (*pb.GetResponse, error) {
return coreClient.Get(ctx, req)
}
func AddPnd(addr, name, description, sbi string) error {
coreClient, err := nbi.CoreClient(addr, grpcWithInsecure)
if err != nil {
return err
}
ctx := context.Background()
req := &pb.SetRequest{
Timestamp: time.Now().UnixNano(),
Pnd: []*pb.SetPnd{
{
Name: name,
Description: description,
Sbi: sbi,
},
},
}
resp, err := coreClient.Set(ctx, req)
if err != nil {
return err
}
log.Info(resp.Status.String())
return nil
}
func GetPnd(addr string, args ...string) (*pb.GetResponse, error) {
coreClient, err := nbi.CoreClient(addr, grpcWithInsecure)
if err != nil {
return nil,err
}
ctx := context.Background()
req := &pb.GetRequest{
Timestamp: time.Now().UnixNano(),
All: false,
Pid: args,
}
return coreClient.Get(ctx, req)
}
func GetChanges(addr, pnd string) (*ppb.GetResponse, error) {
ctx := context.Background()
client, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return nil, err
}
req := &ppb.GetRequest{
Timestamp: time.Now().UnixNano(),
Request: &ppb.GetRequest_Change{
Change: &ppb.GetChange{
All: true,
},
},
Pid: pnd,
}
return client.Get(ctx, req)
}
func Commit(addr, pnd string, cuids ...string) (*ppb.SetResponse,error) {
changes := make([]*ppb.SetChange, len(cuids))
for i, arg := range cuids {
changes[i] = &ppb.SetChange{
Cuid: arg,
Op: ppb.SetChange_COMMIT,
}
}
return commitConfirm(addr, pnd, changes)
}
func Confirm(addr, pnd string, cuids ...string) (*ppb.SetResponse,error) {
changes := make([]*ppb.SetChange, len(cuids))
for i, arg := range cuids {
changes[i] = &ppb.SetChange{
Cuid: arg,
Op: ppb.SetChange_CONFIRM,
}
}
return commitConfirm(addr, pnd, changes)
}
func commitConfirm(addr, pnd string, changes []*ppb.SetChange) (*ppb.SetResponse,error) {
ctx := context.Background()
client, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return nil, err
}
req := &ppb.SetRequest{
Timestamp: time.Now().UnixNano(),
Change: changes,
Pid: pnd,
}
return client.Set(ctx, req)
}
func AddDevice(addr, username, password, sbi, pnd, deviceAddress string) error {
pndClient, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
@@ -90,10 +177,10 @@ func AddDevice(addr, username, password, sbi, pnd, deviceAddress string) error {
return nil
}
func GetDevice(addr, pid, path string, did ...string) error {
func GetDevice(addr, pid, path string, did ...string) (*ppb.GetResponse,error) {
pndClient, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return err
return nil, err
}
var all bool
@@ -112,15 +199,10 @@ func GetDevice(addr, pid, path string, did ...string) error {
Pid: pid,
}
ctx := context.Background()
resp, err := pndClient.Get(ctx, req)
if err != nil {
return err
}
log.Info(resp.String())
return nil
return pndClient.Get(ctx, req)
}
func Update(addr, did, pid, path, value string) error {
func Update(addr, did, pid, path, value string) (*ppb.SetResponse, error) {
req := &ppb.ChangeRequest{
Id: did,
Path: path,
@@ -130,7 +212,7 @@ func Update(addr, did, pid, path, value string) error {
return sendChangeRequest(addr, pid, req)
}
func Replace(addr, did, pid, path, value string) error {
func Replace(addr, did, pid, path, value string) (*ppb.SetResponse, error) {
req := &ppb.ChangeRequest{
Id: did,
Path: path,
@@ -140,7 +222,7 @@ func Replace(addr, did, pid, path, value string) error {
return sendChangeRequest(addr, pid, req)
}
func Delete(addr, did, pid, path string) error {
func Delete(addr, did, pid, path string) (*ppb.SetResponse, error) {
req := &ppb.ChangeRequest{
Id: did,
Path: path,
@@ -149,10 +231,10 @@ func Delete(addr, did, pid, path string) error {
return sendChangeRequest(addr, pid, req)
}
func sendChangeRequest(addr, pid string, req *ppb.ChangeRequest) error {
func sendChangeRequest(addr, pid string, req *ppb.ChangeRequest) (*ppb.SetResponse, error) {
pndClient, err := nbi.PndClient(addr, grpcWithInsecure)
if err != nil {
return err
return nil, err
}
ctx := context.Background()
r := &ppb.SetRequest{
@@ -160,10 +242,5 @@ func sendChangeRequest(addr, pid string, req *ppb.ChangeRequest) error {
ChangeRequest: []*ppb.ChangeRequest{req},
Pid: pid,
}
resp, err := pndClient.Set(ctx, r)
if err != nil {
return err
return pndClient.Set(ctx, r)
}
log.Info(resp.String())
return nil
}
Loading