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