blob: eca4f204e83dc43166f97217277a75fe1d4da16f [file] [log] [blame]
Girish Gowdra64503432020-01-07 10:59:10 +05301/*
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
17package core
18
19import (
20 "fmt"
Girish Gowdra390f12f2021-07-01 15:53:49 -070021 "sync"
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053022
Girish Gowdra390f12f2021-07-01 15:53:49 -070023 "github.com/opencord/voltha-lib-go/v7/pkg/log"
24 "github.com/opencord/voltha-protos/v5/go/openolt"
Girish Gowdra64503432020-01-07 10:59:10 +053025)
26
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053027type DtStagKey struct {
28 ponIntf, onuID, uniID uint32
29}
30
31var currDtStag uint32
32var DtStag map[DtStagKey]uint32
33var DtCtag map[uint32]uint32
Girish Gowdra64503432020-01-07 10:59:10 +053034var AttCtag map[uint32]uint32
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030035var TtCtag map[uint32]uint32
Girish Gowdra390f12f2021-07-01 15:53:49 -070036var mutex sync.RWMutex
Girish Gowdra64503432020-01-07 10:59:10 +053037
38func init() {
Girish Gowdra64503432020-01-07 10:59:10 +053039 AttCtag = make(map[uint32]uint32)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053040 DtCtag = make(map[uint32]uint32)
41 DtStag = make(map[DtStagKey]uint32)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030042 TtCtag = make(map[uint32]uint32)
Girish Gowdra64503432020-01-07 10:59:10 +053043}
44
45const (
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
55var vendorSpecificId = 1000
56
57func GenerateNextONUSerialNumber() *openolt.SerialNumber {
58
59 vi := []byte(vendorName)
60
61 vendorSpecificId += 1
62 vs := []byte(fmt.Sprint(vendorSpecificId))
Girish Gowdra5d7d6442020-09-08 17:03:11 -070063 // logger.Infow(nil, "vendor-id-and-vendor-specific", log.Fields{"vi":vi, "vs":vs})
Girish Gowdra64503432020-01-07 10:59:10 +053064 sn := &openolt.SerialNumber{VendorId: vi, VendorSpecific: vs}
Girish Gowdra5d7d6442020-09-08 17:03:11 -070065 // logger.Infow(nil, "serial-num", log.Fields{"sn":sn})
Girish Gowdra64503432020-01-07 10:59:10 +053066
67 return sn
68}
69
70//MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID
71func MkUniPortNum(intfID, onuID, uniID uint32) uint32 {
72 var limit = int(onuID)
73 if limit > MaxOnusPerPon {
Girish Gowdra5d7d6442020-09-08 17:03:11 -070074 logger.Warn(nil, "Warning: exceeded the MAX ONUS per PON")
Girish Gowdra64503432020-01-07 10:59:10 +053075 }
76 return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID
77}
78
79func GetAttCtag(ponIntf uint32) uint32 {
80 var currCtag uint32
81 var ok bool
Girish Gowdra390f12f2021-07-01 15:53:49 -070082 mutex.Lock()
83 defer mutex.Unlock()
Girish Gowdra64503432020-01-07 10:59:10 +053084 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 Subramanib83b0432020-01-08 13:43:28 +053093func GetDtCtag(ponIntf uint32) uint32 {
94 var currCtag uint32
95 var ok bool
Girish Gowdra390f12f2021-07-01 15:53:49 -070096 mutex.Lock()
97 defer mutex.Unlock()
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053098 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 Kupusoglu66b00d82020-03-13 12:06:33 +0300107func GetTtCtag(ponIntf uint32) uint32 {
108 var currCtag uint32
109 var ok bool
Girish Gowdra390f12f2021-07-01 15:53:49 -0700110 mutex.Lock()
111 defer mutex.Unlock()
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300112 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 Gowdra64503432020-01-07 10:59:10 +0530121func GetAttStag(ponIntf uint32) uint32 {
122 // start with stag 2
123 return ponIntf + 2
124}
125
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530126func 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 Gowdra390f12f2021-07-01 15:53:49 -0700129 mutex.Lock()
130 defer mutex.Unlock()
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530131 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 Kupusoglu66b00d82020-03-13 12:06:33 +0300140func GetTtStag(ponIntf uint32) uint32 {
141 // start with stag 2
142 return ponIntf + 2
143}
144
Girish Gowdra64503432020-01-07 10:59:10 +0530145// TODO: More workflow support to be added here
146func GetCtag(workFlowName string, ponIntf uint32) uint32 {
147 switch workFlowName {
148 case "ATT":
149 return GetAttCtag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530150 case "DT":
151 return GetDtCtag(ponIntf)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300152 case "TT":
153 return GetTtCtag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530154 default:
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700155 logger.Errorw(nil, "unknown-workflowname", log.Fields{"workflow": workFlowName})
Girish Gowdra64503432020-01-07 10:59:10 +0530156 }
157 return 0
158}
159
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530160func GetStag(workFlowName string, ponIntf uint32, onuID uint32, uniID uint32) uint32 {
Girish Gowdra64503432020-01-07 10:59:10 +0530161 switch workFlowName {
162 case "ATT":
163 return GetAttStag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530164 case "DT":
165 return GetDtStag(ponIntf, onuID, uniID)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300166 case "TT":
167 return GetTtStag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530168 default:
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700169 logger.Errorw(nil, "unknown-workflowname", log.Fields{"workflow": workFlowName})
Girish Gowdra64503432020-01-07 10:59:10 +0530170 }
171 return 0
172}