blob: 79a4be2613c18b40dbfc8cf7cfe81d5a2f5d57fb [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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
17package device
18
19import (
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090020 "reflect"
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090021 "sync"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022
23 "gerrit.opencord.org/voltha-bbsim/common/logger"
24 "gerrit.opencord.org/voltha-bbsim/protos"
25 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090026)
27
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090028const (
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090029 ONU_INACTIVE DeviceState = iota //TODO: Each stage name should be more accurate
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090030 ONU_ACTIVE
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090031 ONU_OMCIACTIVE
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032)
33
34type Onu struct {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090035 InternalState DeviceState
Matteo Scandolo88e91892018-11-06 16:29:19 -080036 OltID uint32
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090037 IntfID uint32
38 OperState string
39 SerialNumber *openolt.SerialNumber
40 OnuID uint32
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090041 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042}
43
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090044func NewSN(oltid uint32, intfid uint32, onuid uint32) []byte {
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090045 sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090046 return sn
47}
48
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090049func NewOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090050 onus := []*Onu{}
51 for i := 0; i < int(nonus); i++ {
52 onu := Onu{}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090053 onu.InternalState = ONU_INACTIVE
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090054 onu.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090055 onu.IntfID = intfid
Matteo Scandolo88e91892018-11-06 16:29:19 -080056 onu.OltID = oltid
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090057 onu.OperState = "up"
58 onu.SerialNumber = new(openolt.SerialNumber)
Matteo Scandoloa96633a2018-10-18 16:23:02 -070059 onu.SerialNumber.VendorId = []byte("BBSM")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090060 onu.SerialNumber.VendorSpecific = NewSN(oltid, intfid, uint32(i))
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090061 onus = append(onus, &onu)
62 }
63 return onus
64}
65
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090066func (onu *Onu) Initialize() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090067 onu.OperState = "up"
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090068 onu.InternalState = ONU_INACTIVE
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090069}
70
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090071func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool {
72 for _, onus := range regonus {
73 for _, onu := range onus {
74 if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) {
75 return true
76 }
77 }
78 }
79 return false
80}
81
82func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool {
83 return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific)
84}
85
86func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) {
Matteo Scandolo88e91892018-11-06 16:29:19 -080087 for _, onu := range onus {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090088 onu.OperState = "up"
Matteo Scandolo88e91892018-11-06 16:29:19 -080089 logger.WithFields(log.Fields{
90 "onu": onu.SerialNumber,
91 "pon_interface": ponif,
92 }).Info("ONU discovered.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090093 }
94}
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090095
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090096func (onu *Onu) UpdateIntState(intstate DeviceState) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090097 onu.mu.Lock()
98 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090099 onu.InternalState = intstate
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900100}
101
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900102func (onu *Onu) GetDevkey () Devkey {
103 return Devkey{ID: onu.OnuID, Intfid:onu.IntfID}
104}
105
106func (onu *Onu) GetIntState() DeviceState {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900107 onu.mu.Lock()
108 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900109 return onu.InternalState
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900110}