Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package device |
| 18 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 19 | import ( |
| 20 | "strconv" |
| 21 | "sync" |
| 22 | ) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 23 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 24 | //State represents the OLT States |
| 25 | type State int |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 26 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 27 | // Device interface provides common methods for OLT and ONU devices |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 28 | type Device interface { |
| 29 | Initialize() |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 30 | UpdateIntState(intstate State) |
| 31 | GetIntState() State |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 32 | GetDevkey() Devkey |
| 33 | } |
| 34 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 35 | // Devkey key for OLT/ONU devices |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 36 | type Devkey struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 37 | ID uint32 |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 38 | Intfid uint32 |
| 39 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 40 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 41 | // Olt structure consists required fields for OLT |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 42 | type Olt struct { |
| 43 | ID uint32 |
| 44 | NumPonIntf uint32 |
| 45 | NumNniIntf uint32 |
| 46 | Mac string |
| 47 | SerialNumber string |
| 48 | Manufacture string |
| 49 | Name string |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 50 | InternalState State |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | OperState string |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 52 | NniIntfs []Port |
| 53 | PonIntfs []Port |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 54 | HeartbeatSignature uint32 |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 55 | mu *sync.Mutex |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 58 | // AlarmState informs about the present state of the supported alarms |
| 59 | type AlarmState uint32 |
| 60 | |
| 61 | const ( |
| 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 Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 72 | // Port info for NNI and PON ports |
| 73 | type Port struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 74 | Type string |
| 75 | IntfID uint32 |
| 76 | OperState string |
| 77 | AlarmState AlarmState |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 78 | PortStats PortStats |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 81 | // PortStats for NNI and PON ports |
| 82 | type PortStats struct { |
| 83 | Packets uint64 |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 86 | // Constants for Port types |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 87 | const ( |
| 88 | IntfPon = "pon" |
| 89 | IntfNni = "nni" |
| 90 | ) |
| 91 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 92 | /* OltState |
| 93 | OLT_INACTIVE -> OLT_PREACTIVE -> ACTIVE |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 94 | (ActivateOLT) (Enable) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 95 | <- <- |
| 96 | */ |
| 97 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 98 | // Constants for OLT states |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 99 | const ( |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 100 | OltInactive State = iota // OLT/ONUs are not instantiated |
| 101 | OltPreactive // Before PacketInDaemon Running |
| 102 | OltActive // After PacketInDaemon Running |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 103 | ) |
| 104 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 105 | // OLTAlarmStateToString is used to get alarm state as string |
| 106 | var 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | func NewOlt(oltid uint32, npon uint32, nnni uint32) *Olt { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 115 | olt := Olt{} |
| 116 | olt.ID = oltid |
| 117 | olt.NumPonIntf = npon |
| 118 | olt.NumNniIntf = nnni |
| 119 | olt.Name = "BBSIM OLT" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 120 | olt.InternalState = OltInactive |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 121 | olt.OperState = "up" |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 122 | olt.Manufacture = "BBSIM" |
| 123 | olt.SerialNumber = "BBSIMOLT00" + strconv.FormatInt(int64(oltid), 10) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 124 | olt.NniIntfs = make([]Port, olt.NumNniIntf) |
| 125 | olt.PonIntfs = make([]Port, olt.NumPonIntf) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 126 | olt.HeartbeatSignature = oltid |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 127 | olt.mu = &sync.Mutex{} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 128 | for i := uint32(0); i < olt.NumNniIntf; i++ { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 129 | olt.NniIntfs[i].IntfID = i |
| 130 | olt.NniIntfs[i].OperState = "up" |
| 131 | olt.NniIntfs[i].Type = IntfNni |
| 132 | olt.NniIntfs[i].AlarmState = NniLosCleared |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 133 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 134 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 139 | } |
| 140 | return &olt |
| 141 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 142 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 143 | // Initialize method initializes NNI and PON ports |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 144 | func (olt *Olt) Initialize() { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 145 | olt.InternalState = OltInactive |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 146 | olt.OperState = "up" |
| 147 | for i := uint32(0); i < olt.NumNniIntf; i++ { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 148 | olt.NniIntfs[i].IntfID = i |
| 149 | olt.NniIntfs[i].OperState = "up" |
| 150 | olt.NniIntfs[i].Type = IntfNni |
| 151 | olt.NniIntfs[i].AlarmState = NniLosCleared |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 152 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 153 | 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 NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 158 | } |
| 159 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 160 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 161 | // GetIntState returns internal state of OLT |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 162 | func (olt *Olt) GetIntState() State { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 163 | olt.mu.Lock() |
| 164 | defer olt.mu.Unlock() |
| 165 | return olt.InternalState |
| 166 | } |
| 167 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 168 | // GetDevkey returns device key of OLT |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 169 | func (olt *Olt) GetDevkey() Devkey { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 170 | return Devkey{ID: olt.ID} |
| 171 | } |
| 172 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 173 | // UpdateIntState method updates OLT internal state |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 174 | func (olt *Olt) UpdateIntState(intstate State) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 175 | olt.mu.Lock() |
| 176 | defer olt.mu.Unlock() |
| 177 | olt.InternalState = intstate |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // UpdateNniPortState updates the status of the nni-port |
| 181 | func (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 |
| 187 | func (olt *Olt) UpdatePonPortState(portID uint32, alarmState AlarmState, operState string) { |
| 188 | olt.PonIntfs[portID].AlarmState = alarmState |
| 189 | olt.PonIntfs[portID].OperState = operState |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 190 | } |