blob: db0c747aa70dbd27690965db1e2e7455968696c5 [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 Scandolo8a574812021-05-20 15:18:53 -070023 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
24 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
25 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000026 "sync"
27
Matteo Scandolof9d43412021-01-12 11:11:34 -080028 pb "github.com/opencord/bbsim/api/bbsim"
29 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000030
31 "net"
32 "strconv"
33 "time"
Himani Chawla13b1ee02021-03-15 01:43:53 +053034
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080035 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
36 me "github.com/opencord/omci-lib-go/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010037
Matteo Scandolo3bc73742019-08-20 14:04:04 -070038 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070039 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070041 "github.com/opencord/bbsim/internal/common"
42 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolof9d43412021-01-12 11:11:34 -080043 "github.com/opencord/omci-lib-go"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070044 "github.com/opencord/voltha-protos/v4/go/openolt"
45 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070046 log "github.com/sirupsen/logrus"
47)
48
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070049var onuLogger = log.WithFields(log.Fields{
50 "module": "ONU",
51})
52
Matteo Scandolocedde462021-03-09 17:37:16 -080053const (
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000054 maxOmciMsgCounter = 10
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070055 uniPorts = 4 // TODO this will need to be configurable
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000056)
57
58const (
Matteo Scandolocedde462021-03-09 17:37:16 -080059 // ONU transitions
60 OnuTxInitialize = "initialize"
61 OnuTxDiscover = "discover"
62 OnuTxEnable = "enable"
63 OnuTxDisable = "disable"
64 OnuTxPonDisable = "pon_disable"
65 OnuTxStartImageDownload = "start_image_download"
66 OnuTxProgressImageDownload = "progress_image_download"
67 OnuTxCompleteImageDownload = "complete_image_download"
68 OnuTxFailImageDownload = "fail_image_download"
69 OnuTxActivateImage = "activate_image"
70 OnuTxCommitImage = "commit_image"
71
72 // ONU States
73 OnuStateCreated = "created"
74 OnuStateInitialized = "initialized"
75 OnuStateDiscovered = "discovered"
76 OnuStateEnabled = "enabled"
77 OnuStateDisabled = "disabled"
78 OnuStatePonDisabled = "pon_disabled"
79 OnuStateImageDownloadStarted = "image_download_started"
80 OnuStateImageDownloadInProgress = "image_download_in_progress"
81 OnuStateImageDownloadComplete = "image_download_completed"
82 OnuStateImageDownloadError = "image_download_error"
83 OnuStateImageActivated = "software_image_activated"
84 OnuStateImageCommitted = "software_image_committed"
85
86 // BBR ONU States and Transitions
87 BbrOnuTxSendEapolFlow = "send_eapol_flow"
88 BbrOnuStateEapolFlowSent = "eapol_flow_sent"
89 BbrOnuTxSendDhcpFlow = "send_dhcp_flow"
90 BbrOnuStateDhcpFlowSent = "dhcp_flow_sent"
91)
92
Pragya Arya8bdb4532020-03-02 17:08:09 +053093type FlowKey struct {
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070094 ID uint64
Pragya Arya8bdb4532020-03-02 17:08:09 +053095 Direction string
96}
97
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070098type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080099 ID uint32
100 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700101 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800102 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +0530103 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
104 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -0700105
Matteo Scandolo4a036262020-08-17 15:56:13 -0700106 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800107 // ONU State
Matteo Scandolo8a574812021-05-20 15:18:53 -0700108 UniPorts []UniPortIf
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700109 Flows []FlowKey
110 FlowIds []uint64 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -0700111
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700112 OperState *fsm.FSM
113 SerialNumber *openolt.SerialNumber
114
Matteo Scandolof9d43412021-01-12 11:11:34 -0800115 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700116
117 // OMCI params
Matteo Scandolocedde462021-03-09 17:37:16 -0800118 MibDataSync uint8
119 ImageSoftwareExpectedSections int
120 ImageSoftwareReceivedSections int
121 ActiveImageEntityId uint16
122 CommittedImageEntityId uint16
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700123 StandbyImageVersion string
124 ActiveImageVersion string
125 CommittedImageVersion string
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000126 OmciResponseRate uint8
127 OmciMsgCounter uint8
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800128
129 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700130 tid uint16
131 hpTid uint16
132 seqNumber uint16
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700133 MibDb *omcilib.MibDb
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700134
Anand S Katti09541352020-01-29 15:54:01 +0530135 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
136 TrafficSchedulers *tech_profile.TrafficSchedulers
Himani Chawla13b1ee02021-03-15 01:43:53 +0530137 onuAlarmsInfoLock sync.RWMutex
138 onuAlarmsInfo map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700139}
140
Matteo Scandolo99f18462019-10-28 14:14:28 -0700141func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700142 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700143}
144
Matteo Scandolo8a574812021-05-20 15:18:53 -0700145func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, nextCtag map[string]int, nextStag map[string]int, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700146
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700147 o := Onu{
Matteo Scandolocedde462021-03-09 17:37:16 -0800148 ID: id,
149 PonPortID: pon.ID,
150 PonPort: pon,
Matteo Scandolocedde462021-03-09 17:37:16 -0800151 tid: 0x1,
152 hpTid: 0x8000,
153 seqNumber: 0,
154 DoneChannel: make(chan bool, 1),
155 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
156 Flows: []FlowKey{},
157 DiscoveryDelay: delay,
158 MibDataSync: 0,
159 ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
160 ImageSoftwareReceivedSections: 0,
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700161 //TODO this needs reworking, it's always 0 or 1, possibly base all on the version
162 ActiveImageEntityId: 0, // when we start the SoftwareImage with ID 0 is active and committed
163 CommittedImageEntityId: 0,
164 StandbyImageVersion: "BBSM_IMG_00000",
165 ActiveImageVersion: "BBSM_IMG_00001",
166 CommittedImageVersion: "BBSM_IMG_00001",
167 OmciResponseRate: olt.OmciResponseRate,
168 OmciMsgCounter: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700169 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800170 o.SerialNumber = NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700171 // NOTE this state machine is used to track the operational
172 // state as requested by VOLTHA
173 o.OperState = getOperStateFSM(func(e *fsm.Event) {
174 onuLogger.WithFields(log.Fields{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700175 "OnuId": o.ID,
176 "IntfId": o.PonPortID,
177 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700178 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
179 })
Himani Chawla13b1ee02021-03-15 01:43:53 +0530180 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700181 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
182 o.InternalState = fsm.NewFSM(
Matteo Scandolocedde462021-03-09 17:37:16 -0800183 OnuStateCreated,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700184 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700185 // DEVICE Lifecycle
Matteo Scandolocedde462021-03-09 17:37:16 -0800186 {Name: OnuTxInitialize, Src: []string{OnuStateCreated, OnuStateDisabled, OnuStatePonDisabled}, Dst: OnuStateInitialized},
187 {Name: OnuTxDiscover, Src: []string{OnuStateInitialized}, Dst: OnuStateDiscovered},
188 {Name: OnuTxEnable, Src: []string{OnuStateDiscovered, OnuStatePonDisabled}, Dst: OnuStateEnabled},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100189 // 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 -0800190 {Name: OnuTxDisable, Src: []string{OnuStateEnabled, OnuStatePonDisabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateDisabled},
Pragya Arya6a708d62020-01-01 17:17:20 +0530191 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +0200192 {Name: OnuTxPonDisable, Src: []string{OnuStateEnabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted, OnuStateImageDownloadComplete}, Dst: OnuStatePonDisabled},
Matteo Scandolocedde462021-03-09 17:37:16 -0800193 // Software Image Download related states
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +0200194 {Name: OnuTxStartImageDownload, Src: []string{OnuStateEnabled, OnuStateImageDownloadComplete, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateImageDownloadStarted},
Matteo Scandolocedde462021-03-09 17:37:16 -0800195 {Name: OnuTxProgressImageDownload, Src: []string{OnuStateImageDownloadStarted}, Dst: OnuStateImageDownloadInProgress},
196 {Name: OnuTxCompleteImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadComplete},
197 {Name: OnuTxFailImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadError},
198 {Name: OnuTxActivateImage, Src: []string{OnuStateImageDownloadComplete}, Dst: OnuStateImageActivated},
199 {Name: OnuTxCommitImage, Src: []string{OnuStateEnabled}, Dst: OnuStateImageCommitted}, // the image is committed after a ONU reboot
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700200 // BBR States
201 // TODO add start OMCI state
Matteo Scandolocedde462021-03-09 17:37:16 -0800202 {Name: BbrOnuTxSendEapolFlow, Src: []string{OnuStateInitialized}, Dst: BbrOnuStateEapolFlowSent},
203 {Name: BbrOnuTxSendDhcpFlow, Src: []string{BbrOnuStateEapolFlowSent}, Dst: BbrOnuStateDhcpFlowSent},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700204 },
205 fsm.Callbacks{
206 "enter_state": func(e *fsm.Event) {
207 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700208 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700209 fmt.Sprintf("enter_%s", OnuStateInitialized): func(e *fsm.Event) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100210 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800211 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800212
Matteo Scandolocedde462021-03-09 17:37:16 -0800213 if err := o.OperState.Event(OnuTxEnable); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800214 onuLogger.WithFields(log.Fields{
215 "OnuId": o.ID,
216 "IntfId": o.PonPortID,
217 "OnuSn": o.Sn(),
218 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
219 }
220
Pragya Arya1cbefa42020-01-13 12:15:29 +0530221 if !isMock {
222 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700223 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530224 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100225 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700226 fmt.Sprintf("enter_%s", OnuStateDiscovered): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800227 msg := bbsim.Message{
228 Type: bbsim.OnuDiscIndication,
229 Data: bbsim.OnuDiscIndicationMessage{
230 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100231 },
232 }
233 o.Channel <- msg
234 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700235 fmt.Sprintf("enter_%s", OnuStateEnabled): func(event *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800236
237 if used, sn := o.PonPort.isOnuIdAllocated(o.ID); used {
238 onuLogger.WithFields(log.Fields{
239 "IntfId": o.PonPortID,
240 "OnuId": o.ID,
241 "SerialNumber": o.Sn(),
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700242 }).Errorf("onu-id-duplicated-with-%s", common.OnuSnToString(sn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800243 return
244 } else {
245 o.PonPort.storeOnuId(o.ID, o.SerialNumber)
246 }
247
Matteo Scandolof9d43412021-01-12 11:11:34 -0800248 msg := bbsim.Message{
249 Type: bbsim.OnuIndication,
250 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700251 OnuSN: o.SerialNumber,
252 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800253 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700254 },
255 }
256 o.Channel <- msg
257 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700258 fmt.Sprintf("enter_%s", OnuStateDisabled): func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700259
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700260 o.cleanupOnuState()
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700261
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700262 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800263 if err := o.OperState.Event("disable"); err != nil {
264 onuLogger.WithFields(log.Fields{
265 "OnuId": o.ID,
266 "IntfId": o.PonPortID,
267 "OnuSn": o.Sn(),
268 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
269 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700270 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800271 msg := bbsim.Message{
272 Type: bbsim.OnuIndication,
273 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700274 OnuSN: o.SerialNumber,
275 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800276 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700277 },
278 }
279 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530280
Matteo Scandolo8a574812021-05-20 15:18:53 -0700281 // disable the UNI ports
282 for _, uni := range o.UniPorts {
283 _ = uni.Disable()
284 }
285
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530286 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100287 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolo8a574812021-05-20 15:18:53 -0700288 // NOTE may need to wait for the UNIs to be down too before shutting down the channel
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530289 if len(o.FlowIds) == 0 {
290 close(o.Channel)
291 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700292 },
293 fmt.Sprintf("enter_%s", OnuStatePonDisabled): func(event *fsm.Event) {
294 o.cleanupOnuState()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700295 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700296 // BBR states
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700297 fmt.Sprintf("enter_%s", BbrOnuStateEapolFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800298 msg := bbsim.Message{
299 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700300 }
301 o.Channel <- msg
302 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700303 fmt.Sprintf("enter_%s", BbrOnuStateDhcpFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800304 msg := bbsim.Message{
305 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700306 }
307 o.Channel <- msg
308 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700309 },
310 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100311
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700312 for i := 0; i < uniPorts; i++ {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700313 uni, err := NewUniPort(uint32(i), &o, nextCtag, nextStag)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700314 if err != nil {
315 onuLogger.WithFields(log.Fields{
316 "OnuId": o.ID,
317 "IntfId": o.PonPortID,
318 "OnuSn": o.Sn(),
319 "Err": err,
320 }).Fatal("cannot-create-uni-port")
321 }
322 o.UniPorts = append(o.UniPorts, uni)
323 }
324
325 mibDb, err := omcilib.GenerateMibDatabase(len(o.UniPorts))
326 if err != nil {
327 onuLogger.WithFields(log.Fields{
328 "OnuId": o.ID,
329 "IntfId": o.PonPortID,
330 "OnuSn": o.Sn(),
331 }).Fatal("cannot-generate-mibdb-for-onu")
332 }
333 o.MibDb = mibDb
334
Matteo Scandolo27428702019-10-11 16:21:16 -0700335 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700336}
337
William Kurkian0418bc82019-11-06 12:16:24 -0500338func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700339 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700340 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700341 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700342 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700343 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
344}
345
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700346// cleanupOnuState this method is to clean the local state when the ONU is disabled
347func (o *Onu) cleanupOnuState() {
348 // clean the ONU state
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700349 o.Flows = []FlowKey{}
350 o.PonPort.removeOnuId(o.ID)
351 o.PonPort.removeAllocId(o.SerialNumber)
352 o.PonPort.removeGemPortBySn(o.SerialNumber)
353
354 o.onuAlarmsInfoLock.Lock()
355 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
356 o.onuAlarmsInfoLock.Unlock()
357}
358
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100359// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000360func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700361 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100362 "onuID": o.ID,
363 "onuSN": o.Sn(),
364 "ponPort": o.PonPortID,
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700365 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100366 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700367
Matteo Scandolob307d8a2021-05-10 15:19:27 -0700368 defer onuLogger.WithFields(log.Fields{
369 "onuID": o.ID,
370 "onuSN": o.Sn(),
371 "stream": stream,
372 }).Debug("Stopped handling ONU Indication Channel")
373
David Bainbridge103cf022019-12-16 20:11:35 +0000374loop:
375 for {
376 select {
377 case <-ctx.Done():
378 onuLogger.WithFields(log.Fields{
379 "onuID": o.ID,
380 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700381 }).Debug("ONU message handling canceled via context")
382 break loop
383 case <-stream.Context().Done():
384 onuLogger.WithFields(log.Fields{
385 "onuID": o.ID,
386 "onuSN": o.Sn(),
387 }).Debug("ONU message handling canceled via stream context")
David Bainbridge103cf022019-12-16 20:11:35 +0000388 break loop
389 case message, ok := <-o.Channel:
390 if !ok || ctx.Err() != nil {
391 onuLogger.WithFields(log.Fields{
392 "onuID": o.ID,
393 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700394 }).Debug("ONU message handling canceled via channel close")
David Bainbridge103cf022019-12-16 20:11:35 +0000395 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700396 }
David Bainbridge103cf022019-12-16 20:11:35 +0000397 onuLogger.WithFields(log.Fields{
398 "onuID": o.ID,
399 "onuSN": o.Sn(),
400 "messageType": message.Type,
401 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700402
David Bainbridge103cf022019-12-16 20:11:35 +0000403 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800404 case bbsim.OnuDiscIndication:
405 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000406 // 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 +0530407 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000408 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800409 case bbsim.OnuIndication:
410 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000411 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800412 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800413 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800414 msg, _ := message.Data.(bbsim.OmciMessage)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200415 _ = o.handleOmciRequest(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800416 case bbsim.UniStatusAlarm:
417 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530418 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
419 MeInstance: msg.EntityID,
420 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
421 }
422 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
423 o.onuAlarmsInfoLock.Lock()
424 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
425 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
426 if pkt != nil { //pkt will be nil if we are unable to create the alarm
427 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
428 onuLogger.WithFields(log.Fields{
429 "IntfId": o.PonPortID,
430 "SerialNumber": o.Sn(),
431 "omciPacket": pkt,
432 "adminState": msg.AdminState,
433 "entityID": msg.EntityID,
434 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
435 alarmInfo.SequenceNo--
436 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800437 onuLogger.WithFields(log.Fields{
438 "IntfId": o.PonPortID,
439 "SerialNumber": o.Sn(),
440 "omciPacket": pkt,
441 "adminState": msg.AdminState,
442 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530443 }).Trace("UNI-Link-alarm-sent")
444 if alarmBitMap == [28]byte{0} {
445 delete(o.onuAlarmsInfo, onuAlarmMapKey)
446 } else {
447 alarmInfo.AlarmBitMap = alarmBitMap
448 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
449 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800450 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530451 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800452 case bbsim.FlowAdd:
453 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700454 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800455 case bbsim.FlowRemoved:
456 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700457 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800458 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700459
Matteo Scandolof9d43412021-01-12 11:11:34 -0800460 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000461
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700462 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000463 "IntfId": msg.IntfId,
464 "OnuId": msg.OnuId,
465 "pktType": msg.Type,
466 }).Trace("Received OnuPacketOut Message")
467
Matteo Scandolo8a574812021-05-20 15:18:53 -0700468 uni, err := o.findUniByPortNo(msg.PortNo)
Matteo Scandolo618a6582020-09-09 12:21:29 -0700469
Matteo Scandolo8a574812021-05-20 15:18:53 -0700470 if err != nil {
471 onuLogger.WithFields(log.Fields{
472 "IntfId": msg.IntfId,
473 "OnuId": msg.OnuId,
474 "pktType": msg.Type,
475 "portNo": msg.PortNo,
476 "MacAddress": msg.MacAddress,
477 "Pkt": hex.EncodeToString(msg.Packet.Data()),
478 "OnuSn": o.Sn(),
479 }).Error("Cannot find Uni associated with packet")
480 return
David Bainbridge103cf022019-12-16 20:11:35 +0000481 }
Matteo Scandolo8a574812021-05-20 15:18:53 -0700482 uni.PacketCh <- msg
483 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800484 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000485 // NOTE we only receive BBR packets here.
486 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
487 // 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 -0800488 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000489
Matteo Scandolo8a574812021-05-20 15:18:53 -0700490 onuLogger.WithFields(log.Fields{
491 "IntfId": msg.IntfId,
492 "OnuId": msg.OnuId,
493 "PortNo": msg.PortNo,
494 "GemPortId": msg.GemPortId,
495 "pktType": msg.Type,
David Bainbridge103cf022019-12-16 20:11:35 +0000496 }).Trace("Received OnuPacketIn Message")
497
Matteo Scandolo8a574812021-05-20 15:18:53 -0700498 uni, err := o.findUniByPortNo(msg.PortNo)
499 if err != nil {
500 onuLogger.WithFields(log.Fields{
501 "IntfId": msg.IntfId,
502 "OnuId": msg.OnuId,
503 "PortNo": msg.PortNo,
504 "GemPortId": msg.GemPortId,
505 "pktType": msg.Type,
506 }).Error(err.Error())
507 }
508
509 // BBR has one service and one UNI
510 serviceId := uint32(0)
511 oltId := 0
David Bainbridge103cf022019-12-16 20:11:35 +0000512 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700513 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, msg.GemPortId, o.Sn(), msg.PortNo, uni.ID, serviceId, oltId, o.InternalState, msg.Packet, stream, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000514 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700515 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000516 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800517 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800518 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800519 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
520 o.handleOmciResponse(msg, client)
521 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000522 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800523 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000524 o.sendDhcpFlow(client)
525 default:
526 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700527 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700528 }
529 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700530}
531
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800532func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700533 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700534 sn.VendorId = []byte("BBSM")
535 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700536 return sn
537}
538
Matteo Scandolof9d43412021-01-12 11:11:34 -0800539func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700540 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800541 IntfId: o.PonPortID,
542 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700543 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700544
Matteo Scandolo4747d292019-08-05 11:50:18 -0700545 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700546 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700547 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700548 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700549
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700550 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800551 "IntfId": o.PonPortID,
552 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700553 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700554 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800555 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800556
557 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
558 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800559 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800560 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800561 o.sendOnuDiscIndication(msg, stream)
562 }
563 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700564}
565
Matteo Scandolof9d43412021-01-12 11:11:34 -0800566func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800567 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
568 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700569
570 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700571 IntfId: o.PonPortID,
572 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700573 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700574 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700575 SerialNumber: o.SerialNumber,
576 }}
577 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800578 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700579 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700580 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700581 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700582 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800583 "IntfId": o.PonPortID,
584 "OnuId": o.ID,
585 "VolthaOnuId": msg.OnuID,
586 "OperState": msg.OperState.String(),
587 "AdminState": msg.OperState.String(),
588 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700589 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700590
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700591}
592
Matteo Scandolof9d43412021-01-12 11:11:34 -0800593func (o *Onu) HandleShutdownONU() error {
594
595 dyingGasp := pb.ONUAlarmRequest{
596 AlarmType: "DYING_GASP",
597 SerialNumber: o.Sn(),
598 Status: "on",
599 }
600
601 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
602 onuLogger.WithFields(log.Fields{
603 "OnuId": o.ID,
604 "IntfId": o.PonPortID,
605 "OnuSn": o.Sn(),
606 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
607 return err
608 }
609
610 losReq := pb.ONUAlarmRequest{
611 AlarmType: "ONU_ALARM_LOS",
612 SerialNumber: o.Sn(),
613 Status: "on",
614 }
615
616 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
617 onuLogger.WithFields(log.Fields{
618 "OnuId": o.ID,
619 "IntfId": o.PonPortID,
620 "OnuSn": o.Sn(),
621 }).Errorf("Cannot send LOS: %s", err.Error())
622
623 return err
624 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530625 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800626 // TODO if it's the last ONU on the PON, then send a PON LOS
627
Matteo Scandolocedde462021-03-09 17:37:16 -0800628 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800629 onuLogger.WithFields(log.Fields{
630 "OnuId": o.ID,
631 "IntfId": o.PonPortID,
632 "OnuSn": o.Sn(),
633 }).Errorf("Cannot shutdown ONU: %s", err.Error())
634 return err
635 }
636
637 return nil
638}
639
640func (o *Onu) HandlePowerOnONU() error {
641 intitalState := o.InternalState.Current()
642
643 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800644 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
645 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800646 onuLogger.WithFields(log.Fields{
647 "OnuId": o.ID,
648 "IntfId": o.PonPortID,
649 "OnuSn": o.Sn(),
650 }).Errorf("Cannot poweron ONU: %s", err.Error())
651 return err
652 }
653 }
654
655 // turn off the LOS Alarm
656 losReq := pb.ONUAlarmRequest{
657 AlarmType: "ONU_ALARM_LOS",
658 SerialNumber: o.Sn(),
659 Status: "off",
660 }
661
662 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
663 onuLogger.WithFields(log.Fields{
664 "OnuId": o.ID,
665 "IntfId": o.PonPortID,
666 "OnuSn": o.Sn(),
667 }).Errorf("Cannot send LOS: %s", err.Error())
668 return err
669 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530670 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800671
672 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800673 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800674 onuLogger.WithFields(log.Fields{
675 "OnuId": o.ID,
676 "IntfId": o.PonPortID,
677 "OnuSn": o.Sn(),
678 }).Errorf("Cannot poweron ONU: %s", err.Error())
679 return err
680 }
681
682 // move o directly to enable state only when its a powercycle case
683 // in case of first time o poweron o will be moved to enable on
684 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800685 if intitalState == OnuStateDisabled {
686 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800687 onuLogger.WithFields(log.Fields{
688 "OnuId": o.ID,
689 "IntfId": o.PonPortID,
690 "OnuSn": o.Sn(),
691 }).Errorf("Cannot enable ONU: %s", err.Error())
692 return err
693 }
694 }
695
696 return nil
697}
698
699func (o *Onu) SetAlarm(alarmType string, status string) error {
700 alarmReq := pb.ONUAlarmRequest{
701 AlarmType: alarmType,
702 SerialNumber: o.Sn(),
703 Status: status,
704 }
705
706 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
707 if err != nil {
708 return err
709 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530710 raiseAlarm := false
711 if alarmReq.Status == "on" {
712 raiseAlarm = true
713 }
714 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800715 return nil
716}
717
718func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530719 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700720 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530721 if err != nil {
722 log.Errorf("error in getting msgType %v", err)
723 return
724 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800725 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530726 o.seqNumber = 0
727 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800728 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530729 o.seqNumber++
730 if o.seqNumber > 290 {
731 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
732 }
733 }
734 }
735}
736
Matteo Scandolof9d43412021-01-12 11:11:34 -0800737// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
738// and generate the appropriate response to it
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200739func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800740
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700741 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700742 "omciMsgType": msg.OmciMsg.MessageType,
743 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
744 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700745 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700746 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800747 }).Trace("omci-message-decoded")
748
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000749 if o.OmciMsgCounter < maxOmciMsgCounter {
750 o.OmciMsgCounter++
751 } else {
752 o.OmciMsgCounter = 1
753 }
754 if o.OmciMsgCounter > o.OmciResponseRate {
755 onuLogger.WithFields(log.Fields{
756 "OmciMsgCounter": o.OmciMsgCounter,
757 "OmciResponseRate": o.OmciResponseRate,
758 "omciMsgType": msg.OmciMsg.MessageType,
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200759 }).Debug("skipping-omci-msg-response")
760 return fmt.Errorf("skipping-omci-msg-response-because-of-response-rate-%d", o.OmciResponseRate)
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000761 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800762 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800763 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700764 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800765 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800766 onuLogger.WithFields(log.Fields{
767 "IntfId": o.PonPortID,
768 "OnuId": o.ID,
769 "SerialNumber": o.Sn(),
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700770 }).Debug("received-mib-reset-request")
Matteo Scandolob5913142021-03-19 16:10:18 -0700771 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800772 o.MibDataSync = 0
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700773
774 // if the MIB reset is successful then remove all the stored AllocIds and GemPorts
775 o.PonPort.removeAllocId(o.SerialNumber)
776 o.PonPort.removeGemPortBySn(o.SerialNumber)
Girish Gowdrae2683102021-03-05 08:24:26 -0800777 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800778 case omci.MibUploadRequestType:
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700779 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID, o.MibDb.NumberOfCommands)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800780 case omci.MibUploadNextRequestType:
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700781 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync, o.MibDb)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800782 case omci.GetRequestType:
Girish Gowdra996d81e2021-04-21 16:16:27 -0700783 onuDown := o.OperState.Current() == "down"
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700784 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId,
785 o.CommittedImageEntityId, o.StandbyImageVersion, o.ActiveImageVersion, o.CommittedImageVersion, onuDown)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800786 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800787 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700788 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800789 switch msgObj.EntityClass {
790 case me.PhysicalPathTerminationPointEthernetUniClassID:
791 // if we're Setting a PPTP state
Matteo Scandolo8a574812021-05-20 15:18:53 -0700792 // we need to send the appropriate alarm (handled in the UNI struct)
793 uni, err := o.FindUniByEntityId(msgObj.EntityInstance)
794 if err != nil {
795 onuLogger.Error(err)
796 success = false
797 } else {
798 // 1 locks the UNI, 0 unlocks it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800799 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Matteo Scandolo8a574812021-05-20 15:18:53 -0700800 var err error
Himani Chawla13b1ee02021-03-15 01:43:53 +0530801 if adminState == 1 {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700802 err = uni.Disable()
Girish Gowdra996d81e2021-04-21 16:16:27 -0700803 } else {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700804 err = uni.Enable()
Himani Chawla13b1ee02021-03-15 01:43:53 +0530805 }
Matteo Scandolo8a574812021-05-20 15:18:53 -0700806 if err != nil {
807 onuLogger.WithFields(log.Fields{
808 "IntfId": o.PonPortID,
809 "OnuId": o.ID,
810 "UniMeId": uni.MeId,
811 "UniId": uni.ID,
812 "SerialNumber": o.Sn(),
813 "Err": err.Error(),
814 }).Warn("cannot-change-uni-status")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800815 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800816 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800817 case me.TContClassID:
818 allocId := msgObj.Attributes["AllocId"].(uint16)
819
820 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
821 // otherwise we are adding it
822 if allocId == 255 || allocId == 65535 {
823 onuLogger.WithFields(log.Fields{
824 "IntfId": o.PonPortID,
825 "OnuId": o.ID,
826 "TContId": msgObj.EntityInstance,
827 "AllocId": allocId,
828 "SerialNumber": o.Sn(),
829 }).Trace("freeing-alloc-id-via-omci")
830 o.PonPort.removeAllocId(o.SerialNumber)
831 } else {
832 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
833 onuLogger.WithFields(log.Fields{
834 "IntfId": o.PonPortID,
835 "OnuId": o.ID,
836 "AllocId": allocId,
837 "SerialNumber": o.Sn(),
838 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
839 success = false
840 } else {
841 onuLogger.WithFields(log.Fields{
842 "IntfId": o.PonPortID,
843 "OnuId": o.ID,
844 "TContId": msgObj.EntityInstance,
845 "AllocId": allocId,
846 "SerialNumber": o.Sn(),
847 }).Trace("storing-alloc-id-via-omci")
848 o.PonPort.storeAllocId(allocId, o.SerialNumber)
849 }
850 }
851
852 }
853
854 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700855 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800856 o.MibDataSync++
857 }
858 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700859 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800860 }
861 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800862 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
863 var used bool
864 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700865 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800866 if err == nil {
867 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700868 // GemPort 4069 is reserved for multicast and shared across ONUs
869 if msgObj.EntityInstance != 4069 {
870 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
871 onuLogger.WithFields(log.Fields{
872 "IntfId": o.PonPortID,
873 "OnuId": o.ID,
874 "GemPortId": msgObj.EntityInstance,
875 "SerialNumber": o.Sn(),
876 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
877 } else {
878 onuLogger.WithFields(log.Fields{
879 "IntfId": o.PonPortID,
880 "OnuId": o.ID,
881 "GemPortId": msgObj.EntityInstance,
882 "SerialNumber": o.Sn(),
883 }).Trace("storing-gem-port-id-via-omci")
884 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
885 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800886 }
887 }
888 }
889
890 // if the gemPort is valid then increment the MDS and return a successful response
891 // otherwise fail the request
892 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
893 // validation this check will need to be rewritten
894 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700895 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800896 o.MibDataSync++
897 }
898 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700899 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800900 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800901 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700902 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800903 if err == nil {
904 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
905 onuLogger.WithFields(log.Fields{
906 "IntfId": o.PonPortID,
907 "OnuId": o.ID,
908 "GemPortId": msgObj.EntityInstance,
909 "SerialNumber": o.Sn(),
910 }).Trace("freeing-gem-port-id-via-omci")
911 o.PonPort.removeGemPort(msgObj.EntityInstance)
912 }
913 }
914
Matteo Scandolob5913142021-03-19 16:10:18 -0700915 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800916 o.MibDataSync++
917 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800918 case omci.RebootRequestType:
919
Matteo Scandolob5913142021-03-19 16:10:18 -0700920 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800921
922 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800923 // we run this in a separate goroutine so that
924 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800925 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800926 if err := o.Reboot(10 * time.Second); err != nil {
927 log.WithFields(log.Fields{
928 "IntfId": o.PonPortID,
929 "OnuId": o.ID,
930 "SerialNumber": o.Sn(),
931 "err": err,
932 }).Error("cannot-reboot-onu-after-omci-reboot-request")
933 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800934 }()
935 case omci.TestRequestType:
Girish Gowdra161d27a2021-05-05 12:01:44 -0700936 var classID me.ClassID
937 var omciResult me.Results
938 var instID uint16
939 responsePkt, errResp, classID, instID, omciResult = omcilib.CreateTestResponse(msg.OmciPkt, msg.OmciMsg)
940 // Send TestResult only in case the TestResponse omci result code is me.Success
941 if responsePkt != nil && errResp == nil && omciResult == me.Success {
942 if testResultPkt, err := omcilib.CreateTestResult(classID, instID, msg.OmciMsg.TransactionID); err == nil {
943 // send test results asynchronously
944 go func() {
945 // Send test results after a second to emulate async behavior
946 time.Sleep(1 * time.Second)
947 if testResultPkt != nil {
948 if err := o.sendOmciIndication(testResultPkt, msg.OmciMsg.TransactionID, stream); err != nil {
949 onuLogger.WithFields(log.Fields{
950 "IntfId": o.PonPortID,
951 "SerialNumber": o.Sn(),
952 "omciPacket": testResultPkt,
953 "msg.OmciMsgType": msg.OmciMsg.MessageType,
954 "transCorrId": msg.OmciMsg.TransactionID,
955 }).Errorf("failed-to-send-omci-message: %v", err)
956 }
957 }
958 }()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800959 }
960 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800961 case omci.SynchronizeTimeRequestType:
962 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700963 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800964 case omci.StartSoftwareDownloadRequestType:
965
966 o.ImageSoftwareReceivedSections = 0
967
Matteo Scandolob5913142021-03-19 16:10:18 -0700968 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800969
Matteo Scandolob5913142021-03-19 16:10:18 -0700970 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800971 o.MibDataSync++
972 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
973 onuLogger.WithFields(log.Fields{
974 "OnuId": o.ID,
975 "IntfId": o.PonPortID,
976 "OnuSn": o.Sn(),
977 "Err": err.Error(),
978 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
979 }
980 } else {
981 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700982 "OmciMsgType": msg.OmciMsg.MessageType,
983 "TransCorrId": msg.OmciMsg.TransactionID,
984 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800985 "IntfId": o.PonPortID,
986 "SerialNumber": o.Sn(),
987 }).Error("error-while-processing-start-software-download-request")
988 }
989 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700990 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800991 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700992 "OmciMsgType": msg.OmciMsg.MessageType,
993 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800994 "EntityInstance": msgObj.EntityInstance,
995 "SectionNumber": msgObj.SectionNumber,
996 "SectionData": msgObj.SectionData,
997 }).Trace("received-download-section-request")
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700998 //Extracting the first 14 bytes to use as a version for this image.
999 if o.ImageSoftwareReceivedSections == 0 {
1000 o.StandbyImageVersion = string(msgObj.SectionData[0:14])
1001 }
Matteo Scandolocedde462021-03-09 17:37:16 -08001002 o.ImageSoftwareReceivedSections++
1003 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
1004 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
1005 onuLogger.WithFields(log.Fields{
1006 "OnuId": o.ID,
1007 "IntfId": o.PonPortID,
1008 "OnuSn": o.Sn(),
1009 "Err": err.Error(),
1010 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
1011 }
1012 }
1013 }
1014 case omci.DownloadSectionRequestWithResponseType:
1015 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -07001016 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
1017 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001018 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001019 "OmciMsgType": msg.OmciMsg.MessageType,
1020 "TransCorrId": msg.OmciMsg.TransactionID,
1021 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001022 "IntfId": o.PonPortID,
1023 "SerialNumber": o.Sn(),
1024 }).Error("error-while-processing-create-download-section-response")
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001025 return fmt.Errorf("error-while-processing-create-download-section-response: %s", errResp.Error())
Matteo Scandolocedde462021-03-09 17:37:16 -08001026 }
1027 o.ImageSoftwareReceivedSections++
Matteo Scandolocedde462021-03-09 17:37:16 -08001028 case omci.EndSoftwareDownloadRequestType:
1029
1030 // In the startSoftwareDownload we get the image size and the window size.
1031 // We calculate how many DownloadSection we should receive and validate
1032 // that we got the correct amount when we receive this message
1033 success := true
1034 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
1035 onuLogger.WithFields(log.Fields{
1036 "OnuId": o.ID,
1037 "IntfId": o.PonPortID,
1038 "OnuSn": o.Sn(),
1039 "ExpectedSections": o.ImageSoftwareExpectedSections,
1040 "ReceivedSections": o.ImageSoftwareReceivedSections,
1041 }).Errorf("onu-did-not-receive-all-image-sections")
1042 success = false
1043 }
1044
1045 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001046 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001047 o.MibDataSync++
1048 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1049 onuLogger.WithFields(log.Fields{
1050 "OnuId": o.ID,
1051 "IntfId": o.PonPortID,
1052 "OnuSn": o.Sn(),
1053 "Err": err.Error(),
1054 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1055 }
1056 } else {
1057 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001058 "OmciMsgType": msg.OmciMsg.MessageType,
1059 "TransCorrId": msg.OmciMsg.TransactionID,
1060 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001061 "IntfId": o.PonPortID,
1062 "SerialNumber": o.Sn(),
1063 }).Error("error-while-processing-end-software-download-request")
1064 }
1065 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001066 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001067 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1068 onuLogger.WithFields(log.Fields{
1069 "OnuId": o.ID,
1070 "IntfId": o.PonPortID,
1071 "OnuSn": o.Sn(),
1072 "Err": err.Error(),
1073 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1074 }
1075 }
1076 }
Matteo Scandolocedde462021-03-09 17:37:16 -08001077 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001078 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001079 o.MibDataSync++
1080 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1081 onuLogger.WithFields(log.Fields{
1082 "OnuId": o.ID,
1083 "IntfId": o.PonPortID,
1084 "OnuSn": o.Sn(),
1085 "Err": err.Error(),
1086 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1087 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001088 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001089 o.ActiveImageEntityId = msgObj.EntityInstance
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001090 previousActiveImage := o.ActiveImageVersion
1091 o.ActiveImageVersion = o.StandbyImageVersion
1092 o.StandbyImageVersion = previousActiveImage
Matteo Scandolocedde462021-03-09 17:37:16 -08001093 } else {
1094 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1095 }
1096 onuLogger.WithFields(log.Fields{
1097 "OnuId": o.ID,
1098 "IntfId": o.PonPortID,
1099 "OnuSn": o.Sn(),
1100 "ActiveImageEntityId": o.ActiveImageEntityId,
1101 "CommittedImageEntityId": o.CommittedImageEntityId,
1102 }).Info("onu-software-image-activated")
1103
1104 // powercycle the ONU
1105 // we run this in a separate goroutine so that
1106 // the ActivateSoftwareResponse is sent to VOLTHA
1107 // NOTE do we need to wait before rebooting?
1108 go func() {
1109 if err := o.Reboot(10 * time.Second); err != nil {
1110 log.WithFields(log.Fields{
1111 "IntfId": o.PonPortID,
1112 "OnuId": o.ID,
1113 "SerialNumber": o.Sn(),
1114 "err": err,
1115 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1116 }
1117 }()
1118 }
1119 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001120 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001121 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001122 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001123 // TODO validate that the image to commit is:
1124 // - active
1125 // - not already committed
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001126 o.ActiveImageEntityId = msgObj.EntityInstance
Matteo Scandolocedde462021-03-09 17:37:16 -08001127 o.CommittedImageEntityId = msgObj.EntityInstance
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001128 //committed becomes standby
1129 o.StandbyImageVersion = o.CommittedImageVersion
1130 o.CommittedImageVersion = o.ActiveImageVersion
Matteo Scandolocedde462021-03-09 17:37:16 -08001131 } else {
1132 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1133 }
1134 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1135 onuLogger.WithFields(log.Fields{
1136 "OnuId": o.ID,
1137 "IntfId": o.PonPortID,
1138 "OnuSn": o.Sn(),
1139 "Err": err.Error(),
1140 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1141 }
1142 onuLogger.WithFields(log.Fields{
1143 "OnuId": o.ID,
1144 "IntfId": o.PonPortID,
1145 "OnuSn": o.Sn(),
1146 "ActiveImageEntityId": o.ActiveImageEntityId,
1147 "CommittedImageEntityId": o.CommittedImageEntityId,
1148 }).Info("onu-software-image-committed")
1149 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301150 case omci.GetAllAlarmsRequestType:
1151 // Reset the alarm sequence number on receiving get all alarms request.
1152 o.onuAlarmsInfoLock.Lock()
1153 for key, alarmInfo := range o.onuAlarmsInfo {
1154 // reset the alarm sequence no
1155 alarmInfo.SequenceNo = 0
1156 o.onuAlarmsInfo[key] = alarmInfo
1157 }
1158 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001159 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301160 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001161 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301162 responsePkt = nil //Do not send any response for error case
1163 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001164 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001165 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001166 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1167 "omciPkt": msg.OmciPkt,
1168 "omciMsgType": msg.OmciMsg.MessageType,
1169 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001170 "IntfId": o.PonPortID,
1171 "SerialNumber": o.Sn(),
1172 }).Warnf("OMCI-message-not-supported")
1173 }
1174
1175 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001176 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001177 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001178 "IntfId": o.PonPortID,
1179 "SerialNumber": o.Sn(),
1180 "omciPacket": responsePkt,
1181 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1182 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001183 }).Errorf("failed-to-send-omci-message: %v", err)
1184 }
1185 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001186
Pragya Arya324337e2020-02-20 14:35:08 +05301187 o.publishOmciEvent(msg)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001188 return nil
Matteo Scandolof9d43412021-01-12 11:11:34 -08001189}
Pragya Arya324337e2020-02-20 14:35:08 +05301190
Matteo Scandolof9d43412021-01-12 11:11:34 -08001191// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1192func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1193 indication := &openolt.Indication_OmciInd{
1194 OmciInd: &openolt.OmciIndication{
1195 IntfId: o.PonPortID,
1196 OnuId: o.ID,
1197 Pkt: responsePkt,
1198 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001199 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001200 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1201 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001202 }
1203 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001204 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001205 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001206 "omciPacket": indication.OmciInd.Pkt,
1207 "transCorrId": txId,
1208 }).Trace("omci-message-sent")
1209 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001210}
1211
Matteo Scandolo8a574812021-05-20 15:18:53 -07001212// FindUniById retrieves a UNI by ID
1213func (o *Onu) FindUniById(uniID uint32) (*UniPort, error) {
1214 for _, u := range o.UniPorts {
1215 uni := u.(*UniPort)
1216 if uni.ID == uniID {
1217 return uni, nil
1218 }
Matteo Scandolo27428702019-10-11 16:21:16 -07001219 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001220 return nil, fmt.Errorf("cannot-find-uni-with-id-%d-on-onu-%s", uniID, o.Sn())
1221}
1222
1223// FindUniByEntityId retrieves a uni by MeID (the OMCI entity ID)
1224func (o *Onu) FindUniByEntityId(meId uint16) (*UniPort, error) {
1225 entityId := omcilib.EntityID{}.FromUint16(meId)
1226 for _, u := range o.UniPorts {
1227 uni := u.(*UniPort)
1228 if uni.MeId.Equals(entityId) {
1229 return uni, nil
1230 }
1231 }
1232 return nil, fmt.Errorf("cannot-find-uni-with-meid-%s-on-onu-%s", entityId.ToString(), o.Sn())
Matteo Scandolo27428702019-10-11 16:21:16 -07001233}
1234
William Kurkian0418bc82019-11-06 12:16:24 -05001235func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001236 onuLogger.WithFields(log.Fields{
1237 "IntfId": o.PonPortID,
1238 "OnuId": id,
1239 "SerialNumber": o.Sn(),
1240 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001241 o.ID = id
1242}
1243
Matteo Scandolof9d43412021-01-12 11:11:34 -08001244func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001245 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001246 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001247 "Cookie": msg.Flow.Cookie,
1248 "DstPort": msg.Flow.Classifier.DstPort,
1249 "FlowId": msg.Flow.FlowId,
1250 "FlowType": msg.Flow.FlowType,
1251 "GemportId": msg.Flow.GemportId,
1252 "InnerVlan": msg.Flow.Classifier.IVid,
1253 "IntfId": msg.Flow.AccessIntfId,
1254 "IpProto": msg.Flow.Classifier.IpProto,
1255 "OnuId": msg.Flow.OnuId,
1256 "OnuSn": o.Sn(),
1257 "OuterVlan": msg.Flow.Classifier.OVid,
1258 "PortNo": msg.Flow.PortNo,
1259 "SrcPort": msg.Flow.Classifier.SrcPort,
1260 "UniID": msg.Flow.UniId,
1261 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1262 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1263 "ClassifierIVid": msg.Flow.Classifier.IVid,
1264 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001265 "ReplicateFlow": msg.Flow.ReplicateFlow,
1266 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001267 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001268
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001269 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001270
1271 var gemPortId uint32
1272 if msg.Flow.ReplicateFlow {
1273 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1274 // first available gemport (we only need to send one packet)
1275 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1276 gemPortId = msg.Flow.PbitToGemport[0]
1277 } else {
1278 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1279 gemPortId = uint32(msg.Flow.GemportId)
1280 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001281
1282 uni, err := o.FindUniById(uint32(msg.Flow.UniId))
1283 if err != nil {
1284 onuLogger.WithFields(log.Fields{
1285 "IntfId": o.PonPortID,
1286 "OnuId": o.ID,
1287 "UniId": msg.Flow.UniId,
1288 "PortNo": msg.Flow.PortNo,
1289 "SerialNumber": o.Sn(),
1290 "FlowId": msg.Flow.FlowId,
1291 "FlowType": msg.Flow.FlowType,
1292 }).Error("cannot-find-uni-port-for-flow")
1293 }
1294
1295 uni.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
1296 uni.StorePortNo(msg.Flow.PortNo)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001297
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001298 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001299 uni.HandleAuth()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001300 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1301 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001302 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001303 uni.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001304 }
1305}
1306
Matteo Scandolof9d43412021-01-12 11:11:34 -08001307func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001308 onuLogger.WithFields(log.Fields{
1309 "IntfId": o.PonPortID,
1310 "OnuId": o.ID,
1311 "SerialNumber": o.Sn(),
1312 "FlowId": msg.Flow.FlowId,
1313 "FlowType": msg.Flow.FlowType,
1314 }).Debug("ONU receives FlowRemove")
1315
1316 for idx, flow := range o.FlowIds {
1317 // If the gemport is found, delete it from local cache.
1318 if flow == msg.Flow.FlowId {
1319 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1320 break
1321 }
1322 }
1323
1324 if len(o.FlowIds) == 0 {
1325 onuLogger.WithFields(log.Fields{
1326 "IntfId": o.PonPortID,
1327 "OnuId": o.ID,
1328 "SerialNumber": o.Sn(),
1329 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001330
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301331 // check if ONU delete is performed and
1332 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001333 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301334 close(o.Channel)
1335 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001336 }
1337}
1338
Matteo Scandolocedde462021-03-09 17:37:16 -08001339func (o *Onu) Reboot(timeout time.Duration) error {
1340 onuLogger.WithFields(log.Fields{
1341 "IntfId": o.PonPortID,
1342 "OnuId": o.ID,
1343 "SerialNumber": o.Sn(),
1344 }).Debug("shutting-down-onu")
1345 if err := o.HandleShutdownONU(); err != nil {
1346 return err
1347 }
1348 time.Sleep(timeout)
1349 onuLogger.WithFields(log.Fields{
1350 "IntfId": o.PonPortID,
1351 "OnuId": o.ID,
1352 "SerialNumber": o.Sn(),
1353 }).Debug("power-on-onu")
1354 if err := o.HandlePowerOnONU(); err != nil {
1355 return err
1356 }
1357 return nil
1358}
1359
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001360// BBR methods
1361
1362func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1363 omciMsg := openolt.OmciMsg{
1364 IntfId: intfId,
1365 OnuId: onuId,
1366 Pkt: pktBytes,
1367 }
1368
1369 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1370 log.WithFields(log.Fields{
1371 "IntfId": intfId,
1372 "OnuId": onuId,
1373 "SerialNumber": common.OnuSnToString(sn),
1374 "Pkt": omciMsg.Pkt,
1375 }).Fatalf("Failed to send MIB Reset")
1376 }
1377 log.WithFields(log.Fields{
1378 "IntfId": intfId,
1379 "OnuId": onuId,
1380 "SerialNumber": common.OnuSnToString(sn),
1381 "Pkt": omciMsg.Pkt,
1382 }).Tracef("Sent OMCI message %s", msgType)
1383}
1384
1385func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1386 var next uint16
1387 if len(highPriority) > 0 && highPriority[0] {
1388 next = onu.hpTid
1389 onu.hpTid += 1
1390 if onu.hpTid < 0x8000 {
1391 onu.hpTid = 0x8000
1392 }
1393 } else {
1394 next = onu.tid
1395 onu.tid += 1
1396 if onu.tid >= 0x8000 {
1397 onu.tid = 1
1398 }
1399 }
1400 return next
1401}
1402
1403// TODO move this method in responders/omcisim
Matteo Scandolo8a574812021-05-20 15:18:53 -07001404// StartOmci is called in BBR to start the OMCI state machine
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001405func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1406 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1407 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1408}
1409
Matteo Scandolof9d43412021-01-12 11:11:34 -08001410// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1411func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1412
1413 // we need to encode the packet in HEX
1414 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1415 hex.Encode(pkt, msg.OmciInd.Pkt)
1416 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1417 if err != nil {
1418 log.WithFields(log.Fields{
1419 "IntfId": o.PonPortID,
1420 "SerialNumber": o.Sn(),
1421 "omciPacket": msg.OmciInd.Pkt,
1422 }).Error("BBR Cannot parse OMCI packet")
1423 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001424
1425 log.WithFields(log.Fields{
1426 "IntfId": msg.OmciInd.IntfId,
1427 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001428 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001429 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001430 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301431 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001432 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001433 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001434 log.WithFields(log.Fields{
1435 "IntfId": msg.OmciInd.IntfId,
1436 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001437 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001438 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001439 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001440 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001441 case omci.MibResetResponseType:
1442 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1443 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1444 case omci.MibUploadResponseType:
1445 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1446 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1447 case omci.MibUploadNextResponseType:
1448 o.seqNumber++
Matteo Scandolo8a574812021-05-20 15:18:53 -07001449 // once the mibUpload is complete send a SetRequest for the PPTP to enable the UNI
1450 // NOTE that in BBR we only enable the first UNI
1451 if o.seqNumber == o.MibDb.NumberOfCommands {
1452 meId := omcilib.GenerateUniPortEntityId(1)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001453
Matteo Scandolo8a574812021-05-20 15:18:53 -07001454 meParams := me.ParamData{
1455 EntityID: meId.ToUint16(),
1456 Attributes: me.AttributeValueMap{"AdministrativeState": 0},
1457 }
1458 managedEntity, omciError := me.NewPhysicalPathTerminationPointEthernetUni(meParams)
1459 if omciError.GetError() != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001460 onuLogger.WithFields(log.Fields{
1461 "OnuId": o.ID,
1462 "IntfId": o.PonPortID,
1463 "OnuSn": o.Sn(),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001464 }).Fatal(omciError.GetError())
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001465 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001466
1467 setPPtp, _ := omcilib.CreateSetRequest(managedEntity, 1)
1468 sendOmciMsg(setPPtp, o.PonPortID, o.ID, o.SerialNumber, "setRquest", client)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001469 } else {
1470 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1471 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001472 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001473 case omci.SetResponseType:
1474 // once we set the PPTP to active we can start sending flows
1475
1476 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
1477 onuLogger.WithFields(log.Fields{
1478 "OnuId": o.ID,
1479 "IntfId": o.PonPortID,
1480 "OnuSn": o.Sn(),
1481 }).Errorf("Error while transitioning ONU State %v", err)
1482 }
1483 case omci.AlarmNotificationType:
1484 log.Info("bbr-received-alarm")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001485 }
1486}
1487
1488func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1489
1490 classifierProto := openolt.Classifier{
1491 EthType: uint32(layers.EthernetTypeEAPOL),
1492 OVid: 4091,
1493 }
1494
1495 actionProto := openolt.Action{}
1496
1497 downstreamFlow := openolt.Flow{
1498 AccessIntfId: int32(o.PonPortID),
1499 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001500 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001501 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001502 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001503 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001504 Classifier: &classifierProto,
1505 Action: &actionProto,
1506 Priority: int32(100),
1507 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001508 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1509 // AllocId and GemPorts need to be unique per PON
1510 // for now use the ONU-ID, will need to change once we support multiple UNIs
1511 AllocId: int32(o.ID),
1512 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001513 }
1514
1515 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1516 log.WithFields(log.Fields{
1517 "IntfId": o.PonPortID,
1518 "OnuId": o.ID,
1519 "FlowId": downstreamFlow.FlowId,
1520 "PortNo": downstreamFlow.PortNo,
1521 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001522 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001523 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001524 }
1525 log.WithFields(log.Fields{
1526 "IntfId": o.PonPortID,
1527 "OnuId": o.ID,
1528 "FlowId": downstreamFlow.FlowId,
1529 "PortNo": downstreamFlow.PortNo,
1530 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1531 }).Info("Sent EAPOL Flow")
1532}
1533
1534func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001535
Matteo Scandolo8a574812021-05-20 15:18:53 -07001536 // BBR only works with a single UNI and a single service (ATT HSIA)
1537 hsia := o.UniPorts[0].(*UniPort).Services[0].(*Service)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001538 classifierProto := openolt.Classifier{
1539 EthType: uint32(layers.EthernetTypeIPv4),
1540 SrcPort: uint32(68),
1541 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001542 OVid: uint32(hsia.CTag),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001543 OPbits: 255,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001544 }
1545
1546 actionProto := openolt.Action{}
1547
1548 downstreamFlow := openolt.Flow{
1549 AccessIntfId: int32(o.PonPortID),
1550 OnuId: int32(o.ID),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001551 UniId: int32(0), // BBR only supports a single UNI
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001552 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001553 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001554 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001555 Classifier: &classifierProto,
1556 Action: &actionProto,
1557 Priority: int32(100),
1558 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001559 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1560 // AllocId and GemPorts need to be unique per PON
1561 // for now use the ONU-ID, will need to change once we support multiple UNIs
1562 AllocId: int32(o.ID),
1563 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001564 }
1565
1566 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1567 log.WithFields(log.Fields{
1568 "IntfId": o.PonPortID,
1569 "OnuId": o.ID,
1570 "FlowId": downstreamFlow.FlowId,
1571 "PortNo": downstreamFlow.PortNo,
1572 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001573 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001574 }).Fatalf("Failed to send DHCP Flow")
1575 }
1576 log.WithFields(log.Fields{
1577 "IntfId": o.PonPortID,
1578 "OnuId": o.ID,
1579 "FlowId": downstreamFlow.FlowId,
1580 "PortNo": downstreamFlow.PortNo,
1581 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1582 }).Info("Sent DHCP Flow")
1583}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301584
1585// DeleteFlow method search and delete flowKey from the onu flows slice
1586func (onu *Onu) DeleteFlow(key FlowKey) {
1587 for pos, flowKey := range onu.Flows {
1588 if flowKey == key {
1589 // delete the flowKey by shifting all flowKeys by one
1590 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1591 t := make([]FlowKey, len(onu.Flows))
1592 copy(t, onu.Flows)
1593 onu.Flows = t
1594 break
1595 }
1596 }
1597}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301598
1599func (onu *Onu) ReDiscoverOnu() {
1600 // Wait for few seconds to be sure of the cleanup
1601 time.Sleep(5 * time.Second)
1602
1603 onuLogger.WithFields(log.Fields{
1604 "IntfId": onu.PonPortID,
1605 "OnuId": onu.ID,
1606 "OnuSn": onu.Sn(),
1607 }).Debug("Send ONU Re-Discovery")
1608
1609 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001610 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301611 log.WithFields(log.Fields{
1612 "IntfId": onu.PonPortID,
1613 "OnuSn": onu.Sn(),
1614 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001615 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301616 }
1617
Matteo Scandolocedde462021-03-09 17:37:16 -08001618 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301619 log.WithFields(log.Fields{
1620 "IntfId": onu.PonPortID,
1621 "OnuSn": onu.Sn(),
1622 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001623 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301624 }
1625}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001626
Matteo Scandolo8a574812021-05-20 15:18:53 -07001627// deprecated, delegate this to the uniPort
Matteo Scandolo4a036262020-08-17 15:56:13 -07001628func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001629 // FIXME is there a better way to avoid this loop?
1630 for _, u := range onu.UniPorts {
1631 uni := u.(*UniPort)
1632 for _, s := range uni.Services {
1633 service := s.(*Service)
1634 if service.HwAddress.String() == macAddress.String() {
1635 return service, nil
1636 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001637 }
1638 }
1639 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1640}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301641
Matteo Scandolo8a574812021-05-20 15:18:53 -07001642func (onu *Onu) findUniByPortNo(portNo uint32) (*UniPort, error) {
1643 for _, u := range onu.UniPorts {
1644 uni := u.(*UniPort)
1645 if uni.PortNo == portNo {
1646 return uni, nil
1647 }
1648 }
1649 return nil, fmt.Errorf("cannot-find-uni-with-port-no-%d", portNo)
1650}
1651
Himani Chawla13b1ee02021-03-15 01:43:53 +05301652func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1653 switch alarmType {
1654 case "ONU_ALARM_LOS":
1655 msg := bbsim.Message{
1656 Type: bbsim.UniStatusAlarm,
1657 Data: bbsim.UniStatusAlarmMessage{
1658 OnuSN: o.SerialNumber,
1659 OnuID: o.ID,
1660 EntityID: 257,
1661 RaiseOMCIAlarm: raiseOMCIAlarm,
1662 },
1663 }
1664 o.Channel <- msg
1665 }
1666
1667}
1668
1669func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1670 o.onuAlarmsInfoLock.Lock()
1671 defer o.onuAlarmsInfoLock.Unlock()
1672 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1673 if alarmInfo.SequenceNo == 255 {
1674 alarmInfo.SequenceNo = 1
1675 } else {
1676 alarmInfo.SequenceNo++
1677 }
1678 o.onuAlarmsInfo[key] = alarmInfo
1679 return alarmInfo.SequenceNo
1680 } else {
1681 // This is the first time alarm notification message is being sent
1682 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1683 SequenceNo: 1,
1684 }
1685 return 1
1686 }
1687}