blob: 22ef806c623274426eeec12324f321d2f04fd6e0 [file] [log] [blame]
Tinoj Josephcf161be2022-07-07 19:47:47 +05301/*
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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Tinoj Josephcf161be2022-07-07 19:47:47 +053015
16package application
17
18import (
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.
25type UniPortList struct {
vinokuma926cb3e2023-03-29 11:41:06 +053026 UNIList *util.ConcurrentMap // [UNIPort] UNIPort
Tinoj Josephcf161be2022-07-07 19:47:47 +053027}
28
29// NewUniPortsList is Constructor for UniPortList structure
30func NewUniPortsList() *UniPortList {
vinokuma926cb3e2023-03-29 11:41:06 +053031 var uniPortsList UniPortList
Tinoj Josephcf161be2022-07-07 19:47:47 +053032
vinokuma926cb3e2023-03-29 11:41:06 +053033 uniPortsList.UNIList = util.NewConcurrentMap()
34 return &uniPortsList
Tinoj Josephcf161be2022-07-07 19:47:47 +053035}
36
37// GetUniPortCount returns the number of UNI ports subscribed to
38// current channel.
39func (uniPortsList *UniPortList) GetUniPortCount() uint64 {
vinokuma926cb3e2023-03-29 11:41:06 +053040 return uniPortsList.UNIList.Length()
Tinoj Josephcf161be2022-07-07 19:47:47 +053041}
42
43// PonPortChannels : PON port channel map keeps the active channel list and its
44// count for this group.
45type PonPortChannels struct {
vinokuma926cb3e2023-03-29 11:41:06 +053046 ChannelList *util.ConcurrentMap // [channelIP]*UniPortList
Tinoj Josephcf161be2022-07-07 19:47:47 +053047}
48
49// NewPonPortChannels is constructor for PonPortChannel.
50func NewPonPortChannels() *PonPortChannels {
vinokuma926cb3e2023-03-29 11:41:06 +053051 var ponPortChannel PonPortChannels
Tinoj Josephcf161be2022-07-07 19:47:47 +053052
vinokuma926cb3e2023-03-29 11:41:06 +053053 ponPortChannel.ChannelList = util.NewConcurrentMap()
54 return &ponPortChannel
Tinoj Josephcf161be2022-07-07 19:47:47 +053055}
56
57// GetActiveChannelCount returns the number of active channel count
58// for this pon port in the current group.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053059// #nosec G115
Tinoj Josephcf161be2022-07-07 19:47:47 +053060func (ponPortChannels *PonPortChannels) GetActiveChannelCount() uint32 {
vinokuma926cb3e2023-03-29 11:41:06 +053061 return uint32(ponPortChannels.ChannelList.Length())
Tinoj Josephcf161be2022-07-07 19:47:47 +053062}
63
64// AddChannelToMap Adds new channel to the pon port map
65func (ponPortChannels *PonPortChannels) AddChannelToMap(uniPort, channel string) bool {
vinokuma926cb3e2023-03-29 11:41:06 +053066 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
Tinoj Josephcf161be2022-07-07 19:47:47 +053076}
77
78// RemoveChannelFromMap Removed channel from the pon port map
79func (ponPortChannels *PonPortChannels) RemoveChannelFromMap(uniPort, channel string) bool {
vinokuma926cb3e2023-03-29 11:41:06 +053080 isDeleted := bool(false)
81 uniList, ok := ponPortChannels.ChannelList.Get(channel)
82 if ok {
83 uniList.(*UniPortList).UNIList.Remove(uniPort)
84 if uniList.(*UniPortList).UNIList.Length() == 0 {
85 // Last port from the channel is removed.
86 // Removing channel from PON port map.
87 ponPortChannels.ChannelList.Remove(channel)
88 isDeleted = true
89 } else {
90 ponPortChannels.ChannelList.Set(channel, uniList)
91 }
92 } else {
93 logger.Warnw(ctx, "Channel doesn't exists in the active channels list", log.Fields{"Channel": channel})
94 return isDeleted
95 }
96 return isDeleted
Tinoj Josephcf161be2022-07-07 19:47:47 +053097}