blob: 1ee67dd85daf393cdb54af1ffa96fe4750f64018 [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 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700194 fmt.Sprintf("enter_%s", OnuStateInitialized): func(e *fsm.Event) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100195 // 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 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700211 fmt.Sprintf("enter_%s", OnuStateDiscovered): 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 Scandolod15c0b42021-03-22 11:38:13 -0700220 fmt.Sprintf("enter_%s", OnuStateEnabled): 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(),
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700227 }).Errorf("onu-id-duplicated-with-%s", common.OnuSnToString(sn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800228 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 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700248 fmt.Sprintf("enter_%s", OnuStateDisabled): func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700249
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700250 o.cleanupOnuState()
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700251
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700252 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800253 if err := o.OperState.Event("disable"); err != nil {
254 onuLogger.WithFields(log.Fields{
255 "OnuId": o.ID,
256 "IntfId": o.PonPortID,
257 "OnuSn": o.Sn(),
258 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
259 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700260 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800261 msg := bbsim.Message{
262 Type: bbsim.OnuIndication,
263 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700264 OnuSN: o.SerialNumber,
265 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800266 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700267 },
268 }
269 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530270
271 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100272 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530273 if len(o.FlowIds) == 0 {
274 close(o.Channel)
275 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700276
277 for _, s := range o.Services {
278 s.Disable()
279 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700280
281 },
282 fmt.Sprintf("enter_%s", OnuStatePonDisabled): func(event *fsm.Event) {
283 o.cleanupOnuState()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700284 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700285 // BBR states
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700286 fmt.Sprintf("enter_%s", BbrOnuStateEapolFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800287 msg := bbsim.Message{
288 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700289 }
290 o.Channel <- msg
291 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700292 fmt.Sprintf("enter_%s", BbrOnuStateDhcpFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800293 msg := bbsim.Message{
294 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700295 }
296 o.Channel <- msg
297 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700298 },
299 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100300
Matteo Scandolo27428702019-10-11 16:21:16 -0700301 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700302}
303
William Kurkian0418bc82019-11-06 12:16:24 -0500304func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700305 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700306 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700307 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700308 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700309 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
310}
311
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700312// cleanupOnuState this method is to clean the local state when the ONU is disabled
313func (o *Onu) cleanupOnuState() {
314 // clean the ONU state
315 o.PortNo = 0
316 o.Flows = []FlowKey{}
317 o.PonPort.removeOnuId(o.ID)
318 o.PonPort.removeAllocId(o.SerialNumber)
319 o.PonPort.removeGemPortBySn(o.SerialNumber)
320
321 o.onuAlarmsInfoLock.Lock()
322 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
323 o.onuAlarmsInfoLock.Unlock()
324}
325
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100326// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000327func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700328 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100329 "onuID": o.ID,
330 "onuSN": o.Sn(),
331 "ponPort": o.PonPortID,
332 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700333
David Bainbridge103cf022019-12-16 20:11:35 +0000334loop:
335 for {
336 select {
337 case <-ctx.Done():
338 onuLogger.WithFields(log.Fields{
339 "onuID": o.ID,
340 "onuSN": o.Sn(),
341 }).Tracef("ONU message handling canceled via context")
342 break loop
343 case message, ok := <-o.Channel:
344 if !ok || ctx.Err() != nil {
345 onuLogger.WithFields(log.Fields{
346 "onuID": o.ID,
347 "onuSN": o.Sn(),
348 }).Tracef("ONU message handling canceled via channel close")
349 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700350 }
David Bainbridge103cf022019-12-16 20:11:35 +0000351 onuLogger.WithFields(log.Fields{
352 "onuID": o.ID,
353 "onuSN": o.Sn(),
354 "messageType": message.Type,
355 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700356
David Bainbridge103cf022019-12-16 20:11:35 +0000357 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800358 case bbsim.OnuDiscIndication:
359 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000360 // 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 +0530361 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000362 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800363 case bbsim.OnuIndication:
364 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000365 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800366 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800367 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800368 msg, _ := message.Data.(bbsim.OmciMessage)
369 o.handleOmciRequest(msg, stream)
370 case bbsim.UniStatusAlarm:
371 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530372 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
373 MeInstance: msg.EntityID,
374 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
375 }
376 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
377 o.onuAlarmsInfoLock.Lock()
378 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
379 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
380 if pkt != nil { //pkt will be nil if we are unable to create the alarm
381 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
382 onuLogger.WithFields(log.Fields{
383 "IntfId": o.PonPortID,
384 "SerialNumber": o.Sn(),
385 "omciPacket": pkt,
386 "adminState": msg.AdminState,
387 "entityID": msg.EntityID,
388 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
389 alarmInfo.SequenceNo--
390 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800391 onuLogger.WithFields(log.Fields{
392 "IntfId": o.PonPortID,
393 "SerialNumber": o.Sn(),
394 "omciPacket": pkt,
395 "adminState": msg.AdminState,
396 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530397 }).Trace("UNI-Link-alarm-sent")
398 if alarmBitMap == [28]byte{0} {
399 delete(o.onuAlarmsInfo, onuAlarmMapKey)
400 } else {
401 alarmInfo.AlarmBitMap = alarmBitMap
402 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
403 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800404 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530405 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800406 case bbsim.FlowAdd:
407 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700408 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800409 case bbsim.FlowRemoved:
410 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700411 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800412 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700413
Matteo Scandolof9d43412021-01-12 11:11:34 -0800414 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000415
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700416 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000417 "IntfId": msg.IntfId,
418 "OnuId": msg.OnuId,
419 "pktType": msg.Type,
420 }).Trace("Received OnuPacketOut Message")
421
Matteo Scandolo618a6582020-09-09 12:21:29 -0700422 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
423
424 service, err := o.findServiceByMacAddress(msg.MacAddress)
425 if err != nil {
426 onuLogger.WithFields(log.Fields{
427 "IntfId": msg.IntfId,
428 "OnuId": msg.OnuId,
429 "pktType": msg.Type,
430 "MacAddress": msg.MacAddress,
431 "Pkt": hex.EncodeToString(msg.Packet.Data()),
432 "OnuSn": o.Sn(),
433 }).Error("Cannot find Service associated with packet")
434 return
435 }
436 service.PacketCh <- msg
437 } else if msg.Type == packetHandlers.IGMP {
438 // if it's an IGMP packet we assume we have a single IGMP service
439 for _, s := range o.Services {
440 service := s.(*Service)
441
442 if service.NeedsIgmp {
443 service.PacketCh <- msg
444 }
445 }
David Bainbridge103cf022019-12-16 20:11:35 +0000446 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700447
Matteo Scandolof9d43412021-01-12 11:11:34 -0800448 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000449 // NOTE we only receive BBR packets here.
450 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
451 // 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 -0800452 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000453
454 log.WithFields(log.Fields{
455 "IntfId": msg.IntfId,
456 "OnuId": msg.OnuId,
457 "pktType": msg.Type,
458 }).Trace("Received OnuPacketIn Message")
459
460 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700461 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 +0000462 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700463 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000464 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700465 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800466 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800467 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800468 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
469 o.handleOmciResponse(msg, client)
470 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000471 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800472 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000473 o.sendDhcpFlow(client)
474 default:
475 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700476 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700477 }
478 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100479 onuLogger.WithFields(log.Fields{
480 "onuID": o.ID,
481 "onuSN": o.Sn(),
482 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700483}
484
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800485func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700486 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700487 sn.VendorId = []byte("BBSM")
488 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700489 return sn
490}
491
Matteo Scandolof9d43412021-01-12 11:11:34 -0800492func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700493 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800494 IntfId: o.PonPortID,
495 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700496 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700497
Matteo Scandolo4747d292019-08-05 11:50:18 -0700498 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700499 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700500 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700501 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700502
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700503 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800504 "IntfId": o.PonPortID,
505 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700506 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700507 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800508 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800509
510 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
511 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800512 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800513 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800514 o.sendOnuDiscIndication(msg, stream)
515 }
516 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700517}
518
Matteo Scandolof9d43412021-01-12 11:11:34 -0800519func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800520 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
521 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700522
523 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700524 IntfId: o.PonPortID,
525 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700526 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700527 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700528 SerialNumber: o.SerialNumber,
529 }}
530 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800531 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700532 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700533 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700534 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700535 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800536 "IntfId": o.PonPortID,
537 "OnuId": o.ID,
538 "VolthaOnuId": msg.OnuID,
539 "OperState": msg.OperState.String(),
540 "AdminState": msg.OperState.String(),
541 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700542 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700543
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700544}
545
Matteo Scandolof9d43412021-01-12 11:11:34 -0800546func (o *Onu) HandleShutdownONU() error {
547
548 dyingGasp := pb.ONUAlarmRequest{
549 AlarmType: "DYING_GASP",
550 SerialNumber: o.Sn(),
551 Status: "on",
552 }
553
554 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
555 onuLogger.WithFields(log.Fields{
556 "OnuId": o.ID,
557 "IntfId": o.PonPortID,
558 "OnuSn": o.Sn(),
559 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
560 return err
561 }
562
563 losReq := pb.ONUAlarmRequest{
564 AlarmType: "ONU_ALARM_LOS",
565 SerialNumber: o.Sn(),
566 Status: "on",
567 }
568
569 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
570 onuLogger.WithFields(log.Fields{
571 "OnuId": o.ID,
572 "IntfId": o.PonPortID,
573 "OnuSn": o.Sn(),
574 }).Errorf("Cannot send LOS: %s", err.Error())
575
576 return err
577 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530578 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800579 // TODO if it's the last ONU on the PON, then send a PON LOS
580
Matteo Scandolocedde462021-03-09 17:37:16 -0800581 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800582 onuLogger.WithFields(log.Fields{
583 "OnuId": o.ID,
584 "IntfId": o.PonPortID,
585 "OnuSn": o.Sn(),
586 }).Errorf("Cannot shutdown ONU: %s", err.Error())
587 return err
588 }
589
590 return nil
591}
592
593func (o *Onu) HandlePowerOnONU() error {
594 intitalState := o.InternalState.Current()
595
596 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800597 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
598 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800599 onuLogger.WithFields(log.Fields{
600 "OnuId": o.ID,
601 "IntfId": o.PonPortID,
602 "OnuSn": o.Sn(),
603 }).Errorf("Cannot poweron ONU: %s", err.Error())
604 return err
605 }
606 }
607
608 // turn off the LOS Alarm
609 losReq := pb.ONUAlarmRequest{
610 AlarmType: "ONU_ALARM_LOS",
611 SerialNumber: o.Sn(),
612 Status: "off",
613 }
614
615 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
616 onuLogger.WithFields(log.Fields{
617 "OnuId": o.ID,
618 "IntfId": o.PonPortID,
619 "OnuSn": o.Sn(),
620 }).Errorf("Cannot send LOS: %s", err.Error())
621 return err
622 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530623 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800624
625 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800626 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800627 onuLogger.WithFields(log.Fields{
628 "OnuId": o.ID,
629 "IntfId": o.PonPortID,
630 "OnuSn": o.Sn(),
631 }).Errorf("Cannot poweron ONU: %s", err.Error())
632 return err
633 }
634
635 // move o directly to enable state only when its a powercycle case
636 // in case of first time o poweron o will be moved to enable on
637 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800638 if intitalState == OnuStateDisabled {
639 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800640 onuLogger.WithFields(log.Fields{
641 "OnuId": o.ID,
642 "IntfId": o.PonPortID,
643 "OnuSn": o.Sn(),
644 }).Errorf("Cannot enable ONU: %s", err.Error())
645 return err
646 }
647 }
648
649 return nil
650}
651
652func (o *Onu) SetAlarm(alarmType string, status string) error {
653 alarmReq := pb.ONUAlarmRequest{
654 AlarmType: alarmType,
655 SerialNumber: o.Sn(),
656 Status: status,
657 }
658
659 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
660 if err != nil {
661 return err
662 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530663 raiseAlarm := false
664 if alarmReq.Status == "on" {
665 raiseAlarm = true
666 }
667 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800668 return nil
669}
670
671func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530672 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700673 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530674 if err != nil {
675 log.Errorf("error in getting msgType %v", err)
676 return
677 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800678 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530679 o.seqNumber = 0
680 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800681 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530682 o.seqNumber++
683 if o.seqNumber > 290 {
684 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
685 }
686 }
687 }
688}
689
Scott Bakerb90c4312020-03-12 21:33:25 -0700690// Create a TestResponse packet and send it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800691func (o *Onu) sendTestResult(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolob5913142021-03-19 16:10:18 -0700692 resp, err := omcilib.BuildTestResult(msg.OmciPkt.Data())
Scott Bakerb90c4312020-03-12 21:33:25 -0700693 if err != nil {
694 return err
695 }
696
697 var omciInd openolt.OmciIndication
698 omciInd.IntfId = o.PonPortID
699 omciInd.OnuId = o.ID
700 omciInd.Pkt = resp
701
702 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
703 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Scott Bakerb90c4312020-03-12 21:33:25 -0700704 return err
705 }
706 onuLogger.WithFields(log.Fields{
707 "IntfId": o.PonPortID,
708 "SerialNumber": o.Sn(),
709 "omciPacket": omciInd.Pkt,
710 }).Tracef("Sent TestResult OMCI message")
711
712 return nil
713}
714
Matteo Scandolof9d43412021-01-12 11:11:34 -0800715// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
716// and generate the appropriate response to it
717func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
718
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700719 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700720 "omciMsgType": msg.OmciMsg.MessageType,
721 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
722 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700723 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700724 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800725 }).Trace("omci-message-decoded")
726
727 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800728 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700729 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800730 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800731 onuLogger.WithFields(log.Fields{
732 "IntfId": o.PonPortID,
733 "OnuId": o.ID,
734 "SerialNumber": o.Sn(),
735 }).Debug("received-mib-reset-request-resetting-mds")
Matteo Scandolob5913142021-03-19 16:10:18 -0700736 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800737 o.MibDataSync = 0
738 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800739 case omci.MibUploadRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700740 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800741 case omci.MibUploadNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700742 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800743 case omci.GetRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700744 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId, o.CommittedImageEntityId)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800745 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800746 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700747 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800748 switch msgObj.EntityClass {
749 case me.PhysicalPathTerminationPointEthernetUniClassID:
750 // if we're Setting a PPTP state
751 // we need to send the appropriate alarm
752
753 if msgObj.EntityInstance == 257 {
754 // for now we're only caring about the first UNI
755 // NOTE that the EntityID for the UNI port is for now hardcoded in
756 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
757 // are reported during the MIB Upload sequence
758 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530759 raiseOMCIAlarm := false
760 if adminState == 1 {
761 raiseOMCIAlarm = true
762 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800763 msg := bbsim.Message{
764 Type: bbsim.UniStatusAlarm,
765 Data: bbsim.UniStatusAlarmMessage{
Himani Chawla13b1ee02021-03-15 01:43:53 +0530766 OnuSN: o.SerialNumber,
767 OnuID: o.ID,
768 AdminState: adminState,
769 EntityID: msgObj.EntityInstance,
770 RaiseOMCIAlarm: raiseOMCIAlarm,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800771 },
772 }
773 o.Channel <- msg
774 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800775 case me.TContClassID:
776 allocId := msgObj.Attributes["AllocId"].(uint16)
777
778 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
779 // otherwise we are adding it
780 if allocId == 255 || allocId == 65535 {
781 onuLogger.WithFields(log.Fields{
782 "IntfId": o.PonPortID,
783 "OnuId": o.ID,
784 "TContId": msgObj.EntityInstance,
785 "AllocId": allocId,
786 "SerialNumber": o.Sn(),
787 }).Trace("freeing-alloc-id-via-omci")
788 o.PonPort.removeAllocId(o.SerialNumber)
789 } else {
790 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
791 onuLogger.WithFields(log.Fields{
792 "IntfId": o.PonPortID,
793 "OnuId": o.ID,
794 "AllocId": allocId,
795 "SerialNumber": o.Sn(),
796 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
797 success = false
798 } else {
799 onuLogger.WithFields(log.Fields{
800 "IntfId": o.PonPortID,
801 "OnuId": o.ID,
802 "TContId": msgObj.EntityInstance,
803 "AllocId": allocId,
804 "SerialNumber": o.Sn(),
805 }).Trace("storing-alloc-id-via-omci")
806 o.PonPort.storeAllocId(allocId, o.SerialNumber)
807 }
808 }
809
810 }
811
812 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700813 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800814 o.MibDataSync++
815 }
816 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700817 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800818 }
819 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800820 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
821 var used bool
822 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700823 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800824 if err == nil {
825 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
826 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
827 onuLogger.WithFields(log.Fields{
828 "IntfId": o.PonPortID,
829 "OnuId": o.ID,
830 "GemPortId": msgObj.EntityInstance,
831 "SerialNumber": o.Sn(),
832 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
833 } else {
834 onuLogger.WithFields(log.Fields{
835 "IntfId": o.PonPortID,
836 "OnuId": o.ID,
837 "GemPortId": msgObj.EntityInstance,
838 "SerialNumber": o.Sn(),
839 }).Trace("storing-gem-port-id-via-omci")
840 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
841 }
842 }
843 }
844
845 // if the gemPort is valid then increment the MDS and return a successful response
846 // otherwise fail the request
847 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
848 // validation this check will need to be rewritten
849 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700850 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800851 o.MibDataSync++
852 }
853 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700854 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800855 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800856 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700857 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800858 if err == nil {
859 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
860 onuLogger.WithFields(log.Fields{
861 "IntfId": o.PonPortID,
862 "OnuId": o.ID,
863 "GemPortId": msgObj.EntityInstance,
864 "SerialNumber": o.Sn(),
865 }).Trace("freeing-gem-port-id-via-omci")
866 o.PonPort.removeGemPort(msgObj.EntityInstance)
867 }
868 }
869
Matteo Scandolob5913142021-03-19 16:10:18 -0700870 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800871 o.MibDataSync++
872 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800873 case omci.RebootRequestType:
874
Matteo Scandolob5913142021-03-19 16:10:18 -0700875 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800876
877 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800878 // we run this in a separate goroutine so that
879 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800880 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800881 if err := o.Reboot(10 * time.Second); err != nil {
882 log.WithFields(log.Fields{
883 "IntfId": o.PonPortID,
884 "OnuId": o.ID,
885 "SerialNumber": o.Sn(),
886 "err": err,
887 }).Error("cannot-reboot-onu-after-omci-reboot-request")
888 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800889 }()
890 case omci.TestRequestType:
891
892 // Test message is special, it requires sending two packets:
893 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
894 // second packet, TestResult, reports the result of running the self-test
895 // TestResult can come some time after a TestResponse
896 // TODO: Implement some delay between the TestResponse and the TestResult
Matteo Scandolob5913142021-03-19 16:10:18 -0700897 isTest, err := omcilib.IsTestRequest(msg.OmciPkt.Data())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800898 if (err == nil) && (isTest) {
899 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
900 onuLogger.WithFields(log.Fields{
901 "IntfId": o.PonPortID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800902 "OnuId": o.ID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800903 "SerialNumber": o.Sn(),
Matteo Scandolob5913142021-03-19 16:10:18 -0700904 "omciPacket": msg.OmciPkt.Data(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800905 "msg": msg,
906 "err": sendErr,
907 }).Error("send-TestResult-indication-failed")
908 }
909 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800910 case omci.SynchronizeTimeRequestType:
911 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700912 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800913 case omci.StartSoftwareDownloadRequestType:
914
915 o.ImageSoftwareReceivedSections = 0
916
Matteo Scandolob5913142021-03-19 16:10:18 -0700917 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800918
Matteo Scandolob5913142021-03-19 16:10:18 -0700919 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800920 o.MibDataSync++
921 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
922 onuLogger.WithFields(log.Fields{
923 "OnuId": o.ID,
924 "IntfId": o.PonPortID,
925 "OnuSn": o.Sn(),
926 "Err": err.Error(),
927 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
928 }
929 } else {
930 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700931 "OmciMsgType": msg.OmciMsg.MessageType,
932 "TransCorrId": msg.OmciMsg.TransactionID,
933 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800934 "IntfId": o.PonPortID,
935 "SerialNumber": o.Sn(),
936 }).Error("error-while-processing-start-software-download-request")
937 }
938 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700939 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800940 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700941 "OmciMsgType": msg.OmciMsg.MessageType,
942 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800943 "EntityInstance": msgObj.EntityInstance,
944 "SectionNumber": msgObj.SectionNumber,
945 "SectionData": msgObj.SectionData,
946 }).Trace("received-download-section-request")
947 o.ImageSoftwareReceivedSections++
948 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
949 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
950 onuLogger.WithFields(log.Fields{
951 "OnuId": o.ID,
952 "IntfId": o.PonPortID,
953 "OnuSn": o.Sn(),
954 "Err": err.Error(),
955 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
956 }
957 }
958 }
959 case omci.DownloadSectionRequestWithResponseType:
960 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -0700961 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
962 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800963 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700964 "OmciMsgType": msg.OmciMsg.MessageType,
965 "TransCorrId": msg.OmciMsg.TransactionID,
966 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800967 "IntfId": o.PonPortID,
968 "SerialNumber": o.Sn(),
969 }).Error("error-while-processing-create-download-section-response")
970 return
971 }
972 o.ImageSoftwareReceivedSections++
973
974 case omci.EndSoftwareDownloadRequestType:
975
976 // In the startSoftwareDownload we get the image size and the window size.
977 // We calculate how many DownloadSection we should receive and validate
978 // that we got the correct amount when we receive this message
979 success := true
980 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
981 onuLogger.WithFields(log.Fields{
982 "OnuId": o.ID,
983 "IntfId": o.PonPortID,
984 "OnuSn": o.Sn(),
985 "ExpectedSections": o.ImageSoftwareExpectedSections,
986 "ReceivedSections": o.ImageSoftwareReceivedSections,
987 }).Errorf("onu-did-not-receive-all-image-sections")
988 success = false
989 }
990
991 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700992 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800993 o.MibDataSync++
994 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
995 onuLogger.WithFields(log.Fields{
996 "OnuId": o.ID,
997 "IntfId": o.PonPortID,
998 "OnuSn": o.Sn(),
999 "Err": err.Error(),
1000 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1001 }
1002 } else {
1003 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001004 "OmciMsgType": msg.OmciMsg.MessageType,
1005 "TransCorrId": msg.OmciMsg.TransactionID,
1006 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001007 "IntfId": o.PonPortID,
1008 "SerialNumber": o.Sn(),
1009 }).Error("error-while-processing-end-software-download-request")
1010 }
1011 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001012 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001013 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1014 onuLogger.WithFields(log.Fields{
1015 "OnuId": o.ID,
1016 "IntfId": o.PonPortID,
1017 "OnuSn": o.Sn(),
1018 "Err": err.Error(),
1019 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1020 }
1021 }
1022 }
1023
1024 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001025 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001026 o.MibDataSync++
1027 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1028 onuLogger.WithFields(log.Fields{
1029 "OnuId": o.ID,
1030 "IntfId": o.PonPortID,
1031 "OnuSn": o.Sn(),
1032 "Err": err.Error(),
1033 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1034 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001035 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001036 o.ActiveImageEntityId = msgObj.EntityInstance
1037 } else {
1038 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1039 }
1040 onuLogger.WithFields(log.Fields{
1041 "OnuId": o.ID,
1042 "IntfId": o.PonPortID,
1043 "OnuSn": o.Sn(),
1044 "ActiveImageEntityId": o.ActiveImageEntityId,
1045 "CommittedImageEntityId": o.CommittedImageEntityId,
1046 }).Info("onu-software-image-activated")
1047
1048 // powercycle the ONU
1049 // we run this in a separate goroutine so that
1050 // the ActivateSoftwareResponse is sent to VOLTHA
1051 // NOTE do we need to wait before rebooting?
1052 go func() {
1053 if err := o.Reboot(10 * time.Second); err != nil {
1054 log.WithFields(log.Fields{
1055 "IntfId": o.PonPortID,
1056 "OnuId": o.ID,
1057 "SerialNumber": o.Sn(),
1058 "err": err,
1059 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1060 }
1061 }()
1062 }
1063 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001064 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001065 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001066 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001067 // TODO validate that the image to commit is:
1068 // - active
1069 // - not already committed
1070 o.CommittedImageEntityId = msgObj.EntityInstance
1071 } else {
1072 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1073 }
1074 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1075 onuLogger.WithFields(log.Fields{
1076 "OnuId": o.ID,
1077 "IntfId": o.PonPortID,
1078 "OnuSn": o.Sn(),
1079 "Err": err.Error(),
1080 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1081 }
1082 onuLogger.WithFields(log.Fields{
1083 "OnuId": o.ID,
1084 "IntfId": o.PonPortID,
1085 "OnuSn": o.Sn(),
1086 "ActiveImageEntityId": o.ActiveImageEntityId,
1087 "CommittedImageEntityId": o.CommittedImageEntityId,
1088 }).Info("onu-software-image-committed")
1089 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301090 case omci.GetAllAlarmsRequestType:
1091 // Reset the alarm sequence number on receiving get all alarms request.
1092 o.onuAlarmsInfoLock.Lock()
1093 for key, alarmInfo := range o.onuAlarmsInfo {
1094 // reset the alarm sequence no
1095 alarmInfo.SequenceNo = 0
1096 o.onuAlarmsInfo[key] = alarmInfo
1097 }
1098 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001099 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301100 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001101 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301102 responsePkt = nil //Do not send any response for error case
1103 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001104 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001105 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001106 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1107 "omciPkt": msg.OmciPkt,
1108 "omciMsgType": msg.OmciMsg.MessageType,
1109 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001110 "IntfId": o.PonPortID,
1111 "SerialNumber": o.Sn(),
1112 }).Warnf("OMCI-message-not-supported")
1113 }
1114
1115 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001116 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001117 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001118 "IntfId": o.PonPortID,
1119 "SerialNumber": o.Sn(),
1120 "omciPacket": responsePkt,
1121 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1122 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001123 }).Errorf("failed-to-send-omci-message: %v", err)
1124 }
1125 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001126
Pragya Arya324337e2020-02-20 14:35:08 +05301127 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -08001128}
Pragya Arya324337e2020-02-20 14:35:08 +05301129
Matteo Scandolof9d43412021-01-12 11:11:34 -08001130// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1131func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1132 indication := &openolt.Indication_OmciInd{
1133 OmciInd: &openolt.OmciIndication{
1134 IntfId: o.PonPortID,
1135 OnuId: o.ID,
1136 Pkt: responsePkt,
1137 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001138 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001139 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1140 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001141 }
1142 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001143 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001144 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001145 "omciPacket": indication.OmciInd.Pkt,
1146 "transCorrId": txId,
1147 }).Trace("omci-message-sent")
1148 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001149}
1150
Matteo Scandolo27428702019-10-11 16:21:16 -07001151func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001152 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001153 // we need to add support for multiple UNIs
1154 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001155 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001156 // - change the library so that it reports a single UNI and remove this workaroung
1157 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001158 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001159 onuLogger.WithFields(log.Fields{
1160 "IntfId": o.PonPortID,
1161 "OnuId": o.ID,
1162 "SerialNumber": o.Sn(),
1163 "OnuPortNo": o.PortNo,
1164 "FlowPortNo": portNo,
1165 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001166 o.PortNo = portNo
1167 }
1168}
1169
William Kurkian0418bc82019-11-06 12:16:24 -05001170func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001171 onuLogger.WithFields(log.Fields{
1172 "IntfId": o.PonPortID,
1173 "OnuId": id,
1174 "SerialNumber": o.Sn(),
1175 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001176 o.ID = id
1177}
1178
Matteo Scandolof9d43412021-01-12 11:11:34 -08001179func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001180 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001181 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001182 "Cookie": msg.Flow.Cookie,
1183 "DstPort": msg.Flow.Classifier.DstPort,
1184 "FlowId": msg.Flow.FlowId,
1185 "FlowType": msg.Flow.FlowType,
1186 "GemportId": msg.Flow.GemportId,
1187 "InnerVlan": msg.Flow.Classifier.IVid,
1188 "IntfId": msg.Flow.AccessIntfId,
1189 "IpProto": msg.Flow.Classifier.IpProto,
1190 "OnuId": msg.Flow.OnuId,
1191 "OnuSn": o.Sn(),
1192 "OuterVlan": msg.Flow.Classifier.OVid,
1193 "PortNo": msg.Flow.PortNo,
1194 "SrcPort": msg.Flow.Classifier.SrcPort,
1195 "UniID": msg.Flow.UniId,
1196 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1197 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1198 "ClassifierIVid": msg.Flow.Classifier.IVid,
1199 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001200 "ReplicateFlow": msg.Flow.ReplicateFlow,
1201 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001202 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001203
Matteo Scandolo813402b2019-10-23 19:24:52 -07001204 if msg.Flow.UniId != 0 {
1205 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1206 onuLogger.WithFields(log.Fields{
1207 "IntfId": o.PonPortID,
1208 "OnuId": o.ID,
1209 "SerialNumber": o.Sn(),
1210 }).Debug("Ignoring flow as it's not for the first UNI")
1211 return
1212 }
1213
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001214 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001215
1216 var gemPortId uint32
1217 if msg.Flow.ReplicateFlow {
1218 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1219 // first available gemport (we only need to send one packet)
1220 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1221 gemPortId = msg.Flow.PbitToGemport[0]
1222 } else {
1223 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1224 gemPortId = uint32(msg.Flow.GemportId)
1225 }
1226 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001227
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001228 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001229 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001230 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001231
1232 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001233 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001234 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001235 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1236 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001237 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001238
Matteo Scandolo4a036262020-08-17 15:56:13 -07001239 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001240 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001241 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001242 }
1243}
1244
Matteo Scandolof9d43412021-01-12 11:11:34 -08001245func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001246 onuLogger.WithFields(log.Fields{
1247 "IntfId": o.PonPortID,
1248 "OnuId": o.ID,
1249 "SerialNumber": o.Sn(),
1250 "FlowId": msg.Flow.FlowId,
1251 "FlowType": msg.Flow.FlowType,
1252 }).Debug("ONU receives FlowRemove")
1253
1254 for idx, flow := range o.FlowIds {
1255 // If the gemport is found, delete it from local cache.
1256 if flow == msg.Flow.FlowId {
1257 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1258 break
1259 }
1260 }
1261
1262 if len(o.FlowIds) == 0 {
1263 onuLogger.WithFields(log.Fields{
1264 "IntfId": o.PonPortID,
1265 "OnuId": o.ID,
1266 "SerialNumber": o.Sn(),
1267 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001268
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301269 // check if ONU delete is performed and
1270 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001271 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301272 close(o.Channel)
1273 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001274 }
1275}
1276
Matteo Scandolocedde462021-03-09 17:37:16 -08001277func (o *Onu) Reboot(timeout time.Duration) error {
1278 onuLogger.WithFields(log.Fields{
1279 "IntfId": o.PonPortID,
1280 "OnuId": o.ID,
1281 "SerialNumber": o.Sn(),
1282 }).Debug("shutting-down-onu")
1283 if err := o.HandleShutdownONU(); err != nil {
1284 return err
1285 }
1286 time.Sleep(timeout)
1287 onuLogger.WithFields(log.Fields{
1288 "IntfId": o.PonPortID,
1289 "OnuId": o.ID,
1290 "SerialNumber": o.Sn(),
1291 }).Debug("power-on-onu")
1292 if err := o.HandlePowerOnONU(); err != nil {
1293 return err
1294 }
1295 return nil
1296}
1297
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001298// BBR methods
1299
1300func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1301 omciMsg := openolt.OmciMsg{
1302 IntfId: intfId,
1303 OnuId: onuId,
1304 Pkt: pktBytes,
1305 }
1306
1307 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1308 log.WithFields(log.Fields{
1309 "IntfId": intfId,
1310 "OnuId": onuId,
1311 "SerialNumber": common.OnuSnToString(sn),
1312 "Pkt": omciMsg.Pkt,
1313 }).Fatalf("Failed to send MIB Reset")
1314 }
1315 log.WithFields(log.Fields{
1316 "IntfId": intfId,
1317 "OnuId": onuId,
1318 "SerialNumber": common.OnuSnToString(sn),
1319 "Pkt": omciMsg.Pkt,
1320 }).Tracef("Sent OMCI message %s", msgType)
1321}
1322
1323func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1324 var next uint16
1325 if len(highPriority) > 0 && highPriority[0] {
1326 next = onu.hpTid
1327 onu.hpTid += 1
1328 if onu.hpTid < 0x8000 {
1329 onu.hpTid = 0x8000
1330 }
1331 } else {
1332 next = onu.tid
1333 onu.tid += 1
1334 if onu.tid >= 0x8000 {
1335 onu.tid = 1
1336 }
1337 }
1338 return next
1339}
1340
1341// TODO move this method in responders/omcisim
1342func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1343 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1344 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1345}
1346
Matteo Scandolof9d43412021-01-12 11:11:34 -08001347// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1348func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1349
1350 // we need to encode the packet in HEX
1351 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1352 hex.Encode(pkt, msg.OmciInd.Pkt)
1353 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1354 if err != nil {
1355 log.WithFields(log.Fields{
1356 "IntfId": o.PonPortID,
1357 "SerialNumber": o.Sn(),
1358 "omciPacket": msg.OmciInd.Pkt,
1359 }).Error("BBR Cannot parse OMCI packet")
1360 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001361
1362 log.WithFields(log.Fields{
1363 "IntfId": msg.OmciInd.IntfId,
1364 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001365 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001366 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001367 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301368 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001369 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001370 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001371 log.WithFields(log.Fields{
1372 "IntfId": msg.OmciInd.IntfId,
1373 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001374 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001375 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001376 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001377 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001378 case omci.MibResetResponseType:
1379 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1380 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1381 case omci.MibUploadResponseType:
1382 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1383 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1384 case omci.MibUploadNextResponseType:
1385 o.seqNumber++
1386
1387 if o.seqNumber > 290 {
1388 // 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 -08001389 // 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 -08001390 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001391 onuLogger.WithFields(log.Fields{
1392 "OnuId": o.ID,
1393 "IntfId": o.PonPortID,
1394 "OnuSn": o.Sn(),
1395 }).Errorf("Error while transitioning ONU State %v", err)
1396 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001397 } else {
1398 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1399 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001400 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001401 }
1402}
1403
1404func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1405
1406 classifierProto := openolt.Classifier{
1407 EthType: uint32(layers.EthernetTypeEAPOL),
1408 OVid: 4091,
1409 }
1410
1411 actionProto := openolt.Action{}
1412
1413 downstreamFlow := openolt.Flow{
1414 AccessIntfId: int32(o.PonPortID),
1415 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001416 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001417 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001418 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001419 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001420 Classifier: &classifierProto,
1421 Action: &actionProto,
1422 Priority: int32(100),
1423 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001424 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1425 // AllocId and GemPorts need to be unique per PON
1426 // for now use the ONU-ID, will need to change once we support multiple UNIs
1427 AllocId: int32(o.ID),
1428 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001429 }
1430
1431 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1432 log.WithFields(log.Fields{
1433 "IntfId": o.PonPortID,
1434 "OnuId": o.ID,
1435 "FlowId": downstreamFlow.FlowId,
1436 "PortNo": downstreamFlow.PortNo,
1437 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001438 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001439 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001440 }
1441 log.WithFields(log.Fields{
1442 "IntfId": o.PonPortID,
1443 "OnuId": o.ID,
1444 "FlowId": downstreamFlow.FlowId,
1445 "PortNo": downstreamFlow.PortNo,
1446 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1447 }).Info("Sent EAPOL Flow")
1448}
1449
1450func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001451
1452 // BBR only works with a single service (ATT HSIA)
1453 hsia := o.Services[0].(*Service)
1454
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001455 classifierProto := openolt.Classifier{
1456 EthType: uint32(layers.EthernetTypeIPv4),
1457 SrcPort: uint32(68),
1458 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001459 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001460 }
1461
1462 actionProto := openolt.Action{}
1463
1464 downstreamFlow := openolt.Flow{
1465 AccessIntfId: int32(o.PonPortID),
1466 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001467 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001468 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001469 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001470 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001471 Classifier: &classifierProto,
1472 Action: &actionProto,
1473 Priority: int32(100),
1474 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001475 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1476 // AllocId and GemPorts need to be unique per PON
1477 // for now use the ONU-ID, will need to change once we support multiple UNIs
1478 AllocId: int32(o.ID),
1479 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001480 }
1481
1482 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1483 log.WithFields(log.Fields{
1484 "IntfId": o.PonPortID,
1485 "OnuId": o.ID,
1486 "FlowId": downstreamFlow.FlowId,
1487 "PortNo": downstreamFlow.PortNo,
1488 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001489 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001490 }).Fatalf("Failed to send DHCP Flow")
1491 }
1492 log.WithFields(log.Fields{
1493 "IntfId": o.PonPortID,
1494 "OnuId": o.ID,
1495 "FlowId": downstreamFlow.FlowId,
1496 "PortNo": downstreamFlow.PortNo,
1497 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1498 }).Info("Sent DHCP Flow")
1499}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301500
1501// DeleteFlow method search and delete flowKey from the onu flows slice
1502func (onu *Onu) DeleteFlow(key FlowKey) {
1503 for pos, flowKey := range onu.Flows {
1504 if flowKey == key {
1505 // delete the flowKey by shifting all flowKeys by one
1506 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1507 t := make([]FlowKey, len(onu.Flows))
1508 copy(t, onu.Flows)
1509 onu.Flows = t
1510 break
1511 }
1512 }
1513}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301514
1515func (onu *Onu) ReDiscoverOnu() {
1516 // Wait for few seconds to be sure of the cleanup
1517 time.Sleep(5 * time.Second)
1518
1519 onuLogger.WithFields(log.Fields{
1520 "IntfId": onu.PonPortID,
1521 "OnuId": onu.ID,
1522 "OnuSn": onu.Sn(),
1523 }).Debug("Send ONU Re-Discovery")
1524
1525 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001526 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301527 log.WithFields(log.Fields{
1528 "IntfId": onu.PonPortID,
1529 "OnuSn": onu.Sn(),
1530 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001531 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301532 }
1533
Matteo Scandolocedde462021-03-09 17:37:16 -08001534 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301535 log.WithFields(log.Fields{
1536 "IntfId": onu.PonPortID,
1537 "OnuSn": onu.Sn(),
1538 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001539 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301540 }
1541}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001542
1543func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1544 for _, s := range onu.Services {
1545 if service, ok := s.(*Service); ok {
1546 // EAPOL is a strange case, as packets are untagged
1547 // but we assume we will have a single service requiring EAPOL
1548 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1549 service.GemPort = gemport
1550 }
1551
1552 // For DHCP services we single tag the outgoing packets,
1553 // thus the flow only contains the CTag and we can use that to match the service
1554 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1555 service.GemPort = gemport
1556 }
1557
1558 // for dataplane services match both C and S tags
1559 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1560 service.GemPort = gemport
1561 }
1562 }
1563 }
1564}
1565
1566func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1567 for _, s := range onu.Services {
1568 service := s.(*Service)
1569 if service.HwAddress.String() == macAddress.String() {
1570 return service, nil
1571 }
1572 }
1573 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1574}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301575
1576func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1577 switch alarmType {
1578 case "ONU_ALARM_LOS":
1579 msg := bbsim.Message{
1580 Type: bbsim.UniStatusAlarm,
1581 Data: bbsim.UniStatusAlarmMessage{
1582 OnuSN: o.SerialNumber,
1583 OnuID: o.ID,
1584 EntityID: 257,
1585 RaiseOMCIAlarm: raiseOMCIAlarm,
1586 },
1587 }
1588 o.Channel <- msg
1589 }
1590
1591}
1592
1593func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1594 o.onuAlarmsInfoLock.Lock()
1595 defer o.onuAlarmsInfoLock.Unlock()
1596 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1597 if alarmInfo.SequenceNo == 255 {
1598 alarmInfo.SequenceNo = 1
1599 } else {
1600 alarmInfo.SequenceNo++
1601 }
1602 o.onuAlarmsInfo[key] = alarmInfo
1603 return alarmInfo.SequenceNo
1604 } else {
1605 // This is the first time alarm notification message is being sent
1606 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1607 SequenceNo: 1,
1608 }
1609 return 1
1610 }
1611}