Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translib phase I changes #11

Merged
merged 5 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 33 additions & 40 deletions translib/app_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
////////////////////////////////////////////////////////////////////////////////

/*
Package translib defines the interface for all the app modules
Package translib defines the interface for all the app modules

It exposes register function for all the app modules to register

Expand All @@ -31,19 +31,19 @@ package translib

import (
"errors"
log "github.com/golang/glog"
"github.com/openconfig/ygot/ygot"
"reflect"
"strings"
"github.com/Azure/sonic-mgmt-common/translib/db"
log "github.com/golang/glog"
"github.com/openconfig/ygot/ygot"
)

//Structure containing app module information
type appInfo struct {
appType reflect.Type
ygotRootType reflect.Type
isNative bool
tablesToWatch []*db.TableSpec
appType reflect.Type
ygotRootType reflect.Type
isNative bool
tablesToWatch []*db.TableSpec
}

//Structure containing the app data coming from translib infra
Expand All @@ -52,6 +52,17 @@ type appData struct {
payload []byte
ygotRoot *ygot.GoStruct
ygotTarget *interface{}
appOptions
}

// appOptions holds additional options for appInterface APIs.
// These include RESTCONF query parameters like - depth, fields etc.
type appOptions struct {

// depth limits subtree levels in the response data.
// 0 indicates unlimited depth.
// Valid for GET API only.
depth uint
}

//map containing the base path to app module info
Expand Down Expand Up @@ -79,13 +90,13 @@ type appInterface interface {
//App modules will use this function to register with App interface during boot up
func register(path string, info *appInfo) error {
var err error
log.Info("Registering for path =", path)
log.Info("Registering for path =", path)

if appMap == nil {
appMap = make(map[string]*appInfo)
}

if _, ok := appMap[path]; ok == false {
if _, ok := appMap[path]; !ok {

appMap[path] = info

Expand All @@ -107,21 +118,6 @@ func addModel(model *ModelData) error {
return err
}

//App modules can use this function to unregister itself from the app interface
func unregister(path string) error {
var err error
log.Info("Unregister for path =", path)

_, ok := appMap[path]

if ok {
log.Info("deleting for path =", path)
delete(appMap, path)
}

return err
}

//Translib infra will use this function get the app info for a given path
func getAppModuleInfo(path string) (*appInfo, error) {
var err error
Expand All @@ -137,12 +133,9 @@ func getAppModuleInfo(path string) (*appInfo, error) {
return app, err
}

errStr := "Unsupported path=" + path

err = errors.New(errStr)
log.Error(errStr)

var app *appInfo
/* If no specific app registered fallback to default/common app */
log.Infof("No app module registered for path %s hence fallback to default/common app", path)
app := appMap["*"]

return app, err
anand-kumar-subramanian marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -155,16 +148,16 @@ func getModels() []ModelData {

//Creates a new app from the appType and returns it as an appInterface
func getAppInterface(appType reflect.Type) (appInterface, error) {
var err error
appInstance := reflect.New(appType)
app, ok := appInstance.Interface().(appInterface)

if !ok {
err = errors.New("Invalid appType")
log.Fatal("Appmodule does not confirm to appInterface method conventions for appType=", appType)
} else {
log.Info("cast to appInterface worked", app)
}
var err error
appInstance := reflect.New(appType)
app, ok := appInstance.Interface().(appInterface)

if !ok {
err = errors.New("Invalid appType")
log.Fatal("Appmodule does not confirm to appInterface method conventions for appType=", appType)
} else {
log.Info("cast to appInterface worked", app)
}

return app, err
}
5 changes: 3 additions & 2 deletions translib/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ func startSubscribe(sInfo *subscribeInfo, dbNotificationMap map[db.DBNum][]*noti
stopMap[sInfo.stop] = sInfo

for dbno, nInfoArr := range dbNotificationMap {
opt := getDBOptions(dbno)
isWriteDisabled := true
opt := getDBOptions(dbno, isWriteDisabled)
err = startDBSubscribe(opt, nInfoArr, sInfo)

if err != nil {
Expand Down Expand Up @@ -238,7 +239,7 @@ func getJson (nInfo *notificationInfo) ([]byte, error) {
path := nInfo.path
appInfo := nInfo.appInfo

err := appInitialize(app, appInfo, path, nil, GET)
err := appInitialize(app, appInfo, path, nil, nil, GET)

if err != nil {
return payload, err
Expand Down