blob: 1bcdedd11f6508b364de59cc97454f6d9e870aeb [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"
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053021
Girish Gowdra5d7d6442020-09-08 17:03:11 -070022 "github.com/opencord/voltha-lib-go/v4/pkg/log"
23 "github.com/opencord/voltha-protos/v4/go/openolt"
Girish Gowdra64503432020-01-07 10:59:10 +053024)
25
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053026type DtStagKey struct {
27 ponIntf, onuID, uniID uint32
28}
29
30var currDtStag uint32
31var DtStag map[DtStagKey]uint32
32var DtCtag map[uint32]uint32
Girish Gowdra64503432020-01-07 10:59:10 +053033var AttCtag map[uint32]uint32
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030034var TtCtag map[uint32]uint32
Girish Gowdra64503432020-01-07 10:59:10 +053035
36func init() {
Girish Gowdra64503432020-01-07 10:59:10 +053037 AttCtag = make(map[uint32]uint32)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053038 DtCtag = make(map[uint32]uint32)
39 DtStag = make(map[DtStagKey]uint32)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030040 TtCtag = make(map[uint32]uint32)
Girish Gowdra64503432020-01-07 10:59:10 +053041}
42
43const (
44 vendorName = "ABCD"
45 // Number of bits for the physical UNI of the ONUs
46 bitsForUniID = 4
47 // Number of bits for the ONU ID
48 bitsForONUID = 8
49 //MaxOnusPerPon is Max number of ONUs on any PON port
50 MaxOnusPerPon = 1 << bitsForONUID
51)
52
53var vendorSpecificId = 1000
54
55func GenerateNextONUSerialNumber() *openolt.SerialNumber {
56
57 vi := []byte(vendorName)
58
59 vendorSpecificId += 1
60 vs := []byte(fmt.Sprint(vendorSpecificId))
Girish Gowdra5d7d6442020-09-08 17:03:11 -070061 // logger.Infow(nil, "vendor-id-and-vendor-specific", log.Fields{"vi":vi, "vs":vs})
Girish Gowdra64503432020-01-07 10:59:10 +053062 sn := &openolt.SerialNumber{VendorId: vi, VendorSpecific: vs}
Girish Gowdra5d7d6442020-09-08 17:03:11 -070063 // logger.Infow(nil, "serial-num", log.Fields{"sn":sn})
Girish Gowdra64503432020-01-07 10:59:10 +053064
65 return sn
66}
67
68//MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID
69func MkUniPortNum(intfID, onuID, uniID uint32) uint32 {
70 var limit = int(onuID)
71 if limit > MaxOnusPerPon {
Girish Gowdra5d7d6442020-09-08 17:03:11 -070072 logger.Warn(nil, "Warning: exceeded the MAX ONUS per PON")
Girish Gowdra64503432020-01-07 10:59:10 +053073 }
74 return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID
75}
76
77func GetAttCtag(ponIntf uint32) uint32 {
78 var currCtag uint32
79 var ok bool
80 if currCtag, ok = AttCtag[ponIntf]; !ok {
81 // Start with ctag 2
82 AttCtag[ponIntf] = 2
83 return AttCtag[ponIntf]
84 }
85 AttCtag[ponIntf] = currCtag + 1
86 return AttCtag[ponIntf]
87}
88
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053089func GetDtCtag(ponIntf uint32) uint32 {
90 var currCtag uint32
91 var ok bool
92 if currCtag, ok = DtCtag[ponIntf]; !ok {
93 // Start with ctag 1
94 DtCtag[ponIntf] = 1
95 return DtCtag[ponIntf]
96 }
97 DtCtag[ponIntf] = currCtag + 1
98 return DtCtag[ponIntf]
99}
100
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300101func GetTtCtag(ponIntf uint32) uint32 {
102 var currCtag uint32
103 var ok bool
104 if currCtag, ok = TtCtag[ponIntf]; !ok {
105 // Start with ctag 1
106 TtCtag[ponIntf] = 1
107 return TtCtag[ponIntf]
108 }
109 TtCtag[ponIntf] = currCtag + 1
110 return TtCtag[ponIntf]
111}
112
Girish Gowdra64503432020-01-07 10:59:10 +0530113func GetAttStag(ponIntf uint32) uint32 {
114 // start with stag 2
115 return ponIntf + 2
116}
117
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530118func GetDtStag(ponIntf uint32, onuID uint32, uniID uint32) uint32 {
119 // Dt workflow requires unique stag for each subscriber
120 key := DtStagKey{ponIntf: ponIntf, onuID: onuID, uniID: uniID}
121
122 if value, ok := DtStag[key]; ok {
123 return value
124 } else {
125 DtStag[key] = currDtStag + 1
126 currDtStag = DtStag[key]
127 }
128 return DtStag[key]
129}
130
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300131func GetTtStag(ponIntf uint32) uint32 {
132 // start with stag 2
133 return ponIntf + 2
134}
135
Girish Gowdra64503432020-01-07 10:59:10 +0530136// TODO: More workflow support to be added here
137func GetCtag(workFlowName string, ponIntf uint32) uint32 {
138 switch workFlowName {
139 case "ATT":
140 return GetAttCtag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530141 case "DT":
142 return GetDtCtag(ponIntf)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300143 case "TT":
144 return GetTtCtag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530145 default:
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700146 logger.Errorw(nil, "unknown-workflowname", log.Fields{"workflow": workFlowName})
Girish Gowdra64503432020-01-07 10:59:10 +0530147 }
148 return 0
149}
150
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530151func GetStag(workFlowName string, ponIntf uint32, onuID uint32, uniID uint32) uint32 {
Girish Gowdra64503432020-01-07 10:59:10 +0530152 switch workFlowName {
153 case "ATT":
154 return GetAttStag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530155 case "DT":
156 return GetDtStag(ponIntf, onuID, uniID)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300157 case "TT":
158 return GetTtStag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530159 default:
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700160 logger.Errorw(nil, "unknown-workflowname", log.Fields{"workflow": workFlowName})
Girish Gowdra64503432020-01-07 10:59:10 +0530161 }
162 return 0
163}