blob: f9e2e32358e387b6c8b66cd4c55bd310cb691603 [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 {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700826 // GemPort 4069 is reserved for multicast and shared across ONUs
827 if msgObj.EntityInstance != 4069 {
828 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
829 onuLogger.WithFields(log.Fields{
830 "IntfId": o.PonPortID,
831 "OnuId": o.ID,
832 "GemPortId": msgObj.EntityInstance,
833 "SerialNumber": o.Sn(),
834 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
835 } else {
836 onuLogger.WithFields(log.Fields{
837 "IntfId": o.PonPortID,
838 "OnuId": o.ID,
839 "GemPortId": msgObj.EntityInstance,
840 "SerialNumber": o.Sn(),
841 }).Trace("storing-gem-port-id-via-omci")
842 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
843 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800844 }
845 }
846 }
847
848 // if the gemPort is valid then increment the MDS and return a successful response
849 // otherwise fail the request
850 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
851 // validation this check will need to be rewritten
852 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700853 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800854 o.MibDataSync++
855 }
856 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700857 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800858 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800859 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700860 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800861 if err == nil {
862 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
863 onuLogger.WithFields(log.Fields{
864 "IntfId": o.PonPortID,
865 "OnuId": o.ID,
866 "GemPortId": msgObj.EntityInstance,
867 "SerialNumber": o.Sn(),
868 }).Trace("freeing-gem-port-id-via-omci")
869 o.PonPort.removeGemPort(msgObj.EntityInstance)
870 }
871 }
872
Matteo Scandolob5913142021-03-19 16:10:18 -0700873 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800874 o.MibDataSync++
875 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800876 case omci.RebootRequestType:
877
Matteo Scandolob5913142021-03-19 16:10:18 -0700878 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800879
880 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800881 // we run this in a separate goroutine so that
882 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800883 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800884 if err := o.Reboot(10 * time.Second); err != nil {
885 log.WithFields(log.Fields{
886 "IntfId": o.PonPortID,
887 "OnuId": o.ID,
888 "SerialNumber": o.Sn(),
889 "err": err,
890 }).Error("cannot-reboot-onu-after-omci-reboot-request")
891 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800892 }()
893 case omci.TestRequestType:
894
895 // Test message is special, it requires sending two packets:
896 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
897 // second packet, TestResult, reports the result of running the self-test
898 // TestResult can come some time after a TestResponse
899 // TODO: Implement some delay between the TestResponse and the TestResult
Matteo Scandolob5913142021-03-19 16:10:18 -0700900 isTest, err := omcilib.IsTestRequest(msg.OmciPkt.Data())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800901 if (err == nil) && (isTest) {
902 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
903 onuLogger.WithFields(log.Fields{
904 "IntfId": o.PonPortID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800905 "OnuId": o.ID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800906 "SerialNumber": o.Sn(),
Matteo Scandolob5913142021-03-19 16:10:18 -0700907 "omciPacket": msg.OmciPkt.Data(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800908 "msg": msg,
909 "err": sendErr,
910 }).Error("send-TestResult-indication-failed")
911 }
912 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800913 case omci.SynchronizeTimeRequestType:
914 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700915 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800916 case omci.StartSoftwareDownloadRequestType:
917
918 o.ImageSoftwareReceivedSections = 0
919
Matteo Scandolob5913142021-03-19 16:10:18 -0700920 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800921
Matteo Scandolob5913142021-03-19 16:10:18 -0700922 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800923 o.MibDataSync++
924 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
925 onuLogger.WithFields(log.Fields{
926 "OnuId": o.ID,
927 "IntfId": o.PonPortID,
928 "OnuSn": o.Sn(),
929 "Err": err.Error(),
930 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
931 }
932 } else {
933 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700934 "OmciMsgType": msg.OmciMsg.MessageType,
935 "TransCorrId": msg.OmciMsg.TransactionID,
936 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800937 "IntfId": o.PonPortID,
938 "SerialNumber": o.Sn(),
939 }).Error("error-while-processing-start-software-download-request")
940 }
941 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700942 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800943 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700944 "OmciMsgType": msg.OmciMsg.MessageType,
945 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800946 "EntityInstance": msgObj.EntityInstance,
947 "SectionNumber": msgObj.SectionNumber,
948 "SectionData": msgObj.SectionData,
949 }).Trace("received-download-section-request")
950 o.ImageSoftwareReceivedSections++
951 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
952 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
953 onuLogger.WithFields(log.Fields{
954 "OnuId": o.ID,
955 "IntfId": o.PonPortID,
956 "OnuSn": o.Sn(),
957 "Err": err.Error(),
958 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
959 }
960 }
961 }
962 case omci.DownloadSectionRequestWithResponseType:
963 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -0700964 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
965 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800966 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700967 "OmciMsgType": msg.OmciMsg.MessageType,
968 "TransCorrId": msg.OmciMsg.TransactionID,
969 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800970 "IntfId": o.PonPortID,
971 "SerialNumber": o.Sn(),
972 }).Error("error-while-processing-create-download-section-response")
973 return
974 }
975 o.ImageSoftwareReceivedSections++
976
977 case omci.EndSoftwareDownloadRequestType:
978
979 // In the startSoftwareDownload we get the image size and the window size.
980 // We calculate how many DownloadSection we should receive and validate
981 // that we got the correct amount when we receive this message
982 success := true
983 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
984 onuLogger.WithFields(log.Fields{
985 "OnuId": o.ID,
986 "IntfId": o.PonPortID,
987 "OnuSn": o.Sn(),
988 "ExpectedSections": o.ImageSoftwareExpectedSections,
989 "ReceivedSections": o.ImageSoftwareReceivedSections,
990 }).Errorf("onu-did-not-receive-all-image-sections")
991 success = false
992 }
993
994 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700995 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800996 o.MibDataSync++
997 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
998 onuLogger.WithFields(log.Fields{
999 "OnuId": o.ID,
1000 "IntfId": o.PonPortID,
1001 "OnuSn": o.Sn(),
1002 "Err": err.Error(),
1003 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1004 }
1005 } else {
1006 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001007 "OmciMsgType": msg.OmciMsg.MessageType,
1008 "TransCorrId": msg.OmciMsg.TransactionID,
1009 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001010 "IntfId": o.PonPortID,
1011 "SerialNumber": o.Sn(),
1012 }).Error("error-while-processing-end-software-download-request")
1013 }
1014 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001015 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001016 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1017 onuLogger.WithFields(log.Fields{
1018 "OnuId": o.ID,
1019 "IntfId": o.PonPortID,
1020 "OnuSn": o.Sn(),
1021 "Err": err.Error(),
1022 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1023 }
1024 }
1025 }
1026
1027 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001028 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001029 o.MibDataSync++
1030 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1031 onuLogger.WithFields(log.Fields{
1032 "OnuId": o.ID,
1033 "IntfId": o.PonPortID,
1034 "OnuSn": o.Sn(),
1035 "Err": err.Error(),
1036 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1037 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001038 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001039 o.ActiveImageEntityId = msgObj.EntityInstance
1040 } else {
1041 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1042 }
1043 onuLogger.WithFields(log.Fields{
1044 "OnuId": o.ID,
1045 "IntfId": o.PonPortID,
1046 "OnuSn": o.Sn(),
1047 "ActiveImageEntityId": o.ActiveImageEntityId,
1048 "CommittedImageEntityId": o.CommittedImageEntityId,
1049 }).Info("onu-software-image-activated")
1050
1051 // powercycle the ONU
1052 // we run this in a separate goroutine so that
1053 // the ActivateSoftwareResponse is sent to VOLTHA
1054 // NOTE do we need to wait before rebooting?
1055 go func() {
1056 if err := o.Reboot(10 * time.Second); err != nil {
1057 log.WithFields(log.Fields{
1058 "IntfId": o.PonPortID,
1059 "OnuId": o.ID,
1060 "SerialNumber": o.Sn(),
1061 "err": err,
1062 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1063 }
1064 }()
1065 }
1066 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001067 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001068 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001069 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001070 // TODO validate that the image to commit is:
1071 // - active
1072 // - not already committed
1073 o.CommittedImageEntityId = msgObj.EntityInstance
1074 } else {
1075 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1076 }
1077 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1078 onuLogger.WithFields(log.Fields{
1079 "OnuId": o.ID,
1080 "IntfId": o.PonPortID,
1081 "OnuSn": o.Sn(),
1082 "Err": err.Error(),
1083 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1084 }
1085 onuLogger.WithFields(log.Fields{
1086 "OnuId": o.ID,
1087 "IntfId": o.PonPortID,
1088 "OnuSn": o.Sn(),
1089 "ActiveImageEntityId": o.ActiveImageEntityId,
1090 "CommittedImageEntityId": o.CommittedImageEntityId,
1091 }).Info("onu-software-image-committed")
1092 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301093 case omci.GetAllAlarmsRequestType:
1094 // Reset the alarm sequence number on receiving get all alarms request.
1095 o.onuAlarmsInfoLock.Lock()
1096 for key, alarmInfo := range o.onuAlarmsInfo {
1097 // reset the alarm sequence no
1098 alarmInfo.SequenceNo = 0
1099 o.onuAlarmsInfo[key] = alarmInfo
1100 }
1101 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001102 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301103 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001104 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301105 responsePkt = nil //Do not send any response for error case
1106 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001107 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001108 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001109 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1110 "omciPkt": msg.OmciPkt,
1111 "omciMsgType": msg.OmciMsg.MessageType,
1112 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001113 "IntfId": o.PonPortID,
1114 "SerialNumber": o.Sn(),
1115 }).Warnf("OMCI-message-not-supported")
1116 }
1117
1118 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001119 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001120 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001121 "IntfId": o.PonPortID,
1122 "SerialNumber": o.Sn(),
1123 "omciPacket": responsePkt,
1124 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1125 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001126 }).Errorf("failed-to-send-omci-message: %v", err)
1127 }
1128 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001129
Pragya Arya324337e2020-02-20 14:35:08 +05301130 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -08001131}
Pragya Arya324337e2020-02-20 14:35:08 +05301132
Matteo Scandolof9d43412021-01-12 11:11:34 -08001133// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1134func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1135 indication := &openolt.Indication_OmciInd{
1136 OmciInd: &openolt.OmciIndication{
1137 IntfId: o.PonPortID,
1138 OnuId: o.ID,
1139 Pkt: responsePkt,
1140 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001141 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001142 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1143 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001144 }
1145 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001146 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001147 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001148 "omciPacket": indication.OmciInd.Pkt,
1149 "transCorrId": txId,
1150 }).Trace("omci-message-sent")
1151 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001152}
1153
Matteo Scandolo27428702019-10-11 16:21:16 -07001154func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001155 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001156 // we need to add support for multiple UNIs
1157 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001158 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001159 // - change the library so that it reports a single UNI and remove this workaroung
1160 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001161 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001162 onuLogger.WithFields(log.Fields{
1163 "IntfId": o.PonPortID,
1164 "OnuId": o.ID,
1165 "SerialNumber": o.Sn(),
1166 "OnuPortNo": o.PortNo,
1167 "FlowPortNo": portNo,
1168 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001169 o.PortNo = portNo
1170 }
1171}
1172
William Kurkian0418bc82019-11-06 12:16:24 -05001173func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001174 onuLogger.WithFields(log.Fields{
1175 "IntfId": o.PonPortID,
1176 "OnuId": id,
1177 "SerialNumber": o.Sn(),
1178 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001179 o.ID = id
1180}
1181
Matteo Scandolof9d43412021-01-12 11:11:34 -08001182func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001183 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001184 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001185 "Cookie": msg.Flow.Cookie,
1186 "DstPort": msg.Flow.Classifier.DstPort,
1187 "FlowId": msg.Flow.FlowId,
1188 "FlowType": msg.Flow.FlowType,
1189 "GemportId": msg.Flow.GemportId,
1190 "InnerVlan": msg.Flow.Classifier.IVid,
1191 "IntfId": msg.Flow.AccessIntfId,
1192 "IpProto": msg.Flow.Classifier.IpProto,
1193 "OnuId": msg.Flow.OnuId,
1194 "OnuSn": o.Sn(),
1195 "OuterVlan": msg.Flow.Classifier.OVid,
1196 "PortNo": msg.Flow.PortNo,
1197 "SrcPort": msg.Flow.Classifier.SrcPort,
1198 "UniID": msg.Flow.UniId,
1199 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1200 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1201 "ClassifierIVid": msg.Flow.Classifier.IVid,
1202 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001203 "ReplicateFlow": msg.Flow.ReplicateFlow,
1204 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001205 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001206
Matteo Scandolo813402b2019-10-23 19:24:52 -07001207 if msg.Flow.UniId != 0 {
1208 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1209 onuLogger.WithFields(log.Fields{
1210 "IntfId": o.PonPortID,
1211 "OnuId": o.ID,
1212 "SerialNumber": o.Sn(),
1213 }).Debug("Ignoring flow as it's not for the first UNI")
1214 return
1215 }
1216
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001217 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001218
1219 var gemPortId uint32
1220 if msg.Flow.ReplicateFlow {
1221 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1222 // first available gemport (we only need to send one packet)
1223 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1224 gemPortId = msg.Flow.PbitToGemport[0]
1225 } else {
1226 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1227 gemPortId = uint32(msg.Flow.GemportId)
1228 }
1229 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001230
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001231 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001232 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001233 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001234
1235 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001236 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001237 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001238 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1239 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001240 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001241
Matteo Scandolo4a036262020-08-17 15:56:13 -07001242 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001243 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001244 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001245 }
1246}
1247
Matteo Scandolof9d43412021-01-12 11:11:34 -08001248func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001249 onuLogger.WithFields(log.Fields{
1250 "IntfId": o.PonPortID,
1251 "OnuId": o.ID,
1252 "SerialNumber": o.Sn(),
1253 "FlowId": msg.Flow.FlowId,
1254 "FlowType": msg.Flow.FlowType,
1255 }).Debug("ONU receives FlowRemove")
1256
1257 for idx, flow := range o.FlowIds {
1258 // If the gemport is found, delete it from local cache.
1259 if flow == msg.Flow.FlowId {
1260 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1261 break
1262 }
1263 }
1264
1265 if len(o.FlowIds) == 0 {
1266 onuLogger.WithFields(log.Fields{
1267 "IntfId": o.PonPortID,
1268 "OnuId": o.ID,
1269 "SerialNumber": o.Sn(),
1270 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001271
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301272 // check if ONU delete is performed and
1273 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001274 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301275 close(o.Channel)
1276 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001277 }
1278}
1279
Matteo Scandolocedde462021-03-09 17:37:16 -08001280func (o *Onu) Reboot(timeout time.Duration) error {
1281 onuLogger.WithFields(log.Fields{
1282 "IntfId": o.PonPortID,
1283 "OnuId": o.ID,
1284 "SerialNumber": o.Sn(),
1285 }).Debug("shutting-down-onu")
1286 if err := o.HandleShutdownONU(); err != nil {
1287 return err
1288 }
1289 time.Sleep(timeout)
1290 onuLogger.WithFields(log.Fields{
1291 "IntfId": o.PonPortID,
1292 "OnuId": o.ID,
1293 "SerialNumber": o.Sn(),
1294 }).Debug("power-on-onu")
1295 if err := o.HandlePowerOnONU(); err != nil {
1296 return err
1297 }
1298 return nil
1299}
1300
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001301// BBR methods
1302
1303func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1304 omciMsg := openolt.OmciMsg{
1305 IntfId: intfId,
1306 OnuId: onuId,
1307 Pkt: pktBytes,
1308 }
1309
1310 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1311 log.WithFields(log.Fields{
1312 "IntfId": intfId,
1313 "OnuId": onuId,
1314 "SerialNumber": common.OnuSnToString(sn),
1315 "Pkt": omciMsg.Pkt,
1316 }).Fatalf("Failed to send MIB Reset")
1317 }
1318 log.WithFields(log.Fields{
1319 "IntfId": intfId,
1320 "OnuId": onuId,
1321 "SerialNumber": common.OnuSnToString(sn),
1322 "Pkt": omciMsg.Pkt,
1323 }).Tracef("Sent OMCI message %s", msgType)
1324}
1325
1326func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1327 var next uint16
1328 if len(highPriority) > 0 && highPriority[0] {
1329 next = onu.hpTid
1330 onu.hpTid += 1
1331 if onu.hpTid < 0x8000 {
1332 onu.hpTid = 0x8000
1333 }
1334 } else {
1335 next = onu.tid
1336 onu.tid += 1
1337 if onu.tid >= 0x8000 {
1338 onu.tid = 1
1339 }
1340 }
1341 return next
1342}
1343
1344// TODO move this method in responders/omcisim
1345func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1346 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1347 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1348}
1349
Matteo Scandolof9d43412021-01-12 11:11:34 -08001350// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1351func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1352
1353 // we need to encode the packet in HEX
1354 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1355 hex.Encode(pkt, msg.OmciInd.Pkt)
1356 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1357 if err != nil {
1358 log.WithFields(log.Fields{
1359 "IntfId": o.PonPortID,
1360 "SerialNumber": o.Sn(),
1361 "omciPacket": msg.OmciInd.Pkt,
1362 }).Error("BBR Cannot parse OMCI packet")
1363 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001364
1365 log.WithFields(log.Fields{
1366 "IntfId": msg.OmciInd.IntfId,
1367 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001368 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001369 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001370 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301371 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001372 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001373 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001374 log.WithFields(log.Fields{
1375 "IntfId": msg.OmciInd.IntfId,
1376 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001377 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001378 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001379 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001380 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001381 case omci.MibResetResponseType:
1382 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1383 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1384 case omci.MibUploadResponseType:
1385 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1386 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1387 case omci.MibUploadNextResponseType:
1388 o.seqNumber++
1389
1390 if o.seqNumber > 290 {
1391 // 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 -08001392 // 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 -08001393 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001394 onuLogger.WithFields(log.Fields{
1395 "OnuId": o.ID,
1396 "IntfId": o.PonPortID,
1397 "OnuSn": o.Sn(),
1398 }).Errorf("Error while transitioning ONU State %v", err)
1399 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001400 } else {
1401 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1402 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001403 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001404 }
1405}
1406
1407func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1408
1409 classifierProto := openolt.Classifier{
1410 EthType: uint32(layers.EthernetTypeEAPOL),
1411 OVid: 4091,
1412 }
1413
1414 actionProto := openolt.Action{}
1415
1416 downstreamFlow := openolt.Flow{
1417 AccessIntfId: int32(o.PonPortID),
1418 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001419 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001420 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001421 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001422 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001423 Classifier: &classifierProto,
1424 Action: &actionProto,
1425 Priority: int32(100),
1426 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001427 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1428 // AllocId and GemPorts need to be unique per PON
1429 // for now use the ONU-ID, will need to change once we support multiple UNIs
1430 AllocId: int32(o.ID),
1431 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001432 }
1433
1434 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1435 log.WithFields(log.Fields{
1436 "IntfId": o.PonPortID,
1437 "OnuId": o.ID,
1438 "FlowId": downstreamFlow.FlowId,
1439 "PortNo": downstreamFlow.PortNo,
1440 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001441 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001442 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001443 }
1444 log.WithFields(log.Fields{
1445 "IntfId": o.PonPortID,
1446 "OnuId": o.ID,
1447 "FlowId": downstreamFlow.FlowId,
1448 "PortNo": downstreamFlow.PortNo,
1449 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1450 }).Info("Sent EAPOL Flow")
1451}
1452
1453func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001454
1455 // BBR only works with a single service (ATT HSIA)
1456 hsia := o.Services[0].(*Service)
1457
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001458 classifierProto := openolt.Classifier{
1459 EthType: uint32(layers.EthernetTypeIPv4),
1460 SrcPort: uint32(68),
1461 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001462 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001463 }
1464
1465 actionProto := openolt.Action{}
1466
1467 downstreamFlow := openolt.Flow{
1468 AccessIntfId: int32(o.PonPortID),
1469 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001470 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001471 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001472 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001473 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001474 Classifier: &classifierProto,
1475 Action: &actionProto,
1476 Priority: int32(100),
1477 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001478 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1479 // AllocId and GemPorts need to be unique per PON
1480 // for now use the ONU-ID, will need to change once we support multiple UNIs
1481 AllocId: int32(o.ID),
1482 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001483 }
1484
1485 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1486 log.WithFields(log.Fields{
1487 "IntfId": o.PonPortID,
1488 "OnuId": o.ID,
1489 "FlowId": downstreamFlow.FlowId,
1490 "PortNo": downstreamFlow.PortNo,
1491 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001492 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001493 }).Fatalf("Failed to send DHCP Flow")
1494 }
1495 log.WithFields(log.Fields{
1496 "IntfId": o.PonPortID,
1497 "OnuId": o.ID,
1498 "FlowId": downstreamFlow.FlowId,
1499 "PortNo": downstreamFlow.PortNo,
1500 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1501 }).Info("Sent DHCP Flow")
1502}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301503
1504// DeleteFlow method search and delete flowKey from the onu flows slice
1505func (onu *Onu) DeleteFlow(key FlowKey) {
1506 for pos, flowKey := range onu.Flows {
1507 if flowKey == key {
1508 // delete the flowKey by shifting all flowKeys by one
1509 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1510 t := make([]FlowKey, len(onu.Flows))
1511 copy(t, onu.Flows)
1512 onu.Flows = t
1513 break
1514 }
1515 }
1516}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301517
1518func (onu *Onu) ReDiscoverOnu() {
1519 // Wait for few seconds to be sure of the cleanup
1520 time.Sleep(5 * time.Second)
1521
1522 onuLogger.WithFields(log.Fields{
1523 "IntfId": onu.PonPortID,
1524 "OnuId": onu.ID,
1525 "OnuSn": onu.Sn(),
1526 }).Debug("Send ONU Re-Discovery")
1527
1528 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001529 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301530 log.WithFields(log.Fields{
1531 "IntfId": onu.PonPortID,
1532 "OnuSn": onu.Sn(),
1533 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001534 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301535 }
1536
Matteo Scandolocedde462021-03-09 17:37:16 -08001537 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301538 log.WithFields(log.Fields{
1539 "IntfId": onu.PonPortID,
1540 "OnuSn": onu.Sn(),
1541 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001542 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301543 }
1544}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001545
1546func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1547 for _, s := range onu.Services {
1548 if service, ok := s.(*Service); ok {
1549 // EAPOL is a strange case, as packets are untagged
1550 // but we assume we will have a single service requiring EAPOL
1551 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1552 service.GemPort = gemport
1553 }
1554
1555 // For DHCP services we single tag the outgoing packets,
1556 // thus the flow only contains the CTag and we can use that to match the service
1557 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1558 service.GemPort = gemport
1559 }
1560
1561 // for dataplane services match both C and S tags
1562 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1563 service.GemPort = gemport
1564 }
1565 }
1566 }
1567}
1568
1569func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1570 for _, s := range onu.Services {
1571 service := s.(*Service)
1572 if service.HwAddress.String() == macAddress.String() {
1573 return service, nil
1574 }
1575 }
1576 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1577}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301578
1579func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1580 switch alarmType {
1581 case "ONU_ALARM_LOS":
1582 msg := bbsim.Message{
1583 Type: bbsim.UniStatusAlarm,
1584 Data: bbsim.UniStatusAlarmMessage{
1585 OnuSN: o.SerialNumber,
1586 OnuID: o.ID,
1587 EntityID: 257,
1588 RaiseOMCIAlarm: raiseOMCIAlarm,
1589 },
1590 }
1591 o.Channel <- msg
1592 }
1593
1594}
1595
1596func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1597 o.onuAlarmsInfoLock.Lock()
1598 defer o.onuAlarmsInfoLock.Unlock()
1599 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1600 if alarmInfo.SequenceNo == 255 {
1601 alarmInfo.SequenceNo = 1
1602 } else {
1603 alarmInfo.SequenceNo++
1604 }
1605 o.onuAlarmsInfo[key] = alarmInfo
1606 return alarmInfo.SequenceNo
1607 } else {
1608 // This is the first time alarm notification message is being sent
1609 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1610 SequenceNo: 1,
1611 }
1612 return 1
1613 }
1614}