[VOL-4648] Generate basic response with ID and type for get request on devices
Change-Id: I5899ab9c2ae4449863d88439719c9cdf2036c1f7
diff --git a/internal/core/adapter.go b/internal/core/adapter.go
new file mode 100644
index 0000000..a96767f
--- /dev/null
+++ b/internal/core/adapter.go
@@ -0,0 +1,54 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package core
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/golang/protobuf/ptypes/empty"
+ "github.com/opencord/voltha-lib-go/v7/pkg/log"
+ "github.com/opencord/voltha-northbound-bbf-adapter/internal/clients"
+)
+
+var AdapterInstance *VolthaYangAdapter
+
+type VolthaYangAdapter struct {
+ volthaNbiClient *clients.VolthaNbiClient
+ oltAppClient *clients.OltAppClient
+}
+
+func NewVolthaYangAdapter(nbiClient *clients.VolthaNbiClient, oltClient *clients.OltAppClient) *VolthaYangAdapter {
+ return &VolthaYangAdapter{
+ volthaNbiClient: nbiClient,
+ oltAppClient: oltClient,
+ }
+}
+
+func (t *VolthaYangAdapter) GetDevices(ctx context.Context) ([]YangItem, error) {
+ devices, err := t.volthaNbiClient.Service.ListDevices(ctx, &empty.Empty{})
+ if err != nil {
+ err = fmt.Errorf("get-devices-failed: %v", err)
+ return nil, err
+ }
+
+ items := translateDevices(*devices)
+
+ logger.Debugw(ctx, "get-devices-success", log.Fields{"items": items})
+
+ return items, nil
+}
diff --git a/internal/core/logger.go b/internal/core/logger.go
new file mode 100644
index 0000000..0aa6eef
--- /dev/null
+++ b/internal/core/logger.go
@@ -0,0 +1,32 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package core
+
+import (
+ "github.com/opencord/voltha-lib-go/v7/pkg/log"
+)
+
+var logger log.CLogger
+
+func init() {
+ // Setup this package so that it's log level can be modified at run time
+ var err error
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{})
+ if err != nil {
+ panic(err)
+ }
+}
diff --git a/internal/core/translation.go b/internal/core/translation.go
new file mode 100644
index 0000000..bc7c692
--- /dev/null
+++ b/internal/core/translation.go
@@ -0,0 +1,67 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package core
+
+import (
+ "fmt"
+
+ "github.com/opencord/voltha-protos/v5/go/voltha"
+)
+
+const (
+ DeviceAggregationModel = "bbf-device-aggregation"
+ DevicesPath = "/" + DeviceAggregationModel + ":devices"
+ DeviceTypeOlt = "bbf-device-types:olt"
+ DeviceTypeOnu = "bbf-device-types:onu"
+)
+
+type YangItem struct {
+ Path string
+ Value string
+}
+
+//getDevicePath returns the yang path to the root of the device with a specific ID
+func getDevicePath(id string) string {
+ return fmt.Sprintf("%s/device[name='%s']", DevicesPath, id)
+}
+
+//translateDevice returns a slice of yang items that represent a voltha device
+func translateDevice(device voltha.Device) []YangItem {
+ devicePath := getDevicePath(device.Id)
+
+ typeItem := YangItem{}
+ typeItem.Path = fmt.Sprintf("%s/type", devicePath)
+
+ if device.Root {
+ typeItem.Value = DeviceTypeOlt
+ } else {
+ typeItem.Value = DeviceTypeOnu
+ }
+
+ return []YangItem{typeItem}
+}
+
+//translateDevices returns a slice of yang items that represent a list of voltha devices
+func translateDevices(devices voltha.Devices) []YangItem {
+ result := []YangItem{}
+
+ for _, device := range devices.Items {
+ result = append(result, translateDevice(*device)...)
+ }
+
+ return result
+}
diff --git a/internal/core/translation_test.go b/internal/core/translation_test.go
new file mode 100644
index 0000000..fb2e751
--- /dev/null
+++ b/internal/core/translation_test.go
@@ -0,0 +1,101 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package core
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/opencord/voltha-protos/v5/go/voltha"
+ "github.com/stretchr/testify/assert"
+)
+
+const (
+ testDeviceId = "123145abcdef"
+)
+
+func getItemWithPath(items []YangItem, path string) (value string, ok bool) {
+ for _, item := range items {
+ if item.Path == path {
+ return item.Value, true
+ }
+ }
+
+ return "", false
+}
+
+func TestDevicePath(t *testing.T) {
+ path := getDevicePath(testDeviceId)
+ assert.Equal(t, fmt.Sprintf("/bbf-device-aggregation:devices/device[name='%s']", testDeviceId), path)
+}
+
+func TestTranslateDevice(t *testing.T) {
+ olt := voltha.Device{
+ Id: testDeviceId,
+ Root: true,
+ }
+ items := translateDevice(olt)
+
+ val, ok := getItemWithPath(items, fmt.Sprintf("%s/type", getDevicePath(testDeviceId)))
+ assert.True(t, ok, "No type item for olt")
+ assert.Equal(t, DeviceTypeOlt, val)
+
+ onu := voltha.Device{
+ Id: testDeviceId,
+ Root: false,
+ }
+ items = translateDevice(onu)
+
+ val, ok = getItemWithPath(items, fmt.Sprintf("%s/type", getDevicePath(testDeviceId)))
+ assert.True(t, ok, "No type item for onu")
+ assert.Equal(t, DeviceTypeOnu, val)
+}
+
+func TestTranslateDevices(t *testing.T) {
+ devicesNum := 10
+
+ //Create test devices
+ devices := voltha.Devices{
+ Items: []*voltha.Device{},
+ }
+
+ for i := 0; i < devicesNum; i++ {
+ devices.Items = append(devices.Items, &voltha.Device{
+ Id: fmt.Sprintf("%d", i),
+ Root: i%2 == 0,
+ })
+ }
+
+ //Translate them to items
+ items := translateDevices(devices)
+
+ //Check if the number of generated items is correct
+ singleDeviceItemsNum := len(translateDevice(*devices.Items[0]))
+ assert.Equal(t, singleDeviceItemsNum*devicesNum, len(items))
+
+ //Check if the content is right
+ for i := 0; i < devicesNum; i++ {
+ val, ok := getItemWithPath(items, fmt.Sprintf("%s/type", getDevicePath(devices.Items[i].Id)))
+ assert.True(t, ok, fmt.Sprintf("No type item for device %d", i))
+
+ if devices.Items[i].Root {
+ assert.Equal(t, DeviceTypeOlt, val)
+ } else {
+ assert.Equal(t, DeviceTypeOnu, val)
+ }
+ }
+}
diff --git a/internal/sysrepo/plugin.c b/internal/sysrepo/plugin.c
index edbf0e5..f3ff6a3 100644
--- a/internal/sysrepo/plugin.c
+++ b/internal/sysrepo/plugin.c
@@ -14,16 +14,22 @@
* limitations under the License.
*/
+#include <libyang/libyang.h>
#include <sysrepo.h>
#include <sysrepo/xpath.h>
//Needed to handle callback functions with a working data type in CGO
typedef void (*function)(); // https://golang.org/issue/19835
-//Exported by sysrepo.go
-void get_data_cb();
+//CGO can't see raw structs
+typedef struct lyd_node lyd_node;
-int get_data_cb_wrapper(
+//Exported by sysrepo.go
+sr_error_t get_devices_cb(sr_session_ctx_t *session, lyd_node **parent);
+
+//The wrapper function is needed because CGO cannot express const char*
+//and thus it can't match sysrepo's callback signature
+int get_devices_cb_wrapper(
sr_session_ctx_t *session,
uint32_t subscription_id,
const char *module_name,
@@ -33,7 +39,5 @@
struct lyd_node **parent,
void *private_data)
{
- get_data_cb();
-
- return SR_ERR_OK;
+ return get_devices_cb(session, parent);
}
\ No newline at end of file
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,