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