blob: 438993c55538c15415a6b06ec0ad9acbbc7616bc [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"
Mahir Gunyela1753ae2021-06-23 00:24:56 -070023 "sync"
24
Matteo Scandolo8a574812021-05-20 15:18:53 -070025 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
26 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
27 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000028
Matteo Scandolof9d43412021-01-12 11:11:34 -080029 pb "github.com/opencord/bbsim/api/bbsim"
30 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000031
32 "net"
33 "strconv"
34 "time"
Himani Chawla13b1ee02021-03-15 01:43:53 +053035
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080036 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
37 me "github.com/opencord/omci-lib-go/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010038
Matteo Scandolo3bc73742019-08-20 14:04:04 -070039 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070040 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070041 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070042 "github.com/opencord/bbsim/internal/common"
43 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolof9d43412021-01-12 11:11:34 -080044 "github.com/opencord/omci-lib-go"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000045 "github.com/opencord/voltha-protos/v5/go/openolt"
46 "github.com/opencord/voltha-protos/v5/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070047 log "github.com/sirupsen/logrus"
48)
49
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070050var onuLogger = log.WithFields(log.Fields{
51 "module": "ONU",
52})
53
Matteo Scandolocedde462021-03-09 17:37:16 -080054const (
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000055 maxOmciMsgCounter = 10
56)
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
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700115 AdminLockState uint8 // 0 is enabled, 1 is disabled.
116
Matteo Scandolof9d43412021-01-12 11:11:34 -0800117 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700118
119 // OMCI params
Matteo Scandolocedde462021-03-09 17:37:16 -0800120 MibDataSync uint8
121 ImageSoftwareExpectedSections int
122 ImageSoftwareReceivedSections int
123 ActiveImageEntityId uint16
124 CommittedImageEntityId uint16
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700125 StandbyImageVersion string
126 ActiveImageVersion string
127 CommittedImageVersion string
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000128 OmciResponseRate uint8
129 OmciMsgCounter uint8
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800130
131 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700132 tid uint16
133 hpTid uint16
134 seqNumber uint16
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700135 MibDb *omcilib.MibDb
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700136
Anand S Katti09541352020-01-29 15:54:01 +0530137 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
138 TrafficSchedulers *tech_profile.TrafficSchedulers
Himani Chawla13b1ee02021-03-15 01:43:53 +0530139 onuAlarmsInfoLock sync.RWMutex
140 onuAlarmsInfo map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700141}
142
Matteo Scandolo99f18462019-10-28 14:14:28 -0700143func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700144 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700145}
146
Matteo Scandolo8a574812021-05-20 15:18:53 -0700147func 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 -0700148
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700149 o := Onu{
Matteo Scandolocedde462021-03-09 17:37:16 -0800150 ID: id,
151 PonPortID: pon.ID,
152 PonPort: pon,
Matteo Scandolocedde462021-03-09 17:37:16 -0800153 tid: 0x1,
154 hpTid: 0x8000,
155 seqNumber: 0,
156 DoneChannel: make(chan bool, 1),
157 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
158 Flows: []FlowKey{},
159 DiscoveryDelay: delay,
160 MibDataSync: 0,
161 ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
162 ImageSoftwareReceivedSections: 0,
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700163 //TODO this needs reworking, it's always 0 or 1, possibly base all on the version
164 ActiveImageEntityId: 0, // when we start the SoftwareImage with ID 0 is active and committed
165 CommittedImageEntityId: 0,
166 StandbyImageVersion: "BBSM_IMG_00000",
167 ActiveImageVersion: "BBSM_IMG_00001",
168 CommittedImageVersion: "BBSM_IMG_00001",
169 OmciResponseRate: olt.OmciResponseRate,
170 OmciMsgCounter: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700171 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800172 o.SerialNumber = NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700173 // NOTE this state machine is used to track the operational
174 // state as requested by VOLTHA
175 o.OperState = getOperStateFSM(func(e *fsm.Event) {
176 onuLogger.WithFields(log.Fields{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700177 "OnuId": o.ID,
178 "IntfId": o.PonPortID,
179 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700180 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
181 })
Himani Chawla13b1ee02021-03-15 01:43:53 +0530182 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700183 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
184 o.InternalState = fsm.NewFSM(
Matteo Scandolocedde462021-03-09 17:37:16 -0800185 OnuStateCreated,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700186 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700187 // DEVICE Lifecycle
Matteo Scandolocedde462021-03-09 17:37:16 -0800188 {Name: OnuTxInitialize, Src: []string{OnuStateCreated, OnuStateDisabled, OnuStatePonDisabled}, Dst: OnuStateInitialized},
189 {Name: OnuTxDiscover, Src: []string{OnuStateInitialized}, Dst: OnuStateDiscovered},
190 {Name: OnuTxEnable, Src: []string{OnuStateDiscovered, OnuStatePonDisabled}, Dst: OnuStateEnabled},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100191 // 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 -0800192 {Name: OnuTxDisable, Src: []string{OnuStateEnabled, OnuStatePonDisabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateDisabled},
Pragya Arya6a708d62020-01-01 17:17:20 +0530193 // 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 +0200194 {Name: OnuTxPonDisable, Src: []string{OnuStateEnabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted, OnuStateImageDownloadComplete}, Dst: OnuStatePonDisabled},
Matteo Scandolocedde462021-03-09 17:37:16 -0800195 // Software Image Download related states
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +0200196 {Name: OnuTxStartImageDownload, Src: []string{OnuStateEnabled, OnuStateImageDownloadComplete, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateImageDownloadStarted},
Matteo Scandolocedde462021-03-09 17:37:16 -0800197 {Name: OnuTxProgressImageDownload, Src: []string{OnuStateImageDownloadStarted}, Dst: OnuStateImageDownloadInProgress},
198 {Name: OnuTxCompleteImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadComplete},
199 {Name: OnuTxFailImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadError},
200 {Name: OnuTxActivateImage, Src: []string{OnuStateImageDownloadComplete}, Dst: OnuStateImageActivated},
201 {Name: OnuTxCommitImage, Src: []string{OnuStateEnabled}, Dst: OnuStateImageCommitted}, // the image is committed after a ONU reboot
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700202 // BBR States
203 // TODO add start OMCI state
Matteo Scandolocedde462021-03-09 17:37:16 -0800204 {Name: BbrOnuTxSendEapolFlow, Src: []string{OnuStateInitialized}, Dst: BbrOnuStateEapolFlowSent},
205 {Name: BbrOnuTxSendDhcpFlow, Src: []string{BbrOnuStateEapolFlowSent}, Dst: BbrOnuStateDhcpFlowSent},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700206 },
207 fsm.Callbacks{
208 "enter_state": func(e *fsm.Event) {
209 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700210 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700211 fmt.Sprintf("enter_%s", OnuStateInitialized): func(e *fsm.Event) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100212 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800213 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800214
Matteo Scandolocedde462021-03-09 17:37:16 -0800215 if err := o.OperState.Event(OnuTxEnable); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800216 onuLogger.WithFields(log.Fields{
217 "OnuId": o.ID,
218 "IntfId": o.PonPortID,
219 "OnuSn": o.Sn(),
220 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
221 }
222
Pragya Arya1cbefa42020-01-13 12:15:29 +0530223 if !isMock {
224 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700225 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530226 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100227 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700228 fmt.Sprintf("enter_%s", OnuStateDiscovered): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800229 msg := bbsim.Message{
230 Type: bbsim.OnuDiscIndication,
231 Data: bbsim.OnuDiscIndicationMessage{
232 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100233 },
234 }
235 o.Channel <- msg
236 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700237 fmt.Sprintf("enter_%s", OnuStateEnabled): func(event *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800238
239 if used, sn := o.PonPort.isOnuIdAllocated(o.ID); used {
240 onuLogger.WithFields(log.Fields{
241 "IntfId": o.PonPortID,
242 "OnuId": o.ID,
243 "SerialNumber": o.Sn(),
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700244 }).Errorf("onu-id-duplicated-with-%s", common.OnuSnToString(sn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800245 return
246 } else {
247 o.PonPort.storeOnuId(o.ID, o.SerialNumber)
248 }
249
Matteo Scandolof9d43412021-01-12 11:11:34 -0800250 msg := bbsim.Message{
251 Type: bbsim.OnuIndication,
252 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700253 OnuSN: o.SerialNumber,
254 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800255 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700256 },
257 }
258 o.Channel <- msg
259 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700260 fmt.Sprintf("enter_%s", OnuStateDisabled): func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700261
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700262 o.cleanupOnuState()
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700263
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700264 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800265 if err := o.OperState.Event("disable"); err != nil {
266 onuLogger.WithFields(log.Fields{
267 "OnuId": o.ID,
268 "IntfId": o.PonPortID,
269 "OnuSn": o.Sn(),
270 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
271 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700272 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800273 msg := bbsim.Message{
274 Type: bbsim.OnuIndication,
275 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700276 OnuSN: o.SerialNumber,
277 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800278 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700279 },
280 }
281 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530282
Matteo Scandolo8a574812021-05-20 15:18:53 -0700283 // disable the UNI ports
284 for _, uni := range o.UniPorts {
285 _ = uni.Disable()
286 }
287
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530288 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100289 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolo8a574812021-05-20 15:18:53 -0700290 // NOTE may need to wait for the UNIs to be down too before shutting down the channel
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530291 if len(o.FlowIds) == 0 {
292 close(o.Channel)
293 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700294 },
295 fmt.Sprintf("enter_%s", OnuStatePonDisabled): func(event *fsm.Event) {
296 o.cleanupOnuState()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700297 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700298 // BBR states
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700299 fmt.Sprintf("enter_%s", BbrOnuStateEapolFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800300 msg := bbsim.Message{
301 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700302 }
303 o.Channel <- msg
304 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700305 fmt.Sprintf("enter_%s", BbrOnuStateDhcpFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800306 msg := bbsim.Message{
307 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700308 }
309 o.Channel <- msg
310 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700311 },
312 )
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700313 onuLogger.WithFields(log.Fields{
314 "OnuId": o.ID,
315 "IntfId": o.PonPortID,
316 "OnuSn": o.Sn(),
317 "NumUni": olt.NumUni,
318 }).Debug("creating-uni-ports")
319 for i := 0; i < olt.NumUni; i++ {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700320 uni, err := NewUniPort(uint32(i), &o, nextCtag, nextStag)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700321 if err != nil {
322 onuLogger.WithFields(log.Fields{
323 "OnuId": o.ID,
324 "IntfId": o.PonPortID,
325 "OnuSn": o.Sn(),
326 "Err": err,
327 }).Fatal("cannot-create-uni-port")
328 }
329 o.UniPorts = append(o.UniPorts, uni)
330 }
331
332 mibDb, err := omcilib.GenerateMibDatabase(len(o.UniPorts))
333 if err != nil {
334 onuLogger.WithFields(log.Fields{
335 "OnuId": o.ID,
336 "IntfId": o.PonPortID,
337 "OnuSn": o.Sn(),
338 }).Fatal("cannot-generate-mibdb-for-onu")
339 }
340 o.MibDb = mibDb
341
Matteo Scandolo27428702019-10-11 16:21:16 -0700342 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700343}
344
William Kurkian0418bc82019-11-06 12:16:24 -0500345func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700346 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700347 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700348 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700349 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700350 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
351}
352
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700353// cleanupOnuState this method is to clean the local state when the ONU is disabled
354func (o *Onu) cleanupOnuState() {
355 // clean the ONU state
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700356 o.Flows = []FlowKey{}
357 o.PonPort.removeOnuId(o.ID)
358 o.PonPort.removeAllocId(o.SerialNumber)
359 o.PonPort.removeGemPortBySn(o.SerialNumber)
360
361 o.onuAlarmsInfoLock.Lock()
362 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
363 o.onuAlarmsInfoLock.Unlock()
364}
365
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100366// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000367func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700368 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100369 "onuID": o.ID,
370 "onuSN": o.Sn(),
371 "ponPort": o.PonPortID,
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700372 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100373 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700374
Matteo Scandolob307d8a2021-05-10 15:19:27 -0700375 defer onuLogger.WithFields(log.Fields{
376 "onuID": o.ID,
377 "onuSN": o.Sn(),
378 "stream": stream,
379 }).Debug("Stopped handling ONU Indication Channel")
380
David Bainbridge103cf022019-12-16 20:11:35 +0000381loop:
382 for {
383 select {
384 case <-ctx.Done():
385 onuLogger.WithFields(log.Fields{
386 "onuID": o.ID,
387 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700388 }).Debug("ONU message handling canceled via context")
389 break loop
390 case <-stream.Context().Done():
391 onuLogger.WithFields(log.Fields{
392 "onuID": o.ID,
393 "onuSN": o.Sn(),
394 }).Debug("ONU message handling canceled via stream context")
David Bainbridge103cf022019-12-16 20:11:35 +0000395 break loop
396 case message, ok := <-o.Channel:
397 if !ok || ctx.Err() != nil {
398 onuLogger.WithFields(log.Fields{
399 "onuID": o.ID,
400 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700401 }).Debug("ONU message handling canceled via channel close")
David Bainbridge103cf022019-12-16 20:11:35 +0000402 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700403 }
David Bainbridge103cf022019-12-16 20:11:35 +0000404 onuLogger.WithFields(log.Fields{
405 "onuID": o.ID,
406 "onuSN": o.Sn(),
407 "messageType": message.Type,
408 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700409
David Bainbridge103cf022019-12-16 20:11:35 +0000410 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800411 case bbsim.OnuDiscIndication:
412 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000413 // 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 +0530414 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000415 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800416 case bbsim.OnuIndication:
417 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000418 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800419 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800420 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800421 msg, _ := message.Data.(bbsim.OmciMessage)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200422 _ = o.handleOmciRequest(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800423 case bbsim.UniStatusAlarm:
424 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530425 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
426 MeInstance: msg.EntityID,
427 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
428 }
429 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
430 o.onuAlarmsInfoLock.Lock()
431 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
432 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
433 if pkt != nil { //pkt will be nil if we are unable to create the alarm
434 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
435 onuLogger.WithFields(log.Fields{
436 "IntfId": o.PonPortID,
437 "SerialNumber": o.Sn(),
438 "omciPacket": pkt,
439 "adminState": msg.AdminState,
440 "entityID": msg.EntityID,
441 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
442 alarmInfo.SequenceNo--
443 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800444 onuLogger.WithFields(log.Fields{
445 "IntfId": o.PonPortID,
446 "SerialNumber": o.Sn(),
447 "omciPacket": pkt,
448 "adminState": msg.AdminState,
449 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530450 }).Trace("UNI-Link-alarm-sent")
451 if alarmBitMap == [28]byte{0} {
452 delete(o.onuAlarmsInfo, onuAlarmMapKey)
453 } else {
454 alarmInfo.AlarmBitMap = alarmBitMap
455 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
456 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800457 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530458 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800459 case bbsim.FlowAdd:
460 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700461 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800462 case bbsim.FlowRemoved:
463 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700464 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800465 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700466
Matteo Scandolof9d43412021-01-12 11:11:34 -0800467 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000468
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700469 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000470 "IntfId": msg.IntfId,
471 "OnuId": msg.OnuId,
472 "pktType": msg.Type,
473 }).Trace("Received OnuPacketOut Message")
474
Matteo Scandolo8a574812021-05-20 15:18:53 -0700475 uni, err := o.findUniByPortNo(msg.PortNo)
Matteo Scandolo618a6582020-09-09 12:21:29 -0700476
Matteo Scandolo8a574812021-05-20 15:18:53 -0700477 if err != nil {
478 onuLogger.WithFields(log.Fields{
479 "IntfId": msg.IntfId,
480 "OnuId": msg.OnuId,
481 "pktType": msg.Type,
482 "portNo": msg.PortNo,
483 "MacAddress": msg.MacAddress,
484 "Pkt": hex.EncodeToString(msg.Packet.Data()),
485 "OnuSn": o.Sn(),
486 }).Error("Cannot find Uni associated with packet")
487 return
David Bainbridge103cf022019-12-16 20:11:35 +0000488 }
Matteo Scandolo8a574812021-05-20 15:18:53 -0700489 uni.PacketCh <- msg
490 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800491 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000492 // NOTE we only receive BBR packets here.
493 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
494 // 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 -0800495 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000496
Matteo Scandolo8a574812021-05-20 15:18:53 -0700497 onuLogger.WithFields(log.Fields{
498 "IntfId": msg.IntfId,
499 "OnuId": msg.OnuId,
500 "PortNo": msg.PortNo,
501 "GemPortId": msg.GemPortId,
502 "pktType": msg.Type,
David Bainbridge103cf022019-12-16 20:11:35 +0000503 }).Trace("Received OnuPacketIn Message")
504
Matteo Scandolo8a574812021-05-20 15:18:53 -0700505 uni, err := o.findUniByPortNo(msg.PortNo)
506 if err != nil {
507 onuLogger.WithFields(log.Fields{
508 "IntfId": msg.IntfId,
509 "OnuId": msg.OnuId,
510 "PortNo": msg.PortNo,
511 "GemPortId": msg.GemPortId,
512 "pktType": msg.Type,
513 }).Error(err.Error())
514 }
515
516 // BBR has one service and one UNI
517 serviceId := uint32(0)
518 oltId := 0
David Bainbridge103cf022019-12-16 20:11:35 +0000519 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700520 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 +0000521 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700522 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000523 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800524 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800525 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800526 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
527 o.handleOmciResponse(msg, client)
528 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000529 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800530 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000531 o.sendDhcpFlow(client)
532 default:
533 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700534 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700535 }
536 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700537}
538
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800539func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700540 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700541 sn.VendorId = []byte("BBSM")
542 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700543 return sn
544}
545
Matteo Scandolof9d43412021-01-12 11:11:34 -0800546func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700547 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800548 IntfId: o.PonPortID,
549 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700550 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700551
Matteo Scandolo4747d292019-08-05 11:50:18 -0700552 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700553 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700554 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700555 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700556
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700557 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800558 "IntfId": o.PonPortID,
559 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700560 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700561 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800562 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800563
564 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
565 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800566 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800567 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800568 o.sendOnuDiscIndication(msg, stream)
569 }
570 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700571}
572
Matteo Scandolof9d43412021-01-12 11:11:34 -0800573func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800574 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
575 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700576
577 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700578 IntfId: o.PonPortID,
579 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700580 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700581 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700582 SerialNumber: o.SerialNumber,
583 }}
584 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800585 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700586 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700587 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700588 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700589 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800590 "IntfId": o.PonPortID,
591 "OnuId": o.ID,
592 "VolthaOnuId": msg.OnuID,
593 "OperState": msg.OperState.String(),
594 "AdminState": msg.OperState.String(),
595 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700596 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700597
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700598}
599
Matteo Scandolof9d43412021-01-12 11:11:34 -0800600func (o *Onu) HandleShutdownONU() error {
601
602 dyingGasp := pb.ONUAlarmRequest{
603 AlarmType: "DYING_GASP",
604 SerialNumber: o.Sn(),
605 Status: "on",
606 }
607
608 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
609 onuLogger.WithFields(log.Fields{
610 "OnuId": o.ID,
611 "IntfId": o.PonPortID,
612 "OnuSn": o.Sn(),
613 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
614 return err
615 }
616
617 losReq := pb.ONUAlarmRequest{
618 AlarmType: "ONU_ALARM_LOS",
619 SerialNumber: o.Sn(),
620 Status: "on",
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
630 return err
631 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530632 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800633 // TODO if it's the last ONU on the PON, then send a PON LOS
634
Matteo Scandolocedde462021-03-09 17:37:16 -0800635 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800636 onuLogger.WithFields(log.Fields{
637 "OnuId": o.ID,
638 "IntfId": o.PonPortID,
639 "OnuSn": o.Sn(),
640 }).Errorf("Cannot shutdown ONU: %s", err.Error())
641 return err
642 }
643
644 return nil
645}
646
647func (o *Onu) HandlePowerOnONU() error {
648 intitalState := o.InternalState.Current()
649
650 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800651 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
652 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800653 onuLogger.WithFields(log.Fields{
654 "OnuId": o.ID,
655 "IntfId": o.PonPortID,
656 "OnuSn": o.Sn(),
657 }).Errorf("Cannot poweron ONU: %s", err.Error())
658 return err
659 }
660 }
661
662 // turn off the LOS Alarm
663 losReq := pb.ONUAlarmRequest{
664 AlarmType: "ONU_ALARM_LOS",
665 SerialNumber: o.Sn(),
666 Status: "off",
667 }
668
669 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
670 onuLogger.WithFields(log.Fields{
671 "OnuId": o.ID,
672 "IntfId": o.PonPortID,
673 "OnuSn": o.Sn(),
674 }).Errorf("Cannot send LOS: %s", err.Error())
675 return err
676 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530677 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800678
679 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800680 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800681 onuLogger.WithFields(log.Fields{
682 "OnuId": o.ID,
683 "IntfId": o.PonPortID,
684 "OnuSn": o.Sn(),
685 }).Errorf("Cannot poweron ONU: %s", err.Error())
686 return err
687 }
688
689 // move o directly to enable state only when its a powercycle case
690 // in case of first time o poweron o will be moved to enable on
691 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800692 if intitalState == OnuStateDisabled {
693 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800694 onuLogger.WithFields(log.Fields{
695 "OnuId": o.ID,
696 "IntfId": o.PonPortID,
697 "OnuSn": o.Sn(),
698 }).Errorf("Cannot enable ONU: %s", err.Error())
699 return err
700 }
701 }
702
703 return nil
704}
705
706func (o *Onu) SetAlarm(alarmType string, status string) error {
707 alarmReq := pb.ONUAlarmRequest{
708 AlarmType: alarmType,
709 SerialNumber: o.Sn(),
710 Status: status,
711 }
712
713 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
714 if err != nil {
715 return err
716 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530717 raiseAlarm := false
718 if alarmReq.Status == "on" {
719 raiseAlarm = true
720 }
721 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800722 return nil
723}
724
725func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530726 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700727 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530728 if err != nil {
729 log.Errorf("error in getting msgType %v", err)
730 return
731 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800732 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530733 o.seqNumber = 0
734 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800735 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530736 o.seqNumber++
737 if o.seqNumber > 290 {
738 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
739 }
740 }
741 }
742}
743
Matteo Scandolof9d43412021-01-12 11:11:34 -0800744// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
745// and generate the appropriate response to it
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200746func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800747
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700748 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700749 "omciMsgType": msg.OmciMsg.MessageType,
750 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
751 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700752 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700753 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800754 }).Trace("omci-message-decoded")
755
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000756 if o.OmciMsgCounter < maxOmciMsgCounter {
757 o.OmciMsgCounter++
758 } else {
759 o.OmciMsgCounter = 1
760 }
761 if o.OmciMsgCounter > o.OmciResponseRate {
762 onuLogger.WithFields(log.Fields{
763 "OmciMsgCounter": o.OmciMsgCounter,
764 "OmciResponseRate": o.OmciResponseRate,
765 "omciMsgType": msg.OmciMsg.MessageType,
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200766 }).Debug("skipping-omci-msg-response")
767 return fmt.Errorf("skipping-omci-msg-response-because-of-response-rate-%d", o.OmciResponseRate)
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000768 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800769 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800770 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700771 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800772 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800773 onuLogger.WithFields(log.Fields{
774 "IntfId": o.PonPortID,
775 "OnuId": o.ID,
776 "SerialNumber": o.Sn(),
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700777 }).Debug("received-mib-reset-request")
Matteo Scandolob5913142021-03-19 16:10:18 -0700778 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800779 o.MibDataSync = 0
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700780
781 // if the MIB reset is successful then remove all the stored AllocIds and GemPorts
782 o.PonPort.removeAllocId(o.SerialNumber)
783 o.PonPort.removeGemPortBySn(o.SerialNumber)
Girish Gowdrae2683102021-03-05 08:24:26 -0800784 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800785 case omci.MibUploadRequestType:
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700786 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID, o.MibDb.NumberOfCommands)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800787 case omci.MibUploadNextRequestType:
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700788 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync, o.MibDb)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800789 case omci.GetRequestType:
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700790 onuDown := o.AdminLockState == 1
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700791 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId,
792 o.CommittedImageEntityId, o.StandbyImageVersion, o.ActiveImageVersion, o.CommittedImageVersion, onuDown)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800793 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800794 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700795 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800796 switch msgObj.EntityClass {
797 case me.PhysicalPathTerminationPointEthernetUniClassID:
798 // if we're Setting a PPTP state
Matteo Scandolo8a574812021-05-20 15:18:53 -0700799 // we need to send the appropriate alarm (handled in the UNI struct)
800 uni, err := o.FindUniByEntityId(msgObj.EntityInstance)
801 if err != nil {
802 onuLogger.Error(err)
803 success = false
804 } else {
805 // 1 locks the UNI, 0 unlocks it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800806 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Matteo Scandolo8a574812021-05-20 15:18:53 -0700807 var err error
Himani Chawla13b1ee02021-03-15 01:43:53 +0530808 if adminState == 1 {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700809 err = uni.Disable()
Girish Gowdra996d81e2021-04-21 16:16:27 -0700810 } else {
Matteo Scandolo8a574812021-05-20 15:18:53 -0700811 err = uni.Enable()
Himani Chawla13b1ee02021-03-15 01:43:53 +0530812 }
Matteo Scandolo8a574812021-05-20 15:18:53 -0700813 if err != nil {
814 onuLogger.WithFields(log.Fields{
815 "IntfId": o.PonPortID,
816 "OnuId": o.ID,
817 "UniMeId": uni.MeId,
818 "UniId": uni.ID,
819 "SerialNumber": o.Sn(),
820 "Err": err.Error(),
821 }).Warn("cannot-change-uni-status")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800822 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800823 }
Girish Gowdra1ddcb202021-06-25 12:17:09 -0700824 case me.OnuGClassID:
825 o.AdminLockState = msgObj.Attributes["AdministrativeState"].(uint8)
826 onuLogger.WithFields(log.Fields{
827 "IntfId": o.PonPortID,
828 "OnuId": o.ID,
829 "SerialNumber": o.Sn(),
830 "AdminLockState": o.AdminLockState,
831 }).Debug("set-onu-admin-lock-state")
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800832 case me.TContClassID:
833 allocId := msgObj.Attributes["AllocId"].(uint16)
834
835 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
836 // otherwise we are adding it
837 if allocId == 255 || allocId == 65535 {
838 onuLogger.WithFields(log.Fields{
839 "IntfId": o.PonPortID,
840 "OnuId": o.ID,
841 "TContId": msgObj.EntityInstance,
842 "AllocId": allocId,
843 "SerialNumber": o.Sn(),
844 }).Trace("freeing-alloc-id-via-omci")
845 o.PonPort.removeAllocId(o.SerialNumber)
846 } else {
847 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
848 onuLogger.WithFields(log.Fields{
849 "IntfId": o.PonPortID,
850 "OnuId": o.ID,
851 "AllocId": allocId,
852 "SerialNumber": o.Sn(),
853 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
854 success = false
855 } else {
856 onuLogger.WithFields(log.Fields{
857 "IntfId": o.PonPortID,
858 "OnuId": o.ID,
859 "TContId": msgObj.EntityInstance,
860 "AllocId": allocId,
861 "SerialNumber": o.Sn(),
862 }).Trace("storing-alloc-id-via-omci")
863 o.PonPort.storeAllocId(allocId, o.SerialNumber)
864 }
865 }
866
867 }
868
869 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700870 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800871 o.MibDataSync++
872 }
873 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700874 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800875 }
876 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800877 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
878 var used bool
879 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700880 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800881 if err == nil {
882 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700883 // GemPort 4069 is reserved for multicast and shared across ONUs
884 if msgObj.EntityInstance != 4069 {
885 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
886 onuLogger.WithFields(log.Fields{
887 "IntfId": o.PonPortID,
888 "OnuId": o.ID,
889 "GemPortId": msgObj.EntityInstance,
890 "SerialNumber": o.Sn(),
891 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
892 } else {
893 onuLogger.WithFields(log.Fields{
894 "IntfId": o.PonPortID,
895 "OnuId": o.ID,
896 "GemPortId": msgObj.EntityInstance,
897 "SerialNumber": o.Sn(),
898 }).Trace("storing-gem-port-id-via-omci")
899 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
900 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800901 }
902 }
903 }
904
905 // if the gemPort is valid then increment the MDS and return a successful response
906 // otherwise fail the request
907 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
908 // validation this check will need to be rewritten
909 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700910 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800911 o.MibDataSync++
912 }
913 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700914 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800915 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800916 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700917 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800918 if err == nil {
919 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
920 onuLogger.WithFields(log.Fields{
921 "IntfId": o.PonPortID,
922 "OnuId": o.ID,
923 "GemPortId": msgObj.EntityInstance,
924 "SerialNumber": o.Sn(),
925 }).Trace("freeing-gem-port-id-via-omci")
926 o.PonPort.removeGemPort(msgObj.EntityInstance)
927 }
928 }
929
Matteo Scandolob5913142021-03-19 16:10:18 -0700930 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800931 o.MibDataSync++
932 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800933 case omci.RebootRequestType:
934
Matteo Scandolob5913142021-03-19 16:10:18 -0700935 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800936
937 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800938 // we run this in a separate goroutine so that
939 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800940 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800941 if err := o.Reboot(10 * time.Second); err != nil {
942 log.WithFields(log.Fields{
943 "IntfId": o.PonPortID,
944 "OnuId": o.ID,
945 "SerialNumber": o.Sn(),
946 "err": err,
947 }).Error("cannot-reboot-onu-after-omci-reboot-request")
948 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800949 }()
950 case omci.TestRequestType:
Girish Gowdra161d27a2021-05-05 12:01:44 -0700951 var classID me.ClassID
952 var omciResult me.Results
953 var instID uint16
954 responsePkt, errResp, classID, instID, omciResult = omcilib.CreateTestResponse(msg.OmciPkt, msg.OmciMsg)
955 // Send TestResult only in case the TestResponse omci result code is me.Success
956 if responsePkt != nil && errResp == nil && omciResult == me.Success {
957 if testResultPkt, err := omcilib.CreateTestResult(classID, instID, msg.OmciMsg.TransactionID); err == nil {
958 // send test results asynchronously
959 go func() {
960 // Send test results after a second to emulate async behavior
961 time.Sleep(1 * time.Second)
962 if testResultPkt != nil {
963 if err := o.sendOmciIndication(testResultPkt, msg.OmciMsg.TransactionID, stream); err != nil {
964 onuLogger.WithFields(log.Fields{
965 "IntfId": o.PonPortID,
966 "SerialNumber": o.Sn(),
967 "omciPacket": testResultPkt,
968 "msg.OmciMsgType": msg.OmciMsg.MessageType,
969 "transCorrId": msg.OmciMsg.TransactionID,
970 }).Errorf("failed-to-send-omci-message: %v", err)
971 }
972 }
973 }()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800974 }
975 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800976 case omci.SynchronizeTimeRequestType:
977 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700978 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800979 case omci.StartSoftwareDownloadRequestType:
980
981 o.ImageSoftwareReceivedSections = 0
982
Matteo Scandolob5913142021-03-19 16:10:18 -0700983 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800984
Matteo Scandolob5913142021-03-19 16:10:18 -0700985 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800986 o.MibDataSync++
987 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
988 onuLogger.WithFields(log.Fields{
989 "OnuId": o.ID,
990 "IntfId": o.PonPortID,
991 "OnuSn": o.Sn(),
992 "Err": err.Error(),
993 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
994 }
995 } else {
996 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700997 "OmciMsgType": msg.OmciMsg.MessageType,
998 "TransCorrId": msg.OmciMsg.TransactionID,
999 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001000 "IntfId": o.PonPortID,
1001 "SerialNumber": o.Sn(),
1002 }).Error("error-while-processing-start-software-download-request")
1003 }
1004 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001005 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001006 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001007 "OmciMsgType": msg.OmciMsg.MessageType,
1008 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001009 "EntityInstance": msgObj.EntityInstance,
1010 "SectionNumber": msgObj.SectionNumber,
1011 "SectionData": msgObj.SectionData,
1012 }).Trace("received-download-section-request")
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001013 //Extracting the first 14 bytes to use as a version for this image.
1014 if o.ImageSoftwareReceivedSections == 0 {
1015 o.StandbyImageVersion = string(msgObj.SectionData[0:14])
1016 }
Matteo Scandolocedde462021-03-09 17:37:16 -08001017 o.ImageSoftwareReceivedSections++
1018 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
1019 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
1020 onuLogger.WithFields(log.Fields{
1021 "OnuId": o.ID,
1022 "IntfId": o.PonPortID,
1023 "OnuSn": o.Sn(),
1024 "Err": err.Error(),
1025 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
1026 }
1027 }
1028 }
1029 case omci.DownloadSectionRequestWithResponseType:
1030 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -07001031 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
1032 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001033 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001034 "OmciMsgType": msg.OmciMsg.MessageType,
1035 "TransCorrId": msg.OmciMsg.TransactionID,
1036 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001037 "IntfId": o.PonPortID,
1038 "SerialNumber": o.Sn(),
1039 }).Error("error-while-processing-create-download-section-response")
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001040 return fmt.Errorf("error-while-processing-create-download-section-response: %s", errResp.Error())
Matteo Scandolocedde462021-03-09 17:37:16 -08001041 }
1042 o.ImageSoftwareReceivedSections++
Matteo Scandolocedde462021-03-09 17:37:16 -08001043 case omci.EndSoftwareDownloadRequestType:
1044
1045 // In the startSoftwareDownload we get the image size and the window size.
1046 // We calculate how many DownloadSection we should receive and validate
1047 // that we got the correct amount when we receive this message
1048 success := true
1049 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
1050 onuLogger.WithFields(log.Fields{
1051 "OnuId": o.ID,
1052 "IntfId": o.PonPortID,
1053 "OnuSn": o.Sn(),
1054 "ExpectedSections": o.ImageSoftwareExpectedSections,
1055 "ReceivedSections": o.ImageSoftwareReceivedSections,
1056 }).Errorf("onu-did-not-receive-all-image-sections")
1057 success = false
1058 }
1059
1060 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001061 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001062 o.MibDataSync++
1063 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1064 onuLogger.WithFields(log.Fields{
1065 "OnuId": o.ID,
1066 "IntfId": o.PonPortID,
1067 "OnuSn": o.Sn(),
1068 "Err": err.Error(),
1069 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1070 }
1071 } else {
1072 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001073 "OmciMsgType": msg.OmciMsg.MessageType,
1074 "TransCorrId": msg.OmciMsg.TransactionID,
1075 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001076 "IntfId": o.PonPortID,
1077 "SerialNumber": o.Sn(),
1078 }).Error("error-while-processing-end-software-download-request")
1079 }
1080 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001081 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001082 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1083 onuLogger.WithFields(log.Fields{
1084 "OnuId": o.ID,
1085 "IntfId": o.PonPortID,
1086 "OnuSn": o.Sn(),
1087 "Err": err.Error(),
1088 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1089 }
1090 }
1091 }
Matteo Scandolocedde462021-03-09 17:37:16 -08001092 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001093 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001094 o.MibDataSync++
1095 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1096 onuLogger.WithFields(log.Fields{
1097 "OnuId": o.ID,
1098 "IntfId": o.PonPortID,
1099 "OnuSn": o.Sn(),
1100 "Err": err.Error(),
1101 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1102 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001103 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001104 o.ActiveImageEntityId = msgObj.EntityInstance
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001105 previousActiveImage := o.ActiveImageVersion
1106 o.ActiveImageVersion = o.StandbyImageVersion
1107 o.StandbyImageVersion = previousActiveImage
Matteo Scandolocedde462021-03-09 17:37:16 -08001108 } else {
1109 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
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-activated")
1118
1119 // powercycle the ONU
1120 // we run this in a separate goroutine so that
1121 // the ActivateSoftwareResponse is sent to VOLTHA
1122 // NOTE do we need to wait before rebooting?
1123 go func() {
1124 if err := o.Reboot(10 * time.Second); err != nil {
1125 log.WithFields(log.Fields{
1126 "IntfId": o.PonPortID,
1127 "OnuId": o.ID,
1128 "SerialNumber": o.Sn(),
1129 "err": err,
1130 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1131 }
1132 }()
1133 }
1134 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001135 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001136 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001137 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001138 // TODO validate that the image to commit is:
1139 // - active
1140 // - not already committed
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001141 o.ActiveImageEntityId = msgObj.EntityInstance
Matteo Scandolocedde462021-03-09 17:37:16 -08001142 o.CommittedImageEntityId = msgObj.EntityInstance
Matteo Scandoloc00e97a2021-05-27 11:45:27 -07001143 //committed becomes standby
1144 o.StandbyImageVersion = o.CommittedImageVersion
1145 o.CommittedImageVersion = o.ActiveImageVersion
Matteo Scandolocedde462021-03-09 17:37:16 -08001146 } else {
1147 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1148 }
1149 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1150 onuLogger.WithFields(log.Fields{
1151 "OnuId": o.ID,
1152 "IntfId": o.PonPortID,
1153 "OnuSn": o.Sn(),
1154 "Err": err.Error(),
1155 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1156 }
1157 onuLogger.WithFields(log.Fields{
1158 "OnuId": o.ID,
1159 "IntfId": o.PonPortID,
1160 "OnuSn": o.Sn(),
1161 "ActiveImageEntityId": o.ActiveImageEntityId,
1162 "CommittedImageEntityId": o.CommittedImageEntityId,
1163 }).Info("onu-software-image-committed")
1164 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301165 case omci.GetAllAlarmsRequestType:
1166 // Reset the alarm sequence number on receiving get all alarms request.
1167 o.onuAlarmsInfoLock.Lock()
1168 for key, alarmInfo := range o.onuAlarmsInfo {
1169 // reset the alarm sequence no
1170 alarmInfo.SequenceNo = 0
1171 o.onuAlarmsInfo[key] = alarmInfo
1172 }
1173 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001174 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301175 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001176 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301177 responsePkt = nil //Do not send any response for error case
1178 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001179 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001180 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001181 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1182 "omciPkt": msg.OmciPkt,
1183 "omciMsgType": msg.OmciMsg.MessageType,
1184 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001185 "IntfId": o.PonPortID,
1186 "SerialNumber": o.Sn(),
1187 }).Warnf("OMCI-message-not-supported")
1188 }
1189
1190 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001191 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001192 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001193 "IntfId": o.PonPortID,
1194 "SerialNumber": o.Sn(),
1195 "omciPacket": responsePkt,
1196 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1197 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001198 }).Errorf("failed-to-send-omci-message: %v", err)
1199 }
1200 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001201
Pragya Arya324337e2020-02-20 14:35:08 +05301202 o.publishOmciEvent(msg)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001203 return nil
Matteo Scandolof9d43412021-01-12 11:11:34 -08001204}
Pragya Arya324337e2020-02-20 14:35:08 +05301205
Matteo Scandolof9d43412021-01-12 11:11:34 -08001206// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1207func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1208 indication := &openolt.Indication_OmciInd{
1209 OmciInd: &openolt.OmciIndication{
1210 IntfId: o.PonPortID,
1211 OnuId: o.ID,
1212 Pkt: responsePkt,
1213 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001214 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001215 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1216 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001217 }
1218 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001219 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001220 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001221 "omciPacket": indication.OmciInd.Pkt,
1222 "transCorrId": txId,
1223 }).Trace("omci-message-sent")
1224 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001225}
1226
Matteo Scandolo8a574812021-05-20 15:18:53 -07001227// FindUniById retrieves a UNI by ID
1228func (o *Onu) FindUniById(uniID uint32) (*UniPort, error) {
1229 for _, u := range o.UniPorts {
1230 uni := u.(*UniPort)
1231 if uni.ID == uniID {
1232 return uni, nil
1233 }
Matteo Scandolo27428702019-10-11 16:21:16 -07001234 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001235 return nil, fmt.Errorf("cannot-find-uni-with-id-%d-on-onu-%s", uniID, o.Sn())
1236}
1237
1238// FindUniByEntityId retrieves a uni by MeID (the OMCI entity ID)
1239func (o *Onu) FindUniByEntityId(meId uint16) (*UniPort, error) {
1240 entityId := omcilib.EntityID{}.FromUint16(meId)
1241 for _, u := range o.UniPorts {
1242 uni := u.(*UniPort)
1243 if uni.MeId.Equals(entityId) {
1244 return uni, nil
1245 }
1246 }
1247 return nil, fmt.Errorf("cannot-find-uni-with-meid-%s-on-onu-%s", entityId.ToString(), o.Sn())
Matteo Scandolo27428702019-10-11 16:21:16 -07001248}
1249
William Kurkian0418bc82019-11-06 12:16:24 -05001250func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001251 onuLogger.WithFields(log.Fields{
1252 "IntfId": o.PonPortID,
1253 "OnuId": id,
1254 "SerialNumber": o.Sn(),
1255 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001256 o.ID = id
1257}
1258
Matteo Scandolof9d43412021-01-12 11:11:34 -08001259func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001260 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001261 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001262 "Cookie": msg.Flow.Cookie,
1263 "DstPort": msg.Flow.Classifier.DstPort,
1264 "FlowId": msg.Flow.FlowId,
1265 "FlowType": msg.Flow.FlowType,
1266 "GemportId": msg.Flow.GemportId,
1267 "InnerVlan": msg.Flow.Classifier.IVid,
1268 "IntfId": msg.Flow.AccessIntfId,
1269 "IpProto": msg.Flow.Classifier.IpProto,
1270 "OnuId": msg.Flow.OnuId,
1271 "OnuSn": o.Sn(),
1272 "OuterVlan": msg.Flow.Classifier.OVid,
1273 "PortNo": msg.Flow.PortNo,
1274 "SrcPort": msg.Flow.Classifier.SrcPort,
1275 "UniID": msg.Flow.UniId,
1276 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1277 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1278 "ClassifierIVid": msg.Flow.Classifier.IVid,
1279 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001280 "ReplicateFlow": msg.Flow.ReplicateFlow,
1281 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001282 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001283
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001284 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001285
1286 var gemPortId uint32
1287 if msg.Flow.ReplicateFlow {
1288 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1289 // first available gemport (we only need to send one packet)
1290 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1291 gemPortId = msg.Flow.PbitToGemport[0]
1292 } else {
1293 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1294 gemPortId = uint32(msg.Flow.GemportId)
1295 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001296
1297 uni, err := o.FindUniById(uint32(msg.Flow.UniId))
1298 if err != nil {
1299 onuLogger.WithFields(log.Fields{
1300 "IntfId": o.PonPortID,
1301 "OnuId": o.ID,
1302 "UniId": msg.Flow.UniId,
1303 "PortNo": msg.Flow.PortNo,
1304 "SerialNumber": o.Sn(),
1305 "FlowId": msg.Flow.FlowId,
1306 "FlowType": msg.Flow.FlowType,
1307 }).Error("cannot-find-uni-port-for-flow")
1308 }
1309
1310 uni.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
1311 uni.StorePortNo(msg.Flow.PortNo)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001312
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001313 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001314 uni.HandleAuth()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001315 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1316 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001317 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001318 uni.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001319 }
1320}
1321
Matteo Scandolof9d43412021-01-12 11:11:34 -08001322func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001323 onuLogger.WithFields(log.Fields{
1324 "IntfId": o.PonPortID,
1325 "OnuId": o.ID,
1326 "SerialNumber": o.Sn(),
1327 "FlowId": msg.Flow.FlowId,
1328 "FlowType": msg.Flow.FlowType,
1329 }).Debug("ONU receives FlowRemove")
1330
1331 for idx, flow := range o.FlowIds {
1332 // If the gemport is found, delete it from local cache.
1333 if flow == msg.Flow.FlowId {
1334 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1335 break
1336 }
1337 }
1338
1339 if len(o.FlowIds) == 0 {
1340 onuLogger.WithFields(log.Fields{
1341 "IntfId": o.PonPortID,
1342 "OnuId": o.ID,
1343 "SerialNumber": o.Sn(),
1344 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001345
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301346 // check if ONU delete is performed and
1347 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001348 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301349 close(o.Channel)
1350 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001351 }
1352}
1353
Matteo Scandolocedde462021-03-09 17:37:16 -08001354func (o *Onu) Reboot(timeout time.Duration) error {
1355 onuLogger.WithFields(log.Fields{
1356 "IntfId": o.PonPortID,
1357 "OnuId": o.ID,
1358 "SerialNumber": o.Sn(),
1359 }).Debug("shutting-down-onu")
1360 if err := o.HandleShutdownONU(); err != nil {
1361 return err
1362 }
1363 time.Sleep(timeout)
1364 onuLogger.WithFields(log.Fields{
1365 "IntfId": o.PonPortID,
1366 "OnuId": o.ID,
1367 "SerialNumber": o.Sn(),
1368 }).Debug("power-on-onu")
1369 if err := o.HandlePowerOnONU(); err != nil {
1370 return err
1371 }
1372 return nil
1373}
1374
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001375// BBR methods
1376
1377func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1378 omciMsg := openolt.OmciMsg{
1379 IntfId: intfId,
1380 OnuId: onuId,
1381 Pkt: pktBytes,
1382 }
1383
1384 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1385 log.WithFields(log.Fields{
1386 "IntfId": intfId,
1387 "OnuId": onuId,
1388 "SerialNumber": common.OnuSnToString(sn),
1389 "Pkt": omciMsg.Pkt,
1390 }).Fatalf("Failed to send MIB Reset")
1391 }
1392 log.WithFields(log.Fields{
1393 "IntfId": intfId,
1394 "OnuId": onuId,
1395 "SerialNumber": common.OnuSnToString(sn),
1396 "Pkt": omciMsg.Pkt,
1397 }).Tracef("Sent OMCI message %s", msgType)
1398}
1399
1400func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1401 var next uint16
1402 if len(highPriority) > 0 && highPriority[0] {
1403 next = onu.hpTid
1404 onu.hpTid += 1
1405 if onu.hpTid < 0x8000 {
1406 onu.hpTid = 0x8000
1407 }
1408 } else {
1409 next = onu.tid
1410 onu.tid += 1
1411 if onu.tid >= 0x8000 {
1412 onu.tid = 1
1413 }
1414 }
1415 return next
1416}
1417
1418// TODO move this method in responders/omcisim
Matteo Scandolo8a574812021-05-20 15:18:53 -07001419// StartOmci is called in BBR to start the OMCI state machine
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001420func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1421 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1422 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1423}
1424
Matteo Scandolof9d43412021-01-12 11:11:34 -08001425// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1426func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1427
1428 // we need to encode the packet in HEX
1429 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1430 hex.Encode(pkt, msg.OmciInd.Pkt)
1431 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1432 if err != nil {
1433 log.WithFields(log.Fields{
1434 "IntfId": o.PonPortID,
1435 "SerialNumber": o.Sn(),
1436 "omciPacket": msg.OmciInd.Pkt,
1437 }).Error("BBR Cannot parse OMCI packet")
1438 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001439
1440 log.WithFields(log.Fields{
1441 "IntfId": msg.OmciInd.IntfId,
1442 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001443 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001444 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001445 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301446 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001447 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001448 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001449 log.WithFields(log.Fields{
1450 "IntfId": msg.OmciInd.IntfId,
1451 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001452 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001453 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001454 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001455 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001456 case omci.MibResetResponseType:
1457 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1458 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1459 case omci.MibUploadResponseType:
1460 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1461 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1462 case omci.MibUploadNextResponseType:
1463 o.seqNumber++
Matteo Scandolo8a574812021-05-20 15:18:53 -07001464 // once the mibUpload is complete send a SetRequest for the PPTP to enable the UNI
1465 // NOTE that in BBR we only enable the first UNI
1466 if o.seqNumber == o.MibDb.NumberOfCommands {
1467 meId := omcilib.GenerateUniPortEntityId(1)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001468
Matteo Scandolo8a574812021-05-20 15:18:53 -07001469 meParams := me.ParamData{
1470 EntityID: meId.ToUint16(),
1471 Attributes: me.AttributeValueMap{"AdministrativeState": 0},
1472 }
1473 managedEntity, omciError := me.NewPhysicalPathTerminationPointEthernetUni(meParams)
1474 if omciError.GetError() != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001475 onuLogger.WithFields(log.Fields{
1476 "OnuId": o.ID,
1477 "IntfId": o.PonPortID,
1478 "OnuSn": o.Sn(),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001479 }).Fatal(omciError.GetError())
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001480 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001481
1482 setPPtp, _ := omcilib.CreateSetRequest(managedEntity, 1)
1483 sendOmciMsg(setPPtp, o.PonPortID, o.ID, o.SerialNumber, "setRquest", client)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001484 } else {
1485 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1486 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001487 }
Matteo Scandolo8a574812021-05-20 15:18:53 -07001488 case omci.SetResponseType:
1489 // once we set the PPTP to active we can start sending flows
1490
1491 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
1492 onuLogger.WithFields(log.Fields{
1493 "OnuId": o.ID,
1494 "IntfId": o.PonPortID,
1495 "OnuSn": o.Sn(),
1496 }).Errorf("Error while transitioning ONU State %v", err)
1497 }
1498 case omci.AlarmNotificationType:
1499 log.Info("bbr-received-alarm")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001500 }
1501}
1502
1503func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1504
1505 classifierProto := openolt.Classifier{
1506 EthType: uint32(layers.EthernetTypeEAPOL),
1507 OVid: 4091,
1508 }
1509
1510 actionProto := openolt.Action{}
1511
1512 downstreamFlow := openolt.Flow{
1513 AccessIntfId: int32(o.PonPortID),
1514 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001515 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001516 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001517 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001518 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001519 Classifier: &classifierProto,
1520 Action: &actionProto,
1521 Priority: int32(100),
1522 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001523 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1524 // AllocId and GemPorts need to be unique per PON
1525 // for now use the ONU-ID, will need to change once we support multiple UNIs
1526 AllocId: int32(o.ID),
1527 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001528 }
1529
1530 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1531 log.WithFields(log.Fields{
1532 "IntfId": o.PonPortID,
1533 "OnuId": o.ID,
1534 "FlowId": downstreamFlow.FlowId,
1535 "PortNo": downstreamFlow.PortNo,
1536 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001537 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001538 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001539 }
1540 log.WithFields(log.Fields{
1541 "IntfId": o.PonPortID,
1542 "OnuId": o.ID,
1543 "FlowId": downstreamFlow.FlowId,
1544 "PortNo": downstreamFlow.PortNo,
1545 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1546 }).Info("Sent EAPOL Flow")
1547}
1548
1549func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001550
Matteo Scandolo8a574812021-05-20 15:18:53 -07001551 // BBR only works with a single UNI and a single service (ATT HSIA)
1552 hsia := o.UniPorts[0].(*UniPort).Services[0].(*Service)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001553 classifierProto := openolt.Classifier{
1554 EthType: uint32(layers.EthernetTypeIPv4),
1555 SrcPort: uint32(68),
1556 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001557 OVid: uint32(hsia.CTag),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001558 OPbits: 255,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001559 }
1560
1561 actionProto := openolt.Action{}
1562
1563 downstreamFlow := openolt.Flow{
1564 AccessIntfId: int32(o.PonPortID),
1565 OnuId: int32(o.ID),
Matteo Scandolo8a574812021-05-20 15:18:53 -07001566 UniId: int32(0), // BBR only supports a single UNI
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001567 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001568 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001569 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001570 Classifier: &classifierProto,
1571 Action: &actionProto,
1572 Priority: int32(100),
1573 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001574 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1575 // AllocId and GemPorts need to be unique per PON
1576 // for now use the ONU-ID, will need to change once we support multiple UNIs
1577 AllocId: int32(o.ID),
1578 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001579 }
1580
1581 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1582 log.WithFields(log.Fields{
1583 "IntfId": o.PonPortID,
1584 "OnuId": o.ID,
1585 "FlowId": downstreamFlow.FlowId,
1586 "PortNo": downstreamFlow.PortNo,
1587 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001588 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001589 }).Fatalf("Failed to send DHCP Flow")
1590 }
1591 log.WithFields(log.Fields{
1592 "IntfId": o.PonPortID,
1593 "OnuId": o.ID,
1594 "FlowId": downstreamFlow.FlowId,
1595 "PortNo": downstreamFlow.PortNo,
1596 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1597 }).Info("Sent DHCP Flow")
1598}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301599
1600// DeleteFlow method search and delete flowKey from the onu flows slice
1601func (onu *Onu) DeleteFlow(key FlowKey) {
1602 for pos, flowKey := range onu.Flows {
1603 if flowKey == key {
1604 // delete the flowKey by shifting all flowKeys by one
1605 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1606 t := make([]FlowKey, len(onu.Flows))
1607 copy(t, onu.Flows)
1608 onu.Flows = t
1609 break
1610 }
1611 }
1612}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301613
1614func (onu *Onu) ReDiscoverOnu() {
1615 // Wait for few seconds to be sure of the cleanup
1616 time.Sleep(5 * time.Second)
1617
1618 onuLogger.WithFields(log.Fields{
1619 "IntfId": onu.PonPortID,
1620 "OnuId": onu.ID,
1621 "OnuSn": onu.Sn(),
1622 }).Debug("Send ONU Re-Discovery")
1623
1624 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001625 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301626 log.WithFields(log.Fields{
1627 "IntfId": onu.PonPortID,
1628 "OnuSn": onu.Sn(),
1629 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001630 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301631 }
1632
Matteo Scandolocedde462021-03-09 17:37:16 -08001633 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301634 log.WithFields(log.Fields{
1635 "IntfId": onu.PonPortID,
1636 "OnuSn": onu.Sn(),
1637 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001638 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301639 }
1640}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001641
Matteo Scandolo8a574812021-05-20 15:18:53 -07001642// deprecated, delegate this to the uniPort
Matteo Scandolo4a036262020-08-17 15:56:13 -07001643func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
Matteo Scandolo8a574812021-05-20 15:18:53 -07001644 // FIXME is there a better way to avoid this loop?
1645 for _, u := range onu.UniPorts {
1646 uni := u.(*UniPort)
1647 for _, s := range uni.Services {
1648 service := s.(*Service)
1649 if service.HwAddress.String() == macAddress.String() {
1650 return service, nil
1651 }
Matteo Scandolo4a036262020-08-17 15:56:13 -07001652 }
1653 }
1654 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1655}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301656
Matteo Scandolo8a574812021-05-20 15:18:53 -07001657func (onu *Onu) findUniByPortNo(portNo uint32) (*UniPort, error) {
1658 for _, u := range onu.UniPorts {
1659 uni := u.(*UniPort)
1660 if uni.PortNo == portNo {
1661 return uni, nil
1662 }
1663 }
1664 return nil, fmt.Errorf("cannot-find-uni-with-port-no-%d", portNo)
1665}
1666
Himani Chawla13b1ee02021-03-15 01:43:53 +05301667func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1668 switch alarmType {
1669 case "ONU_ALARM_LOS":
1670 msg := bbsim.Message{
1671 Type: bbsim.UniStatusAlarm,
1672 Data: bbsim.UniStatusAlarmMessage{
1673 OnuSN: o.SerialNumber,
1674 OnuID: o.ID,
1675 EntityID: 257,
1676 RaiseOMCIAlarm: raiseOMCIAlarm,
1677 },
1678 }
1679 o.Channel <- msg
1680 }
1681
1682}
1683
1684func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1685 o.onuAlarmsInfoLock.Lock()
1686 defer o.onuAlarmsInfoLock.Unlock()
1687 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1688 if alarmInfo.SequenceNo == 255 {
1689 alarmInfo.SequenceNo = 1
1690 } else {
1691 alarmInfo.SequenceNo++
1692 }
1693 o.onuAlarmsInfo[key] = alarmInfo
1694 return alarmInfo.SequenceNo
1695 } else {
1696 // This is the first time alarm notification message is being sent
1697 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1698 SequenceNo: 1,
1699 }
1700 return 1
1701 }
1702}