blob: 1389162ea079270a47b6a47a60705dabb63d7976 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070017package devices
18
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070019import (
Pragya Arya996a0892020-03-09 21:47:52 +053020 "math/rand"
Pragya Arya324337e2020-02-20 14:35:08 +053021 "time"
22
23 "github.com/looplab/fsm"
24 "github.com/opencord/bbsim/internal/common"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000025 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070026)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070027
Pragya Arya2225f202020-01-29 18:05:01 +053028type mode int
29
30// Constants for Controlled Activation modes
31const (
32 Default mode = iota
33 OnlyONU
34 OnlyPON
35 Both
36)
37
38// ControlledActivationModes maps string to int value of mode
39var ControlledActivationModes = map[string]mode{
40 "default": Default,
41 "only-onu": OnlyONU,
42 "only-pon": OnlyPON,
43 "both": Both,
44}
45
Matteo Scandolo11006992019-08-28 11:29:46 -070046var newFSM = fsm.NewFSM
47
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070048func getOperStateFSM(cb fsm.Callback) *fsm.FSM {
Matteo Scandolo11006992019-08-28 11:29:46 -070049 return newFSM(
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070050 "down",
51 fsm.Events{
52 {Name: "enable", Src: []string{"down"}, Dst: "up"},
53 {Name: "disable", Src: []string{"up"}, Dst: "down"},
54 },
55 fsm.Callbacks{
56 "enter_state": func(e *fsm.Event) {
57 cb(e)
58 },
59 },
60 )
61}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070062
Pragya Arya324337e2020-02-20 14:35:08 +053063func publishEvent(eventType string, intfID int32, onuID int32, onuSerial string) {
64 if olt.PublishEvents {
65 currentTime := time.Now()
66
67 event := common.Event{
68 EventType: eventType,
69 OltID: olt.ID,
70 IntfID: intfID,
71 OnuID: onuID,
72 OnuSerial: onuSerial,
73 Timestamp: currentTime.Format("2006-01-02 15:04:05.000000000"),
74 EpochTime: currentTime.UnixNano() / 1000000,
75 }
76 olt.EventChannel <- event
77 }
78}
Pragya Arya996a0892020-03-09 21:47:52 +053079
80func getPortStats(packetCount uint64, incrementStat bool) (*openolt.PortStatistics, uint64) {
81 // increment current packet count by random number
82 if incrementStat {
83 packetCount = packetCount + uint64(rand.Intn(50)+1*10)
84 }
85
86 // fill all other stats based on packet count
87 portStats := &openolt.PortStatistics{
88 RxBytes: packetCount * 64,
89 RxPackets: packetCount,
90 RxUcastPackets: packetCount * 40 / 100,
91 RxMcastPackets: packetCount * 30 / 100,
92 RxBcastPackets: packetCount * 30 / 100,
93 RxErrorPackets: 0,
94 TxBytes: packetCount * 64,
95 TxPackets: packetCount,
96 TxUcastPackets: packetCount * 40 / 100,
97 TxMcastPackets: packetCount * 30 / 100,
98 TxBcastPackets: packetCount * 30 / 100,
99 TxErrorPackets: 0,
100 RxCrcErrors: 0,
101 BipErrors: 0,
102 Timestamp: uint32(time.Now().Unix()),
103 }
104
105 return portStats, packetCount
106}
107
108// InterfaceIDToPortNo converts InterfaceID to voltha PortID
109// Refer openolt adapter code(master) voltha-openolt-adapter/adaptercore/olt_platform.go: IntfIDToPortNo()
110func InterfaceIDToPortNo(intfID uint32, intfType string) uint32 {
111 // Converts interface-id to port-numbers that can be understood by the VOLTHA
112 if intfType == "nni" {
113 // nni at voltha starts with 1,048,576
114 // nni = 1,048,576 + InterfaceID
115 return 0x1<<20 + intfID
116 } else if intfType == "pon" {
117 // pon = 536,870,912 + InterfaceID
118 return (0x2 << 28) + intfID
119 }
120 return 0
121}