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 ( |
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" |
| 24 | "voltha-go-controller/log" |
| 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 { |
| 35 | Port string |
| 36 | CVlan uint16 |
| 37 | Pbit uint8 |
| 38 | Version uint8 |
| 39 | Exclude bool |
| 40 | ExcludeList []net.IP |
| 41 | IncludeList []net.IP |
| 42 | QueryTimeoutCount uint32 |
| 43 | PonPortID uint32 |
| 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 { |
| 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 |
| 57 | } |
| 58 | |
| 59 | // InclSourceIsIn checks if a source is in include list |
| 60 | func (igp *IgmpGroupPort) InclSourceIsIn(src net.IP) bool { |
| 61 | return IsIPPresent(src, igp.IncludeList) |
| 62 | } |
| 63 | |
| 64 | // ExclSourceIsIn checks if a source is in exclude list |
| 65 | func (igp *IgmpGroupPort) ExclSourceIsIn(src net.IP) bool { |
| 66 | return IsIPPresent(src, igp.ExcludeList) |
| 67 | } |
| 68 | |
| 69 | // AddInclSource adds a source is in include list |
| 70 | func (igp *IgmpGroupPort) AddInclSource(src net.IP) { |
| 71 | logger.Debugw(ctx, "Adding Include Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 72 | igp.IncludeList = append(igp.IncludeList, src) |
| 73 | } |
| 74 | |
| 75 | // AddExclSource adds a source is in exclude list |
| 76 | func (igp *IgmpGroupPort) AddExclSource(src net.IP) { |
| 77 | logger.Debugw(ctx, "Adding Exclude Source", log.Fields{"Port": igp.Port, "Src": src}) |
| 78 | igp.ExcludeList = append(igp.ExcludeList, src) |
| 79 | } |
| 80 | |
| 81 | // DelInclSource deletes a source is in include list |
| 82 | func (igp *IgmpGroupPort) DelInclSource(src net.IP) { |
| 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 | } |
| 90 | } |
| 91 | |
| 92 | // DelExclSource deletes a source is in exclude list |
| 93 | func (igp *IgmpGroupPort) DelExclSource(src net.IP) { |
| 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 | } |
| 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 { |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 105 | b, err := json.Marshal(igp) |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 109 | if err1 := db.PutIgmpRcvr(cntx, mvlan, gip, device, igp.Port, string(b)); err1 != nil { |
Tinoj Joseph | cf161be | 2022-07-07 19:47:47 +0530 | [diff] [blame] | 110 | return err1 |
| 111 | } |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | // NewIgmpGroupPortFromBytes create the IGMP group port from a byte slice |
| 116 | func NewIgmpGroupPortFromBytes(b []byte) (*IgmpGroupPort, error) { |
| 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 |
| 123 | } |