blob: a99096e078288f0afae9ecdbbef82cc41a849c06 [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 Gowdra64503432020-01-07 10:59:10 +053022 "github.com/opencord/voltha-lib-go/v2/pkg/log"
23 "github.com/opencord/voltha-protos/v2/go/openolt"
24)
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
34
35func init() {
36 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
37 AttCtag = make(map[uint32]uint32)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +053038 DtCtag = make(map[uint32]uint32)
39 DtStag = make(map[DtStagKey]uint32)
Girish Gowdra64503432020-01-07 10:59:10 +053040}
41
42const (
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
52var vendorSpecificId = 1000
53
54func 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
68func 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
76func 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 Subramanib83b0432020-01-08 13:43:28 +053088func 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 Gowdra64503432020-01-07 10:59:10 +0530100func GetAttStag(ponIntf uint32) uint32 {
101 // start with stag 2
102 return ponIntf + 2
103}
104
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530105func 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 Gowdra64503432020-01-07 10:59:10 +0530118// TODO: More workflow support to be added here
119func GetCtag(workFlowName string, ponIntf uint32) uint32 {
120 switch workFlowName {
121 case "ATT":
122 return GetAttCtag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530123 case "DT":
124 return GetDtCtag(ponIntf)
Girish Gowdra64503432020-01-07 10:59:10 +0530125 default:
126 log.Errorw("unknown-workflowname", log.Fields{"workflow": workFlowName})
127 }
128 return 0
129}
130
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530131func GetStag(workFlowName string, ponIntf uint32, onuID uint32, uniID uint32) uint32 {
Girish Gowdra64503432020-01-07 10:59:10 +0530132 switch workFlowName {
133 case "ATT":
134 return GetAttStag(ponIntf)
Thiyagarajan Subramanib83b0432020-01-08 13:43:28 +0530135 case "DT":
136 return GetDtStag(ponIntf, onuID, uniID)
Girish Gowdra64503432020-01-07 10:59:10 +0530137 default:
138 log.Errorw("unknown-workflowname", log.Fields{"workflow": workFlowName})
139 }
140 return 0
141}