blob: 5e74f46a8bce07d77b8698734ae4290d1f85e5e1 [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"
Matteo Scandolo618a6582020-09-09 12:21:29 -070021 "encoding/hex"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070022 "fmt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080023 pb "github.com/opencord/bbsim/api/bbsim"
24 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Himani Chawla13b1ee02021-03-15 01:43:53 +053025 "sync"
26
Matteo Scandolo4a036262020-08-17 15:56:13 -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"
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080030 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
31 me "github.com/opencord/omci-lib-go/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010032 "net"
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080033 "strconv"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010034 "time"
35
Matteo Scandolo3bc73742019-08-20 14:04:04 -070036 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070037 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070038 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070039 "github.com/opencord/bbsim/internal/common"
40 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolof9d43412021-01-12 11:11:34 -080041 "github.com/opencord/omci-lib-go"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070042 "github.com/opencord/voltha-protos/v4/go/openolt"
43 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070044 log "github.com/sirupsen/logrus"
45)
46
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070047var onuLogger = log.WithFields(log.Fields{
48 "module": "ONU",
49})
50
Matteo Scandolocedde462021-03-09 17:37:16 -080051const (
52 // ONU transitions
53 OnuTxInitialize = "initialize"
54 OnuTxDiscover = "discover"
55 OnuTxEnable = "enable"
56 OnuTxDisable = "disable"
57 OnuTxPonDisable = "pon_disable"
58 OnuTxStartImageDownload = "start_image_download"
59 OnuTxProgressImageDownload = "progress_image_download"
60 OnuTxCompleteImageDownload = "complete_image_download"
61 OnuTxFailImageDownload = "fail_image_download"
62 OnuTxActivateImage = "activate_image"
63 OnuTxCommitImage = "commit_image"
64
65 // ONU States
66 OnuStateCreated = "created"
67 OnuStateInitialized = "initialized"
68 OnuStateDiscovered = "discovered"
69 OnuStateEnabled = "enabled"
70 OnuStateDisabled = "disabled"
71 OnuStatePonDisabled = "pon_disabled"
72 OnuStateImageDownloadStarted = "image_download_started"
73 OnuStateImageDownloadInProgress = "image_download_in_progress"
74 OnuStateImageDownloadComplete = "image_download_completed"
75 OnuStateImageDownloadError = "image_download_error"
76 OnuStateImageActivated = "software_image_activated"
77 OnuStateImageCommitted = "software_image_committed"
78
79 // BBR ONU States and Transitions
80 BbrOnuTxSendEapolFlow = "send_eapol_flow"
81 BbrOnuStateEapolFlowSent = "eapol_flow_sent"
82 BbrOnuTxSendDhcpFlow = "send_dhcp_flow"
83 BbrOnuStateDhcpFlowSent = "dhcp_flow_sent"
84)
85
Pragya Arya8bdb4532020-03-02 17:08:09 +053086type FlowKey struct {
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070087 ID uint64
Pragya Arya8bdb4532020-03-02 17:08:09 +053088 Direction string
89}
90
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070091type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080092 ID uint32
93 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070094 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -080095 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053096 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
97 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -070098
99 Services []ServiceIf
100
101 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800102 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -0700103 // PortNo comes with flows and it's used when sending packetIndications,
104 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700105 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800106 PortNo uint32
107 Flows []FlowKey
108 FlowIds []uint64 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -0700109
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700110 OperState *fsm.FSM
111 SerialNumber *openolt.SerialNumber
112
Matteo Scandolof9d43412021-01-12 11:11:34 -0800113 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700114
115 // OMCI params
Matteo Scandolocedde462021-03-09 17:37:16 -0800116 MibDataSync uint8
117 ImageSoftwareExpectedSections int
118 ImageSoftwareReceivedSections int
119 ActiveImageEntityId uint16
120 CommittedImageEntityId uint16
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800121
122 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700123 tid uint16
124 hpTid uint16
125 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700126
Anand S Katti09541352020-01-29 15:54:01 +0530127 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
128 TrafficSchedulers *tech_profile.TrafficSchedulers
Himani Chawla13b1ee02021-03-15 01:43:53 +0530129 onuAlarmsInfoLock sync.RWMutex
130 onuAlarmsInfo map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700131}
132
Matteo Scandolo99f18462019-10-28 14:14:28 -0700133func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700134 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700135}
136
Matteo Scandolo4a036262020-08-17 15:56:13 -0700137func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700138
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700139 o := Onu{
Matteo Scandolocedde462021-03-09 17:37:16 -0800140 ID: id,
141 PonPortID: pon.ID,
142 PonPort: pon,
143 PortNo: 0,
144 tid: 0x1,
145 hpTid: 0x8000,
146 seqNumber: 0,
147 DoneChannel: make(chan bool, 1),
148 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
149 Flows: []FlowKey{},
150 DiscoveryDelay: delay,
151 MibDataSync: 0,
152 ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
153 ImageSoftwareReceivedSections: 0,
154 ActiveImageEntityId: 0, // when we start the SoftwareImage with ID 0 is active and committed
155 CommittedImageEntityId: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700156 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800157 o.SerialNumber = NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700158 // NOTE this state machine is used to track the operational
159 // state as requested by VOLTHA
160 o.OperState = getOperStateFSM(func(e *fsm.Event) {
161 onuLogger.WithFields(log.Fields{
162 "ID": o.ID,
163 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
164 })
Himani Chawla13b1ee02021-03-15 01:43:53 +0530165 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700166 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
167 o.InternalState = fsm.NewFSM(
Matteo Scandolocedde462021-03-09 17:37:16 -0800168 OnuStateCreated,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700169 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700170 // DEVICE Lifecycle
Matteo Scandolocedde462021-03-09 17:37:16 -0800171 {Name: OnuTxInitialize, Src: []string{OnuStateCreated, OnuStateDisabled, OnuStatePonDisabled}, Dst: OnuStateInitialized},
172 {Name: OnuTxDiscover, Src: []string{OnuStateInitialized}, Dst: OnuStateDiscovered},
173 {Name: OnuTxEnable, Src: []string{OnuStateDiscovered, OnuStatePonDisabled}, Dst: OnuStateEnabled},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100174 // 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 -0800175 {Name: OnuTxDisable, Src: []string{OnuStateEnabled, OnuStatePonDisabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateDisabled},
Pragya Arya6a708d62020-01-01 17:17:20 +0530176 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolocedde462021-03-09 17:37:16 -0800177 {Name: OnuTxPonDisable, Src: []string{OnuStateEnabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStatePonDisabled},
178 // Software Image Download related states
179 {Name: OnuTxStartImageDownload, Src: []string{OnuStateEnabled, OnuStateImageDownloadComplete, OnuStateImageDownloadError}, Dst: OnuStateImageDownloadStarted},
180 {Name: OnuTxProgressImageDownload, Src: []string{OnuStateImageDownloadStarted}, Dst: OnuStateImageDownloadInProgress},
181 {Name: OnuTxCompleteImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadComplete},
182 {Name: OnuTxFailImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadError},
183 {Name: OnuTxActivateImage, Src: []string{OnuStateImageDownloadComplete}, Dst: OnuStateImageActivated},
184 {Name: OnuTxCommitImage, Src: []string{OnuStateEnabled}, Dst: OnuStateImageCommitted}, // the image is committed after a ONU reboot
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700185 // BBR States
186 // TODO add start OMCI state
Matteo Scandolocedde462021-03-09 17:37:16 -0800187 {Name: BbrOnuTxSendEapolFlow, Src: []string{OnuStateInitialized}, Dst: BbrOnuStateEapolFlowSent},
188 {Name: BbrOnuTxSendDhcpFlow, Src: []string{BbrOnuStateEapolFlowSent}, Dst: BbrOnuStateDhcpFlowSent},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700189 },
190 fsm.Callbacks{
191 "enter_state": func(e *fsm.Event) {
192 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700193 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100194 "enter_initialized": func(e *fsm.Event) {
195 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800196 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800197
Matteo Scandolocedde462021-03-09 17:37:16 -0800198 if err := o.OperState.Event(OnuTxEnable); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800199 onuLogger.WithFields(log.Fields{
200 "OnuId": o.ID,
201 "IntfId": o.PonPortID,
202 "OnuSn": o.Sn(),
203 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
204 }
205
Pragya Arya1cbefa42020-01-13 12:15:29 +0530206 if !isMock {
207 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700208 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530209 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100210 },
211 "enter_discovered": func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800212 msg := bbsim.Message{
213 Type: bbsim.OnuDiscIndication,
214 Data: bbsim.OnuDiscIndicationMessage{
215 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100216 },
217 }
218 o.Channel <- msg
219 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700220 "enter_enabled": func(event *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800221
222 if used, sn := o.PonPort.isOnuIdAllocated(o.ID); used {
223 onuLogger.WithFields(log.Fields{
224 "IntfId": o.PonPortID,
225 "OnuId": o.ID,
226 "SerialNumber": o.Sn(),
227 }).Errorf("received-omci-with-sn-%s", common.OnuSnToString(sn))
228 return
229 } else {
230 o.PonPort.storeOnuId(o.ID, o.SerialNumber)
231 }
232
Matteo Scandolof9d43412021-01-12 11:11:34 -0800233 msg := bbsim.Message{
234 Type: bbsim.OnuIndication,
235 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700236 OnuSN: o.SerialNumber,
237 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800238 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700239 },
240 }
241 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700242
243 // Once the ONU is enabled start listening for packets
244 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700245 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700246 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700247 },
248 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700249
250 // clean the ONU state
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700251 o.PortNo = 0
252 o.Flows = []FlowKey{}
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800253 o.PonPort.removeOnuId(o.ID)
254 o.PonPort.removeAllocId(o.SerialNumber)
255 o.PonPort.removeGemPortBySn(o.SerialNumber)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700256
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700257 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800258 if err := o.OperState.Event("disable"); err != nil {
259 onuLogger.WithFields(log.Fields{
260 "OnuId": o.ID,
261 "IntfId": o.PonPortID,
262 "OnuSn": o.Sn(),
263 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
264 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700265 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800266 msg := bbsim.Message{
267 Type: bbsim.OnuIndication,
268 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700269 OnuSN: o.SerialNumber,
270 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800271 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700272 },
273 }
274 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530275
276 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100277 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530278 if len(o.FlowIds) == 0 {
279 close(o.Channel)
280 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700281
282 for _, s := range o.Services {
283 s.Disable()
284 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530285 o.onuAlarmsInfoLock.Lock()
286 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
287 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700288 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700289 // BBR states
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700290 "enter_eapol_flow_sent": func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800291 msg := bbsim.Message{
292 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700293 }
294 o.Channel <- msg
295 },
296 "enter_dhcp_flow_sent": func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800297 msg := bbsim.Message{
298 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700299 }
300 o.Channel <- msg
301 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700302 },
303 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100304
Matteo Scandolo27428702019-10-11 16:21:16 -0700305 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700306}
307
William Kurkian0418bc82019-11-06 12:16:24 -0500308func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700309 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700310 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700311 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700312 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700313 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
314}
315
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100316// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000317func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700318 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100319 "onuID": o.ID,
320 "onuSN": o.Sn(),
321 "ponPort": o.PonPortID,
322 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700323
David Bainbridge103cf022019-12-16 20:11:35 +0000324loop:
325 for {
326 select {
327 case <-ctx.Done():
328 onuLogger.WithFields(log.Fields{
329 "onuID": o.ID,
330 "onuSN": o.Sn(),
331 }).Tracef("ONU message handling canceled via context")
332 break loop
333 case message, ok := <-o.Channel:
334 if !ok || ctx.Err() != nil {
335 onuLogger.WithFields(log.Fields{
336 "onuID": o.ID,
337 "onuSN": o.Sn(),
338 }).Tracef("ONU message handling canceled via channel close")
339 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700340 }
David Bainbridge103cf022019-12-16 20:11:35 +0000341 onuLogger.WithFields(log.Fields{
342 "onuID": o.ID,
343 "onuSN": o.Sn(),
344 "messageType": message.Type,
345 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700346
David Bainbridge103cf022019-12-16 20:11:35 +0000347 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800348 case bbsim.OnuDiscIndication:
349 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000350 // 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 +0530351 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000352 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800353 case bbsim.OnuIndication:
354 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000355 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800356 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800357 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800358 msg, _ := message.Data.(bbsim.OmciMessage)
359 o.handleOmciRequest(msg, stream)
360 case bbsim.UniStatusAlarm:
361 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530362 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
363 MeInstance: msg.EntityID,
364 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
365 }
366 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
367 o.onuAlarmsInfoLock.Lock()
368 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
369 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
370 if pkt != nil { //pkt will be nil if we are unable to create the alarm
371 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
372 onuLogger.WithFields(log.Fields{
373 "IntfId": o.PonPortID,
374 "SerialNumber": o.Sn(),
375 "omciPacket": pkt,
376 "adminState": msg.AdminState,
377 "entityID": msg.EntityID,
378 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
379 alarmInfo.SequenceNo--
380 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800381 onuLogger.WithFields(log.Fields{
382 "IntfId": o.PonPortID,
383 "SerialNumber": o.Sn(),
384 "omciPacket": pkt,
385 "adminState": msg.AdminState,
386 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530387 }).Trace("UNI-Link-alarm-sent")
388 if alarmBitMap == [28]byte{0} {
389 delete(o.onuAlarmsInfo, onuAlarmMapKey)
390 } else {
391 alarmInfo.AlarmBitMap = alarmBitMap
392 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
393 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800394 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530395 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800396 case bbsim.FlowAdd:
397 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700398 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800399 case bbsim.FlowRemoved:
400 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700401 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800402 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700403
Matteo Scandolof9d43412021-01-12 11:11:34 -0800404 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000405
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700406 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000407 "IntfId": msg.IntfId,
408 "OnuId": msg.OnuId,
409 "pktType": msg.Type,
410 }).Trace("Received OnuPacketOut Message")
411
Matteo Scandolo618a6582020-09-09 12:21:29 -0700412 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
413
414 service, err := o.findServiceByMacAddress(msg.MacAddress)
415 if err != nil {
416 onuLogger.WithFields(log.Fields{
417 "IntfId": msg.IntfId,
418 "OnuId": msg.OnuId,
419 "pktType": msg.Type,
420 "MacAddress": msg.MacAddress,
421 "Pkt": hex.EncodeToString(msg.Packet.Data()),
422 "OnuSn": o.Sn(),
423 }).Error("Cannot find Service associated with packet")
424 return
425 }
426 service.PacketCh <- msg
427 } else if msg.Type == packetHandlers.IGMP {
428 // if it's an IGMP packet we assume we have a single IGMP service
429 for _, s := range o.Services {
430 service := s.(*Service)
431
432 if service.NeedsIgmp {
433 service.PacketCh <- msg
434 }
435 }
David Bainbridge103cf022019-12-16 20:11:35 +0000436 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700437
Matteo Scandolof9d43412021-01-12 11:11:34 -0800438 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000439 // NOTE we only receive BBR packets here.
440 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
441 // 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 -0800442 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000443
444 log.WithFields(log.Fields{
445 "IntfId": msg.IntfId,
446 "OnuId": msg.OnuId,
447 "pktType": msg.Type,
448 }).Trace("Received OnuPacketIn Message")
449
450 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700451 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, msg.GemPortId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000452 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700453 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000454 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700455 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800456 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800457 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800458 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
459 o.handleOmciResponse(msg, client)
460 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000461 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800462 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000463 o.sendDhcpFlow(client)
464 default:
465 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700466 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700467 }
468 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100469 onuLogger.WithFields(log.Fields{
470 "onuID": o.ID,
471 "onuSN": o.Sn(),
472 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700473}
474
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800475func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700476 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700477 sn.VendorId = []byte("BBSM")
478 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700479 return sn
480}
481
Matteo Scandolof9d43412021-01-12 11:11:34 -0800482func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700483 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800484 IntfId: o.PonPortID,
485 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700486 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700487
Matteo Scandolo4747d292019-08-05 11:50:18 -0700488 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700489 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700490 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700491 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700492
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700493 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800494 "IntfId": o.PonPortID,
495 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700496 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700497 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800498 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800499
500 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
501 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800502 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800503 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800504 o.sendOnuDiscIndication(msg, stream)
505 }
506 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700507}
508
Matteo Scandolof9d43412021-01-12 11:11:34 -0800509func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800510 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
511 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700512
513 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700514 IntfId: o.PonPortID,
515 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700516 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700517 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700518 SerialNumber: o.SerialNumber,
519 }}
520 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800521 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700522 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700523 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700524 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700525 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800526 "IntfId": o.PonPortID,
527 "OnuId": o.ID,
528 "VolthaOnuId": msg.OnuID,
529 "OperState": msg.OperState.String(),
530 "AdminState": msg.OperState.String(),
531 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700532 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700533
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700534}
535
Matteo Scandolof9d43412021-01-12 11:11:34 -0800536func (o *Onu) HandleShutdownONU() error {
537
538 dyingGasp := pb.ONUAlarmRequest{
539 AlarmType: "DYING_GASP",
540 SerialNumber: o.Sn(),
541 Status: "on",
542 }
543
544 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
545 onuLogger.WithFields(log.Fields{
546 "OnuId": o.ID,
547 "IntfId": o.PonPortID,
548 "OnuSn": o.Sn(),
549 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
550 return err
551 }
552
553 losReq := pb.ONUAlarmRequest{
554 AlarmType: "ONU_ALARM_LOS",
555 SerialNumber: o.Sn(),
556 Status: "on",
557 }
558
559 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
560 onuLogger.WithFields(log.Fields{
561 "OnuId": o.ID,
562 "IntfId": o.PonPortID,
563 "OnuSn": o.Sn(),
564 }).Errorf("Cannot send LOS: %s", err.Error())
565
566 return err
567 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530568 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800569 // TODO if it's the last ONU on the PON, then send a PON LOS
570
Matteo Scandolocedde462021-03-09 17:37:16 -0800571 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800572 onuLogger.WithFields(log.Fields{
573 "OnuId": o.ID,
574 "IntfId": o.PonPortID,
575 "OnuSn": o.Sn(),
576 }).Errorf("Cannot shutdown ONU: %s", err.Error())
577 return err
578 }
579
580 return nil
581}
582
583func (o *Onu) HandlePowerOnONU() error {
584 intitalState := o.InternalState.Current()
585
586 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800587 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
588 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800589 onuLogger.WithFields(log.Fields{
590 "OnuId": o.ID,
591 "IntfId": o.PonPortID,
592 "OnuSn": o.Sn(),
593 }).Errorf("Cannot poweron ONU: %s", err.Error())
594 return err
595 }
596 }
597
598 // turn off the LOS Alarm
599 losReq := pb.ONUAlarmRequest{
600 AlarmType: "ONU_ALARM_LOS",
601 SerialNumber: o.Sn(),
602 Status: "off",
603 }
604
605 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
606 onuLogger.WithFields(log.Fields{
607 "OnuId": o.ID,
608 "IntfId": o.PonPortID,
609 "OnuSn": o.Sn(),
610 }).Errorf("Cannot send LOS: %s", err.Error())
611 return err
612 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530613 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800614
615 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800616 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800617 onuLogger.WithFields(log.Fields{
618 "OnuId": o.ID,
619 "IntfId": o.PonPortID,
620 "OnuSn": o.Sn(),
621 }).Errorf("Cannot poweron ONU: %s", err.Error())
622 return err
623 }
624
625 // move o directly to enable state only when its a powercycle case
626 // in case of first time o poweron o will be moved to enable on
627 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800628 if intitalState == OnuStateDisabled {
629 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800630 onuLogger.WithFields(log.Fields{
631 "OnuId": o.ID,
632 "IntfId": o.PonPortID,
633 "OnuSn": o.Sn(),
634 }).Errorf("Cannot enable ONU: %s", err.Error())
635 return err
636 }
637 }
638
639 return nil
640}
641
642func (o *Onu) SetAlarm(alarmType string, status string) error {
643 alarmReq := pb.ONUAlarmRequest{
644 AlarmType: alarmType,
645 SerialNumber: o.Sn(),
646 Status: status,
647 }
648
649 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
650 if err != nil {
651 return err
652 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530653 raiseAlarm := false
654 if alarmReq.Status == "on" {
655 raiseAlarm = true
656 }
657 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800658 return nil
659}
660
661func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530662 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700663 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530664 if err != nil {
665 log.Errorf("error in getting msgType %v", err)
666 return
667 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800668 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530669 o.seqNumber = 0
670 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800671 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530672 o.seqNumber++
673 if o.seqNumber > 290 {
674 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
675 }
676 }
677 }
678}
679
Scott Bakerb90c4312020-03-12 21:33:25 -0700680// Create a TestResponse packet and send it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800681func (o *Onu) sendTestResult(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolob5913142021-03-19 16:10:18 -0700682 resp, err := omcilib.BuildTestResult(msg.OmciPkt.Data())
Scott Bakerb90c4312020-03-12 21:33:25 -0700683 if err != nil {
684 return err
685 }
686
687 var omciInd openolt.OmciIndication
688 omciInd.IntfId = o.PonPortID
689 omciInd.OnuId = o.ID
690 omciInd.Pkt = resp
691
692 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
693 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Scott Bakerb90c4312020-03-12 21:33:25 -0700694 return err
695 }
696 onuLogger.WithFields(log.Fields{
697 "IntfId": o.PonPortID,
698 "SerialNumber": o.Sn(),
699 "omciPacket": omciInd.Pkt,
700 }).Tracef("Sent TestResult OMCI message")
701
702 return nil
703}
704
Matteo Scandolof9d43412021-01-12 11:11:34 -0800705// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
706// and generate the appropriate response to it
707func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
708
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700709 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700710 "omciMsgType": msg.OmciMsg.MessageType,
711 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
712 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700713 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700714 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800715 }).Trace("omci-message-decoded")
716
717 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800718 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700719 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800720 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800721 onuLogger.WithFields(log.Fields{
722 "IntfId": o.PonPortID,
723 "OnuId": o.ID,
724 "SerialNumber": o.Sn(),
725 }).Debug("received-mib-reset-request-resetting-mds")
Matteo Scandolob5913142021-03-19 16:10:18 -0700726 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800727 o.MibDataSync = 0
728 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800729 case omci.MibUploadRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700730 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800731 case omci.MibUploadNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700732 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800733 case omci.GetRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700734 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId, o.CommittedImageEntityId)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800735 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800736 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700737 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800738 switch msgObj.EntityClass {
739 case me.PhysicalPathTerminationPointEthernetUniClassID:
740 // if we're Setting a PPTP state
741 // we need to send the appropriate alarm
742
743 if msgObj.EntityInstance == 257 {
744 // for now we're only caring about the first UNI
745 // NOTE that the EntityID for the UNI port is for now hardcoded in
746 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
747 // are reported during the MIB Upload sequence
748 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530749 raiseOMCIAlarm := false
750 if adminState == 1 {
751 raiseOMCIAlarm = true
752 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800753 msg := bbsim.Message{
754 Type: bbsim.UniStatusAlarm,
755 Data: bbsim.UniStatusAlarmMessage{
Himani Chawla13b1ee02021-03-15 01:43:53 +0530756 OnuSN: o.SerialNumber,
757 OnuID: o.ID,
758 AdminState: adminState,
759 EntityID: msgObj.EntityInstance,
760 RaiseOMCIAlarm: raiseOMCIAlarm,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800761 },
762 }
763 o.Channel <- msg
764 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800765 case me.TContClassID:
766 allocId := msgObj.Attributes["AllocId"].(uint16)
767
768 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
769 // otherwise we are adding it
770 if allocId == 255 || allocId == 65535 {
771 onuLogger.WithFields(log.Fields{
772 "IntfId": o.PonPortID,
773 "OnuId": o.ID,
774 "TContId": msgObj.EntityInstance,
775 "AllocId": allocId,
776 "SerialNumber": o.Sn(),
777 }).Trace("freeing-alloc-id-via-omci")
778 o.PonPort.removeAllocId(o.SerialNumber)
779 } else {
780 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
781 onuLogger.WithFields(log.Fields{
782 "IntfId": o.PonPortID,
783 "OnuId": o.ID,
784 "AllocId": allocId,
785 "SerialNumber": o.Sn(),
786 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
787 success = false
788 } else {
789 onuLogger.WithFields(log.Fields{
790 "IntfId": o.PonPortID,
791 "OnuId": o.ID,
792 "TContId": msgObj.EntityInstance,
793 "AllocId": allocId,
794 "SerialNumber": o.Sn(),
795 }).Trace("storing-alloc-id-via-omci")
796 o.PonPort.storeAllocId(allocId, o.SerialNumber)
797 }
798 }
799
800 }
801
802 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700803 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800804 o.MibDataSync++
805 }
806 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700807 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800808 }
809 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800810 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
811 var used bool
812 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700813 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800814 if err == nil {
815 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
816 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
817 onuLogger.WithFields(log.Fields{
818 "IntfId": o.PonPortID,
819 "OnuId": o.ID,
820 "GemPortId": msgObj.EntityInstance,
821 "SerialNumber": o.Sn(),
822 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
823 } else {
824 onuLogger.WithFields(log.Fields{
825 "IntfId": o.PonPortID,
826 "OnuId": o.ID,
827 "GemPortId": msgObj.EntityInstance,
828 "SerialNumber": o.Sn(),
829 }).Trace("storing-gem-port-id-via-omci")
830 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
831 }
832 }
833 }
834
835 // if the gemPort is valid then increment the MDS and return a successful response
836 // otherwise fail the request
837 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
838 // validation this check will need to be rewritten
839 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700840 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800841 o.MibDataSync++
842 }
843 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700844 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800845 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800846 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700847 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800848 if err == nil {
849 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
850 onuLogger.WithFields(log.Fields{
851 "IntfId": o.PonPortID,
852 "OnuId": o.ID,
853 "GemPortId": msgObj.EntityInstance,
854 "SerialNumber": o.Sn(),
855 }).Trace("freeing-gem-port-id-via-omci")
856 o.PonPort.removeGemPort(msgObj.EntityInstance)
857 }
858 }
859
Matteo Scandolob5913142021-03-19 16:10:18 -0700860 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800861 o.MibDataSync++
862 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800863 case omci.RebootRequestType:
864
Matteo Scandolob5913142021-03-19 16:10:18 -0700865 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800866
867 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800868 // we run this in a separate goroutine so that
869 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800870 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800871 if err := o.Reboot(10 * time.Second); err != nil {
872 log.WithFields(log.Fields{
873 "IntfId": o.PonPortID,
874 "OnuId": o.ID,
875 "SerialNumber": o.Sn(),
876 "err": err,
877 }).Error("cannot-reboot-onu-after-omci-reboot-request")
878 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800879 }()
880 case omci.TestRequestType:
881
882 // Test message is special, it requires sending two packets:
883 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
884 // second packet, TestResult, reports the result of running the self-test
885 // TestResult can come some time after a TestResponse
886 // TODO: Implement some delay between the TestResponse and the TestResult
Matteo Scandolob5913142021-03-19 16:10:18 -0700887 isTest, err := omcilib.IsTestRequest(msg.OmciPkt.Data())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800888 if (err == nil) && (isTest) {
889 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
890 onuLogger.WithFields(log.Fields{
891 "IntfId": o.PonPortID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800892 "OnuId": o.ID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800893 "SerialNumber": o.Sn(),
Matteo Scandolob5913142021-03-19 16:10:18 -0700894 "omciPacket": msg.OmciPkt.Data(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800895 "msg": msg,
896 "err": sendErr,
897 }).Error("send-TestResult-indication-failed")
898 }
899 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800900 case omci.SynchronizeTimeRequestType:
901 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700902 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800903 case omci.StartSoftwareDownloadRequestType:
904
905 o.ImageSoftwareReceivedSections = 0
906
Matteo Scandolob5913142021-03-19 16:10:18 -0700907 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800908
Matteo Scandolob5913142021-03-19 16:10:18 -0700909 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800910 o.MibDataSync++
911 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
912 onuLogger.WithFields(log.Fields{
913 "OnuId": o.ID,
914 "IntfId": o.PonPortID,
915 "OnuSn": o.Sn(),
916 "Err": err.Error(),
917 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
918 }
919 } else {
920 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700921 "OmciMsgType": msg.OmciMsg.MessageType,
922 "TransCorrId": msg.OmciMsg.TransactionID,
923 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800924 "IntfId": o.PonPortID,
925 "SerialNumber": o.Sn(),
926 }).Error("error-while-processing-start-software-download-request")
927 }
928 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700929 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800930 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700931 "OmciMsgType": msg.OmciMsg.MessageType,
932 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800933 "EntityInstance": msgObj.EntityInstance,
934 "SectionNumber": msgObj.SectionNumber,
935 "SectionData": msgObj.SectionData,
936 }).Trace("received-download-section-request")
937 o.ImageSoftwareReceivedSections++
938 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
939 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
940 onuLogger.WithFields(log.Fields{
941 "OnuId": o.ID,
942 "IntfId": o.PonPortID,
943 "OnuSn": o.Sn(),
944 "Err": err.Error(),
945 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
946 }
947 }
948 }
949 case omci.DownloadSectionRequestWithResponseType:
950 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -0700951 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
952 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800953 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700954 "OmciMsgType": msg.OmciMsg.MessageType,
955 "TransCorrId": msg.OmciMsg.TransactionID,
956 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800957 "IntfId": o.PonPortID,
958 "SerialNumber": o.Sn(),
959 }).Error("error-while-processing-create-download-section-response")
960 return
961 }
962 o.ImageSoftwareReceivedSections++
963
964 case omci.EndSoftwareDownloadRequestType:
965
966 // In the startSoftwareDownload we get the image size and the window size.
967 // We calculate how many DownloadSection we should receive and validate
968 // that we got the correct amount when we receive this message
969 success := true
970 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
971 onuLogger.WithFields(log.Fields{
972 "OnuId": o.ID,
973 "IntfId": o.PonPortID,
974 "OnuSn": o.Sn(),
975 "ExpectedSections": o.ImageSoftwareExpectedSections,
976 "ReceivedSections": o.ImageSoftwareReceivedSections,
977 }).Errorf("onu-did-not-receive-all-image-sections")
978 success = false
979 }
980
981 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700982 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800983 o.MibDataSync++
984 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
985 onuLogger.WithFields(log.Fields{
986 "OnuId": o.ID,
987 "IntfId": o.PonPortID,
988 "OnuSn": o.Sn(),
989 "Err": err.Error(),
990 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
991 }
992 } else {
993 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700994 "OmciMsgType": msg.OmciMsg.MessageType,
995 "TransCorrId": msg.OmciMsg.TransactionID,
996 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800997 "IntfId": o.PonPortID,
998 "SerialNumber": o.Sn(),
999 }).Error("error-while-processing-end-software-download-request")
1000 }
1001 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001002 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001003 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1004 onuLogger.WithFields(log.Fields{
1005 "OnuId": o.ID,
1006 "IntfId": o.PonPortID,
1007 "OnuSn": o.Sn(),
1008 "Err": err.Error(),
1009 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1010 }
1011 }
1012 }
1013
1014 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001015 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001016 o.MibDataSync++
1017 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1018 onuLogger.WithFields(log.Fields{
1019 "OnuId": o.ID,
1020 "IntfId": o.PonPortID,
1021 "OnuSn": o.Sn(),
1022 "Err": err.Error(),
1023 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1024 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001025 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001026 o.ActiveImageEntityId = msgObj.EntityInstance
1027 } else {
1028 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1029 }
1030 onuLogger.WithFields(log.Fields{
1031 "OnuId": o.ID,
1032 "IntfId": o.PonPortID,
1033 "OnuSn": o.Sn(),
1034 "ActiveImageEntityId": o.ActiveImageEntityId,
1035 "CommittedImageEntityId": o.CommittedImageEntityId,
1036 }).Info("onu-software-image-activated")
1037
1038 // powercycle the ONU
1039 // we run this in a separate goroutine so that
1040 // the ActivateSoftwareResponse is sent to VOLTHA
1041 // NOTE do we need to wait before rebooting?
1042 go func() {
1043 if err := o.Reboot(10 * time.Second); err != nil {
1044 log.WithFields(log.Fields{
1045 "IntfId": o.PonPortID,
1046 "OnuId": o.ID,
1047 "SerialNumber": o.Sn(),
1048 "err": err,
1049 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1050 }
1051 }()
1052 }
1053 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001054 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001055 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001056 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001057 // TODO validate that the image to commit is:
1058 // - active
1059 // - not already committed
1060 o.CommittedImageEntityId = msgObj.EntityInstance
1061 } else {
1062 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1063 }
1064 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1065 onuLogger.WithFields(log.Fields{
1066 "OnuId": o.ID,
1067 "IntfId": o.PonPortID,
1068 "OnuSn": o.Sn(),
1069 "Err": err.Error(),
1070 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1071 }
1072 onuLogger.WithFields(log.Fields{
1073 "OnuId": o.ID,
1074 "IntfId": o.PonPortID,
1075 "OnuSn": o.Sn(),
1076 "ActiveImageEntityId": o.ActiveImageEntityId,
1077 "CommittedImageEntityId": o.CommittedImageEntityId,
1078 }).Info("onu-software-image-committed")
1079 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301080 case omci.GetAllAlarmsRequestType:
1081 // Reset the alarm sequence number on receiving get all alarms request.
1082 o.onuAlarmsInfoLock.Lock()
1083 for key, alarmInfo := range o.onuAlarmsInfo {
1084 // reset the alarm sequence no
1085 alarmInfo.SequenceNo = 0
1086 o.onuAlarmsInfo[key] = alarmInfo
1087 }
1088 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001089 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301090 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001091 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301092 responsePkt = nil //Do not send any response for error case
1093 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001094 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001095 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001096 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1097 "omciPkt": msg.OmciPkt,
1098 "omciMsgType": msg.OmciMsg.MessageType,
1099 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001100 "IntfId": o.PonPortID,
1101 "SerialNumber": o.Sn(),
1102 }).Warnf("OMCI-message-not-supported")
1103 }
1104
1105 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001106 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001107 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001108 "IntfId": o.PonPortID,
1109 "SerialNumber": o.Sn(),
1110 "omciPacket": responsePkt,
1111 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1112 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001113 }).Errorf("failed-to-send-omci-message: %v", err)
1114 }
1115 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001116
Pragya Arya324337e2020-02-20 14:35:08 +05301117 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -08001118}
Pragya Arya324337e2020-02-20 14:35:08 +05301119
Matteo Scandolof9d43412021-01-12 11:11:34 -08001120// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1121func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1122 indication := &openolt.Indication_OmciInd{
1123 OmciInd: &openolt.OmciIndication{
1124 IntfId: o.PonPortID,
1125 OnuId: o.ID,
1126 Pkt: responsePkt,
1127 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001128 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001129 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1130 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001131 }
1132 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001133 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001134 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001135 "omciPacket": indication.OmciInd.Pkt,
1136 "transCorrId": txId,
1137 }).Trace("omci-message-sent")
1138 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001139}
1140
Matteo Scandolo27428702019-10-11 16:21:16 -07001141func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001142 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001143 // we need to add support for multiple UNIs
1144 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001145 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001146 // - change the library so that it reports a single UNI and remove this workaroung
1147 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001148 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001149 onuLogger.WithFields(log.Fields{
1150 "IntfId": o.PonPortID,
1151 "OnuId": o.ID,
1152 "SerialNumber": o.Sn(),
1153 "OnuPortNo": o.PortNo,
1154 "FlowPortNo": portNo,
1155 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001156 o.PortNo = portNo
1157 }
1158}
1159
William Kurkian0418bc82019-11-06 12:16:24 -05001160func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001161 onuLogger.WithFields(log.Fields{
1162 "IntfId": o.PonPortID,
1163 "OnuId": id,
1164 "SerialNumber": o.Sn(),
1165 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001166 o.ID = id
1167}
1168
Matteo Scandolof9d43412021-01-12 11:11:34 -08001169func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001170 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001171 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001172 "Cookie": msg.Flow.Cookie,
1173 "DstPort": msg.Flow.Classifier.DstPort,
1174 "FlowId": msg.Flow.FlowId,
1175 "FlowType": msg.Flow.FlowType,
1176 "GemportId": msg.Flow.GemportId,
1177 "InnerVlan": msg.Flow.Classifier.IVid,
1178 "IntfId": msg.Flow.AccessIntfId,
1179 "IpProto": msg.Flow.Classifier.IpProto,
1180 "OnuId": msg.Flow.OnuId,
1181 "OnuSn": o.Sn(),
1182 "OuterVlan": msg.Flow.Classifier.OVid,
1183 "PortNo": msg.Flow.PortNo,
1184 "SrcPort": msg.Flow.Classifier.SrcPort,
1185 "UniID": msg.Flow.UniId,
1186 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1187 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1188 "ClassifierIVid": msg.Flow.Classifier.IVid,
1189 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001190 "ReplicateFlow": msg.Flow.ReplicateFlow,
1191 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001192 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001193
Matteo Scandolo813402b2019-10-23 19:24:52 -07001194 if msg.Flow.UniId != 0 {
1195 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1196 onuLogger.WithFields(log.Fields{
1197 "IntfId": o.PonPortID,
1198 "OnuId": o.ID,
1199 "SerialNumber": o.Sn(),
1200 }).Debug("Ignoring flow as it's not for the first UNI")
1201 return
1202 }
1203
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001204 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001205
1206 var gemPortId uint32
1207 if msg.Flow.ReplicateFlow {
1208 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1209 // first available gemport (we only need to send one packet)
1210 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1211 gemPortId = msg.Flow.PbitToGemport[0]
1212 } else {
1213 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1214 gemPortId = uint32(msg.Flow.GemportId)
1215 }
1216 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001217
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001218 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001219 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001220 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001221
1222 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001223 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001224 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001225 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1226 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001227 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001228
Matteo Scandolo4a036262020-08-17 15:56:13 -07001229 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001230 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001231 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001232 }
1233}
1234
Matteo Scandolof9d43412021-01-12 11:11:34 -08001235func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001236 onuLogger.WithFields(log.Fields{
1237 "IntfId": o.PonPortID,
1238 "OnuId": o.ID,
1239 "SerialNumber": o.Sn(),
1240 "FlowId": msg.Flow.FlowId,
1241 "FlowType": msg.Flow.FlowType,
1242 }).Debug("ONU receives FlowRemove")
1243
1244 for idx, flow := range o.FlowIds {
1245 // If the gemport is found, delete it from local cache.
1246 if flow == msg.Flow.FlowId {
1247 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1248 break
1249 }
1250 }
1251
1252 if len(o.FlowIds) == 0 {
1253 onuLogger.WithFields(log.Fields{
1254 "IntfId": o.PonPortID,
1255 "OnuId": o.ID,
1256 "SerialNumber": o.Sn(),
1257 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001258
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301259 // check if ONU delete is performed and
1260 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001261 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301262 close(o.Channel)
1263 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001264 }
1265}
1266
Matteo Scandolocedde462021-03-09 17:37:16 -08001267func (o *Onu) Reboot(timeout time.Duration) error {
1268 onuLogger.WithFields(log.Fields{
1269 "IntfId": o.PonPortID,
1270 "OnuId": o.ID,
1271 "SerialNumber": o.Sn(),
1272 }).Debug("shutting-down-onu")
1273 if err := o.HandleShutdownONU(); err != nil {
1274 return err
1275 }
1276 time.Sleep(timeout)
1277 onuLogger.WithFields(log.Fields{
1278 "IntfId": o.PonPortID,
1279 "OnuId": o.ID,
1280 "SerialNumber": o.Sn(),
1281 }).Debug("power-on-onu")
1282 if err := o.HandlePowerOnONU(); err != nil {
1283 return err
1284 }
1285 return nil
1286}
1287
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001288// BBR methods
1289
1290func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1291 omciMsg := openolt.OmciMsg{
1292 IntfId: intfId,
1293 OnuId: onuId,
1294 Pkt: pktBytes,
1295 }
1296
1297 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1298 log.WithFields(log.Fields{
1299 "IntfId": intfId,
1300 "OnuId": onuId,
1301 "SerialNumber": common.OnuSnToString(sn),
1302 "Pkt": omciMsg.Pkt,
1303 }).Fatalf("Failed to send MIB Reset")
1304 }
1305 log.WithFields(log.Fields{
1306 "IntfId": intfId,
1307 "OnuId": onuId,
1308 "SerialNumber": common.OnuSnToString(sn),
1309 "Pkt": omciMsg.Pkt,
1310 }).Tracef("Sent OMCI message %s", msgType)
1311}
1312
1313func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1314 var next uint16
1315 if len(highPriority) > 0 && highPriority[0] {
1316 next = onu.hpTid
1317 onu.hpTid += 1
1318 if onu.hpTid < 0x8000 {
1319 onu.hpTid = 0x8000
1320 }
1321 } else {
1322 next = onu.tid
1323 onu.tid += 1
1324 if onu.tid >= 0x8000 {
1325 onu.tid = 1
1326 }
1327 }
1328 return next
1329}
1330
1331// TODO move this method in responders/omcisim
1332func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1333 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1334 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1335}
1336
Matteo Scandolof9d43412021-01-12 11:11:34 -08001337// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1338func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1339
1340 // we need to encode the packet in HEX
1341 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1342 hex.Encode(pkt, msg.OmciInd.Pkt)
1343 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1344 if err != nil {
1345 log.WithFields(log.Fields{
1346 "IntfId": o.PonPortID,
1347 "SerialNumber": o.Sn(),
1348 "omciPacket": msg.OmciInd.Pkt,
1349 }).Error("BBR Cannot parse OMCI packet")
1350 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001351
1352 log.WithFields(log.Fields{
1353 "IntfId": msg.OmciInd.IntfId,
1354 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001355 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001356 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001357 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301358 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001359 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001360 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001361 log.WithFields(log.Fields{
1362 "IntfId": msg.OmciInd.IntfId,
1363 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001364 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001365 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001366 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001367 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001368 case omci.MibResetResponseType:
1369 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1370 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1371 case omci.MibUploadResponseType:
1372 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1373 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1374 case omci.MibUploadNextResponseType:
1375 o.seqNumber++
1376
1377 if o.seqNumber > 290 {
1378 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001379 // start sending the flows, we don't care about the OMCI setup in BBR, just that a lot of messages can go through
Matteo Scandolocedde462021-03-09 17:37:16 -08001380 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001381 onuLogger.WithFields(log.Fields{
1382 "OnuId": o.ID,
1383 "IntfId": o.PonPortID,
1384 "OnuSn": o.Sn(),
1385 }).Errorf("Error while transitioning ONU State %v", err)
1386 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001387 } else {
1388 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1389 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001390 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001391 }
1392}
1393
1394func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1395
1396 classifierProto := openolt.Classifier{
1397 EthType: uint32(layers.EthernetTypeEAPOL),
1398 OVid: 4091,
1399 }
1400
1401 actionProto := openolt.Action{}
1402
1403 downstreamFlow := openolt.Flow{
1404 AccessIntfId: int32(o.PonPortID),
1405 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001406 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001407 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001408 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001409 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001410 Classifier: &classifierProto,
1411 Action: &actionProto,
1412 Priority: int32(100),
1413 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001414 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1415 // AllocId and GemPorts need to be unique per PON
1416 // for now use the ONU-ID, will need to change once we support multiple UNIs
1417 AllocId: int32(o.ID),
1418 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001419 }
1420
1421 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1422 log.WithFields(log.Fields{
1423 "IntfId": o.PonPortID,
1424 "OnuId": o.ID,
1425 "FlowId": downstreamFlow.FlowId,
1426 "PortNo": downstreamFlow.PortNo,
1427 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001428 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001429 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001430 }
1431 log.WithFields(log.Fields{
1432 "IntfId": o.PonPortID,
1433 "OnuId": o.ID,
1434 "FlowId": downstreamFlow.FlowId,
1435 "PortNo": downstreamFlow.PortNo,
1436 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1437 }).Info("Sent EAPOL Flow")
1438}
1439
1440func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001441
1442 // BBR only works with a single service (ATT HSIA)
1443 hsia := o.Services[0].(*Service)
1444
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001445 classifierProto := openolt.Classifier{
1446 EthType: uint32(layers.EthernetTypeIPv4),
1447 SrcPort: uint32(68),
1448 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001449 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001450 }
1451
1452 actionProto := openolt.Action{}
1453
1454 downstreamFlow := openolt.Flow{
1455 AccessIntfId: int32(o.PonPortID),
1456 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001457 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001458 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001459 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001460 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001461 Classifier: &classifierProto,
1462 Action: &actionProto,
1463 Priority: int32(100),
1464 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001465 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1466 // AllocId and GemPorts need to be unique per PON
1467 // for now use the ONU-ID, will need to change once we support multiple UNIs
1468 AllocId: int32(o.ID),
1469 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001470 }
1471
1472 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1473 log.WithFields(log.Fields{
1474 "IntfId": o.PonPortID,
1475 "OnuId": o.ID,
1476 "FlowId": downstreamFlow.FlowId,
1477 "PortNo": downstreamFlow.PortNo,
1478 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001479 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001480 }).Fatalf("Failed to send DHCP Flow")
1481 }
1482 log.WithFields(log.Fields{
1483 "IntfId": o.PonPortID,
1484 "OnuId": o.ID,
1485 "FlowId": downstreamFlow.FlowId,
1486 "PortNo": downstreamFlow.PortNo,
1487 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1488 }).Info("Sent DHCP Flow")
1489}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301490
1491// DeleteFlow method search and delete flowKey from the onu flows slice
1492func (onu *Onu) DeleteFlow(key FlowKey) {
1493 for pos, flowKey := range onu.Flows {
1494 if flowKey == key {
1495 // delete the flowKey by shifting all flowKeys by one
1496 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1497 t := make([]FlowKey, len(onu.Flows))
1498 copy(t, onu.Flows)
1499 onu.Flows = t
1500 break
1501 }
1502 }
1503}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301504
1505func (onu *Onu) ReDiscoverOnu() {
1506 // Wait for few seconds to be sure of the cleanup
1507 time.Sleep(5 * time.Second)
1508
1509 onuLogger.WithFields(log.Fields{
1510 "IntfId": onu.PonPortID,
1511 "OnuId": onu.ID,
1512 "OnuSn": onu.Sn(),
1513 }).Debug("Send ONU Re-Discovery")
1514
1515 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001516 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301517 log.WithFields(log.Fields{
1518 "IntfId": onu.PonPortID,
1519 "OnuSn": onu.Sn(),
1520 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001521 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301522 }
1523
Matteo Scandolocedde462021-03-09 17:37:16 -08001524 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301525 log.WithFields(log.Fields{
1526 "IntfId": onu.PonPortID,
1527 "OnuSn": onu.Sn(),
1528 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001529 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301530 }
1531}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001532
1533func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1534 for _, s := range onu.Services {
1535 if service, ok := s.(*Service); ok {
1536 // EAPOL is a strange case, as packets are untagged
1537 // but we assume we will have a single service requiring EAPOL
1538 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1539 service.GemPort = gemport
1540 }
1541
1542 // For DHCP services we single tag the outgoing packets,
1543 // thus the flow only contains the CTag and we can use that to match the service
1544 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1545 service.GemPort = gemport
1546 }
1547
1548 // for dataplane services match both C and S tags
1549 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1550 service.GemPort = gemport
1551 }
1552 }
1553 }
1554}
1555
1556func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1557 for _, s := range onu.Services {
1558 service := s.(*Service)
1559 if service.HwAddress.String() == macAddress.String() {
1560 return service, nil
1561 }
1562 }
1563 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1564}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301565
1566func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1567 switch alarmType {
1568 case "ONU_ALARM_LOS":
1569 msg := bbsim.Message{
1570 Type: bbsim.UniStatusAlarm,
1571 Data: bbsim.UniStatusAlarmMessage{
1572 OnuSN: o.SerialNumber,
1573 OnuID: o.ID,
1574 EntityID: 257,
1575 RaiseOMCIAlarm: raiseOMCIAlarm,
1576 },
1577 }
1578 o.Channel <- msg
1579 }
1580
1581}
1582
1583func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1584 o.onuAlarmsInfoLock.Lock()
1585 defer o.onuAlarmsInfoLock.Unlock()
1586 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1587 if alarmInfo.SequenceNo == 255 {
1588 alarmInfo.SequenceNo = 1
1589 } else {
1590 alarmInfo.SequenceNo++
1591 }
1592 o.onuAlarmsInfo[key] = alarmInfo
1593 return alarmInfo.SequenceNo
1594 } else {
1595 // This is the first time alarm notification message is being sent
1596 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1597 SequenceNo: 1,
1598 }
1599 return 1
1600 }
1601}