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" |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 21 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 23 | "github.com/opencord/voltha-protos/v2/go/openolt" |
| 24 | ) |
| 25 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 26 | type DtStagKey struct { |
| 27 | ponIntf, onuID, uniID uint32 |
| 28 | } |
| 29 | |
| 30 | var currDtStag uint32 |
| 31 | var DtStag map[DtStagKey]uint32 |
| 32 | var DtCtag map[uint32]uint32 |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 33 | var AttCtag map[uint32]uint32 |
| 34 | |
| 35 | func init() { |
| 36 | _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 37 | AttCtag = make(map[uint32]uint32) |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 38 | DtCtag = make(map[uint32]uint32) |
| 39 | DtStag = make(map[DtStagKey]uint32) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | const ( |
| 43 | vendorName = "ABCD" |
| 44 | // Number of bits for the physical UNI of the ONUs |
| 45 | bitsForUniID = 4 |
| 46 | // Number of bits for the ONU ID |
| 47 | bitsForONUID = 8 |
| 48 | //MaxOnusPerPon is Max number of ONUs on any PON port |
| 49 | MaxOnusPerPon = 1 << bitsForONUID |
| 50 | ) |
| 51 | |
| 52 | var vendorSpecificId = 1000 |
| 53 | |
| 54 | func GenerateNextONUSerialNumber() *openolt.SerialNumber { |
| 55 | |
| 56 | vi := []byte(vendorName) |
| 57 | |
| 58 | vendorSpecificId += 1 |
| 59 | vs := []byte(fmt.Sprint(vendorSpecificId)) |
| 60 | // log.Infow("vendor-id-and-vendor-specific", log.Fields{"vi":vi, "vs":vs}) |
| 61 | sn := &openolt.SerialNumber{VendorId: vi, VendorSpecific: vs} |
| 62 | // log.Infow("serial-num", log.Fields{"sn":sn}) |
| 63 | |
| 64 | return sn |
| 65 | } |
| 66 | |
| 67 | //MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID |
| 68 | func MkUniPortNum(intfID, onuID, uniID uint32) uint32 { |
| 69 | var limit = int(onuID) |
| 70 | if limit > MaxOnusPerPon { |
| 71 | log.Warn("Warning: exceeded the MAX ONUS per PON") |
| 72 | } |
| 73 | return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID |
| 74 | } |
| 75 | |
| 76 | func GetAttCtag(ponIntf uint32) uint32 { |
| 77 | var currCtag uint32 |
| 78 | var ok bool |
| 79 | if currCtag, ok = AttCtag[ponIntf]; !ok { |
| 80 | // Start with ctag 2 |
| 81 | AttCtag[ponIntf] = 2 |
| 82 | return AttCtag[ponIntf] |
| 83 | } |
| 84 | AttCtag[ponIntf] = currCtag + 1 |
| 85 | return AttCtag[ponIntf] |
| 86 | } |
| 87 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 88 | func GetDtCtag(ponIntf uint32) uint32 { |
| 89 | var currCtag uint32 |
| 90 | var ok bool |
| 91 | if currCtag, ok = DtCtag[ponIntf]; !ok { |
| 92 | // Start with ctag 1 |
| 93 | DtCtag[ponIntf] = 1 |
| 94 | return DtCtag[ponIntf] |
| 95 | } |
| 96 | DtCtag[ponIntf] = currCtag + 1 |
| 97 | return DtCtag[ponIntf] |
| 98 | } |
| 99 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 100 | func GetAttStag(ponIntf uint32) uint32 { |
| 101 | // start with stag 2 |
| 102 | return ponIntf + 2 |
| 103 | } |
| 104 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 105 | func GetDtStag(ponIntf uint32, onuID uint32, uniID uint32) uint32 { |
| 106 | // Dt workflow requires unique stag for each subscriber |
| 107 | key := DtStagKey{ponIntf: ponIntf, onuID: onuID, uniID: uniID} |
| 108 | |
| 109 | if value, ok := DtStag[key]; ok { |
| 110 | return value |
| 111 | } else { |
| 112 | DtStag[key] = currDtStag + 1 |
| 113 | currDtStag = DtStag[key] |
| 114 | } |
| 115 | return DtStag[key] |
| 116 | } |
| 117 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 118 | // TODO: More workflow support to be added here |
| 119 | func GetCtag(workFlowName string, ponIntf uint32) uint32 { |
| 120 | switch workFlowName { |
| 121 | case "ATT": |
| 122 | return GetAttCtag(ponIntf) |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 123 | case "DT": |
| 124 | return GetDtCtag(ponIntf) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 125 | default: |
| 126 | log.Errorw("unknown-workflowname", log.Fields{"workflow": workFlowName}) |
| 127 | } |
| 128 | return 0 |
| 129 | } |
| 130 | |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 131 | func GetStag(workFlowName string, ponIntf uint32, onuID uint32, uniID uint32) uint32 { |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 132 | switch workFlowName { |
| 133 | case "ATT": |
| 134 | return GetAttStag(ponIntf) |
Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +0530 | [diff] [blame] | 135 | case "DT": |
| 136 | return GetDtStag(ponIntf, onuID, uniID) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 137 | default: |
| 138 | log.Errorw("unknown-workflowname", log.Fields{"workflow": workFlowName}) |
| 139 | } |
| 140 | return 0 |
| 141 | } |