blob: 4f858e8192debc5f5f15df6c8cea7f9f44fd14cb [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.
59func (ponPortChannels *PonPortChannels) GetActiveChannelCount() uint32 {
vinokuma926cb3e2023-03-29 11:41:06 +053060 return uint32(ponPortChannels.ChannelList.Length())
Tinoj Josephcf161be2022-07-07 19:47:47 +053061}
62
63// AddChannelToMap Adds new channel to the pon port map
64func (ponPortChannels *PonPortChannels) AddChannelToMap(uniPort, channel string) bool {
vinokuma926cb3e2023-03-29 11:41:06 +053065 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 Josephcf161be2022-07-07 19:47:47 +053075}
76
77// RemoveChannelFromMap Removed channel from the pon port map
78func (ponPortChannels *PonPortChannels) RemoveChannelFromMap(uniPort, channel string) bool {
vinokuma926cb3e2023-03-29 11:41:06 +053079 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 Josephcf161be2022-07-07 19:47:47 +053096}