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 | |
| 23 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 24 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 25 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type onuState int |
| 29 | |
| 30 | const ( |
| 31 | ONU_PRE_ACTIVATED onuState = iota |
| 32 | ONU_ACTIVATED |
| 33 | ) |
| 34 | |
| 35 | type Onu struct { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 36 | InternalState *onuState |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame^] | 37 | OltID uint32 |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 38 | IntfID uint32 |
| 39 | OperState string |
| 40 | SerialNumber *openolt.SerialNumber |
| 41 | OnuID uint32 |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 42 | mu *sync.Mutex |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 43 | } |
| 44 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 45 | func NewSN(oltid uint32, intfid uint32, onuid uint32) []byte { |
Keita NISHIMOTO | 246b828 | 2018-10-13 04:14:51 +0900 | [diff] [blame] | 46 | sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 47 | return sn |
| 48 | } |
| 49 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 50 | func NewOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | onus := []*Onu{} |
| 52 | for i := 0; i < int(nonus); i++ { |
| 53 | onu := Onu{} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 54 | onu.InternalState = new(onuState) |
| 55 | *onu.InternalState = ONU_PRE_ACTIVATED |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 56 | onu.mu = &sync.Mutex{} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 57 | onu.IntfID = intfid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame^] | 58 | onu.OltID = oltid |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 59 | onu.OperState = "up" |
| 60 | onu.SerialNumber = new(openolt.SerialNumber) |
Matteo Scandolo | a96633a | 2018-10-18 16:23:02 -0700 | [diff] [blame] | 61 | onu.SerialNumber.VendorId = []byte("BBSM") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 62 | onu.SerialNumber.VendorSpecific = NewSN(oltid, intfid, uint32(i)) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 63 | onus = append(onus, &onu) |
| 64 | } |
| 65 | return onus |
| 66 | } |
| 67 | |
Matteo Scandolo | a96633a | 2018-10-18 16:23:02 -0700 | [diff] [blame] | 68 | func (onu *Onu) InitializeStatus() { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 69 | onu.OperState = "up" |
| 70 | *onu.InternalState = ONU_PRE_ACTIVATED |
| 71 | } |
| 72 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 73 | func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool { |
| 74 | for _, onus := range regonus { |
| 75 | for _, onu := range onus { |
| 76 | if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) { |
| 77 | return true |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool { |
| 85 | return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific) |
| 86 | } |
| 87 | |
| 88 | func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame^] | 89 | for _, onu := range onus { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 90 | onu.OperState = "up" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame^] | 91 | logger.WithFields(log.Fields{ |
| 92 | "onu": onu.SerialNumber, |
| 93 | "pon_interface": ponif, |
| 94 | }).Info("ONU discovered.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 95 | } |
| 96 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 97 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 98 | func (onu *Onu) UpdateIntStatus(intstatus onuState) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 99 | onu.mu.Lock() |
| 100 | defer onu.mu.Unlock() |
| 101 | *onu.InternalState = intstatus |
| 102 | } |
| 103 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 104 | func (onu *Onu) GetIntStatus() onuState { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 105 | onu.mu.Lock() |
| 106 | defer onu.mu.Unlock() |
| 107 | return *onu.InternalState |
| 108 | } |