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. |
| 14 | */ |
| 15 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "net" |
| 21 | |
| 22 | "voltha-go-controller/internal/pkg/of" |
| 23 | "voltha-go-controller/log" |
| 24 | ) |
| 25 | |
| 26 | // IgmpGroupPort : IGMP port implements a port which is associated with an IGMP |
| 27 | // version and the list of sources it implements for a given IGMP |
| 28 | // channel. We may improve this to have all IGMP channels so that |
| 29 | // we can implement per subscriber IGMP channel registration limits |
| 30 | // As a rule a single port cannot have both include and exclude |
| 31 | // lists. If we receive a include list we should purge the other |
| 32 | // list which is TODO |
| 33 | type IgmpGroupPort struct { |
| 34 | Port string |
| 35 | CVlan uint16 |
| 36 | Pbit uint8 |
| 37 | Version uint8 |
| 38 | Exclude bool |
| 39 | ExcludeList []net.IP |
| 40 | IncludeList []net.IP |
| 41 | QueryTimeoutCount uint32 |
| 42 | PonPortID uint32 |
| 43 | } |
| 44 | |
| 45 | // NewIgmpGroupPort is constructor for a port |
| 46 | func NewIgmpGroupPort(port string, cvlan uint16, pbit uint8, version uint8, incl bool, ponPortID uint32) *IgmpGroupPort { |
| 47 | var igp IgmpGroupPort |
| 48 | igp.Port = port |
| 49 | igp.CVlan = cvlan |
| 50 | igp.Pbit = pbit |
| 51 | igp.Version = version |
| 52 | igp.Exclude = !incl |
| 53 | igp.QueryTimeoutCount = 0 |
| 54 | igp.PonPortID = ponPortID |
| 55 | return &igp |
| 56 | } |
| 57 | |
| 58 | // InclSourceIsIn checks if a source is in include list |
| 59 | func (igp *IgmpGroupPort) InclSourceIsIn(src net.IP) bool { |
| 60 | return IsIPPresent(src, igp.IncludeList) |
| 61 | } |
| 62 | |
| 63 | // ExclSourceIsIn checks if a source is in exclude list |
| 64 | func (igp *IgmpGroupPort) ExclSourceIsIn(src net.IP) bool { |
| 65 | return IsIPPresent(src, igp.ExcludeList) |
| 66 | } |
| 67 | |
| 68 | // AddInclSource adds a source is in include list |
| 69 | func (igp *IgmpGroupPort) AddInclSource(src net.IP) { |
| 70 | logger.Debugw(ctx, "Adding Include Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 71 | igp.IncludeList = append(igp.IncludeList, src) |
| 72 | } |
| 73 | |
| 74 | // AddExclSource adds a source is in exclude list |
| 75 | func (igp *IgmpGroupPort) AddExclSource(src net.IP) { |
| 76 | logger.Debugw(ctx, "Adding Exclude Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 77 | igp.ExcludeList = append(igp.ExcludeList, src) |
| 78 | } |
| 79 | |
| 80 | // DelInclSource deletes a source is in include list |
| 81 | func (igp *IgmpGroupPort) DelInclSource(src net.IP) { |
| 82 | logger.Debugw(ctx, "Deleting Include Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 83 | for i, addr := range igp.IncludeList { |
| 84 | if addr.Equal(src) { |
| 85 | igp.IncludeList = append(igp.IncludeList[:i], igp.IncludeList[i+1:]...) |
| 86 | return |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // DelExclSource deletes a source is in exclude list |
| 92 | func (igp *IgmpGroupPort) DelExclSource(src net.IP) { |
| 93 | logger.Debugw(ctx, "Deleting Exclude Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 94 | for i, addr := range igp.ExcludeList { |
| 95 | if addr.Equal(src) { |
| 96 | igp.ExcludeList = append(igp.ExcludeList[:i], igp.ExcludeList[i+1:]...) |
| 97 | return |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // WriteToDb is utility to write IGMP Group Port Info to database |
| 103 | func (igp *IgmpGroupPort) WriteToDb(mvlan of.VlanType, gip net.IP, device string) error { |
| 104 | b, err := json.Marshal(igp) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | if err1 := db.PutIgmpRcvr(mvlan, gip, device, igp.Port, string(b)); err1 != nil { |
| 109 | return err1 |
| 110 | } |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | // NewIgmpGroupPortFromBytes create the IGMP group port from a byte slice |
| 115 | func NewIgmpGroupPortFromBytes(b []byte) (*IgmpGroupPort, error) { |
| 116 | var igp IgmpGroupPort |
| 117 | if err := json.Unmarshal(b, &igp); err != nil { |
| 118 | logger.Warnw(ctx, "Decode of port failed", log.Fields{"str": string(b)}) |
| 119 | return nil, err |
| 120 | } |
| 121 | return &igp, nil |
| 122 | } |