Skip to content
Snippets Groups Projects
handler.go 2.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	"github.com/openconfig/gnmi/proto/gnmi"
    	"github.com/openconfig/ygot/ygot"
    )
    
    
    // Config holds the current configuration of the device.
    // Config is used by the gnmiserver to address GET/SET requests.
    // Handlers can use config to e.g. react to internal changes and adjust the
    // configuration data accordingly.
    type Config struct {
    	Data ygot.ValidatedGoStruct
    	mu   sync.RWMutex
    }
    
    func (c *Config) Lock() {
    	c.mu.Lock()
    }
    
    func (c *Config) Unlock() {
    	c.mu.Unlock()
    }
    
    func (c *Config) RLock() {
    	c.mu.RLock()
    }
    
    func (c *Config) RUnlock() {
    	c.mu.RUnlock()
    }
    
    
    // PathHandler handles configuration changes and adjusts the targets config
    // model accordingly.
    //
    // A PathHandler has to provide a map of gnmi.Paths in string form. E.g,
    //   - "/system/config"
    //   - "/system/config/hostname"
    //
    // Changes that address these specific paths will be routed to the all the
    // handlers that are registered to handle those paths.
    //
    // The Init() method is called if a PathHandler gets registered. It should
    // contain the initial setup of the targets config data for which the specific
    // PathHandler is responsible for. Therefore the method is provided with the
    // targets current configuration data.
    //
    // The Update() method is responsible to push down changes down to the operating system.
    // Therefore the configuration data (containing the new changes) is provided,
    // aswell as the single changes as gnmi.Update.
    type PathHandler interface {
    
    	GetName() string
    	GetPaths() map[string]struct{}
    	Init(*Config, func([]*gnmi.Notification) error) error
    
    	// NOTE:  Processing order as defined in the gNMI Spec 3.4: Delete,
    	// Replace, Update
    	// TODO: Add Delete and Replace
    	// Delete(ygot.ValidatedGoStruct, []*gnmi.Path)
    	// Replace(ygot.ValidatedGoStruct, []*gnmi.Update)
    	Update(ygot.ValidatedGoStruct, []*gnmi.Update) error
    }
    
    // HandlerJob contains all updates and deletes whose paths are part of those
    // which the respective handler is responsible for.
    type HandlerJob struct {
    	Updates []*gnmi.Update
    	Deletes []*gnmi.Path
    }
    
    // DefaultPathHandler should be embedded by all PathHandlers to provide a default
    
    // implementation of the PathHandler interface. Config and the PublishToSubs
    // function are provided through the Init() function of the PathHandler.
    // Init is called for each handler when the gnmitargets `Start()`
    // method is called.
    type DefaultPathHandler struct {
    	Name          string
    	Paths         map[string]struct{}
    	Config        *Config
    	PublishToSubs func([]*gnmi.Notification) error
    }
    
    func (dph *DefaultPathHandler) GetName() string {
    	return dph.Name
    }
    
    func (dph *DefaultPathHandler) GetPaths() map[string]struct{} {
    	return dph.Paths
    }
    
    func (dpb *DefaultPathHandler) Init(*Config, func([]*gnmi.Notification) error) error {
    	return fmt.Errorf("not implemented")
    }
    
    func (dph *DefaultPathHandler) Update(ygot.ValidatedGoStruct, []*gnmi.Update) error {
    	return fmt.Errorf("not implemented")
    }