[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)
+		}
+	}
+}