blob: 5e197531169574fc0fd012eaf804106dfed579e3 [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
Elia Battistona1333642022-07-27 12:17:24 +000033 onosClient *clients.OnosClient
Elia Battistone1cecb22022-03-21 10:05:25 +010034}
35
Elia Battistona1333642022-07-27 12:17:24 +000036func NewVolthaYangAdapter(nbiClient *clients.VolthaNbiClient, onosClient *clients.OnosClient) *VolthaYangAdapter {
Elia Battistone1cecb22022-03-21 10:05:25 +010037 return &VolthaYangAdapter{
38 volthaNbiClient: nbiClient,
Elia Battistona1333642022-07-27 12:17:24 +000039 onosClient: onosClient,
Elia Battistone1cecb22022-03-21 10:05:25 +010040 }
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 }
Elia Battistona1333642022-07-27 12:17:24 +000061 logger.Debugw(ctx, "get-onu-ports-success", log.Fields{"deviceId": device.Id, "ports": ports})
Elia Battiston4750d3c2022-07-14 13:24:56 +000062
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}
Elia Battistona1333642022-07-27 12:17:24 +000078
79func (t *VolthaYangAdapter) GetVlans(ctx context.Context) ([]YangItem, error) {
80 services, err := t.onosClient.GetProgrammedSubscribers()
81 if err != nil {
82 return nil, fmt.Errorf("get-programmed-subscribers-failed: %v", err)
83 }
84 logger.Debugw(ctx, "get-programmed-subscribers-success", log.Fields{"services": services})
85
86 //No need for other requests if there are no services
87 if len(services) == 0 {
88 return []YangItem{}, nil
89 }
90
91 ports, err := t.onosClient.GetPorts()
92 if err != nil {
93 return nil, fmt.Errorf("get-onos-ports-failed: %v", err)
94 }
95 logger.Debugw(ctx, "get-onos-ports-success", log.Fields{"ports": ports})
96
97 items, err := translateVlans(services, ports)
98 if err != nil {
99 return nil, fmt.Errorf("cannot-translate-vlans: %v", err)
100 }
101
102 return items, nil
103}
104
105func (t *VolthaYangAdapter) GetBandwidthProfiles(ctx context.Context) ([]YangItem, error) {
106 services, err := t.onosClient.GetProgrammedSubscribers()
107 if err != nil {
108 return nil, fmt.Errorf("get-programmed-subscribers-failed: %v", err)
109 }
110 logger.Debugw(ctx, "get-programmed-subscribers-success", log.Fields{"services": services})
111
112 //No need for other requests if there are no services
113 if len(services) == 0 {
114 return []YangItem{}, nil
115 }
116
117 bwProfilesMap := map[string]bool{}
118 bwProfiles := []clients.BandwidthProfile{}
119
120 for _, service := range services {
121 //Get information on downstream bw profile if new
122 if _, ok := bwProfilesMap[service.TagInfo.DownstreamBandwidthProfile]; !ok {
123 bw, err := t.onosClient.GetBandwidthProfile(service.TagInfo.DownstreamBandwidthProfile)
124 if err != nil {
125 return nil, fmt.Errorf("get-bw-profile-failed: %s %v", service.TagInfo.DownstreamBandwidthProfile, err)
126 }
127 logger.Debugw(ctx, "get-bw-profile-success", log.Fields{"bwProfile": bw})
128
129 bwProfiles = append(bwProfiles, *bw)
130 bwProfilesMap[service.TagInfo.DownstreamBandwidthProfile] = true
131 }
132
133 //Get information on upstream bw profile if new
134 if _, ok := bwProfilesMap[service.TagInfo.UpstreamBandwidthProfile]; !ok {
135 bw, err := t.onosClient.GetBandwidthProfile(service.TagInfo.UpstreamBandwidthProfile)
136 if err != nil {
137 return nil, fmt.Errorf("get-bw-profile-failed: %s %v", service.TagInfo.UpstreamBandwidthProfile, err)
138 }
139 logger.Debugw(ctx, "get-bw-profile-success", log.Fields{"bwProfile": bw})
140
141 bwProfiles = append(bwProfiles, *bw)
142 bwProfilesMap[service.TagInfo.UpstreamBandwidthProfile] = true
143 }
144 }
145
146 items, err := translateBandwidthProfiles(bwProfiles)
147 if err != nil {
148 return nil, fmt.Errorf("cannot-translate-bandwidth-profiles: %v", err)
149 }
150
151 return items, nil
152}
153
154func (t *VolthaYangAdapter) GetServices(ctx context.Context) ([]YangItem, error) {
155 services, err := t.onosClient.GetProgrammedSubscribers()
156 if err != nil {
157 return nil, fmt.Errorf("get-programmed-subscribers-failed: %v", err)
158 }
159 logger.Debugw(ctx, "get-programmed-subscribers-success", log.Fields{"services": services})
160
161 //No need for other requests if there are no services
162 if len(services) == 0 {
163 return []YangItem{}, nil
164 }
165
166 ports, err := t.onosClient.GetPorts()
167 if err != nil {
168 return nil, fmt.Errorf("get-onos-ports-failed: %v", err)
169 }
170 logger.Debugw(ctx, "get-onos-ports-success", log.Fields{"ports": ports})
171
172 items, err := translateServices(services, ports)
173 if err != nil {
174 return nil, fmt.Errorf("cannot-translate-services: %v", err)
175 }
176
177 return items, nil
178}
179
180func (t *VolthaYangAdapter) ProvisionService(portName string, sTag string, cTag string, technologyProfileId string) error {
181 _, err := t.onosClient.ProvisionService(portName, sTag, cTag, technologyProfileId)
182 return err
183}
184
185func (t *VolthaYangAdapter) RemoveService(portName string, sTag string, cTag string, technologyProfileId string) error {
186 _, err := t.onosClient.RemoveService(portName, sTag, cTag, technologyProfileId)
187 return err
188}