blob: 466f383be91364b1248e111680b42802928cb8a1 [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:
Girish Gowdra996d81e2021-04-21 16:16:27 -0700752 onuDown := o.OperState.Current() == "down"
753 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId, o.CommittedImageEntityId, onuDown)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800754 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800755 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700756 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800757 switch msgObj.EntityClass {
758 case me.PhysicalPathTerminationPointEthernetUniClassID:
759 // if we're Setting a PPTP state
760 // we need to send the appropriate alarm
761
762 if msgObj.EntityInstance == 257 {
763 // for now we're only caring about the first UNI
764 // NOTE that the EntityID for the UNI port is for now hardcoded in
765 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
766 // are reported during the MIB Upload sequence
767 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530768 raiseOMCIAlarm := false
769 if adminState == 1 {
770 raiseOMCIAlarm = true
Girish Gowdra996d81e2021-04-21 16:16:27 -0700771 // set the OperState to disabled
772 if err := o.OperState.Event(OnuTxDisable); err != nil {
773 onuLogger.WithFields(log.Fields{
774 "OnuId": o.ID,
775 "IntfId": o.PonPortID,
776 "OnuSn": o.Sn(),
777 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
778 }
779 } else {
780 // set the OperState to enabled
781 if err := o.OperState.Event(OnuTxEnable); err != nil {
782 onuLogger.WithFields(log.Fields{
783 "OnuId": o.ID,
784 "IntfId": o.PonPortID,
785 "OnuSn": o.Sn(),
786 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
787 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530788 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800789 msg := bbsim.Message{
790 Type: bbsim.UniStatusAlarm,
791 Data: bbsim.UniStatusAlarmMessage{
Himani Chawla13b1ee02021-03-15 01:43:53 +0530792 OnuSN: o.SerialNumber,
793 OnuID: o.ID,
794 AdminState: adminState,
795 EntityID: msgObj.EntityInstance,
796 RaiseOMCIAlarm: raiseOMCIAlarm,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800797 },
798 }
799 o.Channel <- msg
800 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800801 case me.TContClassID:
802 allocId := msgObj.Attributes["AllocId"].(uint16)
803
804 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
805 // otherwise we are adding it
806 if allocId == 255 || allocId == 65535 {
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("freeing-alloc-id-via-omci")
814 o.PonPort.removeAllocId(o.SerialNumber)
815 } else {
816 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
817 onuLogger.WithFields(log.Fields{
818 "IntfId": o.PonPortID,
819 "OnuId": o.ID,
820 "AllocId": allocId,
821 "SerialNumber": o.Sn(),
822 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
823 success = false
824 } else {
825 onuLogger.WithFields(log.Fields{
826 "IntfId": o.PonPortID,
827 "OnuId": o.ID,
828 "TContId": msgObj.EntityInstance,
829 "AllocId": allocId,
830 "SerialNumber": o.Sn(),
831 }).Trace("storing-alloc-id-via-omci")
832 o.PonPort.storeAllocId(allocId, o.SerialNumber)
833 }
834 }
835
836 }
837
838 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700839 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800840 o.MibDataSync++
841 }
842 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700843 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800844 }
845 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800846 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
847 var used bool
848 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700849 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800850 if err == nil {
851 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700852 // GemPort 4069 is reserved for multicast and shared across ONUs
853 if msgObj.EntityInstance != 4069 {
854 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
855 onuLogger.WithFields(log.Fields{
856 "IntfId": o.PonPortID,
857 "OnuId": o.ID,
858 "GemPortId": msgObj.EntityInstance,
859 "SerialNumber": o.Sn(),
860 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
861 } else {
862 onuLogger.WithFields(log.Fields{
863 "IntfId": o.PonPortID,
864 "OnuId": o.ID,
865 "GemPortId": msgObj.EntityInstance,
866 "SerialNumber": o.Sn(),
867 }).Trace("storing-gem-port-id-via-omci")
868 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
869 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800870 }
871 }
872 }
873
874 // if the gemPort is valid then increment the MDS and return a successful response
875 // otherwise fail the request
876 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
877 // validation this check will need to be rewritten
878 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700879 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800880 o.MibDataSync++
881 }
882 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700883 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800884 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800885 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700886 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800887 if err == nil {
888 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
889 onuLogger.WithFields(log.Fields{
890 "IntfId": o.PonPortID,
891 "OnuId": o.ID,
892 "GemPortId": msgObj.EntityInstance,
893 "SerialNumber": o.Sn(),
894 }).Trace("freeing-gem-port-id-via-omci")
895 o.PonPort.removeGemPort(msgObj.EntityInstance)
896 }
897 }
898
Matteo Scandolob5913142021-03-19 16:10:18 -0700899 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800900 o.MibDataSync++
901 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800902 case omci.RebootRequestType:
903
Matteo Scandolob5913142021-03-19 16:10:18 -0700904 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800905
906 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800907 // we run this in a separate goroutine so that
908 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800909 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800910 if err := o.Reboot(10 * time.Second); err != nil {
911 log.WithFields(log.Fields{
912 "IntfId": o.PonPortID,
913 "OnuId": o.ID,
914 "SerialNumber": o.Sn(),
915 "err": err,
916 }).Error("cannot-reboot-onu-after-omci-reboot-request")
917 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800918 }()
919 case omci.TestRequestType:
920
921 // Test message is special, it requires sending two packets:
922 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
923 // second packet, TestResult, reports the result of running the self-test
924 // TestResult can come some time after a TestResponse
925 // TODO: Implement some delay between the TestResponse and the TestResult
Matteo Scandolob5913142021-03-19 16:10:18 -0700926 isTest, err := omcilib.IsTestRequest(msg.OmciPkt.Data())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800927 if (err == nil) && (isTest) {
928 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
929 onuLogger.WithFields(log.Fields{
930 "IntfId": o.PonPortID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800931 "OnuId": o.ID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800932 "SerialNumber": o.Sn(),
Matteo Scandolob5913142021-03-19 16:10:18 -0700933 "omciPacket": msg.OmciPkt.Data(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800934 "msg": msg,
935 "err": sendErr,
936 }).Error("send-TestResult-indication-failed")
937 }
938 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800939 case omci.SynchronizeTimeRequestType:
940 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700941 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800942 case omci.StartSoftwareDownloadRequestType:
943
944 o.ImageSoftwareReceivedSections = 0
945
Matteo Scandolob5913142021-03-19 16:10:18 -0700946 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800947
Matteo Scandolob5913142021-03-19 16:10:18 -0700948 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800949 o.MibDataSync++
950 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
951 onuLogger.WithFields(log.Fields{
952 "OnuId": o.ID,
953 "IntfId": o.PonPortID,
954 "OnuSn": o.Sn(),
955 "Err": err.Error(),
956 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
957 }
958 } else {
959 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700960 "OmciMsgType": msg.OmciMsg.MessageType,
961 "TransCorrId": msg.OmciMsg.TransactionID,
962 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800963 "IntfId": o.PonPortID,
964 "SerialNumber": o.Sn(),
965 }).Error("error-while-processing-start-software-download-request")
966 }
967 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700968 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800969 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700970 "OmciMsgType": msg.OmciMsg.MessageType,
971 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800972 "EntityInstance": msgObj.EntityInstance,
973 "SectionNumber": msgObj.SectionNumber,
974 "SectionData": msgObj.SectionData,
975 }).Trace("received-download-section-request")
976 o.ImageSoftwareReceivedSections++
977 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
978 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
979 onuLogger.WithFields(log.Fields{
980 "OnuId": o.ID,
981 "IntfId": o.PonPortID,
982 "OnuSn": o.Sn(),
983 "Err": err.Error(),
984 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
985 }
986 }
987 }
988 case omci.DownloadSectionRequestWithResponseType:
989 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -0700990 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
991 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800992 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700993 "OmciMsgType": msg.OmciMsg.MessageType,
994 "TransCorrId": msg.OmciMsg.TransactionID,
995 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800996 "IntfId": o.PonPortID,
997 "SerialNumber": o.Sn(),
998 }).Error("error-while-processing-create-download-section-response")
999 return
1000 }
1001 o.ImageSoftwareReceivedSections++
1002
1003 case omci.EndSoftwareDownloadRequestType:
1004
1005 // In the startSoftwareDownload we get the image size and the window size.
1006 // We calculate how many DownloadSection we should receive and validate
1007 // that we got the correct amount when we receive this message
1008 success := true
1009 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
1010 onuLogger.WithFields(log.Fields{
1011 "OnuId": o.ID,
1012 "IntfId": o.PonPortID,
1013 "OnuSn": o.Sn(),
1014 "ExpectedSections": o.ImageSoftwareExpectedSections,
1015 "ReceivedSections": o.ImageSoftwareReceivedSections,
1016 }).Errorf("onu-did-not-receive-all-image-sections")
1017 success = false
1018 }
1019
1020 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001021 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001022 o.MibDataSync++
1023 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1024 onuLogger.WithFields(log.Fields{
1025 "OnuId": o.ID,
1026 "IntfId": o.PonPortID,
1027 "OnuSn": o.Sn(),
1028 "Err": err.Error(),
1029 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1030 }
1031 } else {
1032 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001033 "OmciMsgType": msg.OmciMsg.MessageType,
1034 "TransCorrId": msg.OmciMsg.TransactionID,
1035 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001036 "IntfId": o.PonPortID,
1037 "SerialNumber": o.Sn(),
1038 }).Error("error-while-processing-end-software-download-request")
1039 }
1040 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001041 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001042 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1043 onuLogger.WithFields(log.Fields{
1044 "OnuId": o.ID,
1045 "IntfId": o.PonPortID,
1046 "OnuSn": o.Sn(),
1047 "Err": err.Error(),
1048 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1049 }
1050 }
1051 }
1052
1053 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001054 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001055 o.MibDataSync++
1056 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1057 onuLogger.WithFields(log.Fields{
1058 "OnuId": o.ID,
1059 "IntfId": o.PonPortID,
1060 "OnuSn": o.Sn(),
1061 "Err": err.Error(),
1062 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1063 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001064 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001065 o.ActiveImageEntityId = msgObj.EntityInstance
1066 } else {
1067 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1068 }
1069 onuLogger.WithFields(log.Fields{
1070 "OnuId": o.ID,
1071 "IntfId": o.PonPortID,
1072 "OnuSn": o.Sn(),
1073 "ActiveImageEntityId": o.ActiveImageEntityId,
1074 "CommittedImageEntityId": o.CommittedImageEntityId,
1075 }).Info("onu-software-image-activated")
1076
1077 // powercycle the ONU
1078 // we run this in a separate goroutine so that
1079 // the ActivateSoftwareResponse is sent to VOLTHA
1080 // NOTE do we need to wait before rebooting?
1081 go func() {
1082 if err := o.Reboot(10 * time.Second); err != nil {
1083 log.WithFields(log.Fields{
1084 "IntfId": o.PonPortID,
1085 "OnuId": o.ID,
1086 "SerialNumber": o.Sn(),
1087 "err": err,
1088 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1089 }
1090 }()
1091 }
1092 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001093 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001094 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001095 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001096 // TODO validate that the image to commit is:
1097 // - active
1098 // - not already committed
1099 o.CommittedImageEntityId = msgObj.EntityInstance
1100 } else {
1101 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1102 }
1103 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1104 onuLogger.WithFields(log.Fields{
1105 "OnuId": o.ID,
1106 "IntfId": o.PonPortID,
1107 "OnuSn": o.Sn(),
1108 "Err": err.Error(),
1109 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1110 }
1111 onuLogger.WithFields(log.Fields{
1112 "OnuId": o.ID,
1113 "IntfId": o.PonPortID,
1114 "OnuSn": o.Sn(),
1115 "ActiveImageEntityId": o.ActiveImageEntityId,
1116 "CommittedImageEntityId": o.CommittedImageEntityId,
1117 }).Info("onu-software-image-committed")
1118 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301119 case omci.GetAllAlarmsRequestType:
1120 // Reset the alarm sequence number on receiving get all alarms request.
1121 o.onuAlarmsInfoLock.Lock()
1122 for key, alarmInfo := range o.onuAlarmsInfo {
1123 // reset the alarm sequence no
1124 alarmInfo.SequenceNo = 0
1125 o.onuAlarmsInfo[key] = alarmInfo
1126 }
1127 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001128 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301129 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001130 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301131 responsePkt = nil //Do not send any response for error case
1132 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001133 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001134 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001135 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1136 "omciPkt": msg.OmciPkt,
1137 "omciMsgType": msg.OmciMsg.MessageType,
1138 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001139 "IntfId": o.PonPortID,
1140 "SerialNumber": o.Sn(),
1141 }).Warnf("OMCI-message-not-supported")
1142 }
1143
1144 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001145 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001146 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001147 "IntfId": o.PonPortID,
1148 "SerialNumber": o.Sn(),
1149 "omciPacket": responsePkt,
1150 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1151 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001152 }).Errorf("failed-to-send-omci-message: %v", err)
1153 }
1154 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001155
Pragya Arya324337e2020-02-20 14:35:08 +05301156 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -08001157}
Pragya Arya324337e2020-02-20 14:35:08 +05301158
Matteo Scandolof9d43412021-01-12 11:11:34 -08001159// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1160func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1161 indication := &openolt.Indication_OmciInd{
1162 OmciInd: &openolt.OmciIndication{
1163 IntfId: o.PonPortID,
1164 OnuId: o.ID,
1165 Pkt: responsePkt,
1166 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001167 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001168 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1169 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001170 }
1171 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001172 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001173 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001174 "omciPacket": indication.OmciInd.Pkt,
1175 "transCorrId": txId,
1176 }).Trace("omci-message-sent")
1177 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001178}
1179
Matteo Scandolo27428702019-10-11 16:21:16 -07001180func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001181 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001182 // we need to add support for multiple UNIs
1183 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001184 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001185 // - change the library so that it reports a single UNI and remove this workaroung
1186 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001187 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001188 onuLogger.WithFields(log.Fields{
1189 "IntfId": o.PonPortID,
1190 "OnuId": o.ID,
1191 "SerialNumber": o.Sn(),
1192 "OnuPortNo": o.PortNo,
1193 "FlowPortNo": portNo,
1194 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001195 o.PortNo = portNo
1196 }
1197}
1198
William Kurkian0418bc82019-11-06 12:16:24 -05001199func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001200 onuLogger.WithFields(log.Fields{
1201 "IntfId": o.PonPortID,
1202 "OnuId": id,
1203 "SerialNumber": o.Sn(),
1204 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001205 o.ID = id
1206}
1207
Matteo Scandolof9d43412021-01-12 11:11:34 -08001208func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001209 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001210 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001211 "Cookie": msg.Flow.Cookie,
1212 "DstPort": msg.Flow.Classifier.DstPort,
1213 "FlowId": msg.Flow.FlowId,
1214 "FlowType": msg.Flow.FlowType,
1215 "GemportId": msg.Flow.GemportId,
1216 "InnerVlan": msg.Flow.Classifier.IVid,
1217 "IntfId": msg.Flow.AccessIntfId,
1218 "IpProto": msg.Flow.Classifier.IpProto,
1219 "OnuId": msg.Flow.OnuId,
1220 "OnuSn": o.Sn(),
1221 "OuterVlan": msg.Flow.Classifier.OVid,
1222 "PortNo": msg.Flow.PortNo,
1223 "SrcPort": msg.Flow.Classifier.SrcPort,
1224 "UniID": msg.Flow.UniId,
1225 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1226 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1227 "ClassifierIVid": msg.Flow.Classifier.IVid,
1228 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001229 "ReplicateFlow": msg.Flow.ReplicateFlow,
1230 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001231 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001232
Matteo Scandolo813402b2019-10-23 19:24:52 -07001233 if msg.Flow.UniId != 0 {
1234 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1235 onuLogger.WithFields(log.Fields{
1236 "IntfId": o.PonPortID,
1237 "OnuId": o.ID,
1238 "SerialNumber": o.Sn(),
1239 }).Debug("Ignoring flow as it's not for the first UNI")
1240 return
1241 }
1242
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001243 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001244
1245 var gemPortId uint32
1246 if msg.Flow.ReplicateFlow {
1247 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1248 // first available gemport (we only need to send one packet)
1249 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1250 gemPortId = msg.Flow.PbitToGemport[0]
1251 } else {
1252 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1253 gemPortId = uint32(msg.Flow.GemportId)
1254 }
1255 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001256
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001257 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001258 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001259 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001260
1261 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001262 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001263 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001264 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1265 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001266 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001267
Matteo Scandolo4a036262020-08-17 15:56:13 -07001268 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001269 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001270 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001271 }
1272}
1273
Matteo Scandolof9d43412021-01-12 11:11:34 -08001274func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001275 onuLogger.WithFields(log.Fields{
1276 "IntfId": o.PonPortID,
1277 "OnuId": o.ID,
1278 "SerialNumber": o.Sn(),
1279 "FlowId": msg.Flow.FlowId,
1280 "FlowType": msg.Flow.FlowType,
1281 }).Debug("ONU receives FlowRemove")
1282
1283 for idx, flow := range o.FlowIds {
1284 // If the gemport is found, delete it from local cache.
1285 if flow == msg.Flow.FlowId {
1286 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1287 break
1288 }
1289 }
1290
1291 if len(o.FlowIds) == 0 {
1292 onuLogger.WithFields(log.Fields{
1293 "IntfId": o.PonPortID,
1294 "OnuId": o.ID,
1295 "SerialNumber": o.Sn(),
1296 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001297
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301298 // check if ONU delete is performed and
1299 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001300 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301301 close(o.Channel)
1302 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001303 }
1304}
1305
Matteo Scandolocedde462021-03-09 17:37:16 -08001306func (o *Onu) Reboot(timeout time.Duration) error {
1307 onuLogger.WithFields(log.Fields{
1308 "IntfId": o.PonPortID,
1309 "OnuId": o.ID,
1310 "SerialNumber": o.Sn(),
1311 }).Debug("shutting-down-onu")
1312 if err := o.HandleShutdownONU(); err != nil {
1313 return err
1314 }
1315 time.Sleep(timeout)
1316 onuLogger.WithFields(log.Fields{
1317 "IntfId": o.PonPortID,
1318 "OnuId": o.ID,
1319 "SerialNumber": o.Sn(),
1320 }).Debug("power-on-onu")
1321 if err := o.HandlePowerOnONU(); err != nil {
1322 return err
1323 }
1324 return nil
1325}
1326
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001327// BBR methods
1328
1329func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1330 omciMsg := openolt.OmciMsg{
1331 IntfId: intfId,
1332 OnuId: onuId,
1333 Pkt: pktBytes,
1334 }
1335
1336 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1337 log.WithFields(log.Fields{
1338 "IntfId": intfId,
1339 "OnuId": onuId,
1340 "SerialNumber": common.OnuSnToString(sn),
1341 "Pkt": omciMsg.Pkt,
1342 }).Fatalf("Failed to send MIB Reset")
1343 }
1344 log.WithFields(log.Fields{
1345 "IntfId": intfId,
1346 "OnuId": onuId,
1347 "SerialNumber": common.OnuSnToString(sn),
1348 "Pkt": omciMsg.Pkt,
1349 }).Tracef("Sent OMCI message %s", msgType)
1350}
1351
1352func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1353 var next uint16
1354 if len(highPriority) > 0 && highPriority[0] {
1355 next = onu.hpTid
1356 onu.hpTid += 1
1357 if onu.hpTid < 0x8000 {
1358 onu.hpTid = 0x8000
1359 }
1360 } else {
1361 next = onu.tid
1362 onu.tid += 1
1363 if onu.tid >= 0x8000 {
1364 onu.tid = 1
1365 }
1366 }
1367 return next
1368}
1369
1370// TODO move this method in responders/omcisim
1371func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1372 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1373 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1374}
1375
Matteo Scandolof9d43412021-01-12 11:11:34 -08001376// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1377func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1378
1379 // we need to encode the packet in HEX
1380 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1381 hex.Encode(pkt, msg.OmciInd.Pkt)
1382 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1383 if err != nil {
1384 log.WithFields(log.Fields{
1385 "IntfId": o.PonPortID,
1386 "SerialNumber": o.Sn(),
1387 "omciPacket": msg.OmciInd.Pkt,
1388 }).Error("BBR Cannot parse OMCI packet")
1389 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001390
1391 log.WithFields(log.Fields{
1392 "IntfId": msg.OmciInd.IntfId,
1393 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001394 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001395 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001396 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301397 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001398 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001399 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001400 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 Scandolo813402b2019-10-23 19:24:52 -07001404 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001405 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001406 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001407 case omci.MibResetResponseType:
1408 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1409 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1410 case omci.MibUploadResponseType:
1411 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1412 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1413 case omci.MibUploadNextResponseType:
1414 o.seqNumber++
1415
1416 if o.seqNumber > 290 {
1417 // 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 -08001418 // 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 -08001419 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001420 onuLogger.WithFields(log.Fields{
1421 "OnuId": o.ID,
1422 "IntfId": o.PonPortID,
1423 "OnuSn": o.Sn(),
1424 }).Errorf("Error while transitioning ONU State %v", err)
1425 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001426 } else {
1427 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1428 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001429 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001430 }
1431}
1432
1433func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1434
1435 classifierProto := openolt.Classifier{
1436 EthType: uint32(layers.EthernetTypeEAPOL),
1437 OVid: 4091,
1438 }
1439
1440 actionProto := openolt.Action{}
1441
1442 downstreamFlow := openolt.Flow{
1443 AccessIntfId: int32(o.PonPortID),
1444 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001445 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001446 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001447 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001448 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001449 Classifier: &classifierProto,
1450 Action: &actionProto,
1451 Priority: int32(100),
1452 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001453 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1454 // AllocId and GemPorts need to be unique per PON
1455 // for now use the ONU-ID, will need to change once we support multiple UNIs
1456 AllocId: int32(o.ID),
1457 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001458 }
1459
1460 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1461 log.WithFields(log.Fields{
1462 "IntfId": o.PonPortID,
1463 "OnuId": o.ID,
1464 "FlowId": downstreamFlow.FlowId,
1465 "PortNo": downstreamFlow.PortNo,
1466 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001467 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001468 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001469 }
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),
1476 }).Info("Sent EAPOL Flow")
1477}
1478
1479func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001480
1481 // BBR only works with a single service (ATT HSIA)
1482 hsia := o.Services[0].(*Service)
1483
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001484 classifierProto := openolt.Classifier{
1485 EthType: uint32(layers.EthernetTypeIPv4),
1486 SrcPort: uint32(68),
1487 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001488 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001489 }
1490
1491 actionProto := openolt.Action{}
1492
1493 downstreamFlow := openolt.Flow{
1494 AccessIntfId: int32(o.PonPortID),
1495 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001496 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001497 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001498 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001499 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001500 Classifier: &classifierProto,
1501 Action: &actionProto,
1502 Priority: int32(100),
1503 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001504 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1505 // AllocId and GemPorts need to be unique per PON
1506 // for now use the ONU-ID, will need to change once we support multiple UNIs
1507 AllocId: int32(o.ID),
1508 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001509 }
1510
1511 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1512 log.WithFields(log.Fields{
1513 "IntfId": o.PonPortID,
1514 "OnuId": o.ID,
1515 "FlowId": downstreamFlow.FlowId,
1516 "PortNo": downstreamFlow.PortNo,
1517 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001518 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001519 }).Fatalf("Failed to send DHCP Flow")
1520 }
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),
1527 }).Info("Sent DHCP Flow")
1528}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301529
1530// DeleteFlow method search and delete flowKey from the onu flows slice
1531func (onu *Onu) DeleteFlow(key FlowKey) {
1532 for pos, flowKey := range onu.Flows {
1533 if flowKey == key {
1534 // delete the flowKey by shifting all flowKeys by one
1535 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1536 t := make([]FlowKey, len(onu.Flows))
1537 copy(t, onu.Flows)
1538 onu.Flows = t
1539 break
1540 }
1541 }
1542}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301543
1544func (onu *Onu) ReDiscoverOnu() {
1545 // Wait for few seconds to be sure of the cleanup
1546 time.Sleep(5 * time.Second)
1547
1548 onuLogger.WithFields(log.Fields{
1549 "IntfId": onu.PonPortID,
1550 "OnuId": onu.ID,
1551 "OnuSn": onu.Sn(),
1552 }).Debug("Send ONU Re-Discovery")
1553
1554 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001555 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301556 log.WithFields(log.Fields{
1557 "IntfId": onu.PonPortID,
1558 "OnuSn": onu.Sn(),
1559 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001560 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301561 }
1562
Matteo Scandolocedde462021-03-09 17:37:16 -08001563 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301564 log.WithFields(log.Fields{
1565 "IntfId": onu.PonPortID,
1566 "OnuSn": onu.Sn(),
1567 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001568 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301569 }
1570}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001571
1572func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1573 for _, s := range onu.Services {
1574 if service, ok := s.(*Service); ok {
1575 // EAPOL is a strange case, as packets are untagged
1576 // but we assume we will have a single service requiring EAPOL
1577 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1578 service.GemPort = gemport
1579 }
1580
1581 // For DHCP services we single tag the outgoing packets,
1582 // thus the flow only contains the CTag and we can use that to match the service
1583 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1584 service.GemPort = gemport
1585 }
1586
1587 // for dataplane services match both C and S tags
1588 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1589 service.GemPort = gemport
1590 }
1591 }
1592 }
1593}
1594
1595func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1596 for _, s := range onu.Services {
1597 service := s.(*Service)
1598 if service.HwAddress.String() == macAddress.String() {
1599 return service, nil
1600 }
1601 }
1602 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1603}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301604
1605func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1606 switch alarmType {
1607 case "ONU_ALARM_LOS":
1608 msg := bbsim.Message{
1609 Type: bbsim.UniStatusAlarm,
1610 Data: bbsim.UniStatusAlarmMessage{
1611 OnuSN: o.SerialNumber,
1612 OnuID: o.ID,
1613 EntityID: 257,
1614 RaiseOMCIAlarm: raiseOMCIAlarm,
1615 },
1616 }
1617 o.Channel <- msg
1618 }
1619
1620}
1621
1622func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1623 o.onuAlarmsInfoLock.Lock()
1624 defer o.onuAlarmsInfoLock.Unlock()
1625 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1626 if alarmInfo.SequenceNo == 255 {
1627 alarmInfo.SequenceNo = 1
1628 } else {
1629 alarmInfo.SequenceNo++
1630 }
1631 o.onuAlarmsInfo[key] = alarmInfo
1632 return alarmInfo.SequenceNo
1633 } else {
1634 // This is the first time alarm notification message is being sent
1635 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1636 SequenceNo: 1,
1637 }
1638 return 1
1639 }
1640}