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