blob: 6c31ad4a6a674cc23f02140de6404e5a3f508dc5 [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.
14*/
15
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 {
26 UNIList *util.ConcurrentMap // [UNIPort] UNIPort
27}
28
29// NewUniPortsList is Constructor for UniPortList structure
30func 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.
39func (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.
45type PonPortChannels struct {
46 ChannelList *util.ConcurrentMap // [channelIP]*UniPortList
47}
48
49// NewPonPortChannels is constructor for PonPortChannel.
50func 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.
59func (ponPortChannels *PonPortChannels) GetActiveChannelCount() uint32 {
60 return uint32(ponPortChannels.ChannelList.Length())
61}
62
63// AddChannelToMap Adds new channel to the pon port map
64func (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
79func (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}