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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 14 | */ |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 26 | UNIList *util.ConcurrentMap // [UNIPort] UNIPort |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // NewUniPortsList is Constructor for UniPortList structure |
| 30 | func NewUniPortsList() *UniPortList { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 31 | var uniPortsList UniPortList |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 32 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 33 | uniPortsList.UNIList = util.NewConcurrentMap() |
| 34 | return &uniPortsList |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // GetUniPortCount returns the number of UNI ports subscribed to |
| 38 | // current channel. |
| 39 | func (uniPortsList *UniPortList) GetUniPortCount() uint64 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 40 | return uniPortsList.UNIList.Length() |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // PonPortChannels : PON port channel map keeps the active channel list and its |
| 44 | // count for this group. |
| 45 | type PonPortChannels struct { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 46 | ChannelList *util.ConcurrentMap // [channelIP]*UniPortList |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // NewPonPortChannels is constructor for PonPortChannel. |
| 50 | func NewPonPortChannels() *PonPortChannels { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 51 | var ponPortChannel PonPortChannels |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 52 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 53 | ponPortChannel.ChannelList = util.NewConcurrentMap() |
| 54 | return &ponPortChannel |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 60 | return uint32(ponPortChannels.ChannelList.Length()) |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // AddChannelToMap Adds new channel to the pon port map |
| 64 | func (ponPortChannels *PonPortChannels) AddChannelToMap(uniPort, channel string) bool { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 65 | isNewChannel := bool(false) |
| 66 | uniList, ok := ponPortChannels.ChannelList.Get(channel) |
| 67 | if !ok { |
| 68 | // Channel doesn't exists. Adding new channel. |
| 69 | uniList = NewUniPortsList() |
| 70 | isNewChannel = true |
| 71 | } |
| 72 | uniList.(*UniPortList).UNIList.Set(uniPort, uniPort) |
| 73 | ponPortChannels.ChannelList.Set(channel, uniList) |
| 74 | return isNewChannel |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // RemoveChannelFromMap Removed channel from the pon port map |
| 78 | func (ponPortChannels *PonPortChannels) RemoveChannelFromMap(uniPort, channel string) bool { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 79 | isDeleted := bool(false) |
| 80 | uniList, ok := ponPortChannels.ChannelList.Get(channel) |
| 81 | if ok { |
| 82 | uniList.(*UniPortList).UNIList.Remove(uniPort) |
| 83 | if uniList.(*UniPortList).UNIList.Length() == 0 { |
| 84 | // Last port from the channel is removed. |
| 85 | // Removing channel from PON port map. |
| 86 | ponPortChannels.ChannelList.Remove(channel) |
| 87 | isDeleted = true |
| 88 | } else { |
| 89 | ponPortChannels.ChannelList.Set(channel, uniList) |
| 90 | } |
| 91 | } else { |
| 92 | logger.Warnw(ctx, "Channel doesn't exists in the active channels list", log.Fields{"Channel": channel}) |
| 93 | return isDeleted |
| 94 | } |
| 95 | return isDeleted |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 96 | } |