Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "voltha-go-controller/internal/pkg/util" |
| 20 | "voltha-go-controller/log" |
| 21 | ) |
| 22 | |
| 23 | // UniPortList : UNI Port list per channle has stores the UNI port list for this |
| 24 | // channel. |
| 25 | type UniPortList struct { |
| 26 | UNIList *util.ConcurrentMap // [UNIPort] UNIPort |
| 27 | } |
| 28 | |
| 29 | // NewUniPortsList is Constructor for UniPortList structure |
| 30 | func NewUniPortsList() *UniPortList { |
| 31 | var uniPortsList UniPortList |
| 32 | |
| 33 | uniPortsList.UNIList = util.NewConcurrentMap() |
| 34 | return &uniPortsList |
| 35 | } |
| 36 | |
| 37 | // GetUniPortCount returns the number of UNI ports subscribed to |
| 38 | // current channel. |
| 39 | func (uniPortsList *UniPortList) GetUniPortCount() uint64 { |
| 40 | return uniPortsList.UNIList.Length() |
| 41 | } |
| 42 | |
| 43 | // PonPortChannels : PON port channel map keeps the active channel list and its |
| 44 | // count for this group. |
| 45 | type PonPortChannels struct { |
| 46 | ChannelList *util.ConcurrentMap // [channelIP]*UniPortList |
| 47 | } |
| 48 | |
| 49 | // NewPonPortChannels is constructor for PonPortChannel. |
| 50 | func NewPonPortChannels() *PonPortChannels { |
| 51 | var ponPortChannel PonPortChannels |
| 52 | |
| 53 | ponPortChannel.ChannelList = util.NewConcurrentMap() |
| 54 | return &ponPortChannel |
| 55 | } |
| 56 | |
| 57 | // GetActiveChannelCount returns the number of active channel count |
| 58 | // for this pon port in the current group. |
| 59 | func (ponPortChannels *PonPortChannels) GetActiveChannelCount() uint32 { |
| 60 | return uint32(ponPortChannels.ChannelList.Length()) |
| 61 | } |
| 62 | |
| 63 | // AddChannelToMap Adds new channel to the pon port map |
| 64 | func (ponPortChannels *PonPortChannels) AddChannelToMap(uniPort, channel string) bool { |
| 65 | |
| 66 | isNewChannel := bool(false) |
| 67 | uniList, ok := ponPortChannels.ChannelList.Get(channel) |
| 68 | if !ok { |
| 69 | // Channel doesn't exists. Adding new channel. |
| 70 | uniList = NewUniPortsList() |
| 71 | isNewChannel = true |
| 72 | } |
| 73 | uniList.(*UniPortList).UNIList.Set(uniPort, uniPort) |
| 74 | ponPortChannels.ChannelList.Set(channel, uniList) |
| 75 | return isNewChannel |
| 76 | } |
| 77 | |
| 78 | // RemoveChannelFromMap Removed channel from the pon port map |
| 79 | func (ponPortChannels *PonPortChannels) RemoveChannelFromMap(uniPort, channel string) bool { |
| 80 | |
| 81 | isDeleted := bool(false) |
| 82 | uniList, ok := ponPortChannels.ChannelList.Get(channel) |
| 83 | if ok { |
| 84 | uniList.(*UniPortList).UNIList.Remove(uniPort) |
| 85 | if uniList.(*UniPortList).UNIList.Length() == 0 { |
| 86 | // Last port from the channel is removed. |
| 87 | // Removing channel from PON port map. |
| 88 | ponPortChannels.ChannelList.Remove(channel) |
| 89 | isDeleted = true |
| 90 | } else { |
| 91 | ponPortChannels.ChannelList.Set(channel, uniList) |
| 92 | } |
| 93 | } else { |
| 94 | logger.Warnw(ctx, "Channel doesn't exists in the active channels list", log.Fields{"Channel": channel}) |
| 95 | return isDeleted |
| 96 | } |
| 97 | return isDeleted |
| 98 | } |