blob: 593be6ac3cb464c9b745e8b9aeb8fdcf51780b1e [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070020 "context"
Himani Chawla4ff5fab2021-11-09 19:19:29 +053021 "encoding/binary"
Matteo Scandolo618a6582020-09-09 12:21:29 -070022 "encoding/hex"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070023 "fmt"
Elia Battistonfe017662022-01-05 11:43:16 +010024 "math/rand"
Mahir Gunyela1753ae2021-06-23 00:24:56 -070025 "sync"
26
Matteo Scandolo8a574812021-05-20 15:18:53 -070027 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
28 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
29 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000030
Matteo Scandolof9d43412021-01-12 11:11:34 -080031 pb "github.com/opencord/bbsim/api/bbsim"
32 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000033
34 "net"
35 "strconv"
36 "time"
Himani Chawla13b1ee02021-03-15 01:43:53 +053037
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080038 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Andrea Campanella10426e22021-10-15 17:58:04 +020039 me "github.com/opencord/omci-lib-go/v2/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010040
Himani Chawla4ff5fab2021-11-09 19:19:29 +053041 "github.com/boguslaw-wojcik/crc32a"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070042 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070043 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070044 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070045 "github.com/opencord/bbsim/internal/common"
46 omcilib "github.com/opencord/bbsim/internal/common/omci"
Andrea Campanella10426e22021-10-15 17:58:04 +020047 "github.com/opencord/omci-lib-go/v2"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000048 "github.com/opencord/voltha-protos/v5/go/openolt"
49 "github.com/opencord/voltha-protos/v5/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070050 log "github.com/sirupsen/logrus"
51)
52
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070053var onuLogger = log.WithFields(log.Fields{
54 "module": "ONU",
55})
56
Matteo Scandolocedde462021-03-09 17:37:16 -080057const (
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000058 maxOmciMsgCounter = 10
59)
60
61const (
Matteo Scandolocedde462021-03-09 17:37:16 -080062 // ONU transitions
63 OnuTxInitialize = "initialize"
64 OnuTxDiscover = "discover"
65 OnuTxEnable = "enable"
66 OnuTxDisable = "disable"
67 OnuTxPonDisable = "pon_disable"
68 OnuTxStartImageDownload = "start_image_download"
69 OnuTxProgressImageDownload = "progress_image_download"
70 OnuTxCompleteImageDownload = "complete_image_download"
71 OnuTxFailImageDownload = "fail_image_download"
72 OnuTxActivateImage = "activate_image"
73 OnuTxCommitImage = "commit_image"
74
75 // ONU States
76 OnuStateCreated = "created"
77 OnuStateInitialized = "initialized"
78 OnuStateDiscovered = "discovered"
79 OnuStateEnabled = "enabled"
80 OnuStateDisabled = "disabled"
81 OnuStatePonDisabled = "pon_disabled"
82 OnuStateImageDownloadStarted = "image_download_started"
83 OnuStateImageDownloadInProgress = "image_download_in_progress"
84 OnuStateImageDownloadComplete = "image_download_completed"
85 OnuStateImageDownloadError = "image_download_error"
86 OnuStateImageActivated = "software_image_activated"
87 OnuStateImageCommitted = "software_image_committed"
88
89 // BBR ONU States and Transitions
90 BbrOnuTxSendEapolFlow = "send_eapol_flow"
91 BbrOnuStateEapolFlowSent = "eapol_flow_sent"
92 BbrOnuTxSendDhcpFlow = "send_dhcp_flow"
93 BbrOnuStateDhcpFlowSent = "dhcp_flow_sent"
94)
95
Andrea Campanellacc9b1662022-01-26 17:47:48 +010096type FlowKey struct {
yasin saplic07b9522022-01-27 11:23:54 +000097 ID uint64
Andrea Campanellacc9b1662022-01-26 17:47:48 +010098}
99
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700100type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800101 ID uint32
102 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700103 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800104 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +0530105 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
106 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -0700107
Matteo Scandolo4a036262020-08-17 15:56:13 -0700108 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800109 // ONU State
Elia Battistonac63b112022-01-12 18:40:49 +0100110 UniPorts []UniPortIf
111 PotsPorts []PotsPortIf
Andrea Campanellacc9b1662022-01-26 17:47:48 +0100112 Flows []FlowKey
Elia Battistonac63b112022-01-12 18:40:49 +0100113 FlowIds []uint64 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -0700114
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700115 OperState *fsm.FSM
116 SerialNumber *openolt.SerialNumber
117
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700118 AdminLockState uint8 // 0 is enabled, 1 is disabled.
119
Matteo Scandolof9d43412021-01-12 11:11:34 -0800120 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700121
122 // OMCI params
Matteo Scandolocedde462021-03-09 17:37:16 -0800123 MibDataSync uint8
124 ImageSoftwareExpectedSections int
125 ImageSoftwareReceivedSections int
126 ActiveImageEntityId uint16
127 CommittedImageEntityId uint16
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700128 StandbyImageVersion string
129 ActiveImageVersion string
Matteo Scandolo76f6b892021-11-15 16:13:06 -0800130 InDownloadImageVersion string
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700131 CommittedImageVersion string
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000132 OmciResponseRate uint8
133 OmciMsgCounter uint8
Himani Chawla4ff5fab2021-11-09 19:19:29 +0530134 ImageSectionData []byte
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800135
136 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700137 tid uint16
138 hpTid uint16
139 seqNumber uint16
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700140 MibDb *omcilib.MibDb
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700141
Anand S Katti09541352020-01-29 15:54:01 +0530142 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
143 TrafficSchedulers *tech_profile.TrafficSchedulers
Himani Chawla13b1ee02021-03-15 01:43:53 +0530144 onuAlarmsInfoLock sync.RWMutex
145 onuAlarmsInfo map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700146}
147
Matteo Scandolo99f18462019-10-28 14:14:28 -0700148func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700149 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700150}
151
Matteo Scandolo8a574812021-05-20 15:18:53 -0700152func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, nextCtag map[string]int, nextStag map[string]int, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700153
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700154 o := Onu{
Matteo Scandolocedde462021-03-09 17:37:16 -0800155 ID: id,
156 PonPortID: pon.ID,
157 PonPort: pon,
Matteo Scandolocedde462021-03-09 17:37:16 -0800158 tid: 0x1,
159 hpTid: 0x8000,
160 seqNumber: 0,
161 DoneChannel: make(chan bool, 1),
162 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Andrea Campanellacc9b1662022-01-26 17:47:48 +0100163 Flows: []FlowKey{},
Matteo Scandolocedde462021-03-09 17:37:16 -0800164 DiscoveryDelay: delay,
165 MibDataSync: 0,
166 ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
167 ImageSoftwareReceivedSections: 0,
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700168 //TODO this needs reworking, it's always 0 or 1, possibly base all on the version
169 ActiveImageEntityId: 0, // when we start the SoftwareImage with ID 0 is active and committed
170 CommittedImageEntityId: 0,
171 StandbyImageVersion: "BBSM_IMG_00000",
172 ActiveImageVersion: "BBSM_IMG_00001",
173 CommittedImageVersion: "BBSM_IMG_00001",
174 OmciResponseRate: olt.OmciResponseRate,
175 OmciMsgCounter: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700176 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800177 o.SerialNumber = NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700178 // NOTE this state machine is used to track the operational
179 // state as requested by VOLTHA
180 o.OperState = getOperStateFSM(func(e *fsm.Event) {
181 onuLogger.WithFields(log.Fields{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700182 "OnuId": o.ID,
183 "IntfId": o.PonPortID,
184 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700185 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
186 })
Himani Chawla13b1ee02021-03-15 01:43:53 +0530187 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo)
Himani Chawla908a1f52022-01-27 14:39:44 +0530188
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700189 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
190 o.InternalState = fsm.NewFSM(
Matteo Scandolocedde462021-03-09 17:37:16 -0800191 OnuStateCreated,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700192 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700193 // DEVICE Lifecycle
Matteo Scandolocedde462021-03-09 17:37:16 -0800194 {Name: OnuTxInitialize, Src: []string{OnuStateCreated, OnuStateDisabled, OnuStatePonDisabled}, Dst: OnuStateInitialized},
195 {Name: OnuTxDiscover, Src: []string{OnuStateInitialized}, Dst: OnuStateDiscovered},
196 {Name: OnuTxEnable, Src: []string{OnuStateDiscovered, OnuStatePonDisabled}, Dst: OnuStateEnabled},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100197 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Matteo Scandolocedde462021-03-09 17:37:16 -0800198 {Name: OnuTxDisable, Src: []string{OnuStateEnabled, OnuStatePonDisabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateDisabled},
Pragya Arya6a708d62020-01-01 17:17:20 +0530199 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +0200200 {Name: OnuTxPonDisable, Src: []string{OnuStateEnabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted, OnuStateImageDownloadComplete}, Dst: OnuStatePonDisabled},
Matteo Scandolocedde462021-03-09 17:37:16 -0800201 // Software Image Download related states
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +0200202 {Name: OnuTxStartImageDownload, Src: []string{OnuStateEnabled, OnuStateImageDownloadComplete, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateImageDownloadStarted},
Matteo Scandolocedde462021-03-09 17:37:16 -0800203 {Name: OnuTxProgressImageDownload, Src: []string{OnuStateImageDownloadStarted}, Dst: OnuStateImageDownloadInProgress},
204 {Name: OnuTxCompleteImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadComplete},
205 {Name: OnuTxFailImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadError},
206 {Name: OnuTxActivateImage, Src: []string{OnuStateImageDownloadComplete}, Dst: OnuStateImageActivated},
207 {Name: OnuTxCommitImage, Src: []string{OnuStateEnabled}, Dst: OnuStateImageCommitted}, // the image is committed after a ONU reboot
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700208 // BBR States
209 // TODO add start OMCI state
Matteo Scandolocedde462021-03-09 17:37:16 -0800210 {Name: BbrOnuTxSendEapolFlow, Src: []string{OnuStateInitialized}, Dst: BbrOnuStateEapolFlowSent},
211 {Name: BbrOnuTxSendDhcpFlow, Src: []string{BbrOnuStateEapolFlowSent}, Dst: BbrOnuStateDhcpFlowSent},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700212 },
213 fsm.Callbacks{
214 "enter_state": func(e *fsm.Event) {
215 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700216 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700217 fmt.Sprintf("enter_%s", OnuStateInitialized): func(e *fsm.Event) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100218 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800219 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800220
Matteo Scandolocedde462021-03-09 17:37:16 -0800221 if err := o.OperState.Event(OnuTxEnable); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800222 onuLogger.WithFields(log.Fields{
223 "OnuId": o.ID,
224 "IntfId": o.PonPortID,
225 "OnuSn": o.Sn(),
226 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
227 }
228
Pragya Arya1cbefa42020-01-13 12:15:29 +0530229 if !isMock {
230 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700231 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530232 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100233 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700234 fmt.Sprintf("enter_%s", OnuStateDiscovered): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800235 msg := bbsim.Message{
236 Type: bbsim.OnuDiscIndication,
237 Data: bbsim.OnuDiscIndicationMessage{
238 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100239 },
240 }
241 o.Channel <- msg
242 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700243 fmt.Sprintf("enter_%s", OnuStateEnabled): func(event *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800244
245 if used, sn := o.PonPort.isOnuIdAllocated(o.ID); used {
246 onuLogger.WithFields(log.Fields{
247 "IntfId": o.PonPortID,
248 "OnuId": o.ID,
249 "SerialNumber": o.Sn(),
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700250 }).Errorf("onu-id-duplicated-with-%s", common.OnuSnToString(sn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800251 return
252 } else {
253 o.PonPort.storeOnuId(o.ID, o.SerialNumber)
254 }
255
Matteo Scandolof9d43412021-01-12 11:11:34 -0800256 msg := bbsim.Message{
257 Type: bbsim.OnuIndication,
258 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700259 OnuSN: o.SerialNumber,
260 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800261 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700262 },
263 }
264 o.Channel <- msg
265 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700266 fmt.Sprintf("enter_%s", OnuStateDisabled): func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700267
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700268 o.cleanupOnuState()
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700269
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700270 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800271 if err := o.OperState.Event("disable"); err != nil {
272 onuLogger.WithFields(log.Fields{
273 "OnuId": o.ID,
274 "IntfId": o.PonPortID,
275 "OnuSn": o.Sn(),
276 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
277 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700278 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800279 msg := bbsim.Message{
280 Type: bbsim.OnuIndication,
281 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700282 OnuSN: o.SerialNumber,
283 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800284 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700285 },
286 }
287 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530288
Matteo Scandolo8a574812021-05-20 15:18:53 -0700289 // disable the UNI ports
290 for _, uni := range o.UniPorts {
291 _ = uni.Disable()
292 }
293
Elia Battistonac63b112022-01-12 18:40:49 +0100294 // disable the POTS UNI ports
295 for _, pots := range o.PotsPorts {
296 _ = pots.Disable()
297 }
298
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530299 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100300 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolo8a574812021-05-20 15:18:53 -0700301 // NOTE may need to wait for the UNIs to be down too before shutting down the channel
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530302 if len(o.FlowIds) == 0 {
303 close(o.Channel)
304 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700305 },
306 fmt.Sprintf("enter_%s", OnuStatePonDisabled): func(event *fsm.Event) {
307 o.cleanupOnuState()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700308 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700309 // BBR states
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700310 fmt.Sprintf("enter_%s", BbrOnuStateEapolFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800311 msg := bbsim.Message{
312 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700313 }
314 o.Channel <- msg
315 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700316 fmt.Sprintf("enter_%s", BbrOnuStateDhcpFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800317 msg := bbsim.Message{
318 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700319 }
320 o.Channel <- msg
321 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700322 },
323 )
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700324 onuLogger.WithFields(log.Fields{
Elia Battistonac63b112022-01-12 18:40:49 +0100325 "OnuId": o.ID,
326 "IntfId": o.PonPortID,
327 "OnuSn": o.Sn(),
328 "NumUni": olt.NumUni,
329 "NumPots": olt.NumPots,
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700330 }).Debug("creating-uni-ports")
Elia Battistonac63b112022-01-12 18:40:49 +0100331
332 // create Ethernet UNIs
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700333 for i := 0; i < olt.NumUni; i++ {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700334 uni, err := NewUniPort(uint32(i), &o, nextCtag, nextStag)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700335 if err != nil {
336 onuLogger.WithFields(log.Fields{
337 "OnuId": o.ID,
338 "IntfId": o.PonPortID,
339 "OnuSn": o.Sn(),
340 "Err": err,
341 }).Fatal("cannot-create-uni-port")
342 }
343 o.UniPorts = append(o.UniPorts, uni)
344 }
Elia Battistonac63b112022-01-12 18:40:49 +0100345 // create POTS UNIs, with progressive IDs
346 for i := olt.NumUni; i < (olt.NumUni + olt.NumPots); i++ {
347 pots, err := NewPotsPort(uint32(i), &o)
348 if err != nil {
349 onuLogger.WithFields(log.Fields{
350 "OnuId": o.ID,
351 "IntfId": o.PonPortID,
352 "OnuSn": o.Sn(),
353 "Err": err,
354 }).Fatal("cannot-create-pots-port")
355 }
356 o.PotsPorts = append(o.PotsPorts, pots)
357 }
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700358
Elia Battistonb7bea222022-02-18 16:25:00 +0100359 mibDb, err := omcilib.GenerateMibDatabase(len(o.UniPorts), len(o.PotsPorts), o.PonPort.Technology)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700360 if err != nil {
361 onuLogger.WithFields(log.Fields{
362 "OnuId": o.ID,
363 "IntfId": o.PonPortID,
364 "OnuSn": o.Sn(),
365 }).Fatal("cannot-generate-mibdb-for-onu")
366 }
367 o.MibDb = mibDb
368
Matteo Scandolo27428702019-10-11 16:21:16 -0700369 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700370}
371
William Kurkian0418bc82019-11-06 12:16:24 -0500372func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700373 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700374 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700375 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700376 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700377 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
378}
379
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700380// cleanupOnuState this method is to clean the local state when the ONU is disabled
381func (o *Onu) cleanupOnuState() {
382 // clean the ONU state
Andrea Campanellacc9b1662022-01-26 17:47:48 +0100383 o.Flows = []FlowKey{}
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700384 o.PonPort.removeOnuId(o.ID)
Girish Gowdra574834a2022-02-04 15:15:15 -0800385 o.PonPort.removeAllocIdsForOnuSn(o.SerialNumber)
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700386 o.PonPort.removeGemPortBySn(o.SerialNumber)
387
388 o.onuAlarmsInfoLock.Lock()
389 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
390 o.onuAlarmsInfoLock.Unlock()
391}
392
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100393// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000394func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700395 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100396 "onuID": o.ID,
397 "onuSN": o.Sn(),
398 "ponPort": o.PonPortID,
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700399 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100400 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700401
Matteo Scandolob307d8a2021-05-10 15:19:27 -0700402 defer onuLogger.WithFields(log.Fields{
403 "onuID": o.ID,
404 "onuSN": o.Sn(),
405 "stream": stream,
406 }).Debug("Stopped handling ONU Indication Channel")
407
David Bainbridge103cf022019-12-16 20:11:35 +0000408loop:
409 for {
410 select {
411 case <-ctx.Done():
412 onuLogger.WithFields(log.Fields{
413 "onuID": o.ID,
414 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700415 }).Debug("ONU message handling canceled via context")
416 break loop
417 case <-stream.Context().Done():
418 onuLogger.WithFields(log.Fields{
419 "onuID": o.ID,
420 "onuSN": o.Sn(),
421 }).Debug("ONU message handling canceled via stream context")
David Bainbridge103cf022019-12-16 20:11:35 +0000422 break loop
423 case message, ok := <-o.Channel:
424 if !ok || ctx.Err() != nil {
425 onuLogger.WithFields(log.Fields{
426 "onuID": o.ID,
427 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700428 }).Debug("ONU message handling canceled via channel close")
David Bainbridge103cf022019-12-16 20:11:35 +0000429 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700430 }
David Bainbridge103cf022019-12-16 20:11:35 +0000431 onuLogger.WithFields(log.Fields{
432 "onuID": o.ID,
433 "onuSN": o.Sn(),
434 "messageType": message.Type,
435 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700436
David Bainbridge103cf022019-12-16 20:11:35 +0000437 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800438 case bbsim.OnuDiscIndication:
439 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000440 // NOTE we need to slow down and send ONU Discovery Indication in batches to better emulate a real scenario
Pragya Arya2225f202020-01-29 18:05:01 +0530441 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000442 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800443 case bbsim.OnuIndication:
444 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000445 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800446 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800447 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800448 msg, _ := message.Data.(bbsim.OmciMessage)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200449 _ = o.handleOmciRequest(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800450 case bbsim.UniStatusAlarm:
451 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530452 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
453 MeInstance: msg.EntityID,
454 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
455 }
456 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
457 o.onuAlarmsInfoLock.Lock()
458 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
459 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
460 if pkt != nil { //pkt will be nil if we are unable to create the alarm
461 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
462 onuLogger.WithFields(log.Fields{
463 "IntfId": o.PonPortID,
464 "SerialNumber": o.Sn(),
465 "omciPacket": pkt,
466 "adminState": msg.AdminState,
467 "entityID": msg.EntityID,
468 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
469 alarmInfo.SequenceNo--
470 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800471 onuLogger.WithFields(log.Fields{
472 "IntfId": o.PonPortID,
473 "SerialNumber": o.Sn(),
474 "omciPacket": pkt,
475 "adminState": msg.AdminState,
476 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530477 }).Trace("UNI-Link-alarm-sent")
478 if alarmBitMap == [28]byte{0} {
479 delete(o.onuAlarmsInfo, onuAlarmMapKey)
480 } else {
481 alarmInfo.AlarmBitMap = alarmBitMap
482 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
483 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800484 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530485 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800486 case bbsim.FlowAdd:
487 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700488 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800489 case bbsim.FlowRemoved:
490 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700491 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800492 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700493
Matteo Scandolof9d43412021-01-12 11:11:34 -0800494 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000495
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700496 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000497 "IntfId": msg.IntfId,
498 "OnuId": msg.OnuId,
499 "pktType": msg.Type,
500 }).Trace("Received OnuPacketOut Message")
501
Matteo Scandolo8a574812021-05-20 15:18:53 -0700502 uni, err := o.findUniByPortNo(msg.PortNo)
Matteo Scandolo618a6582020-09-09 12:21:29 -0700503
Matteo Scandolo8a574812021-05-20 15:18:53 -0700504 if err != nil {
505 onuLogger.WithFields(log.Fields{
506 "IntfId": msg.IntfId,
507 "OnuId": msg.OnuId,
508 "pktType": msg.Type,
509 "portNo": msg.PortNo,
510 "MacAddress": msg.MacAddress,
511 "Pkt": hex.EncodeToString(msg.Packet.Data()),
512 "OnuSn": o.Sn(),
513 }).Error("Cannot find Uni associated with packet")
514 return
David Bainbridge103cf022019-12-16 20:11:35 +0000515 }
Matteo Scandolo8a574812021-05-20 15:18:53 -0700516 uni.PacketCh <- msg
517 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800518 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000519 // NOTE we only receive BBR packets here.
520 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
521 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
Matteo Scandolof9d43412021-01-12 11:11:34 -0800522 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000523
Matteo Scandolo8a574812021-05-20 15:18:53 -0700524 onuLogger.WithFields(log.Fields{
525 "IntfId": msg.IntfId,
526 "OnuId": msg.OnuId,
527 "PortNo": msg.PortNo,
528 "GemPortId": msg.GemPortId,
529 "pktType": msg.Type,
David Bainbridge103cf022019-12-16 20:11:35 +0000530 }).Trace("Received OnuPacketIn Message")
531
Matteo Scandolo8a574812021-05-20 15:18:53 -0700532 uni, err := o.findUniByPortNo(msg.PortNo)
533 if err != nil {
534 onuLogger.WithFields(log.Fields{
535 "IntfId": msg.IntfId,
536 "OnuId": msg.OnuId,
537 "PortNo": msg.PortNo,
538 "GemPortId": msg.GemPortId,
539 "pktType": msg.Type,
540 }).Error(err.Error())
541 }
542
543 // BBR has one service and one UNI
544 serviceId := uint32(0)
545 oltId := 0
David Bainbridge103cf022019-12-16 20:11:35 +0000546 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700547 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, msg.GemPortId, o.Sn(), msg.PortNo, uni.ID, serviceId, oltId, o.InternalState, msg.Packet, stream, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000548 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700549 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000550 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800551 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800552 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800553 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
554 o.handleOmciResponse(msg, client)
555 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000556 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800557 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000558 o.sendDhcpFlow(client)
559 default:
560 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700561 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700562 }
563 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700564}
565
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800566func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700567 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700568 sn.VendorId = []byte("BBSM")
569 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700570 return sn
571}
572
Matteo Scandolof9d43412021-01-12 11:11:34 -0800573func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700574 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800575 IntfId: o.PonPortID,
576 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700577 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700578
Matteo Scandolo4747d292019-08-05 11:50:18 -0700579 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700580 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700581 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700582 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700583
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700584 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800585 "IntfId": o.PonPortID,
586 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700587 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700588 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800589 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800590
591 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
592 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800593 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800594 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800595 o.sendOnuDiscIndication(msg, stream)
596 }
597 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700598}
599
Matteo Scandolof9d43412021-01-12 11:11:34 -0800600func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800601 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
602 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700603
604 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700605 IntfId: o.PonPortID,
606 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700607 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700608 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700609 SerialNumber: o.SerialNumber,
610 }}
611 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800612 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700613 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700614 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700615 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700616 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800617 "IntfId": o.PonPortID,
618 "OnuId": o.ID,
619 "VolthaOnuId": msg.OnuID,
620 "OperState": msg.OperState.String(),
621 "AdminState": msg.OperState.String(),
622 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700623 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700624
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700625}
626
Matteo Scandolof9d43412021-01-12 11:11:34 -0800627func (o *Onu) HandleShutdownONU() error {
628
629 dyingGasp := pb.ONUAlarmRequest{
630 AlarmType: "DYING_GASP",
631 SerialNumber: o.Sn(),
632 Status: "on",
633 }
634
635 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
636 onuLogger.WithFields(log.Fields{
637 "OnuId": o.ID,
638 "IntfId": o.PonPortID,
639 "OnuSn": o.Sn(),
640 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
641 return err
642 }
643
644 losReq := pb.ONUAlarmRequest{
645 AlarmType: "ONU_ALARM_LOS",
646 SerialNumber: o.Sn(),
647 Status: "on",
648 }
649
650 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
651 onuLogger.WithFields(log.Fields{
652 "OnuId": o.ID,
653 "IntfId": o.PonPortID,
654 "OnuSn": o.Sn(),
655 }).Errorf("Cannot send LOS: %s", err.Error())
656
657 return err
658 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530659 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800660 // TODO if it's the last ONU on the PON, then send a PON LOS
661
Matteo Scandolocedde462021-03-09 17:37:16 -0800662 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800663 onuLogger.WithFields(log.Fields{
664 "OnuId": o.ID,
665 "IntfId": o.PonPortID,
666 "OnuSn": o.Sn(),
667 }).Errorf("Cannot shutdown ONU: %s", err.Error())
668 return err
669 }
670
671 return nil
672}
673
674func (o *Onu) HandlePowerOnONU() error {
675 intitalState := o.InternalState.Current()
676
677 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800678 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
679 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800680 onuLogger.WithFields(log.Fields{
681 "OnuId": o.ID,
682 "IntfId": o.PonPortID,
683 "OnuSn": o.Sn(),
684 }).Errorf("Cannot poweron ONU: %s", err.Error())
685 return err
686 }
687 }
688
689 // turn off the LOS Alarm
690 losReq := pb.ONUAlarmRequest{
691 AlarmType: "ONU_ALARM_LOS",
692 SerialNumber: o.Sn(),
693 Status: "off",
694 }
695
696 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
697 onuLogger.WithFields(log.Fields{
698 "OnuId": o.ID,
699 "IntfId": o.PonPortID,
700 "OnuSn": o.Sn(),
701 }).Errorf("Cannot send LOS: %s", err.Error())
702 return err
703 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530704 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800705
706 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800707 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800708 onuLogger.WithFields(log.Fields{
709 "OnuId": o.ID,
710 "IntfId": o.PonPortID,
711 "OnuSn": o.Sn(),
712 }).Errorf("Cannot poweron ONU: %s", err.Error())
713 return err
714 }
715
716 // move o directly to enable state only when its a powercycle case
717 // in case of first time o poweron o will be moved to enable on
718 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800719 if intitalState == OnuStateDisabled {
720 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800721 onuLogger.WithFields(log.Fields{
722 "OnuId": o.ID,
723 "IntfId": o.PonPortID,
724 "OnuSn": o.Sn(),
725 }).Errorf("Cannot enable ONU: %s", err.Error())
726 return err
727 }
728 }
729
730 return nil
731}
732
733func (o *Onu) SetAlarm(alarmType string, status string) error {
734 alarmReq := pb.ONUAlarmRequest{
735 AlarmType: alarmType,
736 SerialNumber: o.Sn(),
737 Status: status,
738 }
739
740 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
741 if err != nil {
742 return err
743 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530744 raiseAlarm := false
745 if alarmReq.Status == "on" {
746 raiseAlarm = true
747 }
748 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800749 return nil
750}
751
752func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530753 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700754 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530755 if err != nil {
756 log.Errorf("error in getting msgType %v", err)
757 return
758 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800759 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530760 o.seqNumber = 0
761 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800762 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530763 o.seqNumber++
764 if o.seqNumber > 290 {
765 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
766 }
767 }
768 }
769}
770
Matteo Scandolof9d43412021-01-12 11:11:34 -0800771// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
772// and generate the appropriate response to it
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200773func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800774
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700775 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700776 "omciMsgType": msg.OmciMsg.MessageType,
777 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
778 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700779 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700780 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800781 }).Trace("omci-message-decoded")
782
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000783 if o.OmciMsgCounter < maxOmciMsgCounter {
784 o.OmciMsgCounter++
785 } else {
786 o.OmciMsgCounter = 1
787 }
788 if o.OmciMsgCounter > o.OmciResponseRate {
789 onuLogger.WithFields(log.Fields{
790 "OmciMsgCounter": o.OmciMsgCounter,
791 "OmciResponseRate": o.OmciResponseRate,
792 "omciMsgType": msg.OmciMsg.MessageType,
Matteo Scandoloa96e2242021-09-28 10:13:17 -0700793 "txId": msg.OmciMsg.TransactionID,
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200794 }).Debug("skipping-omci-msg-response")
795 return fmt.Errorf("skipping-omci-msg-response-because-of-response-rate-%d", o.OmciResponseRate)
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000796 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800797 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800798 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700799 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800800 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800801 onuLogger.WithFields(log.Fields{
802 "IntfId": o.PonPortID,
803 "OnuId": o.ID,
804 "SerialNumber": o.Sn(),
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700805 }).Debug("received-mib-reset-request")
Matteo Scandolob5913142021-03-19 16:10:18 -0700806 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800807 o.MibDataSync = 0
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700808
809 // if the MIB reset is successful then remove all the stored AllocIds and GemPorts
Girish Gowdra574834a2022-02-04 15:15:15 -0800810 o.PonPort.removeAllocIdsForOnuSn(o.SerialNumber)
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700811 o.PonPort.removeGemPortBySn(o.SerialNumber)
Girish Gowdrae2683102021-03-05 08:24:26 -0800812 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800813 case omci.MibUploadRequestType:
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700814 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID, o.MibDb.NumberOfCommands)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800815 case omci.MibUploadNextRequestType:
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700816 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync, o.MibDb)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800817 case omci.GetRequestType:
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700818 onuDown := o.AdminLockState == 1
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700819 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId,
820 o.CommittedImageEntityId, o.StandbyImageVersion, o.ActiveImageVersion, o.CommittedImageVersion, onuDown)
Himani Chawla908a1f52022-01-27 14:39:44 +0530821
Matteo Scandolof9d43412021-01-12 11:11:34 -0800822 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800823 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700824 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800825 switch msgObj.EntityClass {
826 case me.PhysicalPathTerminationPointEthernetUniClassID:
827 // if we're Setting a PPTP state
Matteo Scandolo8a574812021-05-20 15:18:53 -0700828 // we need to send the appropriate alarm (handled in the UNI struct)
829 uni, err := o.FindUniByEntityId(msgObj.EntityInstance)
830 if err != nil {
831 onuLogger.Error(err)
832 success = false
833 } else {
834 // 1 locks the UNI, 0 unlocks it
Elia Battiston9bfe1102022-02-03 10:38:03 +0100835 adminState := msgObj.Attributes[me.PhysicalPathTerminationPointEthernetUni_AdministrativeState].(uint8)
Matteo Scandolo8a574812021-05-20 15:18:53 -0700836 var err error
Himani Chawla13b1ee02021-03-15 01:43:53 +0530837 if adminState == 1 {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700838 err = uni.Disable()
Girish Gowdra996d81e2021-04-21 16:16:27 -0700839 } else {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700840 err = uni.Enable()
Himani Chawla13b1ee02021-03-15 01:43:53 +0530841 }
Matteo Scandolo8a574812021-05-20 15:18:53 -0700842 if err != nil {
843 onuLogger.WithFields(log.Fields{
844 "IntfId": o.PonPortID,
845 "OnuId": o.ID,
846 "UniMeId": uni.MeId,
847 "UniId": uni.ID,
848 "SerialNumber": o.Sn(),
849 "Err": err.Error(),
850 }).Warn("cannot-change-uni-status")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800851 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800852 }
Elia Battistonac63b112022-01-12 18:40:49 +0100853 case me.PhysicalPathTerminationPointPotsUniClassID:
854 // if we're Setting a PPTP state
855 // we need to send the appropriate alarm (handled in the POTS struct)
856 pots, err := o.FindPotsByEntityId(msgObj.EntityInstance)
857 if err != nil {
858 onuLogger.Error(err)
859 success = false
860 } else {
861 // 1 locks the UNI, 0 unlocks it
Elia Battiston9bfe1102022-02-03 10:38:03 +0100862 adminState := msgObj.Attributes[me.PhysicalPathTerminationPointPotsUni_AdministrativeState].(uint8)
Elia Battistonac63b112022-01-12 18:40:49 +0100863 var err error
864 if adminState == 1 {
865 err = pots.Disable()
866 } else {
867 err = pots.Enable()
868 }
869 if err != nil {
870 onuLogger.WithFields(log.Fields{
871 "IntfId": o.PonPortID,
872 "OnuId": o.ID,
873 "PotsMeId": pots.MeId,
874 "PotsId": pots.ID,
875 "SerialNumber": o.Sn(),
876 "Err": err.Error(),
877 }).Warn("cannot-change-pots-status")
878 }
879 }
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700880 case me.OnuGClassID:
Elia Battiston9bfe1102022-02-03 10:38:03 +0100881 o.AdminLockState = msgObj.Attributes[me.OnuG_AdministrativeState].(uint8)
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700882 onuLogger.WithFields(log.Fields{
883 "IntfId": o.PonPortID,
884 "OnuId": o.ID,
885 "SerialNumber": o.Sn(),
886 "AdminLockState": o.AdminLockState,
887 }).Debug("set-onu-admin-lock-state")
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800888 case me.TContClassID:
Elia Battiston9bfe1102022-02-03 10:38:03 +0100889 allocId := msgObj.Attributes[me.TCont_AllocId].(uint16)
Girish Gowdra574834a2022-02-04 15:15:15 -0800890 entityID := msgObj.Attributes["ManagedEntityId"].(uint16)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800891
892 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
893 // otherwise we are adding it
894 if allocId == 255 || allocId == 65535 {
895 onuLogger.WithFields(log.Fields{
896 "IntfId": o.PonPortID,
897 "OnuId": o.ID,
898 "TContId": msgObj.EntityInstance,
899 "AllocId": allocId,
900 "SerialNumber": o.Sn(),
901 }).Trace("freeing-alloc-id-via-omci")
Girish Gowdra574834a2022-02-04 15:15:15 -0800902 o.PonPort.removeAllocId(o.PonPortID, o.ID, entityID)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800903 } else {
Girish Gowdra574834a2022-02-04 15:15:15 -0800904 if used, allocObj := o.PonPort.isAllocIdAllocated(o.PonPortID, o.ID, entityID); used {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800905 onuLogger.WithFields(log.Fields{
906 "IntfId": o.PonPortID,
907 "OnuId": o.ID,
908 "AllocId": allocId,
909 "SerialNumber": o.Sn(),
Girish Gowdra574834a2022-02-04 15:15:15 -0800910 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(allocObj.OnuSn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800911 success = false
912 } else {
913 onuLogger.WithFields(log.Fields{
914 "IntfId": o.PonPortID,
915 "OnuId": o.ID,
916 "TContId": msgObj.EntityInstance,
917 "AllocId": allocId,
918 "SerialNumber": o.Sn(),
919 }).Trace("storing-alloc-id-via-omci")
Girish Gowdra574834a2022-02-04 15:15:15 -0800920 o.PonPort.storeAllocId(o.PonPortID, o.ID, entityID, allocId, o.SerialNumber)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800921 }
922 }
Himani Chawla908a1f52022-01-27 14:39:44 +0530923 case me.EthernetFrameExtendedPmClassID,
924 me.EthernetFrameExtendedPm64BitClassID:
925 onuLogger.WithFields(log.Fields{
926 "me-instance": msgObj.EntityInstance,
927 }).Debug("set-request-received")
928 // No need to reset counters as onu adapter will simply send the set control block request to actually reset
929 // the counters, and respond with 0's without sending the get request to device.
930 // Also, if we even reset the counters here in cache, then on get we need to restore the counters back which
931 // would be of no use as ultimately the counters need to be restored.
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800932 }
933
934 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700935 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800936 o.MibDataSync++
937 }
938 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700939 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800940 }
941 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800942 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
943 var used bool
944 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700945 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800946 if err == nil {
947 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700948 // GemPort 4069 is reserved for multicast and shared across ONUs
949 if msgObj.EntityInstance != 4069 {
950 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
951 onuLogger.WithFields(log.Fields{
952 "IntfId": o.PonPortID,
953 "OnuId": o.ID,
954 "GemPortId": msgObj.EntityInstance,
955 "SerialNumber": o.Sn(),
956 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
957 } else {
958 onuLogger.WithFields(log.Fields{
959 "IntfId": o.PonPortID,
960 "OnuId": o.ID,
961 "GemPortId": msgObj.EntityInstance,
962 "SerialNumber": o.Sn(),
963 }).Trace("storing-gem-port-id-via-omci")
964 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
965 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800966 }
967 }
968 }
969
970 // if the gemPort is valid then increment the MDS and return a successful response
971 // otherwise fail the request
972 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
973 // validation this check will need to be rewritten
974 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700975 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800976 o.MibDataSync++
977 }
978 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700979 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800980 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800981 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700982 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800983 if err == nil {
984 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
985 onuLogger.WithFields(log.Fields{
986 "IntfId": o.PonPortID,
987 "OnuId": o.ID,
988 "GemPortId": msgObj.EntityInstance,
989 "SerialNumber": o.Sn(),
990 }).Trace("freeing-gem-port-id-via-omci")
991 o.PonPort.removeGemPort(msgObj.EntityInstance)
992 }
993 }
994
Matteo Scandolob5913142021-03-19 16:10:18 -0700995 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800996 o.MibDataSync++
997 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800998 case omci.RebootRequestType:
999
Matteo Scandolob5913142021-03-19 16:10:18 -07001000 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -08001001
1002 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -08001003 // we run this in a separate goroutine so that
1004 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -08001005 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -08001006 if err := o.Reboot(10 * time.Second); err != nil {
1007 log.WithFields(log.Fields{
1008 "IntfId": o.PonPortID,
1009 "OnuId": o.ID,
1010 "SerialNumber": o.Sn(),
1011 "err": err,
1012 }).Error("cannot-reboot-onu-after-omci-reboot-request")
1013 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001014 }()
1015 case omci.TestRequestType:
Girish Gowdra161d27a2021-05-05 12:01:44 -07001016 var classID me.ClassID
1017 var omciResult me.Results
1018 var instID uint16
1019 responsePkt, errResp, classID, instID, omciResult = omcilib.CreateTestResponse(msg.OmciPkt, msg.OmciMsg)
1020 // Send TestResult only in case the TestResponse omci result code is me.Success
1021 if responsePkt != nil && errResp == nil && omciResult == me.Success {
1022 if testResultPkt, err := omcilib.CreateTestResult(classID, instID, msg.OmciMsg.TransactionID); err == nil {
1023 // send test results asynchronously
1024 go func() {
1025 // Send test results after a second to emulate async behavior
1026 time.Sleep(1 * time.Second)
1027 if testResultPkt != nil {
1028 if err := o.sendOmciIndication(testResultPkt, msg.OmciMsg.TransactionID, stream); err != nil {
1029 onuLogger.WithFields(log.Fields{
1030 "IntfId": o.PonPortID,
1031 "SerialNumber": o.Sn(),
1032 "omciPacket": testResultPkt,
1033 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1034 "transCorrId": msg.OmciMsg.TransactionID,
1035 }).Errorf("failed-to-send-omci-message: %v", err)
1036 }
1037 }
1038 }()
Matteo Scandolof9d43412021-01-12 11:11:34 -08001039 }
1040 }
Girish Gowdraa539f522021-02-15 23:00:45 -08001041 case omci.SynchronizeTimeRequestType:
1042 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -07001043 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -08001044 case omci.StartSoftwareDownloadRequestType:
1045
1046 o.ImageSoftwareReceivedSections = 0
Himani Chawla4ff5fab2021-11-09 19:19:29 +05301047 o.ImageSectionData = []byte{}
Matteo Scandolob5913142021-03-19 16:10:18 -07001048 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -08001049
Matteo Scandolob5913142021-03-19 16:10:18 -07001050 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001051 o.MibDataSync++
1052 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
1053 onuLogger.WithFields(log.Fields{
1054 "OnuId": o.ID,
1055 "IntfId": o.PonPortID,
1056 "OnuSn": o.Sn(),
1057 "Err": err.Error(),
1058 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
1059 }
1060 } else {
1061 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001062 "OmciMsgType": msg.OmciMsg.MessageType,
1063 "TransCorrId": msg.OmciMsg.TransactionID,
1064 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001065 "IntfId": o.PonPortID,
1066 "SerialNumber": o.Sn(),
1067 }).Error("error-while-processing-start-software-download-request")
1068 }
1069 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001070 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001071 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001072 "OmciMsgType": msg.OmciMsg.MessageType,
1073 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001074 "EntityInstance": msgObj.EntityInstance,
1075 "SectionNumber": msgObj.SectionNumber,
1076 "SectionData": msgObj.SectionData,
1077 }).Trace("received-download-section-request")
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001078 //Extracting the first 14 bytes to use as a version for this image.
1079 if o.ImageSoftwareReceivedSections == 0 {
Matteo Scandolo76f6b892021-11-15 16:13:06 -08001080 o.InDownloadImageVersion = string(msgObj.SectionData[0:14])
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001081 }
Himani Chawla4ff5fab2021-11-09 19:19:29 +05301082 o.ImageSectionData = append(o.ImageSectionData, msgObj.SectionData...)
Matteo Scandolocedde462021-03-09 17:37:16 -08001083 o.ImageSoftwareReceivedSections++
1084 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
1085 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
1086 onuLogger.WithFields(log.Fields{
1087 "OnuId": o.ID,
1088 "IntfId": o.PonPortID,
1089 "OnuSn": o.Sn(),
1090 "Err": err.Error(),
1091 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
1092 }
1093 }
1094 }
1095 case omci.DownloadSectionRequestWithResponseType:
1096 // NOTE we only need to respond if an ACK is requested
Himani Chawla4ff5fab2021-11-09 19:19:29 +05301097 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001098 onuLogger.WithFields(log.Fields{
Himani Chawla4ff5fab2021-11-09 19:19:29 +05301099 "OmciMsgType": msg.OmciMsg.MessageType,
1100 "TransCorrId": msg.OmciMsg.TransactionID,
1101 "EntityInstance": msgObj.EntityInstance,
1102 "SectionNumber": msgObj.SectionNumber,
1103 "SectionData": msgObj.SectionData,
1104 }).Trace("received-download-section-request-with-response-type")
1105 o.ImageSectionData = append(o.ImageSectionData, msgObj.SectionData...)
1106 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
1107
1108 if errResp != nil {
1109 onuLogger.WithFields(log.Fields{
1110 "OmciMsgType": msg.OmciMsg.MessageType,
1111 "TransCorrId": msg.OmciMsg.TransactionID,
1112 "Err": errResp.Error(),
1113 "IntfId": o.PonPortID,
1114 "SerialNumber": o.Sn(),
1115 }).Error("error-while-processing-create-download-section-response")
1116 return fmt.Errorf("error-while-processing-create-download-section-response: %s", errResp.Error())
1117 }
1118 o.ImageSoftwareReceivedSections++
Matteo Scandolocedde462021-03-09 17:37:16 -08001119 }
Matteo Scandolocedde462021-03-09 17:37:16 -08001120 case omci.EndSoftwareDownloadRequestType:
Matteo Scandolo76f6b892021-11-15 16:13:06 -08001121 success := o.handleEndSoftwareDownloadRequest(msg)
Matteo Scandolocedde462021-03-09 17:37:16 -08001122
Matteo Scandolocedde462021-03-09 17:37:16 -08001123 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001124 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001125 o.MibDataSync++
1126 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1127 onuLogger.WithFields(log.Fields{
1128 "OnuId": o.ID,
1129 "IntfId": o.PonPortID,
1130 "OnuSn": o.Sn(),
1131 "Err": err.Error(),
1132 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1133 }
1134 } else {
1135 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001136 "OmciMsgType": msg.OmciMsg.MessageType,
1137 "TransCorrId": msg.OmciMsg.TransactionID,
1138 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001139 "IntfId": o.PonPortID,
1140 "SerialNumber": o.Sn(),
Matteo Scandolo76f6b892021-11-15 16:13:06 -08001141 }).Error("error-while-responding-to-end-software-download-request")
Matteo Scandolocedde462021-03-09 17:37:16 -08001142 }
1143 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001144 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001145 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1146 onuLogger.WithFields(log.Fields{
1147 "OnuId": o.ID,
1148 "IntfId": o.PonPortID,
1149 "OnuSn": o.Sn(),
1150 "Err": err.Error(),
1151 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1152 }
1153 }
1154 }
Matteo Scandolocedde462021-03-09 17:37:16 -08001155 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001156 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001157 o.MibDataSync++
1158 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1159 onuLogger.WithFields(log.Fields{
1160 "OnuId": o.ID,
1161 "IntfId": o.PonPortID,
1162 "OnuSn": o.Sn(),
1163 "Err": err.Error(),
1164 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1165 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001166 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001167 o.ActiveImageEntityId = msgObj.EntityInstance
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001168 previousActiveImage := o.ActiveImageVersion
1169 o.ActiveImageVersion = o.StandbyImageVersion
1170 o.StandbyImageVersion = previousActiveImage
Matteo Scandolocedde462021-03-09 17:37:16 -08001171 } else {
1172 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1173 }
1174 onuLogger.WithFields(log.Fields{
1175 "OnuId": o.ID,
1176 "IntfId": o.PonPortID,
1177 "OnuSn": o.Sn(),
1178 "ActiveImageEntityId": o.ActiveImageEntityId,
1179 "CommittedImageEntityId": o.CommittedImageEntityId,
1180 }).Info("onu-software-image-activated")
1181
1182 // powercycle the ONU
1183 // we run this in a separate goroutine so that
1184 // the ActivateSoftwareResponse is sent to VOLTHA
1185 // NOTE do we need to wait before rebooting?
1186 go func() {
1187 if err := o.Reboot(10 * time.Second); err != nil {
1188 log.WithFields(log.Fields{
1189 "IntfId": o.PonPortID,
1190 "OnuId": o.ID,
1191 "SerialNumber": o.Sn(),
1192 "err": err,
1193 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1194 }
1195 }()
1196 }
1197 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001198 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001199 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001200 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001201 // TODO validate that the image to commit is:
1202 // - active
1203 // - not already committed
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001204 o.ActiveImageEntityId = msgObj.EntityInstance
Matteo Scandolocedde462021-03-09 17:37:16 -08001205 o.CommittedImageEntityId = msgObj.EntityInstance
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001206 //committed becomes standby
1207 o.StandbyImageVersion = o.CommittedImageVersion
1208 o.CommittedImageVersion = o.ActiveImageVersion
Matteo Scandolocedde462021-03-09 17:37:16 -08001209 } else {
1210 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1211 }
1212 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1213 onuLogger.WithFields(log.Fields{
1214 "OnuId": o.ID,
1215 "IntfId": o.PonPortID,
1216 "OnuSn": o.Sn(),
1217 "Err": err.Error(),
1218 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1219 }
1220 onuLogger.WithFields(log.Fields{
1221 "OnuId": o.ID,
1222 "IntfId": o.PonPortID,
1223 "OnuSn": o.Sn(),
1224 "ActiveImageEntityId": o.ActiveImageEntityId,
1225 "CommittedImageEntityId": o.CommittedImageEntityId,
1226 }).Info("onu-software-image-committed")
1227 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301228 case omci.GetAllAlarmsRequestType:
1229 // Reset the alarm sequence number on receiving get all alarms request.
1230 o.onuAlarmsInfoLock.Lock()
1231 for key, alarmInfo := range o.onuAlarmsInfo {
1232 // reset the alarm sequence no
1233 alarmInfo.SequenceNo = 0
1234 o.onuAlarmsInfo[key] = alarmInfo
1235 }
1236 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001237 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301238 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001239 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301240 responsePkt = nil //Do not send any response for error case
1241 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001242 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001243 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001244 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1245 "omciPkt": msg.OmciPkt,
1246 "omciMsgType": msg.OmciMsg.MessageType,
1247 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001248 "IntfId": o.PonPortID,
1249 "SerialNumber": o.Sn(),
1250 }).Warnf("OMCI-message-not-supported")
1251 }
1252
1253 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001254 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001255 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001256 "IntfId": o.PonPortID,
1257 "SerialNumber": o.Sn(),
1258 "omciPacket": responsePkt,
1259 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1260 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001261 }).Errorf("failed-to-send-omci-message: %v", err)
1262 }
1263 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001264
Pragya Arya324337e2020-02-20 14:35:08 +05301265 o.publishOmciEvent(msg)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001266 return nil
Matteo Scandolof9d43412021-01-12 11:11:34 -08001267}
Pragya Arya324337e2020-02-20 14:35:08 +05301268
Matteo Scandolof9d43412021-01-12 11:11:34 -08001269// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1270func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1271 indication := &openolt.Indication_OmciInd{
1272 OmciInd: &openolt.OmciIndication{
1273 IntfId: o.PonPortID,
1274 OnuId: o.ID,
1275 Pkt: responsePkt,
1276 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001277 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001278 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1279 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001280 }
1281 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001282 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001283 "SerialNumber": o.Sn(),
Holger Hildebrandt02101a62022-04-06 13:00:51 +00001284 "omciPacket": hex.EncodeToString(indication.OmciInd.Pkt),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001285 "transCorrId": txId,
1286 }).Trace("omci-message-sent")
1287 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001288}
1289
Matteo Scandolo8a574812021-05-20 15:18:53 -07001290// FindUniById retrieves a UNI by ID
1291func (o *Onu) FindUniById(uniID uint32) (*UniPort, error) {
1292 for _, u := range o.UniPorts {
1293 uni := u.(*UniPort)
1294 if uni.ID == uniID {
1295 return uni, nil
1296 }
Matteo Scandolo27428702019-10-11 16:21:16 -07001297 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001298 return nil, fmt.Errorf("cannot-find-uni-with-id-%d-on-onu-%s", uniID, o.Sn())
1299}
1300
Elia Battistonac63b112022-01-12 18:40:49 +01001301// FindPotsById retrieves a POTS port by ID
1302func (o *Onu) FindPotsById(uniID uint32) (*PotsPort, error) {
1303 for _, p := range o.PotsPorts {
1304 pots := p.(*PotsPort)
1305 if pots.ID == uniID {
1306 return pots, nil
1307 }
1308 }
1309 return nil, fmt.Errorf("cannot-find-pots-with-id-%d-on-onu-%s", uniID, o.Sn())
1310}
1311
Matteo Scandolo8a574812021-05-20 15:18:53 -07001312// FindUniByEntityId retrieves a uni by MeID (the OMCI entity ID)
1313func (o *Onu) FindUniByEntityId(meId uint16) (*UniPort, error) {
1314 entityId := omcilib.EntityID{}.FromUint16(meId)
1315 for _, u := range o.UniPorts {
1316 uni := u.(*UniPort)
1317 if uni.MeId.Equals(entityId) {
1318 return uni, nil
1319 }
1320 }
1321 return nil, fmt.Errorf("cannot-find-uni-with-meid-%s-on-onu-%s", entityId.ToString(), o.Sn())
Matteo Scandolo27428702019-10-11 16:21:16 -07001322}
1323
Elia Battistonac63b112022-01-12 18:40:49 +01001324// FindPotsByEntityId retrieves a POTS uni by MeID (the OMCI entity ID)
1325func (o *Onu) FindPotsByEntityId(meId uint16) (*PotsPort, error) {
1326 entityId := omcilib.EntityID{}.FromUint16(meId)
1327 for _, p := range o.PotsPorts {
1328 pots := p.(*PotsPort)
1329 if pots.MeId.Equals(entityId) {
1330 return pots, nil
1331 }
1332 }
1333 return nil, fmt.Errorf("cannot-find-pots-with-meid-%s-on-onu-%s", entityId.ToString(), o.Sn())
1334}
1335
William Kurkian0418bc82019-11-06 12:16:24 -05001336func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001337 onuLogger.WithFields(log.Fields{
1338 "IntfId": o.PonPortID,
1339 "OnuId": id,
1340 "SerialNumber": o.Sn(),
1341 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001342 o.ID = id
1343}
1344
Matteo Scandolof9d43412021-01-12 11:11:34 -08001345func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001346 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001347 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001348 "Cookie": msg.Flow.Cookie,
1349 "DstPort": msg.Flow.Classifier.DstPort,
1350 "FlowId": msg.Flow.FlowId,
1351 "FlowType": msg.Flow.FlowType,
1352 "GemportId": msg.Flow.GemportId,
1353 "InnerVlan": msg.Flow.Classifier.IVid,
1354 "IntfId": msg.Flow.AccessIntfId,
1355 "IpProto": msg.Flow.Classifier.IpProto,
1356 "OnuId": msg.Flow.OnuId,
1357 "OnuSn": o.Sn(),
1358 "OuterVlan": msg.Flow.Classifier.OVid,
1359 "PortNo": msg.Flow.PortNo,
1360 "SrcPort": msg.Flow.Classifier.SrcPort,
1361 "UniID": msg.Flow.UniId,
1362 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1363 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1364 "ClassifierIVid": msg.Flow.Classifier.IVid,
1365 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001366 "ReplicateFlow": msg.Flow.ReplicateFlow,
1367 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001368 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001369
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001370 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001371
1372 var gemPortId uint32
1373 if msg.Flow.ReplicateFlow {
1374 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1375 // first available gemport (we only need to send one packet)
1376 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1377 gemPortId = msg.Flow.PbitToGemport[0]
1378 } else {
1379 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1380 gemPortId = uint32(msg.Flow.GemportId)
1381 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001382
1383 uni, err := o.FindUniById(uint32(msg.Flow.UniId))
1384 if err != nil {
1385 onuLogger.WithFields(log.Fields{
1386 "IntfId": o.PonPortID,
1387 "OnuId": o.ID,
1388 "UniId": msg.Flow.UniId,
1389 "PortNo": msg.Flow.PortNo,
1390 "SerialNumber": o.Sn(),
1391 "FlowId": msg.Flow.FlowId,
1392 "FlowType": msg.Flow.FlowType,
1393 }).Error("cannot-find-uni-port-for-flow")
1394 }
1395
1396 uni.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
1397 uni.StorePortNo(msg.Flow.PortNo)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001398
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001399 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Elia Battistonda45f6e2022-02-24 11:12:16 +01001400 onuLogger.WithFields(log.Fields{
1401 "IntfId": o.PonPortID,
1402 "OnuId": o.ID,
1403 "UniId": msg.Flow.UniId,
1404 "PortNo": msg.Flow.PortNo,
1405 "SerialNumber": o.Sn(),
1406 "FlowId": msg.Flow.FlowId,
1407 }).Debug("EAPOL flow detected")
Matteo Scandolo8a574812021-05-20 15:18:53 -07001408 uni.HandleAuth()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001409 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1410 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001411 msg.Flow.Classifier.DstPort == uint32(67) {
Elia Battistonda45f6e2022-02-24 11:12:16 +01001412 onuLogger.WithFields(log.Fields{
1413 "IntfId": o.PonPortID,
1414 "OnuId": o.ID,
1415 "UniId": msg.Flow.UniId,
1416 "PortNo": msg.Flow.PortNo,
1417 "SerialNumber": o.Sn(),
1418 "FlowId": msg.Flow.FlowId,
1419 "FlowType": msg.Flow.FlowType,
1420 }).Debug("DHCP flow detected")
Matteo Scandolo8a574812021-05-20 15:18:53 -07001421 uni.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001422 }
1423}
1424
Matteo Scandolof9d43412021-01-12 11:11:34 -08001425func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001426 onuLogger.WithFields(log.Fields{
1427 "IntfId": o.PonPortID,
1428 "OnuId": o.ID,
1429 "SerialNumber": o.Sn(),
1430 "FlowId": msg.Flow.FlowId,
1431 "FlowType": msg.Flow.FlowType,
1432 }).Debug("ONU receives FlowRemove")
1433
1434 for idx, flow := range o.FlowIds {
1435 // If the gemport is found, delete it from local cache.
1436 if flow == msg.Flow.FlowId {
1437 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1438 break
1439 }
1440 }
1441
1442 if len(o.FlowIds) == 0 {
1443 onuLogger.WithFields(log.Fields{
1444 "IntfId": o.PonPortID,
1445 "OnuId": o.ID,
1446 "SerialNumber": o.Sn(),
1447 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001448
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301449 // check if ONU delete is performed and
1450 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001451 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301452 close(o.Channel)
1453 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001454 }
1455}
1456
Matteo Scandolocedde462021-03-09 17:37:16 -08001457func (o *Onu) Reboot(timeout time.Duration) error {
1458 onuLogger.WithFields(log.Fields{
1459 "IntfId": o.PonPortID,
1460 "OnuId": o.ID,
1461 "SerialNumber": o.Sn(),
1462 }).Debug("shutting-down-onu")
1463 if err := o.HandleShutdownONU(); err != nil {
1464 return err
1465 }
1466 time.Sleep(timeout)
1467 onuLogger.WithFields(log.Fields{
1468 "IntfId": o.PonPortID,
1469 "OnuId": o.ID,
1470 "SerialNumber": o.Sn(),
1471 }).Debug("power-on-onu")
1472 if err := o.HandlePowerOnONU(); err != nil {
1473 return err
1474 }
1475 return nil
1476}
1477
Matteo Scandolo76f6b892021-11-15 16:13:06 -08001478// returns true if the request is successful, false otherwise
1479func (o *Onu) handleEndSoftwareDownloadRequest(msg bbsim.OmciMessage) bool {
1480 msgObj, err := omcilib.ParseEndSoftwareDownloadRequest(msg.OmciPkt)
1481 if err != nil {
1482 onuLogger.WithFields(log.Fields{
1483 "OmciMsgType": msg.OmciMsg.MessageType,
1484 "TransCorrId": msg.OmciMsg.TransactionID,
1485 "Err": err.Error(),
1486 "IntfId": o.PonPortID,
1487 "SerialNumber": o.Sn(),
1488 }).Error("error-while-processing-end-software-download-request")
1489 return false
1490 }
1491
1492 onuLogger.WithFields(log.Fields{
1493 "OnuId": o.ID,
1494 "IntfId": o.PonPortID,
1495 "OnuSn": o.Sn(),
1496 "msgObj": msgObj,
1497 }).Trace("EndSoftwareDownloadRequest received message")
1498
1499 // if the image download is ongoing and we receive a message with
1500 // ImageSize = 0 and Crc = 4294967295 (0xFFFFFFFF) respond with success
1501 if o.ImageSoftwareReceivedSections > 0 &&
1502 msgObj.ImageSize == 0 &&
1503 msgObj.CRC32 == 4294967295 {
1504 o.ImageSoftwareReceivedSections = 0
1505 // NOTE potentially we may want to add a ONU state to reflect
1506 // the software download abort
1507 return true
1508 }
1509
1510 // In the startSoftwareDownload we get the image size and the window size.
1511 // We calculate how many DownloadSection we should receive and validate
1512 // that we got the correct amount when we receive this message
1513 // If the received sections are different from the expected sections
1514 // respond with failure
1515 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
1516 onuLogger.WithFields(log.Fields{
1517 "OnuId": o.ID,
1518 "IntfId": o.PonPortID,
1519 "OnuSn": o.Sn(),
1520 "ExpectedSections": o.ImageSoftwareExpectedSections,
1521 "ReceivedSections": o.ImageSoftwareReceivedSections,
1522 }).Errorf("onu-did-not-receive-all-image-sections")
1523 return false
1524 }
1525
1526 // check the received CRC vs the computed CRC
1527 computedCRC := crc32a.Checksum(o.ImageSectionData[:int(msgObj.ImageSize)])
1528 //Convert the crc to network byte order
1529 var byteSlice = make([]byte, 4)
1530 binary.LittleEndian.PutUint32(byteSlice, computedCRC)
1531 computedCRC = binary.BigEndian.Uint32(byteSlice)
1532 if msgObj.CRC32 != computedCRC {
1533 onuLogger.WithFields(log.Fields{
1534 "OnuId": o.ID,
1535 "IntfId": o.PonPortID,
1536 "OnuSn": o.Sn(),
1537 "ReceivedCRC": msgObj.CRC32,
1538 "CalculatedCRC": computedCRC,
1539 }).Errorf("onu-image-crc-validation-failed")
1540 return false
1541 }
1542
1543 o.StandbyImageVersion = o.InDownloadImageVersion
1544 onuLogger.WithFields(log.Fields{
1545 "OnuId": o.ID,
1546 "IntfId": o.PonPortID,
1547 "OnuSn": o.Sn(),
1548 "StandbyVersion": o.StandbyImageVersion,
1549 }).Debug("onu-image-version-updated")
1550 return true
1551}
1552
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001553// BBR methods
1554
1555func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1556 omciMsg := openolt.OmciMsg{
1557 IntfId: intfId,
1558 OnuId: onuId,
1559 Pkt: pktBytes,
1560 }
1561
1562 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1563 log.WithFields(log.Fields{
1564 "IntfId": intfId,
1565 "OnuId": onuId,
1566 "SerialNumber": common.OnuSnToString(sn),
1567 "Pkt": omciMsg.Pkt,
1568 }).Fatalf("Failed to send MIB Reset")
1569 }
1570 log.WithFields(log.Fields{
1571 "IntfId": intfId,
1572 "OnuId": onuId,
1573 "SerialNumber": common.OnuSnToString(sn),
1574 "Pkt": omciMsg.Pkt,
1575 }).Tracef("Sent OMCI message %s", msgType)
1576}
1577
1578func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1579 var next uint16
1580 if len(highPriority) > 0 && highPriority[0] {
1581 next = onu.hpTid
1582 onu.hpTid += 1
1583 if onu.hpTid < 0x8000 {
1584 onu.hpTid = 0x8000
1585 }
1586 } else {
1587 next = onu.tid
1588 onu.tid += 1
1589 if onu.tid >= 0x8000 {
1590 onu.tid = 1
1591 }
1592 }
1593 return next
1594}
1595
1596// TODO move this method in responders/omcisim
Matteo Scandolo8a574812021-05-20 15:18:53 -07001597// StartOmci is called in BBR to start the OMCI state machine
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001598func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1599 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1600 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1601}
1602
Matteo Scandolof9d43412021-01-12 11:11:34 -08001603// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1604func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1605
1606 // we need to encode the packet in HEX
1607 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1608 hex.Encode(pkt, msg.OmciInd.Pkt)
1609 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1610 if err != nil {
1611 log.WithFields(log.Fields{
1612 "IntfId": o.PonPortID,
1613 "SerialNumber": o.Sn(),
1614 "omciPacket": msg.OmciInd.Pkt,
1615 }).Error("BBR Cannot parse OMCI packet")
1616 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001617
1618 log.WithFields(log.Fields{
1619 "IntfId": msg.OmciInd.IntfId,
1620 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001621 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001622 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001623 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301624 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001625 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001626 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001627 log.WithFields(log.Fields{
1628 "IntfId": msg.OmciInd.IntfId,
1629 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001630 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001631 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001632 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001633 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001634 case omci.MibResetResponseType:
1635 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1636 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1637 case omci.MibUploadResponseType:
1638 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1639 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1640 case omci.MibUploadNextResponseType:
1641 o.seqNumber++
Matteo Scandolo8a574812021-05-20 15:18:53 -07001642 // once the mibUpload is complete send a SetRequest for the PPTP to enable the UNI
1643 // NOTE that in BBR we only enable the first UNI
1644 if o.seqNumber == o.MibDb.NumberOfCommands {
1645 meId := omcilib.GenerateUniPortEntityId(1)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001646
Matteo Scandolo8a574812021-05-20 15:18:53 -07001647 meParams := me.ParamData{
1648 EntityID: meId.ToUint16(),
Elia Battiston9bfe1102022-02-03 10:38:03 +01001649 Attributes: me.AttributeValueMap{me.PhysicalPathTerminationPointEthernetUni_AdministrativeState: 0},
Matteo Scandolo8a574812021-05-20 15:18:53 -07001650 }
1651 managedEntity, omciError := me.NewPhysicalPathTerminationPointEthernetUni(meParams)
1652 if omciError.GetError() != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001653 onuLogger.WithFields(log.Fields{
1654 "OnuId": o.ID,
1655 "IntfId": o.PonPortID,
1656 "OnuSn": o.Sn(),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001657 }).Fatal(omciError.GetError())
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001658 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001659
1660 setPPtp, _ := omcilib.CreateSetRequest(managedEntity, 1)
1661 sendOmciMsg(setPPtp, o.PonPortID, o.ID, o.SerialNumber, "setRquest", client)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001662 } else {
1663 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1664 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001665 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001666 case omci.SetResponseType:
1667 // once we set the PPTP to active we can start sending flows
1668
1669 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
1670 onuLogger.WithFields(log.Fields{
1671 "OnuId": o.ID,
1672 "IntfId": o.PonPortID,
1673 "OnuSn": o.Sn(),
1674 }).Errorf("Error while transitioning ONU State %v", err)
1675 }
1676 case omci.AlarmNotificationType:
1677 log.Info("bbr-received-alarm")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001678 }
1679}
1680
1681func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1682
1683 classifierProto := openolt.Classifier{
1684 EthType: uint32(layers.EthernetTypeEAPOL),
1685 OVid: 4091,
1686 }
1687
1688 actionProto := openolt.Action{}
1689
1690 downstreamFlow := openolt.Flow{
1691 AccessIntfId: int32(o.PonPortID),
1692 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001693 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001694 FlowId: uint64(o.ID),
Elia Battiston560e9552022-01-31 10:44:15 +01001695 FlowType: flowTypeDownstream,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001696 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001697 Classifier: &classifierProto,
1698 Action: &actionProto,
1699 Priority: int32(100),
1700 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001701 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1702 // AllocId and GemPorts need to be unique per PON
1703 // for now use the ONU-ID, will need to change once we support multiple UNIs
1704 AllocId: int32(o.ID),
1705 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001706 }
1707
1708 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1709 log.WithFields(log.Fields{
1710 "IntfId": o.PonPortID,
1711 "OnuId": o.ID,
1712 "FlowId": downstreamFlow.FlowId,
1713 "PortNo": downstreamFlow.PortNo,
1714 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001715 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001716 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001717 }
1718 log.WithFields(log.Fields{
1719 "IntfId": o.PonPortID,
1720 "OnuId": o.ID,
1721 "FlowId": downstreamFlow.FlowId,
1722 "PortNo": downstreamFlow.PortNo,
1723 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1724 }).Info("Sent EAPOL Flow")
1725}
1726
1727func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001728
Matteo Scandolo8a574812021-05-20 15:18:53 -07001729 // BBR only works with a single UNI and a single service (ATT HSIA)
1730 hsia := o.UniPorts[0].(*UniPort).Services[0].(*Service)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001731 classifierProto := openolt.Classifier{
1732 EthType: uint32(layers.EthernetTypeIPv4),
1733 SrcPort: uint32(68),
1734 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001735 OVid: uint32(hsia.CTag),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001736 OPbits: 255,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001737 }
1738
1739 actionProto := openolt.Action{}
1740
1741 downstreamFlow := openolt.Flow{
1742 AccessIntfId: int32(o.PonPortID),
1743 OnuId: int32(o.ID),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001744 UniId: int32(0), // BBR only supports a single UNI
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001745 FlowId: uint64(o.ID),
Elia Battiston560e9552022-01-31 10:44:15 +01001746 FlowType: flowTypeDownstream,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001747 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001748 Classifier: &classifierProto,
1749 Action: &actionProto,
1750 Priority: int32(100),
1751 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001752 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1753 // AllocId and GemPorts need to be unique per PON
1754 // for now use the ONU-ID, will need to change once we support multiple UNIs
1755 AllocId: int32(o.ID),
1756 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001757 }
1758
1759 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1760 log.WithFields(log.Fields{
1761 "IntfId": o.PonPortID,
1762 "OnuId": o.ID,
1763 "FlowId": downstreamFlow.FlowId,
1764 "PortNo": downstreamFlow.PortNo,
1765 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001766 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001767 }).Fatalf("Failed to send DHCP Flow")
1768 }
1769 log.WithFields(log.Fields{
1770 "IntfId": o.PonPortID,
1771 "OnuId": o.ID,
1772 "FlowId": downstreamFlow.FlowId,
1773 "PortNo": downstreamFlow.PortNo,
1774 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1775 }).Info("Sent DHCP Flow")
1776}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301777
Andrea Campanellacc9b1662022-01-26 17:47:48 +01001778// DeleteFlow method search and delete flowKey from the onu flows slice
1779func (onu *Onu) DeleteFlow(key FlowKey) {
1780 for pos, flowKey := range onu.Flows {
1781 if flowKey == key {
1782 // delete the flowKey by shifting all flowKeys by one
1783 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1784 t := make([]FlowKey, len(onu.Flows))
1785 copy(t, onu.Flows)
1786 onu.Flows = t
Pragya Arya8bdb4532020-03-02 17:08:09 +05301787 break
1788 }
1789 }
1790}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301791
1792func (onu *Onu) ReDiscoverOnu() {
1793 // Wait for few seconds to be sure of the cleanup
1794 time.Sleep(5 * time.Second)
1795
1796 onuLogger.WithFields(log.Fields{
1797 "IntfId": onu.PonPortID,
1798 "OnuId": onu.ID,
1799 "OnuSn": onu.Sn(),
1800 }).Debug("Send ONU Re-Discovery")
1801
1802 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001803 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301804 log.WithFields(log.Fields{
1805 "IntfId": onu.PonPortID,
1806 "OnuSn": onu.Sn(),
1807 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001808 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301809 }
1810
Matteo Scandolocedde462021-03-09 17:37:16 -08001811 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301812 log.WithFields(log.Fields{
1813 "IntfId": onu.PonPortID,
1814 "OnuSn": onu.Sn(),
1815 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001816 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301817 }
1818}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001819
Matteo Scandolo8a574812021-05-20 15:18:53 -07001820// deprecated, delegate this to the uniPort
Matteo Scandolo4a036262020-08-17 15:56:13 -07001821func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001822 // FIXME is there a better way to avoid this loop?
1823 for _, u := range onu.UniPorts {
1824 uni := u.(*UniPort)
1825 for _, s := range uni.Services {
1826 service := s.(*Service)
1827 if service.HwAddress.String() == macAddress.String() {
1828 return service, nil
1829 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001830 }
1831 }
1832 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1833}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301834
Matteo Scandolo8a574812021-05-20 15:18:53 -07001835func (onu *Onu) findUniByPortNo(portNo uint32) (*UniPort, error) {
1836 for _, u := range onu.UniPorts {
1837 uni := u.(*UniPort)
1838 if uni.PortNo == portNo {
1839 return uni, nil
1840 }
1841 }
1842 return nil, fmt.Errorf("cannot-find-uni-with-port-no-%d", portNo)
1843}
1844
Himani Chawla13b1ee02021-03-15 01:43:53 +05301845func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1846 switch alarmType {
1847 case "ONU_ALARM_LOS":
1848 msg := bbsim.Message{
1849 Type: bbsim.UniStatusAlarm,
1850 Data: bbsim.UniStatusAlarmMessage{
1851 OnuSN: o.SerialNumber,
1852 OnuID: o.ID,
1853 EntityID: 257,
1854 RaiseOMCIAlarm: raiseOMCIAlarm,
1855 },
1856 }
1857 o.Channel <- msg
1858 }
1859
1860}
1861
1862func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1863 o.onuAlarmsInfoLock.Lock()
1864 defer o.onuAlarmsInfoLock.Unlock()
1865 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1866 if alarmInfo.SequenceNo == 255 {
1867 alarmInfo.SequenceNo = 1
1868 } else {
1869 alarmInfo.SequenceNo++
1870 }
1871 o.onuAlarmsInfo[key] = alarmInfo
1872 return alarmInfo.SequenceNo
1873 } else {
1874 // This is the first time alarm notification message is being sent
1875 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1876 SequenceNo: 1,
1877 }
1878 return 1
1879 }
1880}
Elia Battistonfe017662022-01-05 11:43:16 +01001881
1882func (o *Onu) InvalidateMibDataSync() {
1883 rand.Seed(time.Now().UnixNano())
1884 r := uint8(rand.Intn(10) + 1)
1885
1886 o.MibDataSync += r
1887
1888 // Since MibDataSync is a uint8, summing to it will never
1889 // result in a value higher than 255, but could be 0
1890 if o.MibDataSync == 0 {
1891 o.MibDataSync++
1892 }
1893}