blob: 719f67bef787c702f2aa917754755b6d7059719e [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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020019import (
20 "strconv"
21 "sync"
22)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090023
Zdravko Bozakov078a2712019-07-19 23:25:15 +020024//State represents the OLT States
25type State int
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090026
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020027// Device interface provides common methods for OLT and ONU devices
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090028type Device interface {
29 Initialize()
Zdravko Bozakov078a2712019-07-19 23:25:15 +020030 UpdateIntState(intstate State)
31 GetIntState() State
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090032 GetDevkey() Devkey
33}
34
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020035// Devkey key for OLT/ONU devices
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090036type Devkey struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020037 ID uint32
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090038 Intfid uint32
39}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090040
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020041// Olt structure consists required fields for OLT
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042type Olt struct {
43 ID uint32
44 NumPonIntf uint32
45 NumNniIntf uint32
46 Mac string
47 SerialNumber string
48 Manufacture string
49 Name string
Zdravko Bozakov078a2712019-07-19 23:25:15 +020050 InternalState State
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 OperState string
Zdravko Bozakov078a2712019-07-19 23:25:15 +020052 NniIntfs []Port
53 PonIntfs []Port
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 HeartbeatSignature uint32
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020055 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056}
57
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020058// AlarmState informs about the present state of the supported alarms
59type AlarmState uint32
60
61const (
62 // PonLosCleared alarm state for PON-LOS
63 PonLosCleared AlarmState = iota
64 // NniLosCleared alarm state for NNI-LOS
65 NniLosCleared
66 // PonLosRaised alarm state for PON-LOS
67 PonLosRaised
68 // NniLosRaised for NNI-LOS
69 NniLosRaised
70)
71
Zdravko Bozakov078a2712019-07-19 23:25:15 +020072// Port info for NNI and PON ports
73type Port struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020074 Type string
75 IntfID uint32
76 OperState string
77 AlarmState AlarmState
Zdravko Bozakov078a2712019-07-19 23:25:15 +020078 PortStats PortStats
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090079}
80
Zdravko Bozakov078a2712019-07-19 23:25:15 +020081// PortStats for NNI and PON ports
82type PortStats struct {
83 Packets uint64
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020084}
85
Zdravko Bozakov078a2712019-07-19 23:25:15 +020086// Constants for Port types
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020087const (
88 IntfPon = "pon"
89 IntfNni = "nni"
90)
91
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090092/* OltState
93OLT_INACTIVE -> OLT_PREACTIVE -> ACTIVE
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020094 (ActivateOLT) (Enable)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090095 <- <-
96*/
97
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020098// Constants for OLT states
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090099const (
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200100 OltInactive State = iota // OLT/ONUs are not instantiated
101 OltPreactive // Before PacketInDaemon Running
102 OltActive // After PacketInDaemon Running
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900103)
104
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200105// OLTAlarmStateToString is used to get alarm state as string
106var OLTAlarmStateToString = map[AlarmState]string{
107 PonLosCleared: "PonLosCleared",
108 NniLosCleared: "NniLosCleared",
109 PonLosRaised: "PonLosRaised",
110 NniLosRaised: "NniLosRaised",
111}
112
113// NewOlt initialises the new olt variable with the given values
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900114func NewOlt(oltid uint32, npon uint32, nnni uint32) *Olt {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 olt := Olt{}
116 olt.ID = oltid
117 olt.NumPonIntf = npon
118 olt.NumNniIntf = nnni
119 olt.Name = "BBSIM OLT"
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200120 olt.InternalState = OltInactive
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900121 olt.OperState = "up"
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200122 olt.Manufacture = "BBSIM"
123 olt.SerialNumber = "BBSIMOLT00" + strconv.FormatInt(int64(oltid), 10)
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200124 olt.NniIntfs = make([]Port, olt.NumNniIntf)
125 olt.PonIntfs = make([]Port, olt.NumPonIntf)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900126 olt.HeartbeatSignature = oltid
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900127 olt.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900128 for i := uint32(0); i < olt.NumNniIntf; i++ {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200129 olt.NniIntfs[i].IntfID = i
130 olt.NniIntfs[i].OperState = "up"
131 olt.NniIntfs[i].Type = IntfNni
132 olt.NniIntfs[i].AlarmState = NniLosCleared
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900133 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200134 for i := uint32(0); i < olt.NumPonIntf; i++ {
135 olt.PonIntfs[i].IntfID = i
136 olt.PonIntfs[i].OperState = "up"
137 olt.PonIntfs[i].Type = IntfPon
138 olt.PonIntfs[i].AlarmState = PonLosCleared
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900139 }
140 return &olt
141}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900142
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200143// Initialize method initializes NNI and PON ports
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900144func (olt *Olt) Initialize() {
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200145 olt.InternalState = OltInactive
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900146 olt.OperState = "up"
147 for i := uint32(0); i < olt.NumNniIntf; i++ {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200148 olt.NniIntfs[i].IntfID = i
149 olt.NniIntfs[i].OperState = "up"
150 olt.NniIntfs[i].Type = IntfNni
151 olt.NniIntfs[i].AlarmState = NniLosCleared
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900152 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200153 for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf; i++ {
154 olt.PonIntfs[i].IntfID = i
155 olt.PonIntfs[i].OperState = "up"
156 olt.PonIntfs[i].Type = IntfPon
157 olt.PonIntfs[i].AlarmState = PonLosCleared
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900158 }
159}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900160
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200161// GetIntState returns internal state of OLT
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200162func (olt *Olt) GetIntState() State {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900163 olt.mu.Lock()
164 defer olt.mu.Unlock()
165 return olt.InternalState
166}
167
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200168// GetDevkey returns device key of OLT
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200169func (olt *Olt) GetDevkey() Devkey {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900170 return Devkey{ID: olt.ID}
171}
172
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200173// UpdateIntState method updates OLT internal state
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200174func (olt *Olt) UpdateIntState(intstate State) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900175 olt.mu.Lock()
176 defer olt.mu.Unlock()
177 olt.InternalState = intstate
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200178}
179
180// UpdateNniPortState updates the status of the nni-port
181func (olt *Olt) UpdateNniPortState(portID uint32, alarmState AlarmState, operState string) {
182 olt.NniIntfs[portID].AlarmState = alarmState
183 olt.NniIntfs[portID].OperState = operState
184}
185
186// UpdatePonPortState updates the status of the pon-port
187func (olt *Olt) UpdatePonPortState(portID uint32, alarmState AlarmState, operState string) {
188 olt.PonIntfs[portID].AlarmState = alarmState
189 olt.PonIntfs[portID].OperState = operState
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900190}