Skip to content
Snippets Groups Projects
Commit 7c2555fe authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

linter pleasing

parent f9b0f9ba
Branches
No related tags found
9 merge requests!246Develop,!245Develop into Master,!244Master into develop2 into master,!219Draft: Testing,!214Test pipelines,!195DO NOT MERGE 2,!194DO NOT MERGE! just for testing,!147Commit-Confirm Mechanic for PND,!138Develop
This commit is part of merge request !138. Comments created here will be created in the context of that merge request.
...@@ -23,14 +23,15 @@ const ( ...@@ -23,14 +23,15 @@ const (
// CtxKeyOpts context key for gnmi.SubscribeOptions // CtxKeyOpts context key for gnmi.SubscribeOptions
CtxKeyOpts CtxKeyType = "opts" CtxKeyOpts CtxKeyType = "opts"
// CtxKeyConfig is a context key for gnmi.Config // CtxKeyConfig is a context key for gnmi.Config
CtxKeyConfig = "config" CtxKeyConfig = "config"
// CtxKeyOperation is a context key for a gNMI operation (update, replace, delete)
CtxKeyOperation = "op" CtxKeyOperation = "op"
) )
var opmap = map[Operation]string{ var opmap = map[Operation]string{
TRANSPORT_UPDATE: "update", TransportUpdate: "update",
TRANSPORT_REPLACE: "replace", TransportReplace: "replace",
TRANSPORT_DELETE: "delete", TransportDelete: "delete",
} }
// Gnmi implements the Transport interface and provides an SBI with the // Gnmi implements the Transport interface and provides an SBI with the
...@@ -114,8 +115,8 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error { ...@@ -114,8 +115,8 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error {
} }
} }
opts = append(opts, &gnmi.Operation{ opts = append(opts, &gnmi.Operation{
// Hardcoded TRANSPORT_UPDATE until multiple operations are supported // Hardcoded TransportUpdate until multiple operations are supported
Type: opmap[TRANSPORT_UPDATE], Type: opmap[TransportUpdate],
Origin: "", Origin: "",
Target: "", Target: "",
Path: gnmi.SplitPath(attrs[0]), Path: gnmi.SplitPath(attrs[0]),
...@@ -185,9 +186,9 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload ...interface{}) error { ...@@ -185,9 +186,9 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload ...interface{}) error {
req := &gpb.SetRequest{} req := &gpb.SetRequest{}
if diff.Update != nil { if diff.Update != nil {
switch op { switch op {
case TRANSPORT_UPDATE: case TransportUpdate:
req.Update = diff.Update req.Update = diff.Update
case TRANSPORT_REPLACE: case TransportReplace:
req.Replace = diff.Update req.Replace = diff.Update
default: default:
return &ErrOperationNotSupported{} return &ErrOperationNotSupported{}
......
...@@ -14,9 +14,9 @@ import ( ...@@ -14,9 +14,9 @@ import (
) )
var apiOpmap = map[string]Operation{ var apiOpmap = map[string]Operation{
"update": TRANSPORT_UPDATE, "update": TransportUpdate,
"replace": TRANSPORT_REPLACE, "replace": TransportReplace,
"delete": TRANSPORT_DELETE, "delete": TransportDelete,
} }
func stopHttpServer() error { func stopHttpServer() error {
...@@ -190,7 +190,7 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) { ...@@ -190,7 +190,7 @@ func httpHandler(writer http.ResponseWriter, request *http.Request) {
} }
writer.WriteHeader(http.StatusOK) writer.WriteHeader(http.StatusOK)
case "delete": case "delete":
if err := pnd.ChangeOND(id, TRANSPORT_DELETE, query.Get("path")); err != nil { if err := pnd.ChangeOND(id, TransportDelete, query.Get("path")); err != nil {
handleServerError(writer, err) handleServerError(writer, err)
return return
} }
......
...@@ -30,6 +30,10 @@ func init() { ...@@ -30,6 +30,10 @@ func init() {
log.Debugf("change timeout set to %v", changeTimeout) log.Debugf("change timeout set to %v", changeTimeout)
} }
// NewChange takes a Device UUID, a pair GoStructs (current and intended state)
// a callback function and a channel for errors and returns a *Change
// The callback function is used by the Commit() and Confirm() functions. It
// must define how the change is carried out.
func NewChange(device uuid.UUID, currentState ygot.GoStruct, change ygot.GoStruct, callback func(ygot.GoStruct, ygot.GoStruct) error, errChan chan error) *Change { func NewChange(device uuid.UUID, currentState ygot.GoStruct, change ygot.GoStruct, callback func(ygot.GoStruct, ygot.GoStruct) error, errChan chan error) *Change {
return &Change{ return &Change{
cuid: uuid.New(), cuid: uuid.New(),
...@@ -66,10 +70,14 @@ type Change struct { ...@@ -66,10 +70,14 @@ type Change struct {
Done chan int Done chan int
} }
// ID returns the Change's UUID
func (c *Change) ID() uuid.UUID { func (c *Change) ID() uuid.UUID {
return c.cuid return c.cuid
} }
// Commit pushes the cange to the OND using the callback() function
// and starts the timeout-timer for the Change. If the timer expires
// the change is rolled back.
func (c *Change) Commit() error { func (c *Change) Commit() error {
if err := c.callback(c.intendedState, c.previousState); err != nil { if err := c.callback(c.intendedState, c.previousState); err != nil {
return err return err
...@@ -102,6 +110,7 @@ func (c *Change) rollbackHandler(ctx context.Context) { ...@@ -102,6 +110,7 @@ func (c *Change) rollbackHandler(ctx context.Context) {
} }
} }
// Confirm confirms a committed Change and stops the rollback timer.
func (c *Change) Confirm() error { func (c *Change) Confirm() error {
c.lock.RLock() c.lock.RLock()
if !c.committed { if !c.committed {
......
...@@ -2,7 +2,7 @@ package nucleus ...@@ -2,7 +2,7 @@ package nucleus
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi" "code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
. "code.fbi.h-da.de/cocsn/gosdn/nucleus/pnd" . "code.fbi.h-da.de/cocsn/gosdn/nucleus/pnd" //nolint
"context" "context"
"github.com/openconfig/ygot/ygot" "github.com/openconfig/ygot/ygot"
"github.com/openconfig/ygot/ytypes" "github.com/openconfig/ygot/ytypes"
...@@ -288,12 +288,12 @@ func (pnd *pndImplementation) ChangeOND(uuid uuid.UUID, operation interface{}, p ...@@ -288,12 +288,12 @@ func (pnd *pndImplementation) ChangeOND(uuid uuid.UUID, operation interface{}, p
} }
switch operation { switch operation {
case TRANSPORT_UPDATE, TRANSPORT_REPLACE: case TransportUpdate, TransportReplace:
typedValue := gnmi.TypedValue(value[0]) typedValue := gnmi.TypedValue(value[0])
if err := ytypes.SetNode(d.SBI.Schema().RootSchema(), cpy, p, typedValue); err != nil { if err := ytypes.SetNode(d.SBI.Schema().RootSchema(), cpy, p, typedValue); err != nil {
return err return err
} }
case TRANSPORT_DELETE: case TransportDelete:
if err := ytypes.DeleteNode(d.SBI.Schema().RootSchema(), cpy, p); err != nil { if err := ytypes.DeleteNode(d.SBI.Schema().RootSchema(), cpy, p); err != nil {
return err return err
} }
......
...@@ -543,7 +543,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { ...@@ -543,7 +543,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) {
fields: fields{}, fields: fields{},
args: args{ args: args{
uuid: mdid, uuid: mdid,
operation: TRANSPORT_UPDATE, operation: TransportUpdate,
path: "/system/config/hostname", path: "/system/config/hostname",
value: []string{"ceos3000"}, value: []string{"ceos3000"},
}, },
...@@ -554,7 +554,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { ...@@ -554,7 +554,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) {
fields: fields{}, fields: fields{},
args: args{ args: args{
uuid: mdid, uuid: mdid,
operation: TRANSPORT_REPLACE, operation: TransportReplace,
path: "/system/config/hostname", path: "/system/config/hostname",
value: []string{"ceos3000"}, value: []string{"ceos3000"},
}, },
...@@ -565,7 +565,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { ...@@ -565,7 +565,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) {
fields: fields{}, fields: fields{},
args: args{ args: args{
uuid: mdid, uuid: mdid,
operation: TRANSPORT_DELETE, operation: TransportDelete,
path: "/system/config/hostname", path: "/system/config/hostname",
}, },
wantErr: false, wantErr: false,
...@@ -575,7 +575,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { ...@@ -575,7 +575,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) {
fields: fields{}, fields: fields{},
args: args{ args: args{
uuid: mdid, uuid: mdid,
operation: TRANSPORT_DELETE, operation: TransportDelete,
path: "/system/config/hostname", path: "/system/config/hostname",
value: []string{"ceos3000"}, value: []string{"ceos3000"},
}, },
...@@ -597,7 +597,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { ...@@ -597,7 +597,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) {
fields: fields{}, fields: fields{},
args: args{ args: args{
uuid: mdid, uuid: mdid,
operation: TRANSPORT_UPDATE, operation: TransportUpdate,
path: "/system/config/hostname", path: "/system/config/hostname",
value: []string{"ceos3000", "ceos3001"}, value: []string{"ceos3000", "ceos3001"},
}, },
...@@ -608,7 +608,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) { ...@@ -608,7 +608,7 @@ func Test_pndImplementation_ChangeOND(t *testing.T) {
fields: fields{}, fields: fields{},
args: args{ args: args{
uuid: did, uuid: did,
operation: TRANSPORT_UPDATE, operation: TransportUpdate,
}, },
wantErr: true, wantErr: true,
}, },
......
package nucleus package nucleus
import ( import (
. "code.fbi.h-da.de/cocsn/gosdn/nucleus/pnd" p "code.fbi.h-da.de/cocsn/gosdn/nucleus/pnd"
"github.com/google/uuid" "github.com/google/uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"reflect" "reflect"
...@@ -145,16 +145,16 @@ type changeStore struct { ...@@ -145,16 +145,16 @@ type changeStore struct {
store store
} }
func (s changeStore) get(id uuid.UUID) (*Change, error) { func (s changeStore) get(id uuid.UUID) (*p.Change, error) {
item, err := s.store.get(id) item, err := s.store.get(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
change, ok := item.(*Change) change, ok := item.(*p.Change)
if !ok { if !ok {
return nil, &ErrInvalidTypeAssertion{ return nil, &ErrInvalidTypeAssertion{
v: change, v: change,
t: reflect.TypeOf(&Change{}), t: reflect.TypeOf(&p.Change{}),
} }
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
......
...@@ -7,17 +7,22 @@ import ( ...@@ -7,17 +7,22 @@ import (
"io" "io"
) )
// Operation codes numerous operations used to change the state of remote resources.
// It is used as a unified code. Each Transport implementation needs to map these
// accordingly to its specification.
type Operation int type Operation int
const ( const (
TRANSPORT_UPDATE Operation = iota // TransportUpdate codes an update operation
TRANSPORT_REPLACE TransportUpdate Operation = iota
TRANSPORT_DELETE // TransportReplace codes a replace operation
TransportReplace
// TransportDelete codes a delete operation
TransportDelete
) )
// Transport provides an interface for // Transport provides an interface for Transport implementations
// Transport implementations like RESTCONF // like RESTCONF or gnmi
// or gnmi
type Transport interface { type Transport interface {
Get(ctx context.Context, params ...string) (interface{}, error) Get(ctx context.Context, params ...string) (interface{}, error)
Set(ctx context.Context, params ...interface{}) error Set(ctx context.Context, params ...interface{}) error
......
...@@ -5,23 +5,7 @@ import ( ...@@ -5,23 +5,7 @@ import (
"strings" "strings"
) )
func FromString(path string) *gpb.Path { // ToStrings translates a gNMI path to a slice of strings
elements := strings.Split(path, delim)
elem := make([]*gpb.PathElem, len(elements))
for i, e := range elements {
elem[i] = &gpb.PathElem{
Name: e,
Key: nil,
}
}
return &gpb.Path{
Element: nil,
Origin: "",
Elem: elem,
Target: "",
}
}
func ToStrings(path *gpb.Path) []string { func ToStrings(path *gpb.Path) []string {
elems := make([]string, len(path.Elem)) elems := make([]string, len(path.Elem))
for i, e := range path.Elem { for i, e := range path.Elem {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment