blob: b0af2c27f3272998d44e779efcce6c6f9d0d7982 [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"
Zdravko Bozakov078a2712019-07-19 23:25:15 +020025 techprofile "github.com/opencord/voltha-protos/go/tech_profile"
Matteo Scandolo88e91892018-11-06 16:29:19 -080026 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090027)
28
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020029// Constants for the ONU states
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090030const (
Zdravko Bozakov078a2712019-07-19 23:25:15 +020031 OnuFree State = iota // TODO: Each stage name should be more accurate
32 OnuInactive
33 OnuLosRaised
34 OnuLosOnOltPonLos
35 OnuOmciChannelLosRaised
36 OnuActive
37 OnuOmciActive
38 OnuAuthenticated
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039)
40
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020041// ONUState maps int value of device state to string
Zdravko Bozakov078a2712019-07-19 23:25:15 +020042var ONUState = map[State]string{
43 OnuFree: "ONU_FREE",
44 OnuInactive: "ONU_INACTIVE",
45 OnuLosRaised: "ONU_LOS_RAISED",
46 OnuLosOnOltPonLos: "ONU_LOS_ON_OLT_PON_LOS",
47 OnuOmciChannelLosRaised: "ONU_OMCI_CHANNEL_LOS_RAISED",
48 OnuActive: "ONU_ACTIVE",
49 OnuOmciActive: "ONU_OMCIACTIVE",
50 OnuAuthenticated: "ONU_AUTHENTICATED",
51}
52
53// FlowKey used for FlowMap key
54type FlowKey struct {
55 FlowID uint32
56 FlowDirection string
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020057}
58
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020059// Onu structure stores information of ONUs
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090060type Onu struct {
Zdravko Bozakov078a2712019-07-19 23:25:15 +020061 InternalState State
Matteo Scandolo88e91892018-11-06 16:29:19 -080062 OltID uint32
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090063 IntfID uint32
64 OperState string
65 SerialNumber *openolt.SerialNumber
66 OnuID uint32
Zdravko Bozakov078a2712019-07-19 23:25:15 +020067 GemPortMap map[uint32][]uint32 // alloc-id is used as key and corresponding gem-ports are stored in slice
68 Tconts *techprofile.TrafficSchedulers
69 Flows []FlowKey
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090070 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090071}
72
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020073// NewSN constructs and returns serial number based on the OLT ID, intf ID and ONU ID
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090074func NewSN(oltid uint32, intfid uint32, onuid uint32) []byte {
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090075 sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090076 return sn
77}
78
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020079// NewOnus initializes and returns slice of Onu objects
Zdravko Bozakov078a2712019-07-19 23:25:15 +020080func NewOnus(oltid uint32, intfid uint32, nonus uint32) []*Onu {
81 var onus []*Onu
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020082 for i := 1; i <= int(nonus); i++ {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090083 onu := Onu{}
Zdravko Bozakov078a2712019-07-19 23:25:15 +020084 onu.InternalState = OnuFree // New Onu Initialised with state ONU_FREE
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090085 onu.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090086 onu.IntfID = intfid
Matteo Scandolo88e91892018-11-06 16:29:19 -080087 onu.OltID = oltid
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020088 onu.OperState = "down"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090089 onu.SerialNumber = new(openolt.SerialNumber)
Matteo Scandoloa96633a2018-10-18 16:23:02 -070090 onu.SerialNumber.VendorId = []byte("BBSM")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090091 onu.SerialNumber.VendorSpecific = NewSN(oltid, intfid, uint32(i))
Zdravko Bozakov078a2712019-07-19 23:25:15 +020092 onu.GemPortMap = make(map[uint32][]uint32)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090093 onus = append(onus, &onu)
94 }
95 return onus
96}
97
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020098// Initialize method initializes ONU state to up and ONU_INACTIVE
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090099func (onu *Onu) Initialize() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900100 onu.OperState = "up"
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200101 onu.InternalState = OnuInactive
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900102}
103
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200104// ValidateSN compares two serial numbers and returns result as true/false
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900105func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool {
106 return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific)
107}
108
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200109// UpdateOnusOpStatus method updates ONU oper status
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200110func UpdateOnusOpStatus(ponif uint32, onu *Onu, opstatus string) {
111 onu.OperState = opstatus
112 logger.WithFields(log.Fields{
113 "onu": onu.SerialNumber,
114 "pon_interface": ponif,
115 }).Info("ONU OperState Updated")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900116}
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900117
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200118// UpdateIntState method updates ONU internal state
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200119func (onu *Onu) UpdateIntState(intstate State) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900120 onu.mu.Lock()
121 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900122 onu.InternalState = intstate
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900123}
124
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200125// GetDevkey returns ONU device key
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200126func (onu *Onu) GetDevkey() Devkey {
127 return Devkey{ID: onu.OnuID, Intfid: onu.IntfID}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900128}
129
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200130// GetIntState returns ONU internal state
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200131func (onu *Onu) GetIntState() State {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900132 onu.mu.Lock()
133 defer onu.mu.Unlock()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900134 return onu.InternalState
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900135}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200136
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200137// DeleteFlow method search and delete flowKey from the onu flows slice
138func (onu *Onu) DeleteFlow(key FlowKey) {
139 for pos, flowKey := range onu.Flows {
140 if flowKey == key {
141 // delete the flowKey by shifting all flowKeys by one
142 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
143 t := make([]FlowKey, len(onu.Flows))
144 copy(t, onu.Flows)
145 onu.Flows = t
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200146 break
147 }
148 }
149}