Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 21 | "sync" |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 22 | |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 24 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 25 | ) |
| 26 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 27 | type DtStagKey struct { |
| 28 | ponIntf, onuID, uniID uint32 |
| 29 | } |
| 30 | |
| 31 | var currDtStag uint32 |
| 32 | var DtStag map[DtStagKey]uint32 |
| 33 | var DtCtag map[uint32]uint32 |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 34 | var AttCtag map[uint32]uint32 |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 35 | var TtCtag map[uint32]uint32 |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 36 | var mutex sync.RWMutex |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 37 | |
| 38 | func init() { |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 39 | AttCtag = make(map[uint32]uint32) |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 40 | DtCtag = make(map[uint32]uint32) |
| 41 | DtStag = make(map[DtStagKey]uint32) |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 42 | TtCtag = make(map[uint32]uint32) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | const ( |
| 46 | vendorName = "ABCD" |
| 47 | // Number of bits for the physical UNI of the ONUs |
| 48 | bitsForUniID = 4 |
| 49 | // Number of bits for the ONU ID |
| 50 | bitsForONUID = 8 |
| 51 | //MaxOnusPerPon is Max number of ONUs on any PON port |
| 52 | MaxOnusPerPon = 1 << bitsForONUID |
| 53 | ) |
| 54 | |
| 55 | var vendorSpecificId = 1000 |
| 56 | |
| 57 | func GenerateNextONUSerialNumber() *openolt.SerialNumber { |
| 58 | |
| 59 | vi := []byte(vendorName) |
| 60 | |
| 61 | vendorSpecificId += 1 |
| 62 | vs := []byte(fmt.Sprint(vendorSpecificId)) |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 63 | // logger.Infow(nil, "vendor-id-and-vendor-specific", log.Fields{"vi":vi, "vs":vs}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 64 | sn := &openolt.SerialNumber{VendorId: vi, VendorSpecific: vs} |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 65 | // logger.Infow(nil, "serial-num", log.Fields{"sn":sn}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 66 | |
| 67 | return sn |
| 68 | } |
| 69 | |
| 70 | //MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID |
| 71 | func MkUniPortNum(intfID, onuID, uniID uint32) uint32 { |
| 72 | var limit = int(onuID) |
| 73 | if limit > MaxOnusPerPon { |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 74 | logger.Warn(nil, "Warning: exceeded the MAX ONUS per PON") |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 75 | } |
| 76 | return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID |
| 77 | } |
| 78 | |
| 79 | func GetAttCtag(ponIntf uint32) uint32 { |
| 80 | var currCtag uint32 |
| 81 | var ok bool |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 82 | mutex.Lock() |
| 83 | defer mutex.Unlock() |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 84 | if currCtag, ok = AttCtag[ponIntf]; !ok { |
| 85 | // Start with ctag 2 |
| 86 | AttCtag[ponIntf] = 2 |
| 87 | return AttCtag[ponIntf] |
| 88 | } |
| 89 | AttCtag[ponIntf] = currCtag + 1 |
| 90 | return AttCtag[ponIntf] |
| 91 | } |
| 92 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 93 | func GetDtCtag(ponIntf uint32) uint32 { |
| 94 | var currCtag uint32 |
| 95 | var ok bool |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 96 | mutex.Lock() |
| 97 | defer mutex.Unlock() |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 98 | if currCtag, ok = DtCtag[ponIntf]; !ok { |
| 99 | // Start with ctag 1 |
| 100 | DtCtag[ponIntf] = 1 |
| 101 | return DtCtag[ponIntf] |
| 102 | } |
| 103 | DtCtag[ponIntf] = currCtag + 1 |
| 104 | return DtCtag[ponIntf] |
| 105 | } |
| 106 | |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 107 | func GetTtCtag(ponIntf uint32) uint32 { |
| 108 | var currCtag uint32 |
| 109 | var ok bool |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 110 | mutex.Lock() |
| 111 | defer mutex.Unlock() |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 112 | if currCtag, ok = TtCtag[ponIntf]; !ok { |
| 113 | // Start with ctag 1 |
| 114 | TtCtag[ponIntf] = 1 |
| 115 | return TtCtag[ponIntf] |
| 116 | } |
| 117 | TtCtag[ponIntf] = currCtag + 1 |
| 118 | return TtCtag[ponIntf] |
| 119 | } |
| 120 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 121 | func GetAttStag(ponIntf uint32) uint32 { |
| 122 | // start with stag 2 |
| 123 | return ponIntf + 2 |
| 124 | } |
| 125 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 126 | func GetDtStag(ponIntf uint32, onuID uint32, uniID uint32) uint32 { |
| 127 | // Dt workflow requires unique stag for each subscriber |
| 128 | key := DtStagKey{ponIntf: ponIntf, onuID: onuID, uniID: uniID} |
Girish Gowdra | 390f12f | 2021-07-01 15:53:49 -0700 | [diff] [blame] | 129 | mutex.Lock() |
| 130 | defer mutex.Unlock() |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 131 | if value, ok := DtStag[key]; ok { |
| 132 | return value |
| 133 | } else { |
| 134 | DtStag[key] = currDtStag + 1 |
| 135 | currDtStag = DtStag[key] |
| 136 | } |
| 137 | return DtStag[key] |
| 138 | } |
| 139 | |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 140 | func GetTtStag(ponIntf uint32) uint32 { |
| 141 | // start with stag 2 |
| 142 | return ponIntf + 2 |
| 143 | } |
| 144 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 145 | // TODO: More workflow support to be added here |
| 146 | func GetCtag(workFlowName string, ponIntf uint32) uint32 { |
| 147 | switch workFlowName { |
| 148 | case "ATT": |
| 149 | return GetAttCtag(ponIntf) |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 150 | case "DT": |
| 151 | return GetDtCtag(ponIntf) |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 152 | case "TT": |
| 153 | return GetTtCtag(ponIntf) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 154 | default: |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 155 | logger.Errorw(nil, "unknown-workflowname", log.Fields{"workflow": workFlowName}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 156 | } |
| 157 | return 0 |
| 158 | } |
| 159 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 160 | func GetStag(workFlowName string, ponIntf uint32, onuID uint32, uniID uint32) uint32 { |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 161 | switch workFlowName { |
| 162 | case "ATT": |
| 163 | return GetAttStag(ponIntf) |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 164 | case "DT": |
| 165 | return GetDtStag(ponIntf, onuID, uniID) |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 166 | case "TT": |
| 167 | return GetTtStag(ponIntf) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 168 | default: |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame] | 169 | logger.Errorw(nil, "unknown-workflowname", log.Fields{"workflow": workFlowName}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 170 | } |
| 171 | return 0 |
| 172 | } |