blob: 6a5965092ce550a5746f1b770fd9993c8c5b1642 [file] [log] [blame]
Elia Battistone1cecb22022-03-21 10:05:25 +01001/*
2* Copyright 2022-present Open Networking Foundation
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7
8* http://www.apache.org/licenses/LICENSE-2.0
9
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15 */
16
17package core
18
19import (
20 "context"
21 "fmt"
22
23 "github.com/golang/protobuf/ptypes/empty"
24 "github.com/opencord/voltha-lib-go/v7/pkg/log"
25 "github.com/opencord/voltha-northbound-bbf-adapter/internal/clients"
Elia Battiston4750d3c2022-07-14 13:24:56 +000026 "github.com/opencord/voltha-protos/v5/go/voltha"
Elia Battistone1cecb22022-03-21 10:05:25 +010027)
28
29var AdapterInstance *VolthaYangAdapter
30
31type VolthaYangAdapter struct {
32 volthaNbiClient *clients.VolthaNbiClient
33 oltAppClient *clients.OltAppClient
34}
35
36func NewVolthaYangAdapter(nbiClient *clients.VolthaNbiClient, oltClient *clients.OltAppClient) *VolthaYangAdapter {
37 return &VolthaYangAdapter{
38 volthaNbiClient: nbiClient,
39 oltAppClient: oltClient,
40 }
41}
42
43func (t *VolthaYangAdapter) GetDevices(ctx context.Context) ([]YangItem, error) {
44 devices, err := t.volthaNbiClient.Service.ListDevices(ctx, &empty.Empty{})
45 if err != nil {
Elia Battiston4750d3c2022-07-14 13:24:56 +000046 return nil, fmt.Errorf("get-devices-failed: %v", err)
Elia Battistone1cecb22022-03-21 10:05:25 +010047 }
Elia Battiston4750d3c2022-07-14 13:24:56 +000048 logger.Debugw(ctx, "get-devices-success", log.Fields{"devices": devices})
Elia Battistone1cecb22022-03-21 10:05:25 +010049
Elia Battiston4750d3c2022-07-14 13:24:56 +000050 items := []YangItem{}
Elia Battistone1cecb22022-03-21 10:05:25 +010051
Elia Battiston4750d3c2022-07-14 13:24:56 +000052 for _, device := range devices.Items {
53 items = append(items, translateDevice(device)...)
54
55 if !device.Root {
56 //If the device is an ONU, also expose UNIs
57 ports, err := t.volthaNbiClient.Service.ListDevicePorts(ctx, &voltha.ID{Id: device.Id})
58 if err != nil {
59 return nil, fmt.Errorf("get-onu-ports-failed: %v", err)
60 }
61 logger.Debugw(ctx, "get-ports-success", log.Fields{"deviceId": device.Id, "ports": ports})
62
63 portsItems, err := translateOnuPorts(device.Id, ports)
64 if err != nil {
65 logger.Errorw(ctx, "cannot-translate-onu-ports", log.Fields{
66 "deviceId": device.Id,
67 "err": err,
68 })
69 continue
70 }
71
72 items = append(items, portsItems...)
73 }
74 }
Elia Battistone1cecb22022-03-21 10:05:25 +010075
76 return items, nil
77}