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 | "log" |
| 21 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 22 | "reflect" |
| 23 | ) |
| 24 | |
| 25 | type onuState int |
| 26 | |
| 27 | const ( |
| 28 | ONU_PRE_ACTIVATED onuState = iota |
| 29 | ONU_ACTIVATED |
| 30 | ) |
| 31 | |
| 32 | type Onu struct { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 33 | InternalState *onuState |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 34 | IntfID uint32 |
| 35 | OperState string |
| 36 | SerialNumber *openolt.SerialNumber |
| 37 | OnuID uint32 |
| 38 | } |
| 39 | |
Keita NISHIMOTO | 246b828 | 2018-10-13 04:14:51 +0900 | [diff] [blame] | 40 | func createSN(oltid uint32, intfid uint32, onuid uint32) []byte { |
| 41 | sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 42 | return sn |
| 43 | } |
| 44 | |
| 45 | func CreateOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu { |
| 46 | onus := []*Onu{} |
| 47 | for i := 0; i < int(nonus); i++ { |
| 48 | onu := Onu{} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 49 | onu.InternalState = new(onuState) |
| 50 | *onu.InternalState = ONU_PRE_ACTIVATED |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | onu.IntfID = intfid |
| 52 | onu.OperState = "up" |
| 53 | onu.SerialNumber = new(openolt.SerialNumber) |
| 54 | onu.SerialNumber.VendorId = []byte("NONE") |
Keita NISHIMOTO | 246b828 | 2018-10-13 04:14:51 +0900 | [diff] [blame] | 55 | onu.SerialNumber.VendorSpecific = createSN(oltid, intfid, uint32(i)) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 56 | onus = append(onus, &onu) |
| 57 | } |
| 58 | return onus |
| 59 | } |
| 60 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 61 | func (onu *Onu) InitializeStatus(){ |
| 62 | onu.OperState = "up" |
| 63 | *onu.InternalState = ONU_PRE_ACTIVATED |
| 64 | } |
| 65 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 66 | func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool { |
| 67 | for _, onus := range regonus { |
| 68 | for _, onu := range onus { |
| 69 | if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) { |
| 70 | return true |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool { |
| 78 | return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific) |
| 79 | } |
| 80 | |
| 81 | func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) { |
| 82 | for i, onu := range onus { |
| 83 | onu.OperState = "up" |
| 84 | log.Printf("(PONIF:%d) ONU [%d] discovered.\n", ponif, i) |
| 85 | } |
| 86 | } |