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