Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 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 Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var AdapterInstance *VolthaYangAdapter |
| 30 | |
| 31 | type VolthaYangAdapter struct { |
| 32 | volthaNbiClient *clients.VolthaNbiClient |
Elia Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame^] | 33 | onosClient *clients.OnosClient |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Elia Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame^] | 36 | func NewVolthaYangAdapter(nbiClient *clients.VolthaNbiClient, onosClient *clients.OnosClient) *VolthaYangAdapter { |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 37 | return &VolthaYangAdapter{ |
| 38 | volthaNbiClient: nbiClient, |
Elia Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame^] | 39 | onosClient: onosClient, |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | func (t *VolthaYangAdapter) GetDevices(ctx context.Context) ([]YangItem, error) { |
| 44 | devices, err := t.volthaNbiClient.Service.ListDevices(ctx, &empty.Empty{}) |
| 45 | if err != nil { |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 46 | return nil, fmt.Errorf("get-devices-failed: %v", err) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 47 | } |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 48 | logger.Debugw(ctx, "get-devices-success", log.Fields{"devices": devices}) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 49 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 50 | items := []YangItem{} |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 51 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 52 | 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 Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame^] | 61 | logger.Debugw(ctx, "get-onu-ports-success", log.Fields{"deviceId": device.Id, "ports": ports}) |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 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 Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 75 | |
| 76 | return items, nil |
| 77 | } |
Elia Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame^] | 78 | |
| 79 | func (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 | |
| 105 | func (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 | |
| 154 | func (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 | |
| 180 | func (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 | |
| 185 | func (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 | } |