Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 20 | "reflect" |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 21 | "sync" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-bbsim/common/logger" |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 24 | openolt "github.com/opencord/voltha-protos/go/openolt" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 25 | techprofile "github.com/opencord/voltha-protos/go/tech_profile" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 26 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 29 | // Constants for the ONU states |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 30 | const ( |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 31 | OnuFree State = iota // TODO: Each stage name should be more accurate |
| 32 | OnuInactive |
| 33 | OnuLosRaised |
| 34 | OnuLosOnOltPonLos |
| 35 | OnuOmciChannelLosRaised |
| 36 | OnuActive |
| 37 | OnuOmciActive |
| 38 | OnuAuthenticated |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 39 | ) |
| 40 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 41 | // ONUState maps int value of device state to string |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 42 | var ONUState = map[State]string{ |
| 43 | OnuFree: "ONU_FREE", |
| 44 | OnuInactive: "ONU_INACTIVE", |
| 45 | OnuLosRaised: "ONU_LOS_RAISED", |
| 46 | OnuLosOnOltPonLos: "ONU_LOS_ON_OLT_PON_LOS", |
| 47 | OnuOmciChannelLosRaised: "ONU_OMCI_CHANNEL_LOS_RAISED", |
| 48 | OnuActive: "ONU_ACTIVE", |
| 49 | OnuOmciActive: "ONU_OMCIACTIVE", |
| 50 | OnuAuthenticated: "ONU_AUTHENTICATED", |
| 51 | } |
| 52 | |
| 53 | // FlowKey used for FlowMap key |
| 54 | type FlowKey struct { |
| 55 | FlowID uint32 |
| 56 | FlowDirection string |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 59 | // Onu structure stores information of ONUs |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 60 | type Onu struct { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 61 | InternalState State |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 62 | OltID uint32 |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 63 | IntfID uint32 |
| 64 | OperState string |
| 65 | SerialNumber *openolt.SerialNumber |
| 66 | OnuID uint32 |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 67 | GemPortMap map[uint32][]uint32 // alloc-id is used as key and corresponding gem-ports are stored in slice |
| 68 | Tconts *techprofile.TrafficSchedulers |
| 69 | Flows []FlowKey |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 70 | mu *sync.Mutex |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 73 | // NewSN constructs and returns serial number based on the OLT ID, intf ID and ONU ID |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 74 | func NewSN(oltid uint32, intfid uint32, onuid uint32) []byte { |
Keita NISHIMOTO | 246b828 | 2018-10-13 04:14:51 +0900 | [diff] [blame] | 75 | sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 76 | return sn |
| 77 | } |
| 78 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 79 | // NewOnus initializes and returns slice of Onu objects |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 80 | func NewOnus(oltid uint32, intfid uint32, nonus uint32) []*Onu { |
| 81 | var onus []*Onu |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 82 | for i := 1; i <= int(nonus); i++ { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 83 | onu := Onu{} |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 84 | onu.InternalState = OnuFree // New Onu Initialised with state ONU_FREE |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 85 | onu.mu = &sync.Mutex{} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 86 | onu.IntfID = intfid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 87 | onu.OltID = oltid |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 88 | onu.OperState = "down" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 89 | onu.SerialNumber = new(openolt.SerialNumber) |
Matteo Scandolo | a96633a | 2018-10-18 16:23:02 -0700 | [diff] [blame] | 90 | onu.SerialNumber.VendorId = []byte("BBSM") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 91 | onu.SerialNumber.VendorSpecific = NewSN(oltid, intfid, uint32(i)) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 92 | onu.GemPortMap = make(map[uint32][]uint32) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 93 | onus = append(onus, &onu) |
| 94 | } |
| 95 | return onus |
| 96 | } |
| 97 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 98 | // Initialize method initializes ONU state to up and ONU_INACTIVE |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 99 | func (onu *Onu) Initialize() { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 100 | onu.OperState = "up" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 101 | onu.InternalState = OnuInactive |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 102 | } |
| 103 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 104 | // ValidateSN compares two serial numbers and returns result as true/false |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 105 | func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool { |
| 106 | return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific) |
| 107 | } |
| 108 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 109 | // UpdateOnusOpStatus method updates ONU oper status |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 110 | func UpdateOnusOpStatus(ponif uint32, onu *Onu, opstatus string) { |
| 111 | onu.OperState = opstatus |
| 112 | logger.WithFields(log.Fields{ |
| 113 | "onu": onu.SerialNumber, |
| 114 | "pon_interface": ponif, |
| 115 | }).Info("ONU OperState Updated") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 116 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 117 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 118 | // UpdateIntState method updates ONU internal state |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 119 | func (onu *Onu) UpdateIntState(intstate State) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 120 | onu.mu.Lock() |
| 121 | defer onu.mu.Unlock() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 122 | onu.InternalState = intstate |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 123 | } |
| 124 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 125 | // GetDevkey returns ONU device key |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 126 | func (onu *Onu) GetDevkey() Devkey { |
| 127 | return Devkey{ID: onu.OnuID, Intfid: onu.IntfID} |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 130 | // GetIntState returns ONU internal state |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 131 | func (onu *Onu) GetIntState() State { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 132 | onu.mu.Lock() |
| 133 | defer onu.mu.Unlock() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 134 | return onu.InternalState |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 135 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 136 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 137 | // DeleteFlow method search and delete flowKey from the onu flows slice |
| 138 | func (onu *Onu) DeleteFlow(key FlowKey) { |
| 139 | for pos, flowKey := range onu.Flows { |
| 140 | if flowKey == key { |
| 141 | // delete the flowKey by shifting all flowKeys by one |
| 142 | onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...) |
| 143 | t := make([]FlowKey, len(onu.Flows)) |
| 144 | copy(t, onu.Flows) |
| 145 | onu.Flows = t |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 146 | break |
| 147 | } |
| 148 | } |
| 149 | } |