blob: b3dc2c3ce3968af528f98e2645d356e2ce0d6d81 [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
24type DeviceState int
25
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020026// Device interface provides common methods for OLT and ONU devices
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090027type Device interface {
28 Initialize()
29 UpdateIntState(intstate DeviceState)
30 GetIntState() DeviceState
31 GetDevkey() Devkey
32}
33
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020034// Devkey key for OLT/ONU devices
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090035type Devkey struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020036 ID uint32
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090037 Intfid uint32
38}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020040// Olt structure consists required fields for OLT
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090041type Olt struct {
42 ID uint32
43 NumPonIntf uint32
44 NumNniIntf uint32
45 Mac string
46 SerialNumber string
47 Manufacture string
48 Name string
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090049 InternalState DeviceState
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090050 OperState string
Keita NISHIMOTO9617c852019-06-17 21:46:44 +090051 NniIntfs []nniIntf
52 PonIntfs []ponIntf
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090053 HeartbeatSignature uint32
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020054 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090055}
56
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020057// AlarmState informs about the present state of the supported alarms
58type AlarmState uint32
59
60const (
61 // PonLosCleared alarm state for PON-LOS
62 PonLosCleared AlarmState = iota
63 // NniLosCleared alarm state for NNI-LOS
64 NniLosCleared
65 // PonLosRaised alarm state for PON-LOS
66 PonLosRaised
67 // NniLosRaised for NNI-LOS
68 NniLosRaised
69)
70
71type ponIntf struct {
72 Type string
73 IntfID uint32
74 OperState string
75 AlarmState AlarmState
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090076}
77
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020078type nniIntf struct {
79 Type string
80 IntfID uint32
81 OperState string
82 AlarmState AlarmState
83}
84
85// Constants for port types
86const (
87 IntfPon = "pon"
88 IntfNni = "nni"
89)
90
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090091/* OltState
92OLT_INACTIVE -> OLT_PREACTIVE -> ACTIVE
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020093 (ActivateOLT) (Enable)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090094 <- <-
95*/
96
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020097// Constants for OLT states
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090098const (
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020099 OLT_INACTIVE DeviceState = iota // OLT/ONUs are not instantiated
100 OLT_PREACTIVE // Before PacketInDaemon Running
101 OLT_ACTIVE // After PacketInDaemon Running
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900102)
103
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200104// OLTAlarmStateToString is used to get alarm state as string
105var OLTAlarmStateToString = map[AlarmState]string{
106 PonLosCleared: "PonLosCleared",
107 NniLosCleared: "NniLosCleared",
108 PonLosRaised: "PonLosRaised",
109 NniLosRaised: "NniLosRaised",
110}
111
112// NewOlt initialises the new olt variable with the given values
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113func NewOlt(oltid uint32, npon uint32, nnni uint32) *Olt {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900114 olt := Olt{}
115 olt.ID = oltid
116 olt.NumPonIntf = npon
117 olt.NumNniIntf = nnni
118 olt.Name = "BBSIM OLT"
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900119 olt.InternalState = OLT_INACTIVE
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900120 olt.OperState = "up"
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200121 olt.Manufacture = "BBSIM"
122 olt.SerialNumber = "BBSIMOLT00" + strconv.FormatInt(int64(oltid), 10)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200123 olt.NniIntfs = make([]nniIntf, olt.NumNniIntf)
124 olt.PonIntfs = make([]ponIntf, olt.NumPonIntf)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900125 olt.HeartbeatSignature = oltid
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900126 olt.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900127 for i := uint32(0); i < olt.NumNniIntf; i++ {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200128 olt.NniIntfs[i].IntfID = i
129 olt.NniIntfs[i].OperState = "up"
130 olt.NniIntfs[i].Type = IntfNni
131 olt.NniIntfs[i].AlarmState = NniLosCleared
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900132 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200133 for i := uint32(0); i < olt.NumPonIntf; i++ {
134 olt.PonIntfs[i].IntfID = i
135 olt.PonIntfs[i].OperState = "up"
136 olt.PonIntfs[i].Type = IntfPon
137 olt.PonIntfs[i].AlarmState = PonLosCleared
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900138 }
139 return &olt
140}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900141
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200142// Initialize method initializes NNI and PON ports
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900143func (olt *Olt) Initialize() {
144 olt.InternalState = OLT_INACTIVE
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900145 olt.OperState = "up"
146 for i := uint32(0); i < olt.NumNniIntf; i++ {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200147 olt.NniIntfs[i].IntfID = i
148 olt.NniIntfs[i].OperState = "up"
149 olt.NniIntfs[i].Type = IntfNni
150 olt.NniIntfs[i].AlarmState = NniLosCleared
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900151 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200152 for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf; i++ {
153 olt.PonIntfs[i].IntfID = i
154 olt.PonIntfs[i].OperState = "up"
155 olt.PonIntfs[i].Type = IntfPon
156 olt.PonIntfs[i].AlarmState = PonLosCleared
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900157 }
158}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900159
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200160// GetIntState returns internal state of OLT
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900161func (olt *Olt) GetIntState() DeviceState {
162 olt.mu.Lock()
163 defer olt.mu.Unlock()
164 return olt.InternalState
165}
166
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200167// GetDevkey returns device key of OLT
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200168func (olt *Olt) GetDevkey() Devkey {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900169 return Devkey{ID: olt.ID}
170}
171
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200172// UpdateIntState method updates OLT internal state
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900173func (olt *Olt) UpdateIntState(intstate DeviceState) {
174 olt.mu.Lock()
175 defer olt.mu.Unlock()
176 olt.InternalState = intstate
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200177}
178
179// UpdateNniPortState updates the status of the nni-port
180func (olt *Olt) UpdateNniPortState(portID uint32, alarmState AlarmState, operState string) {
181 olt.NniIntfs[portID].AlarmState = alarmState
182 olt.NniIntfs[portID].OperState = operState
183}
184
185// UpdatePonPortState updates the status of the pon-port
186func (olt *Olt) UpdatePonPortState(portID uint32, alarmState AlarmState, operState string) {
187 olt.PonIntfs[portID].AlarmState = alarmState
188 olt.PonIntfs[portID].OperState = operState
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900189}