Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 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 Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 17 | package devices |
| 18 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 19 | import ( |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 20 | "math/rand" |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 21 | "time" |
| 22 | |
| 23 | "github.com/looplab/fsm" |
| 24 | "github.com/opencord/bbsim/internal/common" |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 26 | ) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 27 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 28 | type mode int |
| 29 | |
| 30 | // Constants for Controlled Activation modes |
| 31 | const ( |
| 32 | Default mode = iota |
| 33 | OnlyONU |
| 34 | OnlyPON |
| 35 | Both |
| 36 | ) |
| 37 | |
| 38 | // ControlledActivationModes maps string to int value of mode |
| 39 | var ControlledActivationModes = map[string]mode{ |
| 40 | "default": Default, |
| 41 | "only-onu": OnlyONU, |
| 42 | "only-pon": OnlyPON, |
| 43 | "both": Both, |
| 44 | } |
| 45 | |
Abhay Kumar | c5723cc | 2023-06-08 12:09:30 +0530 | [diff] [blame^] | 46 | // Constants for openolt Flows |
Elia Battiston | 560e955 | 2022-01-31 10:44:15 +0100 | [diff] [blame] | 47 | const ( |
| 48 | flowTypeUpstream = "upstream" |
| 49 | flowTypeDownstream = "downstream" |
| 50 | flowTagTypeSingle = "single_tag" |
| 51 | flowTagTypeDouble = "double_tag" |
| 52 | ) |
| 53 | |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 54 | var newFSM = fsm.NewFSM |
| 55 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 56 | func getOperStateFSM(cb fsm.Callback) *fsm.FSM { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 57 | return newFSM( |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 58 | "down", |
| 59 | fsm.Events{ |
| 60 | {Name: "enable", Src: []string{"down"}, Dst: "up"}, |
| 61 | {Name: "disable", Src: []string{"up"}, Dst: "down"}, |
| 62 | }, |
| 63 | fsm.Callbacks{ |
| 64 | "enter_state": func(e *fsm.Event) { |
| 65 | cb(e) |
| 66 | }, |
| 67 | }, |
| 68 | ) |
| 69 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 70 | |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 71 | func publishEvent(eventType string, intfID int32, onuID int32, onuSerial string) { |
| 72 | if olt.PublishEvents { |
| 73 | currentTime := time.Now() |
| 74 | |
| 75 | event := common.Event{ |
| 76 | EventType: eventType, |
| 77 | OltID: olt.ID, |
| 78 | IntfID: intfID, |
| 79 | OnuID: onuID, |
| 80 | OnuSerial: onuSerial, |
| 81 | Timestamp: currentTime.Format("2006-01-02 15:04:05.000000000"), |
| 82 | EpochTime: currentTime.UnixNano() / 1000000, |
| 83 | } |
| 84 | olt.EventChannel <- event |
| 85 | } |
| 86 | } |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 87 | |
| 88 | func getPortStats(packetCount uint64, incrementStat bool) (*openolt.PortStatistics, uint64) { |
| 89 | // increment current packet count by random number |
| 90 | if incrementStat { |
| 91 | packetCount = packetCount + uint64(rand.Intn(50)+1*10) |
| 92 | } |
| 93 | |
| 94 | // fill all other stats based on packet count |
| 95 | portStats := &openolt.PortStatistics{ |
| 96 | RxBytes: packetCount * 64, |
| 97 | RxPackets: packetCount, |
| 98 | RxUcastPackets: packetCount * 40 / 100, |
| 99 | RxMcastPackets: packetCount * 30 / 100, |
| 100 | RxBcastPackets: packetCount * 30 / 100, |
| 101 | RxErrorPackets: 0, |
| 102 | TxBytes: packetCount * 64, |
| 103 | TxPackets: packetCount, |
| 104 | TxUcastPackets: packetCount * 40 / 100, |
| 105 | TxMcastPackets: packetCount * 30 / 100, |
| 106 | TxBcastPackets: packetCount * 30 / 100, |
| 107 | TxErrorPackets: 0, |
| 108 | RxCrcErrors: 0, |
| 109 | BipErrors: 0, |
| 110 | Timestamp: uint32(time.Now().Unix()), |
| 111 | } |
| 112 | |
| 113 | return portStats, packetCount |
| 114 | } |
| 115 | |
| 116 | // InterfaceIDToPortNo converts InterfaceID to voltha PortID |
| 117 | // Refer openolt adapter code(master) voltha-openolt-adapter/adaptercore/olt_platform.go: IntfIDToPortNo() |
| 118 | func InterfaceIDToPortNo(intfID uint32, intfType string) uint32 { |
| 119 | // Converts interface-id to port-numbers that can be understood by the VOLTHA |
| 120 | if intfType == "nni" { |
| 121 | // nni at voltha starts with 1,048,576 |
| 122 | // nni = 1,048,576 + InterfaceID |
| 123 | return 0x1<<20 + intfID |
| 124 | } else if intfType == "pon" { |
| 125 | // pon = 536,870,912 + InterfaceID |
| 126 | return (0x2 << 28) + intfID |
| 127 | } |
| 128 | return 0 |
| 129 | } |