[VOL-4648] Generate basic response with ID and type for get request on devices
Change-Id: I5899ab9c2ae4449863d88439719c9cdf2036c1f7
diff --git a/internal/sysrepo/sysrepo.go b/internal/sysrepo/sysrepo.go
index b0ebc1f..57de757 100644
--- a/internal/sysrepo/sysrepo.go
+++ b/internal/sysrepo/sysrepo.go
@@ -17,19 +17,16 @@
package sysrepo
//#cgo CFLAGS: -I/usr/include
-//#cgo LDFLAGS: -lsysrepo -Wl,--allow-multiple-definition
+//#cgo LDFLAGS: -lsysrepo -lyang -Wl,--allow-multiple-definition
//#include "plugin.c"
import "C"
import (
"context"
"fmt"
+ "unsafe"
"github.com/opencord/voltha-lib-go/v7/pkg/log"
-)
-
-const (
- BASE_YANG_MODEL = "bbf-device-aggregation"
- DEVICES_YANG_MODEL = "/" + BASE_YANG_MODEL + ":devices"
+ "github.com/opencord/voltha-northbound-bbf-adapter/internal/core"
)
type SysrepoPlugin struct {
@@ -42,10 +39,48 @@
return C.GoString(C.sr_strerror(code))
}
+func freeCString(str *C.char) {
+ if str != nil {
+ C.free(unsafe.Pointer(str))
+ str = nil
+ }
+}
+
+func updateYangItems(ctx context.Context, session *C.sr_session_ctx_t, parent **C.lyd_node, items []core.YangItem) error {
+ conn := C.sr_session_get_connection(session)
+ if conn == nil {
+ return fmt.Errorf("null-connection")
+ }
+
+ //libyang context
+ ly_ctx := C.sr_get_context(conn)
+ if ly_ctx == nil {
+ return fmt.Errorf("null-libyang-context")
+ }
+
+ for _, item := range items {
+ logger.Debugw(ctx, "updating-yang-item", log.Fields{"item": item})
+
+ path := C.CString(item.Path)
+ value := C.CString(item.Value)
+
+ lyErr := C.lyd_new_path(*parent, ly_ctx, path, value, 0, nil)
+ if lyErr != C.LY_SUCCESS {
+ freeCString(path)
+ freeCString(value)
+ return fmt.Errorf("libyang-new-path-failed: %d", lyErr)
+ }
+
+ freeCString(path)
+ freeCString(value)
+ }
+
+ return nil
+}
+
//createPluginState populates a SysrepoPlugin struct by establishing
//a connection and a session
func (p *SysrepoPlugin) createSession(ctx context.Context) error {
-
var errCode C.int
//Populates connection
@@ -70,17 +105,42 @@
return nil
}
-//export get_data_cb
-func get_data_cb() {
- //This function is a callback for the retrieval of data from sysrepo
+//export get_devices_cb
+func get_devices_cb(session *C.sr_session_ctx_t, parent **C.lyd_node) C.sr_error_t {
+ //This function is a callback for the retrieval of devices from sysrepo
//The "export" comment instructs CGO to create a C function for it
- //As a placeholder, it just reports that a request to get data
- //has been received from the netconf server
-
- //TODO: get actual information
ctx := context.Background()
- logger.Info(ctx, ">>>>>>>RECEIVED REQUEST FROM SYSREPO<<<<<<<")
+ logger.Debug(ctx, "processing-get-data-request")
+
+ if session == nil {
+ logger.Error(ctx, "sysrepo-get-data-null-session")
+ return C.SR_ERR_OPERATION_FAILED
+ }
+
+ if parent == nil {
+ logger.Error(ctx, "sysrepo-get-data-null-parent-node")
+ return C.SR_ERR_OPERATION_FAILED
+ }
+
+ if core.AdapterInstance == nil {
+ logger.Error(ctx, "sysrepo-get-data-nil-translator")
+ return C.SR_ERR_OPERATION_FAILED
+ }
+
+ devices, err := core.AdapterInstance.GetDevices(ctx)
+ if err != nil {
+ logger.Errorw(ctx, "sysrepo-get-data-translator-error", log.Fields{"err": err})
+ return C.SR_ERR_OPERATION_FAILED
+ }
+
+ err = updateYangItems(ctx, session, parent, devices)
+ if err != nil {
+ logger.Errorw(ctx, "sysrepo-get-data-update-error", log.Fields{"err": err})
+ return C.SR_ERR_OPERATION_FAILED
+ }
+
+ return C.SR_ERR_OK
}
func StartNewPlugin(ctx context.Context) (*SysrepoPlugin, error) {
@@ -98,11 +158,17 @@
//Set callbacks for events
//Subscribe with a callback to the request of data on a certain path
+ module := C.CString(core.DeviceAggregationModel)
+ defer freeCString(module)
+
+ path := C.CString(core.DevicesPath + "/*")
+ defer freeCString(path)
+
errCode := C.sr_oper_get_items_subscribe(
plugin.session,
- C.CString(BASE_YANG_MODEL),
- C.CString(DEVICES_YANG_MODEL+"/*"),
- C.function(C.get_data_cb_wrapper),
+ module,
+ path,
+ C.function(C.get_devices_cb_wrapper),
C.NULL,
C.SR_SUBSCR_CTX_REUSE,
&plugin.subscription,