blob: aafa793ce77de01c2b4f38260035e8aa2a567ad0 [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
Zack Williams2abf3932019-08-05 14:07:05 -070023 "github.com/opencord/voltha-bbsim/common/logger"
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -040024 openolt "github.com/opencord/voltha-protos/go/openolt"
Matteo Scandolo88e91892018-11-06 16:29:19 -080025 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090026)
27
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020028// Constants for the ONU states
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029const (
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020030 ONU_INACTIVE DeviceState = iota // TODO: Each stage name should be more accurate
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090031 ONU_ACTIVE
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090032 ONU_OMCIACTIVE
Keita NISHIMOTO2807c542019-06-04 22:58:32 +090033 ONU_AUTHENTICATED
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020034 ONU_LOS_RAISED
35 ONU_OMCI_CHANNEL_LOS_RAISED
36 ONU_LOS_ON_OLT_PON_LOS // TODO give more suitable and crisp name
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020037 ONU_FREE
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090038)
39
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020040// ONUState maps int value of device state to string
41var ONUState = map[DeviceState]string{
42 ONU_INACTIVE: "ONU_INACTIVE",
43 ONU_ACTIVE: "ONU_ACTIVE",
44 ONU_OMCIACTIVE: "ONU_OMCIACTIVE",
Kailash95bb5ac2019-07-01 20:56:37 -070045 ONU_AUTHENTICATED: "ONU_AUTHENTICATED",
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020046 ONU_LOS_RAISED: "ONU_LOS_RAISED",
47 ONU_OMCI_CHANNEL_LOS_RAISED: "ONU_OMCI_CHANNEL_LOS_RAISED",
48 ONU_LOS_ON_OLT_PON_LOS: "ONU_LOS_ON_OLT_PON_LOS",
Kailash95bb5ac2019-07-01 20:56:37 -070049 ONU_FREE: "ONU_FREE",
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020050}
51
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020052// Onu structure stores information of ONUs
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090053type Onu struct {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090054 InternalState DeviceState
Matteo Scandolo88e91892018-11-06 16:29:19 -080055 OltID uint32
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056 IntfID uint32
57 OperState string
58 SerialNumber *openolt.SerialNumber
59 OnuID uint32
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +090060 GemportID uint16
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020061 FlowIDs []uint32
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090062 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090063}
64
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020065// NewSN constructs and returns serial number based on the OLT ID, intf ID and ONU ID
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090066func NewSN(oltid uint32, intfid uint32, onuid uint32) []byte {
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090067 sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090068 return sn
69}
70
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020071// NewOnus initializes and returns slice of Onu objects
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090072func NewOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090073 onus := []*Onu{}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020074 for i := 1; i <= int(nonus); i++ {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090075 onu := Onu{}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020076 onu.InternalState = ONU_FREE // New Onu Initialised with state ONU_FREE
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090077 onu.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090078 onu.IntfID = intfid
Matteo Scandolo88e91892018-11-06 16:29:19 -080079 onu.OltID = oltid
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020080 onu.OperState = "down"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090081 onu.SerialNumber = new(openolt.SerialNumber)
Matteo Scandoloa96633a2018-10-18 16:23:02 -070082 onu.SerialNumber.VendorId = []byte("BBSM")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090083 onu.SerialNumber.VendorSpecific = NewSN(oltid, intfid, uint32(i))
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +090084 onu.GemportID = 0
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090085 onus = append(onus, &onu)
86 }
87 return onus
88}
89
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020090// Initialize method initializes ONU state to up and ONU_INACTIVE
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090091func (onu *Onu) Initialize() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090092 onu.OperState = "up"
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090093 onu.InternalState = ONU_INACTIVE
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090094}
95
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020096// ValidateONU method validate ONU based on the serial number in onuMap
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090097func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool {
98 for _, onus := range regonus {
99 for _, onu := range onus {
100 if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) {
101 return true
102 }
103 }
104 }
105 return false
106}
107
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200108// ValidateSN compares two serial numbers and returns result as true/false
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900109func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool {
110 return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific)
111}
112
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200113// UpdateOnusOpStatus method updates ONU oper status
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200114func UpdateOnusOpStatus(ponif uint32, onu *Onu, opstatus string) {
115 onu.OperState = opstatus
116 logger.WithFields(log.Fields{
117 "onu": onu.SerialNumber,
118 "pon_interface": ponif,
119 }).Info("ONU OperState Updated")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900120}
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900121
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200122// UpdateIntState method updates ONU internal state
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900123func (onu *Onu) UpdateIntState(intstate DeviceState) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900124 onu.mu.Lock()
125 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900126 onu.InternalState = intstate
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900127}
128
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200129// GetDevkey returns ONU device key
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200130func (onu *Onu) GetDevkey() Devkey {
131 return Devkey{ID: onu.OnuID, Intfid: onu.IntfID}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900132}
133
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200134// GetIntState returns ONU internal state
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900135func (onu *Onu) GetIntState() DeviceState {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900136 onu.mu.Lock()
137 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900138 return onu.InternalState
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900139}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200140
141// DeleteFlowID method search and delete flowID from the onu flowIDs slice
142func (onu *Onu) DeleteFlowID(flowID uint32) {
143 for pos, id := range onu.FlowIDs {
144 if id == flowID {
145 // delete the flowID by shifting all flowIDs by one
146 onu.FlowIDs = append(onu.FlowIDs[:pos], onu.FlowIDs[pos+1:]...)
147 t := make([]uint32, len(onu.FlowIDs))
148 copy(t, onu.FlowIDs)
149 onu.FlowIDs = t
150 break
151 }
152 }
153}