blob: f2338d7320fb026acea3bf819e10156d3f7af562 [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 NISHIMOTO2807c542019-06-04 22:58:32 +090032 ONU_AUTHENTICATED
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020033 ONU_FREE
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090034)
35
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020036// Onu structure stores information of ONUs
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090037type Onu struct {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090038 InternalState DeviceState
Matteo Scandolo88e91892018-11-06 16:29:19 -080039 OltID uint32
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090040 IntfID uint32
41 OperState string
42 SerialNumber *openolt.SerialNumber
43 OnuID uint32
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +090044 GemportID uint16
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090045 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090046}
47
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020048// NewSN constructs and returns serial number based on the OLT ID, intf ID and ONU ID
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090049func NewSN(oltid uint32, intfid uint32, onuid uint32) []byte {
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090050 sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 return sn
52}
53
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020054// NewOnus initializes and returns slice of Onu objects
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090055func NewOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056 onus := []*Onu{}
57 for i := 0; i < int(nonus); i++ {
58 onu := Onu{}
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020059 onu.InternalState = ONU_FREE
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090060 onu.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090061 onu.IntfID = intfid
Matteo Scandolo88e91892018-11-06 16:29:19 -080062 onu.OltID = oltid
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020063 onu.OperState = "down"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090064 onu.SerialNumber = new(openolt.SerialNumber)
Matteo Scandoloa96633a2018-10-18 16:23:02 -070065 onu.SerialNumber.VendorId = []byte("BBSM")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090066 onu.SerialNumber.VendorSpecific = NewSN(oltid, intfid, uint32(i))
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +090067 onu.GemportID = 0
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090068 onus = append(onus, &onu)
69 }
70 return onus
71}
72
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020073// Initialize method initializes ONU state to up and ONU_INACTIVE
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090074func (onu *Onu) Initialize() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090075 onu.OperState = "up"
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090076 onu.InternalState = ONU_INACTIVE
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090077}
78
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020079// ValidateONU method validate ONU based on the serial number in onuMap
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090080func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool {
81 for _, onus := range regonus {
82 for _, onu := range onus {
83 if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) {
84 return true
85 }
86 }
87 }
88 return false
89}
90
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020091// ValidateSN compares two serial numbers and returns result as true/false
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090092func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool {
93 return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific)
94}
95
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020096// UpdateOnusOpStatus method updates ONU oper status
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090097func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) {
Matteo Scandolo88e91892018-11-06 16:29:19 -080098 for _, onu := range onus {
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020099 onu.OperState = opstatus
Matteo Scandolo88e91892018-11-06 16:29:19 -0800100 logger.WithFields(log.Fields{
101 "onu": onu.SerialNumber,
102 "pon_interface": ponif,
103 }).Info("ONU discovered.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900104 }
105}
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900106
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200107// UpdateIntState method updates ONU internal state
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900108func (onu *Onu) UpdateIntState(intstate DeviceState) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900109 onu.mu.Lock()
110 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900111 onu.InternalState = intstate
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900112}
113
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200114// GetDevkey returns ONU device key
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900115func (onu *Onu) GetDevkey () Devkey {
116 return Devkey{ID: onu.OnuID, Intfid:onu.IntfID}
117}
118
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200119// GetIntState returns ONU internal state
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900120func (onu *Onu) GetIntState() DeviceState {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900121 onu.mu.Lock()
122 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900123 return onu.InternalState
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900124}