blob: a8d67d68d1a62aff23239c2eebe8eb5468e40a35 [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,
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700332 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100333 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700334
David Bainbridge103cf022019-12-16 20:11:35 +0000335loop:
336 for {
337 select {
338 case <-ctx.Done():
339 onuLogger.WithFields(log.Fields{
340 "onuID": o.ID,
341 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700342 }).Debug("ONU message handling canceled via context")
343 break loop
344 case <-stream.Context().Done():
345 onuLogger.WithFields(log.Fields{
346 "onuID": o.ID,
347 "onuSN": o.Sn(),
348 }).Debug("ONU message handling canceled via stream context")
David Bainbridge103cf022019-12-16 20:11:35 +0000349 break loop
350 case message, ok := <-o.Channel:
351 if !ok || ctx.Err() != nil {
352 onuLogger.WithFields(log.Fields{
353 "onuID": o.ID,
354 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700355 }).Debug("ONU message handling canceled via channel close")
David Bainbridge103cf022019-12-16 20:11:35 +0000356 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700357 }
David Bainbridge103cf022019-12-16 20:11:35 +0000358 onuLogger.WithFields(log.Fields{
359 "onuID": o.ID,
360 "onuSN": o.Sn(),
361 "messageType": message.Type,
362 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700363
David Bainbridge103cf022019-12-16 20:11:35 +0000364 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800365 case bbsim.OnuDiscIndication:
366 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000367 // 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 +0530368 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000369 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800370 case bbsim.OnuIndication:
371 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000372 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800373 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800374 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800375 msg, _ := message.Data.(bbsim.OmciMessage)
376 o.handleOmciRequest(msg, stream)
377 case bbsim.UniStatusAlarm:
378 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530379 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
380 MeInstance: msg.EntityID,
381 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
382 }
383 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
384 o.onuAlarmsInfoLock.Lock()
385 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
386 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
387 if pkt != nil { //pkt will be nil if we are unable to create the alarm
388 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
389 onuLogger.WithFields(log.Fields{
390 "IntfId": o.PonPortID,
391 "SerialNumber": o.Sn(),
392 "omciPacket": pkt,
393 "adminState": msg.AdminState,
394 "entityID": msg.EntityID,
395 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
396 alarmInfo.SequenceNo--
397 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800398 onuLogger.WithFields(log.Fields{
399 "IntfId": o.PonPortID,
400 "SerialNumber": o.Sn(),
401 "omciPacket": pkt,
402 "adminState": msg.AdminState,
403 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530404 }).Trace("UNI-Link-alarm-sent")
405 if alarmBitMap == [28]byte{0} {
406 delete(o.onuAlarmsInfo, onuAlarmMapKey)
407 } else {
408 alarmInfo.AlarmBitMap = alarmBitMap
409 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
410 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800411 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530412 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800413 case bbsim.FlowAdd:
414 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700415 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800416 case bbsim.FlowRemoved:
417 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700418 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800419 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700420
Matteo Scandolof9d43412021-01-12 11:11:34 -0800421 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000422
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700423 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000424 "IntfId": msg.IntfId,
425 "OnuId": msg.OnuId,
426 "pktType": msg.Type,
427 }).Trace("Received OnuPacketOut Message")
428
Matteo Scandolo618a6582020-09-09 12:21:29 -0700429 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
430
431 service, err := o.findServiceByMacAddress(msg.MacAddress)
432 if err != nil {
433 onuLogger.WithFields(log.Fields{
434 "IntfId": msg.IntfId,
435 "OnuId": msg.OnuId,
436 "pktType": msg.Type,
437 "MacAddress": msg.MacAddress,
438 "Pkt": hex.EncodeToString(msg.Packet.Data()),
439 "OnuSn": o.Sn(),
440 }).Error("Cannot find Service associated with packet")
441 return
442 }
443 service.PacketCh <- msg
444 } else if msg.Type == packetHandlers.IGMP {
445 // if it's an IGMP packet we assume we have a single IGMP service
446 for _, s := range o.Services {
447 service := s.(*Service)
448
449 if service.NeedsIgmp {
450 service.PacketCh <- msg
451 }
452 }
David Bainbridge103cf022019-12-16 20:11:35 +0000453 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700454
Matteo Scandolof9d43412021-01-12 11:11:34 -0800455 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000456 // NOTE we only receive BBR packets here.
457 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
458 // 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 -0800459 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000460
461 log.WithFields(log.Fields{
462 "IntfId": msg.IntfId,
463 "OnuId": msg.OnuId,
464 "pktType": msg.Type,
465 }).Trace("Received OnuPacketIn Message")
466
467 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700468 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 +0000469 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700470 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000471 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700472 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800473 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800474 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800475 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
476 o.handleOmciResponse(msg, client)
477 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000478 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800479 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000480 o.sendDhcpFlow(client)
481 default:
482 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700483 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700484 }
485 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100486 onuLogger.WithFields(log.Fields{
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700487 "onuID": o.ID,
488 "onuSN": o.Sn(),
489 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100490 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700491}
492
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800493func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700494 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700495 sn.VendorId = []byte("BBSM")
496 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700497 return sn
498}
499
Matteo Scandolof9d43412021-01-12 11:11:34 -0800500func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700501 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800502 IntfId: o.PonPortID,
503 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700504 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700505
Matteo Scandolo4747d292019-08-05 11:50:18 -0700506 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700507 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700508 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700509 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700510
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700511 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800512 "IntfId": o.PonPortID,
513 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700514 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700515 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800516 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800517
518 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
519 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800520 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800521 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800522 o.sendOnuDiscIndication(msg, stream)
523 }
524 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700525}
526
Matteo Scandolof9d43412021-01-12 11:11:34 -0800527func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800528 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
529 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700530
531 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700532 IntfId: o.PonPortID,
533 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700534 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700535 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700536 SerialNumber: o.SerialNumber,
537 }}
538 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800539 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700540 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700541 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700542 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700543 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800544 "IntfId": o.PonPortID,
545 "OnuId": o.ID,
546 "VolthaOnuId": msg.OnuID,
547 "OperState": msg.OperState.String(),
548 "AdminState": msg.OperState.String(),
549 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700550 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700551
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700552}
553
Matteo Scandolof9d43412021-01-12 11:11:34 -0800554func (o *Onu) HandleShutdownONU() error {
555
556 dyingGasp := pb.ONUAlarmRequest{
557 AlarmType: "DYING_GASP",
558 SerialNumber: o.Sn(),
559 Status: "on",
560 }
561
562 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
563 onuLogger.WithFields(log.Fields{
564 "OnuId": o.ID,
565 "IntfId": o.PonPortID,
566 "OnuSn": o.Sn(),
567 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
568 return err
569 }
570
571 losReq := pb.ONUAlarmRequest{
572 AlarmType: "ONU_ALARM_LOS",
573 SerialNumber: o.Sn(),
574 Status: "on",
575 }
576
577 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
578 onuLogger.WithFields(log.Fields{
579 "OnuId": o.ID,
580 "IntfId": o.PonPortID,
581 "OnuSn": o.Sn(),
582 }).Errorf("Cannot send LOS: %s", err.Error())
583
584 return err
585 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530586 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800587 // TODO if it's the last ONU on the PON, then send a PON LOS
588
Matteo Scandolocedde462021-03-09 17:37:16 -0800589 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800590 onuLogger.WithFields(log.Fields{
591 "OnuId": o.ID,
592 "IntfId": o.PonPortID,
593 "OnuSn": o.Sn(),
594 }).Errorf("Cannot shutdown ONU: %s", err.Error())
595 return err
596 }
597
598 return nil
599}
600
601func (o *Onu) HandlePowerOnONU() error {
602 intitalState := o.InternalState.Current()
603
604 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800605 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
606 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800607 onuLogger.WithFields(log.Fields{
608 "OnuId": o.ID,
609 "IntfId": o.PonPortID,
610 "OnuSn": o.Sn(),
611 }).Errorf("Cannot poweron ONU: %s", err.Error())
612 return err
613 }
614 }
615
616 // turn off the LOS Alarm
617 losReq := pb.ONUAlarmRequest{
618 AlarmType: "ONU_ALARM_LOS",
619 SerialNumber: o.Sn(),
620 Status: "off",
621 }
622
623 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
624 onuLogger.WithFields(log.Fields{
625 "OnuId": o.ID,
626 "IntfId": o.PonPortID,
627 "OnuSn": o.Sn(),
628 }).Errorf("Cannot send LOS: %s", err.Error())
629 return err
630 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530631 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800632
633 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800634 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800635 onuLogger.WithFields(log.Fields{
636 "OnuId": o.ID,
637 "IntfId": o.PonPortID,
638 "OnuSn": o.Sn(),
639 }).Errorf("Cannot poweron ONU: %s", err.Error())
640 return err
641 }
642
643 // move o directly to enable state only when its a powercycle case
644 // in case of first time o poweron o will be moved to enable on
645 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800646 if intitalState == OnuStateDisabled {
647 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800648 onuLogger.WithFields(log.Fields{
649 "OnuId": o.ID,
650 "IntfId": o.PonPortID,
651 "OnuSn": o.Sn(),
652 }).Errorf("Cannot enable ONU: %s", err.Error())
653 return err
654 }
655 }
656
657 return nil
658}
659
660func (o *Onu) SetAlarm(alarmType string, status string) error {
661 alarmReq := pb.ONUAlarmRequest{
662 AlarmType: alarmType,
663 SerialNumber: o.Sn(),
664 Status: status,
665 }
666
667 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
668 if err != nil {
669 return err
670 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530671 raiseAlarm := false
672 if alarmReq.Status == "on" {
673 raiseAlarm = true
674 }
675 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800676 return nil
677}
678
679func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530680 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700681 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530682 if err != nil {
683 log.Errorf("error in getting msgType %v", err)
684 return
685 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800686 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530687 o.seqNumber = 0
688 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800689 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530690 o.seqNumber++
691 if o.seqNumber > 290 {
692 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
693 }
694 }
695 }
696}
697
Scott Bakerb90c4312020-03-12 21:33:25 -0700698// Create a TestResponse packet and send it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800699func (o *Onu) sendTestResult(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolob5913142021-03-19 16:10:18 -0700700 resp, err := omcilib.BuildTestResult(msg.OmciPkt.Data())
Scott Bakerb90c4312020-03-12 21:33:25 -0700701 if err != nil {
702 return err
703 }
704
705 var omciInd openolt.OmciIndication
706 omciInd.IntfId = o.PonPortID
707 omciInd.OnuId = o.ID
708 omciInd.Pkt = resp
709
710 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
711 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Scott Bakerb90c4312020-03-12 21:33:25 -0700712 return err
713 }
714 onuLogger.WithFields(log.Fields{
715 "IntfId": o.PonPortID,
716 "SerialNumber": o.Sn(),
717 "omciPacket": omciInd.Pkt,
718 }).Tracef("Sent TestResult OMCI message")
719
720 return nil
721}
722
Matteo Scandolof9d43412021-01-12 11:11:34 -0800723// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
724// and generate the appropriate response to it
725func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
726
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700727 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700728 "omciMsgType": msg.OmciMsg.MessageType,
729 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
730 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700731 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700732 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800733 }).Trace("omci-message-decoded")
734
735 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800736 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700737 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800738 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800739 onuLogger.WithFields(log.Fields{
740 "IntfId": o.PonPortID,
741 "OnuId": o.ID,
742 "SerialNumber": o.Sn(),
743 }).Debug("received-mib-reset-request-resetting-mds")
Matteo Scandolob5913142021-03-19 16:10:18 -0700744 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800745 o.MibDataSync = 0
746 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800747 case omci.MibUploadRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700748 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800749 case omci.MibUploadNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700750 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800751 case omci.GetRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700752 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId, o.CommittedImageEntityId)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800753 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800754 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700755 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800756 switch msgObj.EntityClass {
757 case me.PhysicalPathTerminationPointEthernetUniClassID:
758 // if we're Setting a PPTP state
759 // we need to send the appropriate alarm
760
761 if msgObj.EntityInstance == 257 {
762 // for now we're only caring about the first UNI
763 // NOTE that the EntityID for the UNI port is for now hardcoded in
764 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
765 // are reported during the MIB Upload sequence
766 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530767 raiseOMCIAlarm := false
768 if adminState == 1 {
769 raiseOMCIAlarm = true
770 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800771 msg := bbsim.Message{
772 Type: bbsim.UniStatusAlarm,
773 Data: bbsim.UniStatusAlarmMessage{
Himani Chawla13b1ee02021-03-15 01:43:53 +0530774 OnuSN: o.SerialNumber,
775 OnuID: o.ID,
776 AdminState: adminState,
777 EntityID: msgObj.EntityInstance,
778 RaiseOMCIAlarm: raiseOMCIAlarm,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800779 },
780 }
781 o.Channel <- msg
782 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800783 case me.TContClassID:
784 allocId := msgObj.Attributes["AllocId"].(uint16)
785
786 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
787 // otherwise we are adding it
788 if allocId == 255 || allocId == 65535 {
789 onuLogger.WithFields(log.Fields{
790 "IntfId": o.PonPortID,
791 "OnuId": o.ID,
792 "TContId": msgObj.EntityInstance,
793 "AllocId": allocId,
794 "SerialNumber": o.Sn(),
795 }).Trace("freeing-alloc-id-via-omci")
796 o.PonPort.removeAllocId(o.SerialNumber)
797 } else {
798 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
799 onuLogger.WithFields(log.Fields{
800 "IntfId": o.PonPortID,
801 "OnuId": o.ID,
802 "AllocId": allocId,
803 "SerialNumber": o.Sn(),
804 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
805 success = false
806 } else {
807 onuLogger.WithFields(log.Fields{
808 "IntfId": o.PonPortID,
809 "OnuId": o.ID,
810 "TContId": msgObj.EntityInstance,
811 "AllocId": allocId,
812 "SerialNumber": o.Sn(),
813 }).Trace("storing-alloc-id-via-omci")
814 o.PonPort.storeAllocId(allocId, o.SerialNumber)
815 }
816 }
817
818 }
819
820 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700821 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800822 o.MibDataSync++
823 }
824 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700825 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800826 }
827 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800828 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
829 var used bool
830 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700831 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800832 if err == nil {
833 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700834 // GemPort 4069 is reserved for multicast and shared across ONUs
835 if msgObj.EntityInstance != 4069 {
836 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
837 onuLogger.WithFields(log.Fields{
838 "IntfId": o.PonPortID,
839 "OnuId": o.ID,
840 "GemPortId": msgObj.EntityInstance,
841 "SerialNumber": o.Sn(),
842 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
843 } else {
844 onuLogger.WithFields(log.Fields{
845 "IntfId": o.PonPortID,
846 "OnuId": o.ID,
847 "GemPortId": msgObj.EntityInstance,
848 "SerialNumber": o.Sn(),
849 }).Trace("storing-gem-port-id-via-omci")
850 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
851 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800852 }
853 }
854 }
855
856 // if the gemPort is valid then increment the MDS and return a successful response
857 // otherwise fail the request
858 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
859 // validation this check will need to be rewritten
860 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700861 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800862 o.MibDataSync++
863 }
864 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700865 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800866 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800867 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700868 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800869 if err == nil {
870 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
871 onuLogger.WithFields(log.Fields{
872 "IntfId": o.PonPortID,
873 "OnuId": o.ID,
874 "GemPortId": msgObj.EntityInstance,
875 "SerialNumber": o.Sn(),
876 }).Trace("freeing-gem-port-id-via-omci")
877 o.PonPort.removeGemPort(msgObj.EntityInstance)
878 }
879 }
880
Matteo Scandolob5913142021-03-19 16:10:18 -0700881 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800882 o.MibDataSync++
883 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800884 case omci.RebootRequestType:
885
Matteo Scandolob5913142021-03-19 16:10:18 -0700886 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800887
888 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800889 // we run this in a separate goroutine so that
890 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800891 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800892 if err := o.Reboot(10 * time.Second); err != nil {
893 log.WithFields(log.Fields{
894 "IntfId": o.PonPortID,
895 "OnuId": o.ID,
896 "SerialNumber": o.Sn(),
897 "err": err,
898 }).Error("cannot-reboot-onu-after-omci-reboot-request")
899 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800900 }()
901 case omci.TestRequestType:
902
903 // Test message is special, it requires sending two packets:
904 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
905 // second packet, TestResult, reports the result of running the self-test
906 // TestResult can come some time after a TestResponse
907 // TODO: Implement some delay between the TestResponse and the TestResult
Matteo Scandolob5913142021-03-19 16:10:18 -0700908 isTest, err := omcilib.IsTestRequest(msg.OmciPkt.Data())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800909 if (err == nil) && (isTest) {
910 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
911 onuLogger.WithFields(log.Fields{
912 "IntfId": o.PonPortID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800913 "OnuId": o.ID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800914 "SerialNumber": o.Sn(),
Matteo Scandolob5913142021-03-19 16:10:18 -0700915 "omciPacket": msg.OmciPkt.Data(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800916 "msg": msg,
917 "err": sendErr,
918 }).Error("send-TestResult-indication-failed")
919 }
920 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800921 case omci.SynchronizeTimeRequestType:
922 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700923 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800924 case omci.StartSoftwareDownloadRequestType:
925
926 o.ImageSoftwareReceivedSections = 0
927
Matteo Scandolob5913142021-03-19 16:10:18 -0700928 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800929
Matteo Scandolob5913142021-03-19 16:10:18 -0700930 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800931 o.MibDataSync++
932 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
933 onuLogger.WithFields(log.Fields{
934 "OnuId": o.ID,
935 "IntfId": o.PonPortID,
936 "OnuSn": o.Sn(),
937 "Err": err.Error(),
938 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
939 }
940 } else {
941 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700942 "OmciMsgType": msg.OmciMsg.MessageType,
943 "TransCorrId": msg.OmciMsg.TransactionID,
944 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800945 "IntfId": o.PonPortID,
946 "SerialNumber": o.Sn(),
947 }).Error("error-while-processing-start-software-download-request")
948 }
949 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700950 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800951 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700952 "OmciMsgType": msg.OmciMsg.MessageType,
953 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800954 "EntityInstance": msgObj.EntityInstance,
955 "SectionNumber": msgObj.SectionNumber,
956 "SectionData": msgObj.SectionData,
957 }).Trace("received-download-section-request")
958 o.ImageSoftwareReceivedSections++
959 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
960 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
961 onuLogger.WithFields(log.Fields{
962 "OnuId": o.ID,
963 "IntfId": o.PonPortID,
964 "OnuSn": o.Sn(),
965 "Err": err.Error(),
966 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
967 }
968 }
969 }
970 case omci.DownloadSectionRequestWithResponseType:
971 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -0700972 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
973 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800974 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700975 "OmciMsgType": msg.OmciMsg.MessageType,
976 "TransCorrId": msg.OmciMsg.TransactionID,
977 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800978 "IntfId": o.PonPortID,
979 "SerialNumber": o.Sn(),
980 }).Error("error-while-processing-create-download-section-response")
981 return
982 }
983 o.ImageSoftwareReceivedSections++
984
985 case omci.EndSoftwareDownloadRequestType:
986
987 // In the startSoftwareDownload we get the image size and the window size.
988 // We calculate how many DownloadSection we should receive and validate
989 // that we got the correct amount when we receive this message
990 success := true
991 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
992 onuLogger.WithFields(log.Fields{
993 "OnuId": o.ID,
994 "IntfId": o.PonPortID,
995 "OnuSn": o.Sn(),
996 "ExpectedSections": o.ImageSoftwareExpectedSections,
997 "ReceivedSections": o.ImageSoftwareReceivedSections,
998 }).Errorf("onu-did-not-receive-all-image-sections")
999 success = false
1000 }
1001
1002 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001003 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001004 o.MibDataSync++
1005 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1006 onuLogger.WithFields(log.Fields{
1007 "OnuId": o.ID,
1008 "IntfId": o.PonPortID,
1009 "OnuSn": o.Sn(),
1010 "Err": err.Error(),
1011 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1012 }
1013 } else {
1014 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001015 "OmciMsgType": msg.OmciMsg.MessageType,
1016 "TransCorrId": msg.OmciMsg.TransactionID,
1017 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001018 "IntfId": o.PonPortID,
1019 "SerialNumber": o.Sn(),
1020 }).Error("error-while-processing-end-software-download-request")
1021 }
1022 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001023 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001024 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1025 onuLogger.WithFields(log.Fields{
1026 "OnuId": o.ID,
1027 "IntfId": o.PonPortID,
1028 "OnuSn": o.Sn(),
1029 "Err": err.Error(),
1030 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1031 }
1032 }
1033 }
1034
1035 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001036 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001037 o.MibDataSync++
1038 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1039 onuLogger.WithFields(log.Fields{
1040 "OnuId": o.ID,
1041 "IntfId": o.PonPortID,
1042 "OnuSn": o.Sn(),
1043 "Err": err.Error(),
1044 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1045 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001046 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001047 o.ActiveImageEntityId = msgObj.EntityInstance
1048 } else {
1049 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1050 }
1051 onuLogger.WithFields(log.Fields{
1052 "OnuId": o.ID,
1053 "IntfId": o.PonPortID,
1054 "OnuSn": o.Sn(),
1055 "ActiveImageEntityId": o.ActiveImageEntityId,
1056 "CommittedImageEntityId": o.CommittedImageEntityId,
1057 }).Info("onu-software-image-activated")
1058
1059 // powercycle the ONU
1060 // we run this in a separate goroutine so that
1061 // the ActivateSoftwareResponse is sent to VOLTHA
1062 // NOTE do we need to wait before rebooting?
1063 go func() {
1064 if err := o.Reboot(10 * time.Second); err != nil {
1065 log.WithFields(log.Fields{
1066 "IntfId": o.PonPortID,
1067 "OnuId": o.ID,
1068 "SerialNumber": o.Sn(),
1069 "err": err,
1070 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1071 }
1072 }()
1073 }
1074 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001075 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001076 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001077 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001078 // TODO validate that the image to commit is:
1079 // - active
1080 // - not already committed
1081 o.CommittedImageEntityId = msgObj.EntityInstance
1082 } else {
1083 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1084 }
1085 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1086 onuLogger.WithFields(log.Fields{
1087 "OnuId": o.ID,
1088 "IntfId": o.PonPortID,
1089 "OnuSn": o.Sn(),
1090 "Err": err.Error(),
1091 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1092 }
1093 onuLogger.WithFields(log.Fields{
1094 "OnuId": o.ID,
1095 "IntfId": o.PonPortID,
1096 "OnuSn": o.Sn(),
1097 "ActiveImageEntityId": o.ActiveImageEntityId,
1098 "CommittedImageEntityId": o.CommittedImageEntityId,
1099 }).Info("onu-software-image-committed")
1100 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301101 case omci.GetAllAlarmsRequestType:
1102 // Reset the alarm sequence number on receiving get all alarms request.
1103 o.onuAlarmsInfoLock.Lock()
1104 for key, alarmInfo := range o.onuAlarmsInfo {
1105 // reset the alarm sequence no
1106 alarmInfo.SequenceNo = 0
1107 o.onuAlarmsInfo[key] = alarmInfo
1108 }
1109 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001110 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301111 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001112 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301113 responsePkt = nil //Do not send any response for error case
1114 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001115 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001116 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001117 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1118 "omciPkt": msg.OmciPkt,
1119 "omciMsgType": msg.OmciMsg.MessageType,
1120 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001121 "IntfId": o.PonPortID,
1122 "SerialNumber": o.Sn(),
1123 }).Warnf("OMCI-message-not-supported")
1124 }
1125
1126 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001127 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001128 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001129 "IntfId": o.PonPortID,
1130 "SerialNumber": o.Sn(),
1131 "omciPacket": responsePkt,
1132 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1133 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001134 }).Errorf("failed-to-send-omci-message: %v", err)
1135 }
1136 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001137
Pragya Arya324337e2020-02-20 14:35:08 +05301138 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -08001139}
Pragya Arya324337e2020-02-20 14:35:08 +05301140
Matteo Scandolof9d43412021-01-12 11:11:34 -08001141// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1142func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1143 indication := &openolt.Indication_OmciInd{
1144 OmciInd: &openolt.OmciIndication{
1145 IntfId: o.PonPortID,
1146 OnuId: o.ID,
1147 Pkt: responsePkt,
1148 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001149 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001150 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1151 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001152 }
1153 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001154 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001155 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001156 "omciPacket": indication.OmciInd.Pkt,
1157 "transCorrId": txId,
1158 }).Trace("omci-message-sent")
1159 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001160}
1161
Matteo Scandolo27428702019-10-11 16:21:16 -07001162func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001163 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001164 // we need to add support for multiple UNIs
1165 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001166 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001167 // - change the library so that it reports a single UNI and remove this workaroung
1168 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001169 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001170 onuLogger.WithFields(log.Fields{
1171 "IntfId": o.PonPortID,
1172 "OnuId": o.ID,
1173 "SerialNumber": o.Sn(),
1174 "OnuPortNo": o.PortNo,
1175 "FlowPortNo": portNo,
1176 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001177 o.PortNo = portNo
1178 }
1179}
1180
William Kurkian0418bc82019-11-06 12:16:24 -05001181func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001182 onuLogger.WithFields(log.Fields{
1183 "IntfId": o.PonPortID,
1184 "OnuId": id,
1185 "SerialNumber": o.Sn(),
1186 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001187 o.ID = id
1188}
1189
Matteo Scandolof9d43412021-01-12 11:11:34 -08001190func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001191 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001192 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001193 "Cookie": msg.Flow.Cookie,
1194 "DstPort": msg.Flow.Classifier.DstPort,
1195 "FlowId": msg.Flow.FlowId,
1196 "FlowType": msg.Flow.FlowType,
1197 "GemportId": msg.Flow.GemportId,
1198 "InnerVlan": msg.Flow.Classifier.IVid,
1199 "IntfId": msg.Flow.AccessIntfId,
1200 "IpProto": msg.Flow.Classifier.IpProto,
1201 "OnuId": msg.Flow.OnuId,
1202 "OnuSn": o.Sn(),
1203 "OuterVlan": msg.Flow.Classifier.OVid,
1204 "PortNo": msg.Flow.PortNo,
1205 "SrcPort": msg.Flow.Classifier.SrcPort,
1206 "UniID": msg.Flow.UniId,
1207 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1208 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1209 "ClassifierIVid": msg.Flow.Classifier.IVid,
1210 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001211 "ReplicateFlow": msg.Flow.ReplicateFlow,
1212 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001213 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001214
Matteo Scandolo813402b2019-10-23 19:24:52 -07001215 if msg.Flow.UniId != 0 {
1216 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1217 onuLogger.WithFields(log.Fields{
1218 "IntfId": o.PonPortID,
1219 "OnuId": o.ID,
1220 "SerialNumber": o.Sn(),
1221 }).Debug("Ignoring flow as it's not for the first UNI")
1222 return
1223 }
1224
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001225 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001226
1227 var gemPortId uint32
1228 if msg.Flow.ReplicateFlow {
1229 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1230 // first available gemport (we only need to send one packet)
1231 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1232 gemPortId = msg.Flow.PbitToGemport[0]
1233 } else {
1234 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1235 gemPortId = uint32(msg.Flow.GemportId)
1236 }
1237 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001238
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001239 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001240 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001241 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001242
1243 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001244 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001245 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001246 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1247 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001248 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001249
Matteo Scandolo4a036262020-08-17 15:56:13 -07001250 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001251 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001252 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001253 }
1254}
1255
Matteo Scandolof9d43412021-01-12 11:11:34 -08001256func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001257 onuLogger.WithFields(log.Fields{
1258 "IntfId": o.PonPortID,
1259 "OnuId": o.ID,
1260 "SerialNumber": o.Sn(),
1261 "FlowId": msg.Flow.FlowId,
1262 "FlowType": msg.Flow.FlowType,
1263 }).Debug("ONU receives FlowRemove")
1264
1265 for idx, flow := range o.FlowIds {
1266 // If the gemport is found, delete it from local cache.
1267 if flow == msg.Flow.FlowId {
1268 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1269 break
1270 }
1271 }
1272
1273 if len(o.FlowIds) == 0 {
1274 onuLogger.WithFields(log.Fields{
1275 "IntfId": o.PonPortID,
1276 "OnuId": o.ID,
1277 "SerialNumber": o.Sn(),
1278 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001279
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301280 // check if ONU delete is performed and
1281 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001282 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301283 close(o.Channel)
1284 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001285 }
1286}
1287
Matteo Scandolocedde462021-03-09 17:37:16 -08001288func (o *Onu) Reboot(timeout time.Duration) error {
1289 onuLogger.WithFields(log.Fields{
1290 "IntfId": o.PonPortID,
1291 "OnuId": o.ID,
1292 "SerialNumber": o.Sn(),
1293 }).Debug("shutting-down-onu")
1294 if err := o.HandleShutdownONU(); err != nil {
1295 return err
1296 }
1297 time.Sleep(timeout)
1298 onuLogger.WithFields(log.Fields{
1299 "IntfId": o.PonPortID,
1300 "OnuId": o.ID,
1301 "SerialNumber": o.Sn(),
1302 }).Debug("power-on-onu")
1303 if err := o.HandlePowerOnONU(); err != nil {
1304 return err
1305 }
1306 return nil
1307}
1308
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001309// BBR methods
1310
1311func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1312 omciMsg := openolt.OmciMsg{
1313 IntfId: intfId,
1314 OnuId: onuId,
1315 Pkt: pktBytes,
1316 }
1317
1318 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1319 log.WithFields(log.Fields{
1320 "IntfId": intfId,
1321 "OnuId": onuId,
1322 "SerialNumber": common.OnuSnToString(sn),
1323 "Pkt": omciMsg.Pkt,
1324 }).Fatalf("Failed to send MIB Reset")
1325 }
1326 log.WithFields(log.Fields{
1327 "IntfId": intfId,
1328 "OnuId": onuId,
1329 "SerialNumber": common.OnuSnToString(sn),
1330 "Pkt": omciMsg.Pkt,
1331 }).Tracef("Sent OMCI message %s", msgType)
1332}
1333
1334func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1335 var next uint16
1336 if len(highPriority) > 0 && highPriority[0] {
1337 next = onu.hpTid
1338 onu.hpTid += 1
1339 if onu.hpTid < 0x8000 {
1340 onu.hpTid = 0x8000
1341 }
1342 } else {
1343 next = onu.tid
1344 onu.tid += 1
1345 if onu.tid >= 0x8000 {
1346 onu.tid = 1
1347 }
1348 }
1349 return next
1350}
1351
1352// TODO move this method in responders/omcisim
1353func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1354 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1355 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1356}
1357
Matteo Scandolof9d43412021-01-12 11:11:34 -08001358// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1359func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1360
1361 // we need to encode the packet in HEX
1362 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1363 hex.Encode(pkt, msg.OmciInd.Pkt)
1364 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1365 if err != nil {
1366 log.WithFields(log.Fields{
1367 "IntfId": o.PonPortID,
1368 "SerialNumber": o.Sn(),
1369 "omciPacket": msg.OmciInd.Pkt,
1370 }).Error("BBR Cannot parse OMCI packet")
1371 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001372
1373 log.WithFields(log.Fields{
1374 "IntfId": msg.OmciInd.IntfId,
1375 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001376 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001377 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001378 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301379 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001380 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001381 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001382 log.WithFields(log.Fields{
1383 "IntfId": msg.OmciInd.IntfId,
1384 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001385 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001386 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001387 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001388 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001389 case omci.MibResetResponseType:
1390 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1391 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1392 case omci.MibUploadResponseType:
1393 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1394 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1395 case omci.MibUploadNextResponseType:
1396 o.seqNumber++
1397
1398 if o.seqNumber > 290 {
1399 // 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 -08001400 // 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 -08001401 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001402 onuLogger.WithFields(log.Fields{
1403 "OnuId": o.ID,
1404 "IntfId": o.PonPortID,
1405 "OnuSn": o.Sn(),
1406 }).Errorf("Error while transitioning ONU State %v", err)
1407 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001408 } else {
1409 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1410 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001411 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001412 }
1413}
1414
1415func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1416
1417 classifierProto := openolt.Classifier{
1418 EthType: uint32(layers.EthernetTypeEAPOL),
1419 OVid: 4091,
1420 }
1421
1422 actionProto := openolt.Action{}
1423
1424 downstreamFlow := openolt.Flow{
1425 AccessIntfId: int32(o.PonPortID),
1426 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001427 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001428 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001429 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001430 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001431 Classifier: &classifierProto,
1432 Action: &actionProto,
1433 Priority: int32(100),
1434 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001435 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1436 // AllocId and GemPorts need to be unique per PON
1437 // for now use the ONU-ID, will need to change once we support multiple UNIs
1438 AllocId: int32(o.ID),
1439 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001440 }
1441
1442 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1443 log.WithFields(log.Fields{
1444 "IntfId": o.PonPortID,
1445 "OnuId": o.ID,
1446 "FlowId": downstreamFlow.FlowId,
1447 "PortNo": downstreamFlow.PortNo,
1448 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001449 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001450 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001451 }
1452 log.WithFields(log.Fields{
1453 "IntfId": o.PonPortID,
1454 "OnuId": o.ID,
1455 "FlowId": downstreamFlow.FlowId,
1456 "PortNo": downstreamFlow.PortNo,
1457 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1458 }).Info("Sent EAPOL Flow")
1459}
1460
1461func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001462
1463 // BBR only works with a single service (ATT HSIA)
1464 hsia := o.Services[0].(*Service)
1465
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001466 classifierProto := openolt.Classifier{
1467 EthType: uint32(layers.EthernetTypeIPv4),
1468 SrcPort: uint32(68),
1469 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001470 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001471 }
1472
1473 actionProto := openolt.Action{}
1474
1475 downstreamFlow := openolt.Flow{
1476 AccessIntfId: int32(o.PonPortID),
1477 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001478 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001479 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001480 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001481 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001482 Classifier: &classifierProto,
1483 Action: &actionProto,
1484 Priority: int32(100),
1485 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001486 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1487 // AllocId and GemPorts need to be unique per PON
1488 // for now use the ONU-ID, will need to change once we support multiple UNIs
1489 AllocId: int32(o.ID),
1490 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001491 }
1492
1493 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1494 log.WithFields(log.Fields{
1495 "IntfId": o.PonPortID,
1496 "OnuId": o.ID,
1497 "FlowId": downstreamFlow.FlowId,
1498 "PortNo": downstreamFlow.PortNo,
1499 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001500 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001501 }).Fatalf("Failed to send DHCP Flow")
1502 }
1503 log.WithFields(log.Fields{
1504 "IntfId": o.PonPortID,
1505 "OnuId": o.ID,
1506 "FlowId": downstreamFlow.FlowId,
1507 "PortNo": downstreamFlow.PortNo,
1508 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1509 }).Info("Sent DHCP Flow")
1510}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301511
1512// DeleteFlow method search and delete flowKey from the onu flows slice
1513func (onu *Onu) DeleteFlow(key FlowKey) {
1514 for pos, flowKey := range onu.Flows {
1515 if flowKey == key {
1516 // delete the flowKey by shifting all flowKeys by one
1517 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1518 t := make([]FlowKey, len(onu.Flows))
1519 copy(t, onu.Flows)
1520 onu.Flows = t
1521 break
1522 }
1523 }
1524}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301525
1526func (onu *Onu) ReDiscoverOnu() {
1527 // Wait for few seconds to be sure of the cleanup
1528 time.Sleep(5 * time.Second)
1529
1530 onuLogger.WithFields(log.Fields{
1531 "IntfId": onu.PonPortID,
1532 "OnuId": onu.ID,
1533 "OnuSn": onu.Sn(),
1534 }).Debug("Send ONU Re-Discovery")
1535
1536 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001537 if err := onu.InternalState.Event(OnuTxInitialize); 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", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301543 }
1544
Matteo Scandolocedde462021-03-09 17:37:16 -08001545 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301546 log.WithFields(log.Fields{
1547 "IntfId": onu.PonPortID,
1548 "OnuSn": onu.Sn(),
1549 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001550 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301551 }
1552}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001553
1554func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1555 for _, s := range onu.Services {
1556 if service, ok := s.(*Service); ok {
1557 // EAPOL is a strange case, as packets are untagged
1558 // but we assume we will have a single service requiring EAPOL
1559 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1560 service.GemPort = gemport
1561 }
1562
1563 // For DHCP services we single tag the outgoing packets,
1564 // thus the flow only contains the CTag and we can use that to match the service
1565 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1566 service.GemPort = gemport
1567 }
1568
1569 // for dataplane services match both C and S tags
1570 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1571 service.GemPort = gemport
1572 }
1573 }
1574 }
1575}
1576
1577func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1578 for _, s := range onu.Services {
1579 service := s.(*Service)
1580 if service.HwAddress.String() == macAddress.String() {
1581 return service, nil
1582 }
1583 }
1584 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1585}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301586
1587func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1588 switch alarmType {
1589 case "ONU_ALARM_LOS":
1590 msg := bbsim.Message{
1591 Type: bbsim.UniStatusAlarm,
1592 Data: bbsim.UniStatusAlarmMessage{
1593 OnuSN: o.SerialNumber,
1594 OnuID: o.ID,
1595 EntityID: 257,
1596 RaiseOMCIAlarm: raiseOMCIAlarm,
1597 },
1598 }
1599 o.Channel <- msg
1600 }
1601
1602}
1603
1604func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1605 o.onuAlarmsInfoLock.Lock()
1606 defer o.onuAlarmsInfoLock.Unlock()
1607 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1608 if alarmInfo.SequenceNo == 255 {
1609 alarmInfo.SequenceNo = 1
1610 } else {
1611 alarmInfo.SequenceNo++
1612 }
1613 o.onuAlarmsInfo[key] = alarmInfo
1614 return alarmInfo.SequenceNo
1615 } else {
1616 // This is the first time alarm notification message is being sent
1617 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1618 SequenceNo: 1,
1619 }
1620 return 1
1621 }
1622}