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 ( |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 19 | "context" |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 20 | "encoding/json" |
| 21 | "net" |
| 22 | |
| 23 | "voltha-go-controller/internal/pkg/of" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 24 | "voltha-go-controller/log" |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 25 | ) |
| 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 |
| 34 | type IgmpGroupPort struct { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 35 | 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 Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // NewIgmpGroupPort is constructor for a port |
| 47 | func NewIgmpGroupPort(port string, cvlan uint16, pbit uint8, version uint8, incl bool, ponPortID uint32) *IgmpGroupPort { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 48 | 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 Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // InclSourceIsIn checks if a source is in include list |
| 60 | func (igp *IgmpGroupPort) InclSourceIsIn(src net.IP) bool { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 61 | return IsIPPresent(src, igp.IncludeList) |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // ExclSourceIsIn checks if a source is in exclude list |
| 65 | func (igp *IgmpGroupPort) ExclSourceIsIn(src net.IP) bool { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 66 | return IsIPPresent(src, igp.ExcludeList) |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // AddInclSource adds a source is in include list |
| 70 | func (igp *IgmpGroupPort) AddInclSource(src net.IP) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 71 | logger.Debugw(ctx, "Adding Include Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 72 | igp.IncludeList = append(igp.IncludeList, src) |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // AddExclSource adds a source is in exclude list |
| 76 | func (igp *IgmpGroupPort) AddExclSource(src net.IP) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 77 | logger.Debugw(ctx, "Adding Exclude Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 78 | igp.ExcludeList = append(igp.ExcludeList, src) |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // DelInclSource deletes a source is in include list |
| 82 | func (igp *IgmpGroupPort) DelInclSource(src net.IP) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 83 | 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 Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // DelExclSource deletes a source is in exclude list |
| 93 | func (igp *IgmpGroupPort) DelExclSource(src net.IP) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 94 | 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 Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // WriteToDb is utility to write IGMP Group Port Info to database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 104 | func (igp *IgmpGroupPort) WriteToDb(cntx context.Context, mvlan of.VlanType, gip net.IP, device string) error { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 105 | 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 Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // NewIgmpGroupPortFromBytes create the IGMP group port from a byte slice |
| 116 | func NewIgmpGroupPortFromBytes(b []byte) (*IgmpGroupPort, error) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 117 | 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 Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 123 | } |