blob: 549b663cfcf6128a52ff1441a0a26c6da7d9271e [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"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000023 "sync"
24
Matteo Scandolof9d43412021-01-12 11:11:34 -080025 pb "github.com/opencord/bbsim/api/bbsim"
26 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000027
28 "net"
29 "strconv"
30 "time"
Himani Chawla13b1ee02021-03-15 01:43:53 +053031
Matteo Scandolo4a036262020-08-17 15:56:13 -070032 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
33 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
34 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080035 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
36 me "github.com/opencord/omci-lib-go/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010037
Matteo Scandolo3bc73742019-08-20 14:04:04 -070038 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070039 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070041 "github.com/opencord/bbsim/internal/common"
42 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolof9d43412021-01-12 11:11:34 -080043 "github.com/opencord/omci-lib-go"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070044 "github.com/opencord/voltha-protos/v4/go/openolt"
45 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070046 log "github.com/sirupsen/logrus"
47)
48
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070049var onuLogger = log.WithFields(log.Fields{
50 "module": "ONU",
51})
52
Matteo Scandolocedde462021-03-09 17:37:16 -080053const (
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000054 maxOmciMsgCounter = 10
55)
56
57const (
Matteo Scandolocedde462021-03-09 17:37:16 -080058 // ONU transitions
59 OnuTxInitialize = "initialize"
60 OnuTxDiscover = "discover"
61 OnuTxEnable = "enable"
62 OnuTxDisable = "disable"
63 OnuTxPonDisable = "pon_disable"
64 OnuTxStartImageDownload = "start_image_download"
65 OnuTxProgressImageDownload = "progress_image_download"
66 OnuTxCompleteImageDownload = "complete_image_download"
67 OnuTxFailImageDownload = "fail_image_download"
68 OnuTxActivateImage = "activate_image"
69 OnuTxCommitImage = "commit_image"
70
71 // ONU States
72 OnuStateCreated = "created"
73 OnuStateInitialized = "initialized"
74 OnuStateDiscovered = "discovered"
75 OnuStateEnabled = "enabled"
76 OnuStateDisabled = "disabled"
77 OnuStatePonDisabled = "pon_disabled"
78 OnuStateImageDownloadStarted = "image_download_started"
79 OnuStateImageDownloadInProgress = "image_download_in_progress"
80 OnuStateImageDownloadComplete = "image_download_completed"
81 OnuStateImageDownloadError = "image_download_error"
82 OnuStateImageActivated = "software_image_activated"
83 OnuStateImageCommitted = "software_image_committed"
84
85 // BBR ONU States and Transitions
86 BbrOnuTxSendEapolFlow = "send_eapol_flow"
87 BbrOnuStateEapolFlowSent = "eapol_flow_sent"
88 BbrOnuTxSendDhcpFlow = "send_dhcp_flow"
89 BbrOnuStateDhcpFlowSent = "dhcp_flow_sent"
90)
91
Pragya Arya8bdb4532020-03-02 17:08:09 +053092type FlowKey struct {
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070093 ID uint64
Pragya Arya8bdb4532020-03-02 17:08:09 +053094 Direction string
95}
96
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070097type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080098 ID uint32
99 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700100 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800101 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +0530102 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
103 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -0700104
105 Services []ServiceIf
106
107 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800108 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -0700109 // PortNo comes with flows and it's used when sending packetIndications,
110 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700111 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800112 PortNo uint32
113 Flows []FlowKey
114 FlowIds []uint64 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -0700115
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700116 OperState *fsm.FSM
117 SerialNumber *openolt.SerialNumber
118
Matteo Scandolof9d43412021-01-12 11:11:34 -0800119 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700120
121 // OMCI params
Matteo Scandolocedde462021-03-09 17:37:16 -0800122 MibDataSync uint8
123 ImageSoftwareExpectedSections int
124 ImageSoftwareReceivedSections int
125 ActiveImageEntityId uint16
126 CommittedImageEntityId uint16
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000127 OmciResponseRate uint8
128 OmciMsgCounter uint8
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800129
130 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700131 tid uint16
132 hpTid uint16
133 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700134
Anand S Katti09541352020-01-29 15:54:01 +0530135 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
136 TrafficSchedulers *tech_profile.TrafficSchedulers
Himani Chawla13b1ee02021-03-15 01:43:53 +0530137 onuAlarmsInfoLock sync.RWMutex
138 onuAlarmsInfo map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700139}
140
Matteo Scandolo99f18462019-10-28 14:14:28 -0700141func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700142 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700143}
144
Matteo Scandolo4a036262020-08-17 15:56:13 -0700145func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700146
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700147 o := Onu{
Matteo Scandolocedde462021-03-09 17:37:16 -0800148 ID: id,
149 PonPortID: pon.ID,
150 PonPort: pon,
151 PortNo: 0,
152 tid: 0x1,
153 hpTid: 0x8000,
154 seqNumber: 0,
155 DoneChannel: make(chan bool, 1),
156 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
157 Flows: []FlowKey{},
158 DiscoveryDelay: delay,
159 MibDataSync: 0,
160 ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
161 ImageSoftwareReceivedSections: 0,
162 ActiveImageEntityId: 0, // when we start the SoftwareImage with ID 0 is active and committed
163 CommittedImageEntityId: 0,
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000164 OmciResponseRate: olt.OmciResponseRate,
165 OmciMsgCounter: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700166 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800167 o.SerialNumber = NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700168 // NOTE this state machine is used to track the operational
169 // state as requested by VOLTHA
170 o.OperState = getOperStateFSM(func(e *fsm.Event) {
171 onuLogger.WithFields(log.Fields{
172 "ID": o.ID,
173 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
174 })
Himani Chawla13b1ee02021-03-15 01:43:53 +0530175 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700176 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
177 o.InternalState = fsm.NewFSM(
Matteo Scandolocedde462021-03-09 17:37:16 -0800178 OnuStateCreated,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700179 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700180 // DEVICE Lifecycle
Matteo Scandolocedde462021-03-09 17:37:16 -0800181 {Name: OnuTxInitialize, Src: []string{OnuStateCreated, OnuStateDisabled, OnuStatePonDisabled}, Dst: OnuStateInitialized},
182 {Name: OnuTxDiscover, Src: []string{OnuStateInitialized}, Dst: OnuStateDiscovered},
183 {Name: OnuTxEnable, Src: []string{OnuStateDiscovered, OnuStatePonDisabled}, Dst: OnuStateEnabled},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100184 // 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 -0800185 {Name: OnuTxDisable, Src: []string{OnuStateEnabled, OnuStatePonDisabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateDisabled},
Pragya Arya6a708d62020-01-01 17:17:20 +0530186 // 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 -0800187 {Name: OnuTxPonDisable, Src: []string{OnuStateEnabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStatePonDisabled},
188 // Software Image Download related states
189 {Name: OnuTxStartImageDownload, Src: []string{OnuStateEnabled, OnuStateImageDownloadComplete, OnuStateImageDownloadError}, Dst: OnuStateImageDownloadStarted},
190 {Name: OnuTxProgressImageDownload, Src: []string{OnuStateImageDownloadStarted}, Dst: OnuStateImageDownloadInProgress},
191 {Name: OnuTxCompleteImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadComplete},
192 {Name: OnuTxFailImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadError},
193 {Name: OnuTxActivateImage, Src: []string{OnuStateImageDownloadComplete}, Dst: OnuStateImageActivated},
194 {Name: OnuTxCommitImage, Src: []string{OnuStateEnabled}, Dst: OnuStateImageCommitted}, // the image is committed after a ONU reboot
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700195 // BBR States
196 // TODO add start OMCI state
Matteo Scandolocedde462021-03-09 17:37:16 -0800197 {Name: BbrOnuTxSendEapolFlow, Src: []string{OnuStateInitialized}, Dst: BbrOnuStateEapolFlowSent},
198 {Name: BbrOnuTxSendDhcpFlow, Src: []string{BbrOnuStateEapolFlowSent}, Dst: BbrOnuStateDhcpFlowSent},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700199 },
200 fsm.Callbacks{
201 "enter_state": func(e *fsm.Event) {
202 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700203 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700204 fmt.Sprintf("enter_%s", OnuStateInitialized): func(e *fsm.Event) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100205 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800206 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800207
Matteo Scandolocedde462021-03-09 17:37:16 -0800208 if err := o.OperState.Event(OnuTxEnable); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800209 onuLogger.WithFields(log.Fields{
210 "OnuId": o.ID,
211 "IntfId": o.PonPortID,
212 "OnuSn": o.Sn(),
213 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
214 }
215
Pragya Arya1cbefa42020-01-13 12:15:29 +0530216 if !isMock {
217 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700218 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530219 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100220 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700221 fmt.Sprintf("enter_%s", OnuStateDiscovered): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800222 msg := bbsim.Message{
223 Type: bbsim.OnuDiscIndication,
224 Data: bbsim.OnuDiscIndicationMessage{
225 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100226 },
227 }
228 o.Channel <- msg
229 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700230 fmt.Sprintf("enter_%s", OnuStateEnabled): func(event *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800231
232 if used, sn := o.PonPort.isOnuIdAllocated(o.ID); used {
233 onuLogger.WithFields(log.Fields{
234 "IntfId": o.PonPortID,
235 "OnuId": o.ID,
236 "SerialNumber": o.Sn(),
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700237 }).Errorf("onu-id-duplicated-with-%s", common.OnuSnToString(sn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800238 return
239 } else {
240 o.PonPort.storeOnuId(o.ID, o.SerialNumber)
241 }
242
Matteo Scandolof9d43412021-01-12 11:11:34 -0800243 msg := bbsim.Message{
244 Type: bbsim.OnuIndication,
245 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700246 OnuSN: o.SerialNumber,
247 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800248 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700249 },
250 }
251 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700252
253 // Once the ONU is enabled start listening for packets
254 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700255 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700256 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700257 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700258 fmt.Sprintf("enter_%s", OnuStateDisabled): func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700259
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700260 o.cleanupOnuState()
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700261
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700262 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800263 if err := o.OperState.Event("disable"); err != nil {
264 onuLogger.WithFields(log.Fields{
265 "OnuId": o.ID,
266 "IntfId": o.PonPortID,
267 "OnuSn": o.Sn(),
268 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
269 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700270 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800271 msg := bbsim.Message{
272 Type: bbsim.OnuIndication,
273 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700274 OnuSN: o.SerialNumber,
275 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800276 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700277 },
278 }
279 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530280
281 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100282 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530283 if len(o.FlowIds) == 0 {
284 close(o.Channel)
285 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700286
287 for _, s := range o.Services {
288 s.Disable()
289 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700290
291 },
292 fmt.Sprintf("enter_%s", OnuStatePonDisabled): func(event *fsm.Event) {
293 o.cleanupOnuState()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700294 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700295 // BBR states
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700296 fmt.Sprintf("enter_%s", BbrOnuStateEapolFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800297 msg := bbsim.Message{
298 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700299 }
300 o.Channel <- msg
301 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700302 fmt.Sprintf("enter_%s", BbrOnuStateDhcpFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800303 msg := bbsim.Message{
304 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700305 }
306 o.Channel <- msg
307 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700308 },
309 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100310
Matteo Scandolo27428702019-10-11 16:21:16 -0700311 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700312}
313
William Kurkian0418bc82019-11-06 12:16:24 -0500314func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700315 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700316 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700317 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700318 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700319 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
320}
321
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700322// cleanupOnuState this method is to clean the local state when the ONU is disabled
323func (o *Onu) cleanupOnuState() {
324 // clean the ONU state
325 o.PortNo = 0
326 o.Flows = []FlowKey{}
327 o.PonPort.removeOnuId(o.ID)
328 o.PonPort.removeAllocId(o.SerialNumber)
329 o.PonPort.removeGemPortBySn(o.SerialNumber)
330
331 o.onuAlarmsInfoLock.Lock()
332 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
333 o.onuAlarmsInfoLock.Unlock()
334}
335
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100336// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000337func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700338 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100339 "onuID": o.ID,
340 "onuSN": o.Sn(),
341 "ponPort": o.PonPortID,
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700342 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100343 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700344
David Bainbridge103cf022019-12-16 20:11:35 +0000345loop:
346 for {
347 select {
348 case <-ctx.Done():
349 onuLogger.WithFields(log.Fields{
350 "onuID": o.ID,
351 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700352 }).Debug("ONU message handling canceled via context")
353 break loop
354 case <-stream.Context().Done():
355 onuLogger.WithFields(log.Fields{
356 "onuID": o.ID,
357 "onuSN": o.Sn(),
358 }).Debug("ONU message handling canceled via stream context")
David Bainbridge103cf022019-12-16 20:11:35 +0000359 break loop
360 case message, ok := <-o.Channel:
361 if !ok || ctx.Err() != nil {
362 onuLogger.WithFields(log.Fields{
363 "onuID": o.ID,
364 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700365 }).Debug("ONU message handling canceled via channel close")
David Bainbridge103cf022019-12-16 20:11:35 +0000366 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700367 }
David Bainbridge103cf022019-12-16 20:11:35 +0000368 onuLogger.WithFields(log.Fields{
369 "onuID": o.ID,
370 "onuSN": o.Sn(),
371 "messageType": message.Type,
372 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700373
David Bainbridge103cf022019-12-16 20:11:35 +0000374 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800375 case bbsim.OnuDiscIndication:
376 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000377 // 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 +0530378 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000379 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800380 case bbsim.OnuIndication:
381 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000382 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800383 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800384 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800385 msg, _ := message.Data.(bbsim.OmciMessage)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200386 _ = o.handleOmciRequest(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800387 case bbsim.UniStatusAlarm:
388 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530389 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
390 MeInstance: msg.EntityID,
391 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
392 }
393 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
394 o.onuAlarmsInfoLock.Lock()
395 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
396 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
397 if pkt != nil { //pkt will be nil if we are unable to create the alarm
398 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
399 onuLogger.WithFields(log.Fields{
400 "IntfId": o.PonPortID,
401 "SerialNumber": o.Sn(),
402 "omciPacket": pkt,
403 "adminState": msg.AdminState,
404 "entityID": msg.EntityID,
405 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
406 alarmInfo.SequenceNo--
407 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800408 onuLogger.WithFields(log.Fields{
409 "IntfId": o.PonPortID,
410 "SerialNumber": o.Sn(),
411 "omciPacket": pkt,
412 "adminState": msg.AdminState,
413 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530414 }).Trace("UNI-Link-alarm-sent")
415 if alarmBitMap == [28]byte{0} {
416 delete(o.onuAlarmsInfo, onuAlarmMapKey)
417 } else {
418 alarmInfo.AlarmBitMap = alarmBitMap
419 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
420 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800421 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530422 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800423 case bbsim.FlowAdd:
424 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700425 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800426 case bbsim.FlowRemoved:
427 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700428 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800429 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700430
Matteo Scandolof9d43412021-01-12 11:11:34 -0800431 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000432
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700433 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000434 "IntfId": msg.IntfId,
435 "OnuId": msg.OnuId,
436 "pktType": msg.Type,
437 }).Trace("Received OnuPacketOut Message")
438
Matteo Scandolo618a6582020-09-09 12:21:29 -0700439 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
440
441 service, err := o.findServiceByMacAddress(msg.MacAddress)
442 if err != nil {
443 onuLogger.WithFields(log.Fields{
444 "IntfId": msg.IntfId,
445 "OnuId": msg.OnuId,
446 "pktType": msg.Type,
447 "MacAddress": msg.MacAddress,
448 "Pkt": hex.EncodeToString(msg.Packet.Data()),
449 "OnuSn": o.Sn(),
450 }).Error("Cannot find Service associated with packet")
451 return
452 }
453 service.PacketCh <- msg
454 } else if msg.Type == packetHandlers.IGMP {
455 // if it's an IGMP packet we assume we have a single IGMP service
456 for _, s := range o.Services {
457 service := s.(*Service)
458
459 if service.NeedsIgmp {
460 service.PacketCh <- msg
461 }
462 }
David Bainbridge103cf022019-12-16 20:11:35 +0000463 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700464
Matteo Scandolof9d43412021-01-12 11:11:34 -0800465 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000466 // NOTE we only receive BBR packets here.
467 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
468 // 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 -0800469 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000470
471 log.WithFields(log.Fields{
472 "IntfId": msg.IntfId,
473 "OnuId": msg.OnuId,
474 "pktType": msg.Type,
475 }).Trace("Received OnuPacketIn Message")
476
477 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700478 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 +0000479 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700480 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000481 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700482 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800483 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800484 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800485 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
486 o.handleOmciResponse(msg, client)
487 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000488 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800489 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000490 o.sendDhcpFlow(client)
491 default:
492 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700493 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700494 }
495 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100496 onuLogger.WithFields(log.Fields{
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700497 "onuID": o.ID,
498 "onuSN": o.Sn(),
499 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100500 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700501}
502
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800503func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700504 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700505 sn.VendorId = []byte("BBSM")
506 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700507 return sn
508}
509
Matteo Scandolof9d43412021-01-12 11:11:34 -0800510func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700511 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800512 IntfId: o.PonPortID,
513 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700514 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700515
Matteo Scandolo4747d292019-08-05 11:50:18 -0700516 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700517 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700518 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700519 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700520
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700521 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800522 "IntfId": o.PonPortID,
523 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700524 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700525 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800526 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800527
528 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
529 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800530 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800531 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800532 o.sendOnuDiscIndication(msg, stream)
533 }
534 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700535}
536
Matteo Scandolof9d43412021-01-12 11:11:34 -0800537func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800538 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
539 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700540
541 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700542 IntfId: o.PonPortID,
543 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700544 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700545 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700546 SerialNumber: o.SerialNumber,
547 }}
548 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800549 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700550 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700551 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700552 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700553 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800554 "IntfId": o.PonPortID,
555 "OnuId": o.ID,
556 "VolthaOnuId": msg.OnuID,
557 "OperState": msg.OperState.String(),
558 "AdminState": msg.OperState.String(),
559 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700560 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700561
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700562}
563
Matteo Scandolof9d43412021-01-12 11:11:34 -0800564func (o *Onu) HandleShutdownONU() error {
565
566 dyingGasp := pb.ONUAlarmRequest{
567 AlarmType: "DYING_GASP",
568 SerialNumber: o.Sn(),
569 Status: "on",
570 }
571
572 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
573 onuLogger.WithFields(log.Fields{
574 "OnuId": o.ID,
575 "IntfId": o.PonPortID,
576 "OnuSn": o.Sn(),
577 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
578 return err
579 }
580
581 losReq := pb.ONUAlarmRequest{
582 AlarmType: "ONU_ALARM_LOS",
583 SerialNumber: o.Sn(),
584 Status: "on",
585 }
586
587 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
588 onuLogger.WithFields(log.Fields{
589 "OnuId": o.ID,
590 "IntfId": o.PonPortID,
591 "OnuSn": o.Sn(),
592 }).Errorf("Cannot send LOS: %s", err.Error())
593
594 return err
595 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530596 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800597 // TODO if it's the last ONU on the PON, then send a PON LOS
598
Matteo Scandolocedde462021-03-09 17:37:16 -0800599 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800600 onuLogger.WithFields(log.Fields{
601 "OnuId": o.ID,
602 "IntfId": o.PonPortID,
603 "OnuSn": o.Sn(),
604 }).Errorf("Cannot shutdown ONU: %s", err.Error())
605 return err
606 }
607
608 return nil
609}
610
611func (o *Onu) HandlePowerOnONU() error {
612 intitalState := o.InternalState.Current()
613
614 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800615 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
616 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800617 onuLogger.WithFields(log.Fields{
618 "OnuId": o.ID,
619 "IntfId": o.PonPortID,
620 "OnuSn": o.Sn(),
621 }).Errorf("Cannot poweron ONU: %s", err.Error())
622 return err
623 }
624 }
625
626 // turn off the LOS Alarm
627 losReq := pb.ONUAlarmRequest{
628 AlarmType: "ONU_ALARM_LOS",
629 SerialNumber: o.Sn(),
630 Status: "off",
631 }
632
633 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
634 onuLogger.WithFields(log.Fields{
635 "OnuId": o.ID,
636 "IntfId": o.PonPortID,
637 "OnuSn": o.Sn(),
638 }).Errorf("Cannot send LOS: %s", err.Error())
639 return err
640 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530641 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800642
643 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800644 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800645 onuLogger.WithFields(log.Fields{
646 "OnuId": o.ID,
647 "IntfId": o.PonPortID,
648 "OnuSn": o.Sn(),
649 }).Errorf("Cannot poweron ONU: %s", err.Error())
650 return err
651 }
652
653 // move o directly to enable state only when its a powercycle case
654 // in case of first time o poweron o will be moved to enable on
655 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800656 if intitalState == OnuStateDisabled {
657 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800658 onuLogger.WithFields(log.Fields{
659 "OnuId": o.ID,
660 "IntfId": o.PonPortID,
661 "OnuSn": o.Sn(),
662 }).Errorf("Cannot enable ONU: %s", err.Error())
663 return err
664 }
665 }
666
667 return nil
668}
669
670func (o *Onu) SetAlarm(alarmType string, status string) error {
671 alarmReq := pb.ONUAlarmRequest{
672 AlarmType: alarmType,
673 SerialNumber: o.Sn(),
674 Status: status,
675 }
676
677 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
678 if err != nil {
679 return err
680 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530681 raiseAlarm := false
682 if alarmReq.Status == "on" {
683 raiseAlarm = true
684 }
685 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800686 return nil
687}
688
689func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530690 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700691 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530692 if err != nil {
693 log.Errorf("error in getting msgType %v", err)
694 return
695 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800696 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530697 o.seqNumber = 0
698 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800699 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530700 o.seqNumber++
701 if o.seqNumber > 290 {
702 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
703 }
704 }
705 }
706}
707
Matteo Scandolof9d43412021-01-12 11:11:34 -0800708// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
709// and generate the appropriate response to it
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200710func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800711
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700712 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700713 "omciMsgType": msg.OmciMsg.MessageType,
714 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
715 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700716 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700717 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800718 }).Trace("omci-message-decoded")
719
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000720 if o.OmciMsgCounter < maxOmciMsgCounter {
721 o.OmciMsgCounter++
722 } else {
723 o.OmciMsgCounter = 1
724 }
725 if o.OmciMsgCounter > o.OmciResponseRate {
726 onuLogger.WithFields(log.Fields{
727 "OmciMsgCounter": o.OmciMsgCounter,
728 "OmciResponseRate": o.OmciResponseRate,
729 "omciMsgType": msg.OmciMsg.MessageType,
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200730 }).Debug("skipping-omci-msg-response")
731 return fmt.Errorf("skipping-omci-msg-response-because-of-response-rate-%d", o.OmciResponseRate)
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000732 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800733 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800734 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700735 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800736 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800737 onuLogger.WithFields(log.Fields{
738 "IntfId": o.PonPortID,
739 "OnuId": o.ID,
740 "SerialNumber": o.Sn(),
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700741 }).Debug("received-mib-reset-request")
Matteo Scandolob5913142021-03-19 16:10:18 -0700742 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800743 o.MibDataSync = 0
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700744
745 // if the MIB reset is successful then remove all the stored AllocIds and GemPorts
746 o.PonPort.removeAllocId(o.SerialNumber)
747 o.PonPort.removeGemPortBySn(o.SerialNumber)
Girish Gowdrae2683102021-03-05 08:24:26 -0800748 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800749 case omci.MibUploadRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700750 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800751 case omci.MibUploadNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700752 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800753 case omci.GetRequestType:
Girish Gowdra996d81e2021-04-21 16:16:27 -0700754 onuDown := o.OperState.Current() == "down"
755 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId, o.CommittedImageEntityId, onuDown)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800756 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800757 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700758 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800759 switch msgObj.EntityClass {
760 case me.PhysicalPathTerminationPointEthernetUniClassID:
761 // if we're Setting a PPTP state
762 // we need to send the appropriate alarm
763
764 if msgObj.EntityInstance == 257 {
765 // for now we're only caring about the first UNI
766 // NOTE that the EntityID for the UNI port is for now hardcoded in
767 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
768 // are reported during the MIB Upload sequence
769 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530770 raiseOMCIAlarm := false
771 if adminState == 1 {
772 raiseOMCIAlarm = true
Girish Gowdra996d81e2021-04-21 16:16:27 -0700773 // set the OperState to disabled
774 if err := o.OperState.Event(OnuTxDisable); err != nil {
775 onuLogger.WithFields(log.Fields{
776 "OnuId": o.ID,
777 "IntfId": o.PonPortID,
778 "OnuSn": o.Sn(),
779 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
780 }
781 } else {
782 // set the OperState to enabled
783 if err := o.OperState.Event(OnuTxEnable); err != nil {
784 onuLogger.WithFields(log.Fields{
785 "OnuId": o.ID,
786 "IntfId": o.PonPortID,
787 "OnuSn": o.Sn(),
788 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
789 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530790 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800791 msg := bbsim.Message{
792 Type: bbsim.UniStatusAlarm,
793 Data: bbsim.UniStatusAlarmMessage{
Himani Chawla13b1ee02021-03-15 01:43:53 +0530794 OnuSN: o.SerialNumber,
795 OnuID: o.ID,
796 AdminState: adminState,
797 EntityID: msgObj.EntityInstance,
798 RaiseOMCIAlarm: raiseOMCIAlarm,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800799 },
800 }
801 o.Channel <- msg
802 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800803 case me.TContClassID:
804 allocId := msgObj.Attributes["AllocId"].(uint16)
805
806 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
807 // otherwise we are adding it
808 if allocId == 255 || allocId == 65535 {
809 onuLogger.WithFields(log.Fields{
810 "IntfId": o.PonPortID,
811 "OnuId": o.ID,
812 "TContId": msgObj.EntityInstance,
813 "AllocId": allocId,
814 "SerialNumber": o.Sn(),
815 }).Trace("freeing-alloc-id-via-omci")
816 o.PonPort.removeAllocId(o.SerialNumber)
817 } else {
818 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
819 onuLogger.WithFields(log.Fields{
820 "IntfId": o.PonPortID,
821 "OnuId": o.ID,
822 "AllocId": allocId,
823 "SerialNumber": o.Sn(),
824 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
825 success = false
826 } else {
827 onuLogger.WithFields(log.Fields{
828 "IntfId": o.PonPortID,
829 "OnuId": o.ID,
830 "TContId": msgObj.EntityInstance,
831 "AllocId": allocId,
832 "SerialNumber": o.Sn(),
833 }).Trace("storing-alloc-id-via-omci")
834 o.PonPort.storeAllocId(allocId, o.SerialNumber)
835 }
836 }
837
838 }
839
840 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700841 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800842 o.MibDataSync++
843 }
844 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700845 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800846 }
847 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800848 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
849 var used bool
850 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700851 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800852 if err == nil {
853 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700854 // GemPort 4069 is reserved for multicast and shared across ONUs
855 if msgObj.EntityInstance != 4069 {
856 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
857 onuLogger.WithFields(log.Fields{
858 "IntfId": o.PonPortID,
859 "OnuId": o.ID,
860 "GemPortId": msgObj.EntityInstance,
861 "SerialNumber": o.Sn(),
862 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
863 } else {
864 onuLogger.WithFields(log.Fields{
865 "IntfId": o.PonPortID,
866 "OnuId": o.ID,
867 "GemPortId": msgObj.EntityInstance,
868 "SerialNumber": o.Sn(),
869 }).Trace("storing-gem-port-id-via-omci")
870 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
871 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800872 }
873 }
874 }
875
876 // if the gemPort is valid then increment the MDS and return a successful response
877 // otherwise fail the request
878 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
879 // validation this check will need to be rewritten
880 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700881 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800882 o.MibDataSync++
883 }
884 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700885 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800886 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800887 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700888 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800889 if err == nil {
890 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
891 onuLogger.WithFields(log.Fields{
892 "IntfId": o.PonPortID,
893 "OnuId": o.ID,
894 "GemPortId": msgObj.EntityInstance,
895 "SerialNumber": o.Sn(),
896 }).Trace("freeing-gem-port-id-via-omci")
897 o.PonPort.removeGemPort(msgObj.EntityInstance)
898 }
899 }
900
Matteo Scandolob5913142021-03-19 16:10:18 -0700901 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800902 o.MibDataSync++
903 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800904 case omci.RebootRequestType:
905
Matteo Scandolob5913142021-03-19 16:10:18 -0700906 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800907
908 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800909 // we run this in a separate goroutine so that
910 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800911 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800912 if err := o.Reboot(10 * time.Second); err != nil {
913 log.WithFields(log.Fields{
914 "IntfId": o.PonPortID,
915 "OnuId": o.ID,
916 "SerialNumber": o.Sn(),
917 "err": err,
918 }).Error("cannot-reboot-onu-after-omci-reboot-request")
919 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800920 }()
921 case omci.TestRequestType:
Girish Gowdra161d27a2021-05-05 12:01:44 -0700922 var classID me.ClassID
923 var omciResult me.Results
924 var instID uint16
925 responsePkt, errResp, classID, instID, omciResult = omcilib.CreateTestResponse(msg.OmciPkt, msg.OmciMsg)
926 // Send TestResult only in case the TestResponse omci result code is me.Success
927 if responsePkt != nil && errResp == nil && omciResult == me.Success {
928 if testResultPkt, err := omcilib.CreateTestResult(classID, instID, msg.OmciMsg.TransactionID); err == nil {
929 // send test results asynchronously
930 go func() {
931 // Send test results after a second to emulate async behavior
932 time.Sleep(1 * time.Second)
933 if testResultPkt != nil {
934 if err := o.sendOmciIndication(testResultPkt, msg.OmciMsg.TransactionID, stream); err != nil {
935 onuLogger.WithFields(log.Fields{
936 "IntfId": o.PonPortID,
937 "SerialNumber": o.Sn(),
938 "omciPacket": testResultPkt,
939 "msg.OmciMsgType": msg.OmciMsg.MessageType,
940 "transCorrId": msg.OmciMsg.TransactionID,
941 }).Errorf("failed-to-send-omci-message: %v", err)
942 }
943 }
944 }()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800945 }
946 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800947 case omci.SynchronizeTimeRequestType:
948 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700949 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800950 case omci.StartSoftwareDownloadRequestType:
951
952 o.ImageSoftwareReceivedSections = 0
953
Matteo Scandolob5913142021-03-19 16:10:18 -0700954 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800955
Matteo Scandolob5913142021-03-19 16:10:18 -0700956 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800957 o.MibDataSync++
958 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
959 onuLogger.WithFields(log.Fields{
960 "OnuId": o.ID,
961 "IntfId": o.PonPortID,
962 "OnuSn": o.Sn(),
963 "Err": err.Error(),
964 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
965 }
966 } else {
967 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700968 "OmciMsgType": msg.OmciMsg.MessageType,
969 "TransCorrId": msg.OmciMsg.TransactionID,
970 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800971 "IntfId": o.PonPortID,
972 "SerialNumber": o.Sn(),
973 }).Error("error-while-processing-start-software-download-request")
974 }
975 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700976 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800977 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700978 "OmciMsgType": msg.OmciMsg.MessageType,
979 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800980 "EntityInstance": msgObj.EntityInstance,
981 "SectionNumber": msgObj.SectionNumber,
982 "SectionData": msgObj.SectionData,
983 }).Trace("received-download-section-request")
984 o.ImageSoftwareReceivedSections++
985 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
986 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
987 onuLogger.WithFields(log.Fields{
988 "OnuId": o.ID,
989 "IntfId": o.PonPortID,
990 "OnuSn": o.Sn(),
991 "Err": err.Error(),
992 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
993 }
994 }
995 }
996 case omci.DownloadSectionRequestWithResponseType:
997 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -0700998 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
999 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001000 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001001 "OmciMsgType": msg.OmciMsg.MessageType,
1002 "TransCorrId": msg.OmciMsg.TransactionID,
1003 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001004 "IntfId": o.PonPortID,
1005 "SerialNumber": o.Sn(),
1006 }).Error("error-while-processing-create-download-section-response")
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001007 return fmt.Errorf("error-while-processing-create-download-section-response: %s", errResp.Error())
Matteo Scandolocedde462021-03-09 17:37:16 -08001008 }
1009 o.ImageSoftwareReceivedSections++
1010
1011 case omci.EndSoftwareDownloadRequestType:
1012
1013 // In the startSoftwareDownload we get the image size and the window size.
1014 // We calculate how many DownloadSection we should receive and validate
1015 // that we got the correct amount when we receive this message
1016 success := true
1017 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
1018 onuLogger.WithFields(log.Fields{
1019 "OnuId": o.ID,
1020 "IntfId": o.PonPortID,
1021 "OnuSn": o.Sn(),
1022 "ExpectedSections": o.ImageSoftwareExpectedSections,
1023 "ReceivedSections": o.ImageSoftwareReceivedSections,
1024 }).Errorf("onu-did-not-receive-all-image-sections")
1025 success = false
1026 }
1027
1028 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001029 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001030 o.MibDataSync++
1031 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1032 onuLogger.WithFields(log.Fields{
1033 "OnuId": o.ID,
1034 "IntfId": o.PonPortID,
1035 "OnuSn": o.Sn(),
1036 "Err": err.Error(),
1037 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1038 }
1039 } else {
1040 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001041 "OmciMsgType": msg.OmciMsg.MessageType,
1042 "TransCorrId": msg.OmciMsg.TransactionID,
1043 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001044 "IntfId": o.PonPortID,
1045 "SerialNumber": o.Sn(),
1046 }).Error("error-while-processing-end-software-download-request")
1047 }
1048 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001049 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001050 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1051 onuLogger.WithFields(log.Fields{
1052 "OnuId": o.ID,
1053 "IntfId": o.PonPortID,
1054 "OnuSn": o.Sn(),
1055 "Err": err.Error(),
1056 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1057 }
1058 }
1059 }
1060
1061 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001062 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001063 o.MibDataSync++
1064 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1065 onuLogger.WithFields(log.Fields{
1066 "OnuId": o.ID,
1067 "IntfId": o.PonPortID,
1068 "OnuSn": o.Sn(),
1069 "Err": err.Error(),
1070 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1071 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001072 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001073 o.ActiveImageEntityId = msgObj.EntityInstance
1074 } else {
1075 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1076 }
1077 onuLogger.WithFields(log.Fields{
1078 "OnuId": o.ID,
1079 "IntfId": o.PonPortID,
1080 "OnuSn": o.Sn(),
1081 "ActiveImageEntityId": o.ActiveImageEntityId,
1082 "CommittedImageEntityId": o.CommittedImageEntityId,
1083 }).Info("onu-software-image-activated")
1084
1085 // powercycle the ONU
1086 // we run this in a separate goroutine so that
1087 // the ActivateSoftwareResponse is sent to VOLTHA
1088 // NOTE do we need to wait before rebooting?
1089 go func() {
1090 if err := o.Reboot(10 * time.Second); err != nil {
1091 log.WithFields(log.Fields{
1092 "IntfId": o.PonPortID,
1093 "OnuId": o.ID,
1094 "SerialNumber": o.Sn(),
1095 "err": err,
1096 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1097 }
1098 }()
1099 }
1100 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001101 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001102 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001103 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001104 // TODO validate that the image to commit is:
1105 // - active
1106 // - not already committed
1107 o.CommittedImageEntityId = msgObj.EntityInstance
1108 } else {
1109 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1110 }
1111 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1112 onuLogger.WithFields(log.Fields{
1113 "OnuId": o.ID,
1114 "IntfId": o.PonPortID,
1115 "OnuSn": o.Sn(),
1116 "Err": err.Error(),
1117 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1118 }
1119 onuLogger.WithFields(log.Fields{
1120 "OnuId": o.ID,
1121 "IntfId": o.PonPortID,
1122 "OnuSn": o.Sn(),
1123 "ActiveImageEntityId": o.ActiveImageEntityId,
1124 "CommittedImageEntityId": o.CommittedImageEntityId,
1125 }).Info("onu-software-image-committed")
1126 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301127 case omci.GetAllAlarmsRequestType:
1128 // Reset the alarm sequence number on receiving get all alarms request.
1129 o.onuAlarmsInfoLock.Lock()
1130 for key, alarmInfo := range o.onuAlarmsInfo {
1131 // reset the alarm sequence no
1132 alarmInfo.SequenceNo = 0
1133 o.onuAlarmsInfo[key] = alarmInfo
1134 }
1135 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001136 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301137 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001138 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301139 responsePkt = nil //Do not send any response for error case
1140 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001141 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001142 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001143 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1144 "omciPkt": msg.OmciPkt,
1145 "omciMsgType": msg.OmciMsg.MessageType,
1146 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001147 "IntfId": o.PonPortID,
1148 "SerialNumber": o.Sn(),
1149 }).Warnf("OMCI-message-not-supported")
1150 }
1151
1152 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001153 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001154 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001155 "IntfId": o.PonPortID,
1156 "SerialNumber": o.Sn(),
1157 "omciPacket": responsePkt,
1158 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1159 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001160 }).Errorf("failed-to-send-omci-message: %v", err)
1161 }
1162 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001163
Pragya Arya324337e2020-02-20 14:35:08 +05301164 o.publishOmciEvent(msg)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001165 return nil
Matteo Scandolof9d43412021-01-12 11:11:34 -08001166}
Pragya Arya324337e2020-02-20 14:35:08 +05301167
Matteo Scandolof9d43412021-01-12 11:11:34 -08001168// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1169func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1170 indication := &openolt.Indication_OmciInd{
1171 OmciInd: &openolt.OmciIndication{
1172 IntfId: o.PonPortID,
1173 OnuId: o.ID,
1174 Pkt: responsePkt,
1175 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001176 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001177 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1178 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001179 }
1180 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001181 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001182 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001183 "omciPacket": indication.OmciInd.Pkt,
1184 "transCorrId": txId,
1185 }).Trace("omci-message-sent")
1186 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001187}
1188
Matteo Scandolo27428702019-10-11 16:21:16 -07001189func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001190 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001191 // we need to add support for multiple UNIs
1192 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001193 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001194 // - change the library so that it reports a single UNI and remove this workaroung
1195 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001196 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001197 onuLogger.WithFields(log.Fields{
1198 "IntfId": o.PonPortID,
1199 "OnuId": o.ID,
1200 "SerialNumber": o.Sn(),
1201 "OnuPortNo": o.PortNo,
1202 "FlowPortNo": portNo,
1203 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001204 o.PortNo = portNo
1205 }
1206}
1207
William Kurkian0418bc82019-11-06 12:16:24 -05001208func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001209 onuLogger.WithFields(log.Fields{
1210 "IntfId": o.PonPortID,
1211 "OnuId": id,
1212 "SerialNumber": o.Sn(),
1213 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001214 o.ID = id
1215}
1216
Matteo Scandolof9d43412021-01-12 11:11:34 -08001217func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001218 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001219 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001220 "Cookie": msg.Flow.Cookie,
1221 "DstPort": msg.Flow.Classifier.DstPort,
1222 "FlowId": msg.Flow.FlowId,
1223 "FlowType": msg.Flow.FlowType,
1224 "GemportId": msg.Flow.GemportId,
1225 "InnerVlan": msg.Flow.Classifier.IVid,
1226 "IntfId": msg.Flow.AccessIntfId,
1227 "IpProto": msg.Flow.Classifier.IpProto,
1228 "OnuId": msg.Flow.OnuId,
1229 "OnuSn": o.Sn(),
1230 "OuterVlan": msg.Flow.Classifier.OVid,
1231 "PortNo": msg.Flow.PortNo,
1232 "SrcPort": msg.Flow.Classifier.SrcPort,
1233 "UniID": msg.Flow.UniId,
1234 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1235 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1236 "ClassifierIVid": msg.Flow.Classifier.IVid,
1237 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001238 "ReplicateFlow": msg.Flow.ReplicateFlow,
1239 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001240 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001241
Matteo Scandolo813402b2019-10-23 19:24:52 -07001242 if msg.Flow.UniId != 0 {
1243 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1244 onuLogger.WithFields(log.Fields{
1245 "IntfId": o.PonPortID,
1246 "OnuId": o.ID,
1247 "SerialNumber": o.Sn(),
1248 }).Debug("Ignoring flow as it's not for the first UNI")
1249 return
1250 }
1251
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001252 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001253
1254 var gemPortId uint32
1255 if msg.Flow.ReplicateFlow {
1256 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1257 // first available gemport (we only need to send one packet)
1258 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1259 gemPortId = msg.Flow.PbitToGemport[0]
1260 } else {
1261 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1262 gemPortId = uint32(msg.Flow.GemportId)
1263 }
1264 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001265
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001266 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001267 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001268 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001269
1270 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001271 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001272 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001273 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1274 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001275 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001276
Matteo Scandolo4a036262020-08-17 15:56:13 -07001277 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001278 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001279 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001280 }
1281}
1282
Matteo Scandolof9d43412021-01-12 11:11:34 -08001283func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001284 onuLogger.WithFields(log.Fields{
1285 "IntfId": o.PonPortID,
1286 "OnuId": o.ID,
1287 "SerialNumber": o.Sn(),
1288 "FlowId": msg.Flow.FlowId,
1289 "FlowType": msg.Flow.FlowType,
1290 }).Debug("ONU receives FlowRemove")
1291
1292 for idx, flow := range o.FlowIds {
1293 // If the gemport is found, delete it from local cache.
1294 if flow == msg.Flow.FlowId {
1295 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1296 break
1297 }
1298 }
1299
1300 if len(o.FlowIds) == 0 {
1301 onuLogger.WithFields(log.Fields{
1302 "IntfId": o.PonPortID,
1303 "OnuId": o.ID,
1304 "SerialNumber": o.Sn(),
1305 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001306
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301307 // check if ONU delete is performed and
1308 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001309 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301310 close(o.Channel)
1311 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001312 }
1313}
1314
Matteo Scandolocedde462021-03-09 17:37:16 -08001315func (o *Onu) Reboot(timeout time.Duration) error {
1316 onuLogger.WithFields(log.Fields{
1317 "IntfId": o.PonPortID,
1318 "OnuId": o.ID,
1319 "SerialNumber": o.Sn(),
1320 }).Debug("shutting-down-onu")
1321 if err := o.HandleShutdownONU(); err != nil {
1322 return err
1323 }
1324 time.Sleep(timeout)
1325 onuLogger.WithFields(log.Fields{
1326 "IntfId": o.PonPortID,
1327 "OnuId": o.ID,
1328 "SerialNumber": o.Sn(),
1329 }).Debug("power-on-onu")
1330 if err := o.HandlePowerOnONU(); err != nil {
1331 return err
1332 }
1333 return nil
1334}
1335
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001336// BBR methods
1337
1338func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1339 omciMsg := openolt.OmciMsg{
1340 IntfId: intfId,
1341 OnuId: onuId,
1342 Pkt: pktBytes,
1343 }
1344
1345 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1346 log.WithFields(log.Fields{
1347 "IntfId": intfId,
1348 "OnuId": onuId,
1349 "SerialNumber": common.OnuSnToString(sn),
1350 "Pkt": omciMsg.Pkt,
1351 }).Fatalf("Failed to send MIB Reset")
1352 }
1353 log.WithFields(log.Fields{
1354 "IntfId": intfId,
1355 "OnuId": onuId,
1356 "SerialNumber": common.OnuSnToString(sn),
1357 "Pkt": omciMsg.Pkt,
1358 }).Tracef("Sent OMCI message %s", msgType)
1359}
1360
1361func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1362 var next uint16
1363 if len(highPriority) > 0 && highPriority[0] {
1364 next = onu.hpTid
1365 onu.hpTid += 1
1366 if onu.hpTid < 0x8000 {
1367 onu.hpTid = 0x8000
1368 }
1369 } else {
1370 next = onu.tid
1371 onu.tid += 1
1372 if onu.tid >= 0x8000 {
1373 onu.tid = 1
1374 }
1375 }
1376 return next
1377}
1378
1379// TODO move this method in responders/omcisim
1380func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1381 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1382 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1383}
1384
Matteo Scandolof9d43412021-01-12 11:11:34 -08001385// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1386func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1387
1388 // we need to encode the packet in HEX
1389 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1390 hex.Encode(pkt, msg.OmciInd.Pkt)
1391 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1392 if err != nil {
1393 log.WithFields(log.Fields{
1394 "IntfId": o.PonPortID,
1395 "SerialNumber": o.Sn(),
1396 "omciPacket": msg.OmciInd.Pkt,
1397 }).Error("BBR Cannot parse OMCI packet")
1398 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001399
1400 log.WithFields(log.Fields{
1401 "IntfId": msg.OmciInd.IntfId,
1402 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001403 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001404 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001405 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301406 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001407 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001408 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001409 log.WithFields(log.Fields{
1410 "IntfId": msg.OmciInd.IntfId,
1411 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001412 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001413 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001414 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001415 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001416 case omci.MibResetResponseType:
1417 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1418 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1419 case omci.MibUploadResponseType:
1420 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1421 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1422 case omci.MibUploadNextResponseType:
1423 o.seqNumber++
1424
1425 if o.seqNumber > 290 {
1426 // 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 -08001427 // 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 -08001428 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001429 onuLogger.WithFields(log.Fields{
1430 "OnuId": o.ID,
1431 "IntfId": o.PonPortID,
1432 "OnuSn": o.Sn(),
1433 }).Errorf("Error while transitioning ONU State %v", err)
1434 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001435 } else {
1436 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1437 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001438 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001439 }
1440}
1441
1442func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1443
1444 classifierProto := openolt.Classifier{
1445 EthType: uint32(layers.EthernetTypeEAPOL),
1446 OVid: 4091,
1447 }
1448
1449 actionProto := openolt.Action{}
1450
1451 downstreamFlow := openolt.Flow{
1452 AccessIntfId: int32(o.PonPortID),
1453 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001454 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001455 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001456 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001457 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001458 Classifier: &classifierProto,
1459 Action: &actionProto,
1460 Priority: int32(100),
1461 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001462 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1463 // AllocId and GemPorts need to be unique per PON
1464 // for now use the ONU-ID, will need to change once we support multiple UNIs
1465 AllocId: int32(o.ID),
1466 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001467 }
1468
1469 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1470 log.WithFields(log.Fields{
1471 "IntfId": o.PonPortID,
1472 "OnuId": o.ID,
1473 "FlowId": downstreamFlow.FlowId,
1474 "PortNo": downstreamFlow.PortNo,
1475 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001476 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001477 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001478 }
1479 log.WithFields(log.Fields{
1480 "IntfId": o.PonPortID,
1481 "OnuId": o.ID,
1482 "FlowId": downstreamFlow.FlowId,
1483 "PortNo": downstreamFlow.PortNo,
1484 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1485 }).Info("Sent EAPOL Flow")
1486}
1487
1488func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001489
1490 // BBR only works with a single service (ATT HSIA)
1491 hsia := o.Services[0].(*Service)
1492
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001493 classifierProto := openolt.Classifier{
1494 EthType: uint32(layers.EthernetTypeIPv4),
1495 SrcPort: uint32(68),
1496 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001497 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001498 }
1499
1500 actionProto := openolt.Action{}
1501
1502 downstreamFlow := openolt.Flow{
1503 AccessIntfId: int32(o.PonPortID),
1504 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001505 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001506 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001507 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001508 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001509 Classifier: &classifierProto,
1510 Action: &actionProto,
1511 Priority: int32(100),
1512 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001513 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1514 // AllocId and GemPorts need to be unique per PON
1515 // for now use the ONU-ID, will need to change once we support multiple UNIs
1516 AllocId: int32(o.ID),
1517 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001518 }
1519
1520 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1521 log.WithFields(log.Fields{
1522 "IntfId": o.PonPortID,
1523 "OnuId": o.ID,
1524 "FlowId": downstreamFlow.FlowId,
1525 "PortNo": downstreamFlow.PortNo,
1526 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001527 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001528 }).Fatalf("Failed to send DHCP Flow")
1529 }
1530 log.WithFields(log.Fields{
1531 "IntfId": o.PonPortID,
1532 "OnuId": o.ID,
1533 "FlowId": downstreamFlow.FlowId,
1534 "PortNo": downstreamFlow.PortNo,
1535 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1536 }).Info("Sent DHCP Flow")
1537}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301538
1539// DeleteFlow method search and delete flowKey from the onu flows slice
1540func (onu *Onu) DeleteFlow(key FlowKey) {
1541 for pos, flowKey := range onu.Flows {
1542 if flowKey == key {
1543 // delete the flowKey by shifting all flowKeys by one
1544 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1545 t := make([]FlowKey, len(onu.Flows))
1546 copy(t, onu.Flows)
1547 onu.Flows = t
1548 break
1549 }
1550 }
1551}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301552
1553func (onu *Onu) ReDiscoverOnu() {
1554 // Wait for few seconds to be sure of the cleanup
1555 time.Sleep(5 * time.Second)
1556
1557 onuLogger.WithFields(log.Fields{
1558 "IntfId": onu.PonPortID,
1559 "OnuId": onu.ID,
1560 "OnuSn": onu.Sn(),
1561 }).Debug("Send ONU Re-Discovery")
1562
1563 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001564 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301565 log.WithFields(log.Fields{
1566 "IntfId": onu.PonPortID,
1567 "OnuSn": onu.Sn(),
1568 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001569 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301570 }
1571
Matteo Scandolocedde462021-03-09 17:37:16 -08001572 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301573 log.WithFields(log.Fields{
1574 "IntfId": onu.PonPortID,
1575 "OnuSn": onu.Sn(),
1576 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001577 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301578 }
1579}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001580
1581func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1582 for _, s := range onu.Services {
1583 if service, ok := s.(*Service); ok {
1584 // EAPOL is a strange case, as packets are untagged
1585 // but we assume we will have a single service requiring EAPOL
1586 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1587 service.GemPort = gemport
1588 }
1589
1590 // For DHCP services we single tag the outgoing packets,
1591 // thus the flow only contains the CTag and we can use that to match the service
1592 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1593 service.GemPort = gemport
1594 }
1595
1596 // for dataplane services match both C and S tags
1597 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1598 service.GemPort = gemport
1599 }
1600 }
1601 }
1602}
1603
1604func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1605 for _, s := range onu.Services {
1606 service := s.(*Service)
1607 if service.HwAddress.String() == macAddress.String() {
1608 return service, nil
1609 }
1610 }
1611 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1612}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301613
1614func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1615 switch alarmType {
1616 case "ONU_ALARM_LOS":
1617 msg := bbsim.Message{
1618 Type: bbsim.UniStatusAlarm,
1619 Data: bbsim.UniStatusAlarmMessage{
1620 OnuSN: o.SerialNumber,
1621 OnuID: o.ID,
1622 EntityID: 257,
1623 RaiseOMCIAlarm: raiseOMCIAlarm,
1624 },
1625 }
1626 o.Channel <- msg
1627 }
1628
1629}
1630
1631func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1632 o.onuAlarmsInfoLock.Lock()
1633 defer o.onuAlarmsInfoLock.Unlock()
1634 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1635 if alarmInfo.SequenceNo == 255 {
1636 alarmInfo.SequenceNo = 1
1637 } else {
1638 alarmInfo.SequenceNo++
1639 }
1640 o.onuAlarmsInfo[key] = alarmInfo
1641 return alarmInfo.SequenceNo
1642 } else {
1643 // This is the first time alarm notification message is being sent
1644 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1645 SequenceNo: 1,
1646 }
1647 return 1
1648 }
1649}