blob: 171fca0d8e557863a465fd1ec1fec27d92e3d112 [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 (
Tinoj Joseph07cc5372022-07-18 22:53:51 +053019 "context"
Tinoj Josephcf161be2022-07-07 19:47:47 +053020 "encoding/json"
21 "net"
22
23 "voltha-go-controller/internal/pkg/of"
vinokuma926cb3e2023-03-29 11:41:06 +053024 "voltha-go-controller/log"
Tinoj Josephcf161be2022-07-07 19:47:47 +053025)
26
27// IgmpGroupPort : IGMP port implements a port which is associated with an IGMP
28// version and the list of sources it implements for a given IGMP
29// channel. We may improve this to have all IGMP channels so that
30// we can implement per subscriber IGMP channel registration limits
31// As a rule a single port cannot have both include and exclude
32// lists. If we receive a include list we should purge the other
33// list which is TODO
34type IgmpGroupPort struct {
vinokuma926cb3e2023-03-29 11:41:06 +053035 Port string
36 ExcludeList []net.IP
37 IncludeList []net.IP
38 QueryTimeoutCount uint32
39 PonPortID uint32
40 CVlan uint16
41 Pbit uint8
42 Version uint8
43 Exclude bool
Tinoj Josephcf161be2022-07-07 19:47:47 +053044}
45
46// NewIgmpGroupPort is constructor for a port
47func NewIgmpGroupPort(port string, cvlan uint16, pbit uint8, version uint8, incl bool, ponPortID uint32) *IgmpGroupPort {
vinokuma926cb3e2023-03-29 11:41:06 +053048 var igp IgmpGroupPort
49 igp.Port = port
50 igp.CVlan = cvlan
51 igp.Pbit = pbit
52 igp.Version = version
53 igp.Exclude = !incl
54 igp.QueryTimeoutCount = 0
55 igp.PonPortID = ponPortID
56 return &igp
Tinoj Josephcf161be2022-07-07 19:47:47 +053057}
58
59// InclSourceIsIn checks if a source is in include list
60func (igp *IgmpGroupPort) InclSourceIsIn(src net.IP) bool {
vinokuma926cb3e2023-03-29 11:41:06 +053061 return IsIPPresent(src, igp.IncludeList)
Tinoj Josephcf161be2022-07-07 19:47:47 +053062}
63
64// ExclSourceIsIn checks if a source is in exclude list
65func (igp *IgmpGroupPort) ExclSourceIsIn(src net.IP) bool {
vinokuma926cb3e2023-03-29 11:41:06 +053066 return IsIPPresent(src, igp.ExcludeList)
Tinoj Josephcf161be2022-07-07 19:47:47 +053067}
68
69// AddInclSource adds a source is in include list
70func (igp *IgmpGroupPort) AddInclSource(src net.IP) {
vinokuma926cb3e2023-03-29 11:41:06 +053071 logger.Debugw(ctx, "Adding Include Source", log.Fields{"Port": igp.Port, "Src": src})
72 igp.IncludeList = append(igp.IncludeList, src)
Tinoj Josephcf161be2022-07-07 19:47:47 +053073}
74
75// AddExclSource adds a source is in exclude list
76func (igp *IgmpGroupPort) AddExclSource(src net.IP) {
vinokuma926cb3e2023-03-29 11:41:06 +053077 logger.Debugw(ctx, "Adding Exclude Source", log.Fields{"Port": igp.Port, "Src": src})
78 igp.ExcludeList = append(igp.ExcludeList, src)
Tinoj Josephcf161be2022-07-07 19:47:47 +053079}
80
81// DelInclSource deletes a source is in include list
82func (igp *IgmpGroupPort) DelInclSource(src net.IP) {
vinokuma926cb3e2023-03-29 11:41:06 +053083 logger.Debugw(ctx, "Deleting Include Source", log.Fields{"Port": igp.Port, "Src": src})
84 for i, addr := range igp.IncludeList {
85 if addr.Equal(src) {
86 igp.IncludeList = append(igp.IncludeList[:i], igp.IncludeList[i+1:]...)
87 return
88 }
89 }
Tinoj Josephcf161be2022-07-07 19:47:47 +053090}
91
92// DelExclSource deletes a source is in exclude list
93func (igp *IgmpGroupPort) DelExclSource(src net.IP) {
vinokuma926cb3e2023-03-29 11:41:06 +053094 logger.Debugw(ctx, "Deleting Exclude Source", log.Fields{"Port": igp.Port, "Src": src})
95 for i, addr := range igp.ExcludeList {
96 if addr.Equal(src) {
97 igp.ExcludeList = append(igp.ExcludeList[:i], igp.ExcludeList[i+1:]...)
98 return
99 }
100 }
Tinoj Josephcf161be2022-07-07 19:47:47 +0530101}
102
103// WriteToDb is utility to write IGMP Group Port Info to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530104func (igp *IgmpGroupPort) WriteToDb(cntx context.Context, mvlan of.VlanType, gip net.IP, device string) error {
vinokuma926cb3e2023-03-29 11:41:06 +0530105 b, err := json.Marshal(igp)
106 if err != nil {
107 return err
108 }
109 if err1 := db.PutIgmpRcvr(cntx, mvlan, gip, device, igp.Port, string(b)); err1 != nil {
110 return err1
111 }
112 return nil
Tinoj Josephcf161be2022-07-07 19:47:47 +0530113}
114
115// NewIgmpGroupPortFromBytes create the IGMP group port from a byte slice
116func NewIgmpGroupPortFromBytes(b []byte) (*IgmpGroupPort, error) {
vinokuma926cb3e2023-03-29 11:41:06 +0530117 var igp IgmpGroupPort
118 if err := json.Unmarshal(b, &igp); err != nil {
119 logger.Warnw(ctx, "Decode of port failed", log.Fields{"str": string(b)})
120 return nil, err
121 }
122 return &igp, nil
Tinoj Josephcf161be2022-07-07 19:47:47 +0530123}