blob: b239443ff9d029ea71c605f7bfd069088b18ab6f [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
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030022 "github.com/opencord/voltha-lib-go/v3/pkg/log"
23 "github.com/opencord/voltha-protos/v3/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() {
37 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
38 AttCtag = make(map[uint32]uint32)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053039 DtCtag = make(map[uint32]uint32)
40 DtStag = make(map[DtStagKey]uint32)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +030041 TtCtag = make(map[uint32]uint32)
Girish Gowdra64503432020-01-07 10:59:10 +053042}
43
44const (
45 vendorName = "ABCD"
46 // Number of bits for the physical UNI of the ONUs
47 bitsForUniID = 4
48 // Number of bits for the ONU ID
49 bitsForONUID = 8
50 //MaxOnusPerPon is Max number of ONUs on any PON port
51 MaxOnusPerPon = 1 << bitsForONUID
52)
53
54var vendorSpecificId = 1000
55
56func GenerateNextONUSerialNumber() *openolt.SerialNumber {
57
58 vi := []byte(vendorName)
59
60 vendorSpecificId += 1
61 vs := []byte(fmt.Sprint(vendorSpecificId))
62 // log.Infow("vendor-id-and-vendor-specific", log.Fields{"vi":vi, "vs":vs})
63 sn := &openolt.SerialNumber{VendorId: vi, VendorSpecific: vs}
64 // log.Infow("serial-num", log.Fields{"sn":sn})
65
66 return sn
67}
68
69//MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID
70func MkUniPortNum(intfID, onuID, uniID uint32) uint32 {
71 var limit = int(onuID)
72 if limit > MaxOnusPerPon {
73 log.Warn("Warning: exceeded the MAX ONUS per PON")
74 }
75 return (intfID << (bitsForUniID + bitsForONUID)) | (onuID << bitsForUniID) | uniID
76}
77
78func GetAttCtag(ponIntf uint32) uint32 {
79 var currCtag uint32
80 var ok bool
81 if currCtag, ok = AttCtag[ponIntf]; !ok {
82 // Start with ctag 2
83 AttCtag[ponIntf] = 2
84 return AttCtag[ponIntf]
85 }
86 AttCtag[ponIntf] = currCtag + 1
87 return AttCtag[ponIntf]
88}
89
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053090func GetDtCtag(ponIntf uint32) uint32 {
91 var currCtag uint32
92 var ok bool
93 if currCtag, ok = DtCtag[ponIntf]; !ok {
94 // Start with ctag 1
95 DtCtag[ponIntf] = 1
96 return DtCtag[ponIntf]
97 }
98 DtCtag[ponIntf] = currCtag + 1
99 return DtCtag[ponIntf]
100}
101
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300102func GetTtCtag(ponIntf uint32) uint32 {
103 var currCtag uint32
104 var ok bool
105 if currCtag, ok = TtCtag[ponIntf]; !ok {
106 // Start with ctag 1
107 TtCtag[ponIntf] = 1
108 return TtCtag[ponIntf]
109 }
110 TtCtag[ponIntf] = currCtag + 1
111 return TtCtag[ponIntf]
112}
113
Girish Gowdra64503432020-01-07 10:59:10 +0530114func GetAttStag(ponIntf uint32) uint32 {
115 // start with stag 2
116 return ponIntf + 2
117}
118
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530119func GetDtStag(ponIntf uint32, onuID uint32, uniID uint32) uint32 {
120 // Dt workflow requires unique stag for each subscriber
121 key := DtStagKey{ponIntf: ponIntf, onuID: onuID, uniID: uniID}
122
123 if value, ok := DtStag[key]; ok {
124 return value
125 } else {
126 DtStag[key] = currDtStag + 1
127 currDtStag = DtStag[key]
128 }
129 return DtStag[key]
130}
131
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300132func GetTtStag(ponIntf uint32) uint32 {
133 // start with stag 2
134 return ponIntf + 2
135}
136
Girish Gowdra64503432020-01-07 10:59:10 +0530137// TODO: More workflow support to be added here
138func GetCtag(workFlowName string, ponIntf uint32) uint32 {
139 switch workFlowName {
140 case "ATT":
141 return GetAttCtag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530142 case "DT":
143 return GetDtCtag(ponIntf)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300144 case "TT":
145 return GetTtCtag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530146 default:
147 log.Errorw("unknown-workflowname", log.Fields{"workflow": workFlowName})
148 }
149 return 0
150}
151
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530152func GetStag(workFlowName string, ponIntf uint32, onuID uint32, uniID uint32) uint32 {
Girish Gowdra64503432020-01-07 10:59:10 +0530153 switch workFlowName {
154 case "ATT":
155 return GetAttStag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530156 case "DT":
157 return GetDtStag(ponIntf, onuID, uniID)
Orhan Kupusoglu66b00d82020-03-13 12:06:33 +0300158 case "TT":
159 return GetTtStag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530160 default:
161 log.Errorw("unknown-workflowname", log.Fields{"workflow": workFlowName})
162 }
163 return 0
164}