diff --git a/cli/grpc.go b/cli/grpc.go index bac5efa61752eef7bbbdda52a5454a9ef1297ae4..7962269f749d5bd1f64cda1378e00b0a7b3a267b 100644 --- a/cli/grpc.go +++ b/cli/grpc.go @@ -2,6 +2,7 @@ package cli import ( "context" + "errors" "time" ppb "code.fbi.h-da.de/cocsn/api/go/gosdn/pnd" @@ -18,6 +19,7 @@ import ( // TODO: Delete once proper certs are set up var grpcWithInsecure = grpc.WithInsecure() +// Init initialises the CLI client. func Init(addr string) error { ctx := context.Background() resp, err := getAllCore(ctx, addr) @@ -37,6 +39,7 @@ func Init(addr string) error { return nil } +// GetIds requests all UUID information from the controller func GetIds(addr string) ([]*ppb.PrincipalNetworkDomain, error) { ctx := context.Background() resp, err := getAllCore(ctx, addr) @@ -58,6 +61,8 @@ func getAllCore(ctx context.Context, addr string) (*pb.GetResponse, error) { return coreClient.Get(ctx, req) } +// AddPnd takes a name, description and SBI UUID to create a new +// PrincipalNetworkDomain on the controller func AddPnd(addr, name, description, sbi string) error { coreClient, err := nbi.CoreClient(addr, grpcWithInsecure) if err != nil { @@ -83,20 +88,26 @@ func AddPnd(addr, name, description, sbi string) error { return nil } +// GetPnd requests one or several PrincipalNetworkDomains from the +// controller. To request all PrincipalNetworkDomains without providing +// names or UUIDs use GetIds() func GetPnd(addr string, args ...string) (*pb.GetResponse, error) { coreClient, err := nbi.CoreClient(addr, grpcWithInsecure) if err != nil { return nil, err } + if len(args) <= 0 { + return nil, errors.New("not enough arguments") + } ctx := context.Background() req := &pb.GetRequest{ Timestamp: time.Now().UnixNano(), - All: false, Pid: args, } return coreClient.Get(ctx, req) } +// GetChanges requests all pending and unconfirmed changes from the controller func GetChanges(addr, pnd string) (*ppb.GetResponse, error) { ctx := context.Background() client, err := nbi.PndClient(addr, grpcWithInsecure) @@ -115,6 +126,8 @@ func GetChanges(addr, pnd string) (*ppb.GetResponse, error) { return client.Get(ctx, req) } +// Commit sends a commit request for one or multiple changes to the +// controller. func Commit(addr, pnd string, cuids ...string) (*ppb.SetResponse, error) { changes := make([]*ppb.SetChange, len(cuids)) for i, arg := range cuids { @@ -126,6 +139,8 @@ func Commit(addr, pnd string, cuids ...string) (*ppb.SetResponse, error) { return commitConfirm(addr, pnd, changes) } +// Confirm sends a confirm request for one or multiple changes to the +// controller func Confirm(addr, pnd string, cuids ...string) (*ppb.SetResponse, error) { changes := make([]*ppb.SetChange, len(cuids)) for i, arg := range cuids { @@ -151,6 +166,8 @@ func commitConfirm(addr, pnd string, changes []*ppb.SetChange) (*ppb.SetResponse return client.Set(ctx, req) } +// 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, username, password, sbi, pnd, deviceAddress, deviceName string) error { pndClient, err := nbi.PndClient(addr, grpcWithInsecure) if err != nil { @@ -184,6 +201,9 @@ func AddDevice(addr, username, password, sbi, pnd, deviceAddress, deviceName str return nil } +// GetDevice requests one or multiple devices belongin to a given +// PrincipalNetworkDomain from the controller. If no device identifier +// is provided, all devices are requested. func GetDevice(addr, pid, path string, did ...string) (*ppb.GetResponse, error) { pndClient, err := nbi.PndClient(addr, grpcWithInsecure) if err != nil { @@ -209,6 +229,8 @@ func GetDevice(addr, pid, path string, did ...string) (*ppb.GetResponse, error) return pndClient.Get(ctx, req) } +// Update creates a ChangeRequest to update the given path with the given value +// at the given OND on the controller. func Update(addr, did, pid, path, value string) (*ppb.SetResponse, error) { req := &ppb.ChangeRequest{ Id: did, @@ -219,6 +241,8 @@ func Update(addr, did, pid, path, value string) (*ppb.SetResponse, error) { return sendChangeRequest(addr, pid, req) } +// Replace creates a ChangeRequest to replace the given path with the given value +// at the given OND on the controller. func Replace(addr, did, pid, path, value string) (*ppb.SetResponse, error) { req := &ppb.ChangeRequest{ Id: did, @@ -229,6 +253,8 @@ func Replace(addr, did, pid, path, value string) (*ppb.SetResponse, error) { return sendChangeRequest(addr, pid, req) } +// Delete creates a ChangeRequest to delete the given path node +// at the given OND on the controller. func Delete(addr, did, pid, path string) (*ppb.SetResponse, error) { req := &ppb.ChangeRequest{ Id: did, diff --git a/cmd/pnd.go b/cmd/pnd.go index 4a3cdd32d711be89e9540f79e72a336d768a63c2..a17c4d2896fe4bf1a75057c1730859747d70a5ee 100644 --- a/cmd/pnd.go +++ b/cmd/pnd.go @@ -28,6 +28,7 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + package cmd import ( diff --git a/northbound/client/core.go b/northbound/client/core.go index cfe4ac9e55d65ee4dcc58ae0f0c6bea837f0164b..2e5278923214769c5c5b4adf43dfdf07f50a251d 100644 --- a/northbound/client/core.go +++ b/northbound/client/core.go @@ -5,6 +5,9 @@ import ( "google.golang.org/grpc" ) +// CoreClient returns a client for the gRPC Core service. It takes +// the address of the gRPC endpoint and optional grpc.DialOption +// as argument func CoreClient(addr string, opts ...grpc.DialOption) (pb.CoreClient, error) { conn, err := grpc.Dial(addr, opts...) if err != nil { diff --git a/northbound/client/pnd.go b/northbound/client/pnd.go index 3ab659c823e8f1e6d05a61aaf867630ecd37419e..f5ca9101611758b7c30704f501aa1ed84aeaa9ac 100644 --- a/northbound/client/pnd.go +++ b/northbound/client/pnd.go @@ -5,6 +5,9 @@ import ( "google.golang.org/grpc" ) +// PndClient returns a client for the gRPC PND service. It takes +// the address of the gRPC endpoint and optional grpc.DialOption +// as argument func PndClient(addr string, opts ...grpc.DialOption) (ppb.PndClient, error) { conn, err := grpc.Dial(addr, opts...) if err != nil { diff --git a/northbound/server/nbi.go b/northbound/server/nbi.go index a24e94b18e138a0bb4fd11820ce2e9e93e27a0de..d5f69ce38d9a8f579203c7fee864da918916ee27 100644 --- a/northbound/server/nbi.go +++ b/northbound/server/nbi.go @@ -6,6 +6,7 @@ import ( var pndc *nucleus.PndStore +// NewNBI receives a PndStore and returns a nre gRPC *NorthboundInterface func NewNBI(pnds *nucleus.PndStore) *NorthboundInterface { pndc = pnds return &NorthboundInterface{ @@ -14,6 +15,8 @@ func NewNBI(pnds *nucleus.PndStore) *NorthboundInterface { } } +// NorthboundInterface is the representation of the +// gRPC services used provided. type NorthboundInterface struct { Pnd *pnd Core *core diff --git a/nucleus/change.go b/nucleus/change.go index c9f0c41e7417f7efc281d7f6fb3e8fcd89b8fcdb..b1a3242717c4736289dcb8ec78e6c9453bf06f4a 100644 --- a/nucleus/change.go +++ b/nucleus/change.go @@ -134,10 +134,12 @@ func (c *Change) Confirm() error { return nil } +// Age returns the passed time since the Change was created func (c *Change) Age() time.Duration { - return time.Now().Sub(c.timestamp) + return time.Since(c.timestamp) } +// State returns the changes's state. func (c *Change) State() ppb.Change_State { if !c.committed { return ppb.Change_PENDING diff --git a/nucleus/southbound.go b/nucleus/southbound.go index 34d3a0960a059b710dfab7be738ac48d452af23f..11d63ddb6e537c78d6e0d9ecb29652efd53df77f 100644 --- a/nucleus/southbound.go +++ b/nucleus/southbound.go @@ -163,4 +163,5 @@ func (oc *OpenConfig) ID() uuid.UUID { return oc.id } +// Type returns the Southbound's type func (oc *OpenConfig) Type() spb.Type { return spb.Type_OPENCONFIG }