blob: 3cbb5fc040b7e0074d58eece071d7e4ca9dae252 [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 (
20 "context"
21 "errors"
22 "fmt"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020023 "net"
24 "sync"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010025 "time"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020026
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070027 "github.com/google/gopacket"
28 "github.com/google/gopacket/layers"
Matteo Scandolodf3f85d2020-01-15 12:50:48 -080029 "github.com/google/gopacket/pcap"
Matteo Scandolo4747d292019-08-05 11:50:18 -070030 "github.com/looplab/fsm"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070031 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070032 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010033 "github.com/opencord/bbsim/internal/common"
William Kurkian9dadc5b2019-10-22 13:51:57 -040034 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080035 "github.com/opencord/voltha-protos/v2/go/openolt"
36 "github.com/opencord/voltha-protos/v2/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070037 log "github.com/sirupsen/logrus"
38 "google.golang.org/grpc"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010039 "google.golang.org/grpc/reflection"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040)
41
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070042var oltLogger = log.WithFields(log.Fields{
Matteo Scandolo84f7d482019-08-08 19:00:47 -070043 "module": "OLT",
44})
45
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070046type OltDevice struct {
David Bainbridge103cf022019-12-16 20:11:35 +000047 sync.Mutex
48
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070049 // BBSIM Internals
50 ID int
51 SerialNumber string
52 NumNni int
53 NumPon int
54 NumOnuPerPon int
55 InternalState *fsm.FSM
56 channel chan Message
Zdravko Bozakov681364d2019-11-10 14:28:46 +010057 nniPktInChannel chan *bbsim.PacketMsg // packets coming in from the NNI and going to VOLTHA
Matteo Scandolo401503a2019-12-11 14:48:14 -080058 nniHandle *pcap.Handle // handle on the NNI interface, close it when shutting down the NNI channel
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070059
Matteo Scandoloe33447a2019-10-31 12:38:23 -070060 Delay int
61
Matteo Scandolo27428702019-10-11 16:21:16 -070062 Pons []*PonPort
63 Nnis []*NniPort
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070064
65 // OLT Attributes
66 OperState *fsm.FSM
David Bainbridge103cf022019-12-16 20:11:35 +000067
68 enableContext context.Context
69 enableContextCancel context.CancelFunc
Matteo Scandolo4747d292019-08-05 11:50:18 -070070}
71
Matteo Scandolo27428702019-10-11 16:21:16 -070072var olt OltDevice
Zdravko Bozakov681364d2019-11-10 14:28:46 +010073var oltServer *grpc.Server
Matteo Scandolo84f7d482019-08-08 19:00:47 -070074
Matteo Scandolo27428702019-10-11 16:21:16 -070075func GetOLT() *OltDevice {
76 return &olt
Matteo Scandolo84f7d482019-08-08 19:00:47 -070077}
78
Zdravko Bozakov681364d2019-11-10 14:28:46 +010079func CreateOLT(oltId int, nni int, pon int, onuPerPon int, sTag int, cTagInit int, auth bool, dhcp bool, delay int, isMock bool) *OltDevice {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070080 oltLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -070081 "ID": oltId,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070082 "NumNni": nni,
83 "NumPon": pon,
84 "NumOnuPerPon": onuPerPon,
Matteo Scandolo4747d292019-08-05 11:50:18 -070085 }).Debug("CreateOLT")
86
Matteo Scandolo84f7d482019-08-08 19:00:47 -070087 olt = OltDevice{
Matteo Scandolo40e067f2019-10-16 16:59:41 -070088 ID: oltId,
89 SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", oltId),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070090 OperState: getOperStateFSM(func(e *fsm.Event) {
91 oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst)
92 }),
Zdravko Bozakov681364d2019-11-10 14:28:46 +010093 NumNni: nni,
94 NumPon: pon,
95 NumOnuPerPon: onuPerPon,
96 Pons: []*PonPort{},
97 Nnis: []*NniPort{},
98 Delay: delay,
Matteo Scandolo4747d292019-08-05 11:50:18 -070099 }
100
101 // OLT State machine
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700102 // NOTE do we need 2 state machines for the OLT? (InternalState and OperState)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700103 olt.InternalState = fsm.NewFSM(
104 "created",
105 fsm.Events{
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800106 {Name: "initialize", Src: []string{"created", "deleted"}, Dst: "initialized"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100107 {Name: "enable", Src: []string{"initialized", "disabled"}, Dst: "enabled"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700108 {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"},
Mahir Gunyel6dad4452020-01-06 12:59:04 -0800109 //delete event in enabled state below is for reboot OLT case.
110 {Name: "delete", Src: []string{"disabled", "enabled"}, Dst: "deleted"},
Matteo Scandolo4747d292019-08-05 11:50:18 -0700111 },
112 fsm.Callbacks{
113 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700114 oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700115 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100116 "enter_initialized": func(e *fsm.Event) { olt.InitOlt() },
Matteo Scandolo4747d292019-08-05 11:50:18 -0700117 },
118 )
119
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700120 if isMock != true {
121 // create NNI Port
122 nniPort, err := CreateNNI(&olt)
123 if err != nil {
124 oltLogger.Fatalf("Couldn't create NNI Port: %v", err)
125 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700126
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700127 olt.Nnis = append(olt.Nnis, &nniPort)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700128 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700129
Matteo Scandolo4747d292019-08-05 11:50:18 -0700130 // create PON ports
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700131 availableCTag := cTagInit
Matteo Scandolo4747d292019-08-05 11:50:18 -0700132 for i := 0; i < pon; i++ {
Pragya Arya6a708d62020-01-01 17:17:20 +0530133 p := CreatePonPort(olt, uint32(i))
Matteo Scandolo4747d292019-08-05 11:50:18 -0700134
135 // create ONU devices
136 for j := 0; j < onuPerPon; j++ {
Pragya Arya6a708d62020-01-01 17:17:20 +0530137 o := CreateONU(olt, *p, uint32(j+1), sTag, availableCTag, auth, dhcp)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700138 p.Onus = append(p.Onus, o)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700139 availableCTag = availableCTag + 1
Matteo Scandolo4747d292019-08-05 11:50:18 -0700140 }
141
Pragya Arya6a708d62020-01-01 17:17:20 +0530142 olt.Pons = append(olt.Pons, p)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700143 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100144
Matteo Scandolod32c3822019-11-26 15:57:46 -0700145 if isMock != true {
146 if err := olt.InternalState.Event("initialize"); err != nil {
147 log.Errorf("Error initializing OLT: %v", err)
148 return nil
149 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100150 }
151
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700152 return &olt
153}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700154
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100155func (o *OltDevice) InitOlt() error {
156
157 if oltServer == nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800158 oltServer, _ = o.newOltServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100159 } else {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800160 // FIXME there should never be a server running if we are initializing the OLT
161 oltLogger.Fatal("OLT server already running.")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100162 }
163
164 // create new channel for processOltMessages Go routine
165 o.channel = make(chan Message)
166
167 o.nniPktInChannel = make(chan *bbsim.PacketMsg, 1024)
168 // FIXME we are assuming we have only one NNI
169 if o.Nnis[0] != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800170 // NOTE we want to make sure the state is down when we initialize the OLT,
171 // the NNI may be in a bad state after a disable/reboot as we are not disabling it for
172 // in-band management
173 o.Nnis[0].OperState.SetState("down")
174 ch, handle, err := o.Nnis[0].NewVethChan()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100175 if err == nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800176 oltLogger.WithFields(log.Fields{
177 "Type": o.Nnis[0].Type,
178 "IntfId": o.Nnis[0].ID,
179 "OperState": o.Nnis[0].OperState.Current(),
180 }).Info("NNI Channel created")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100181 o.nniPktInChannel = ch
Matteo Scandolo401503a2019-12-11 14:48:14 -0800182 o.nniHandle = handle
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100183 } else {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800184 oltLogger.Errorf("Error getting NNI channel: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100185 }
186 }
187
188 for i := range olt.Pons {
189 for _, onu := range olt.Pons[i].Onus {
190 if err := onu.InternalState.Event("initialize"); err != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800191 oltLogger.Errorf("Error initializing ONU: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100192 return err
193 }
194 }
195 }
196
197 return nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700198}
199
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800200func (o *OltDevice) RestartOLT() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100201
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800202 rebootDelay := common.Options.Olt.OltRebootDelay
203
204 oltLogger.WithFields(log.Fields{
205 "oltId": o.ID,
206 }).Infof("Simulating OLT restart... (%ds)", rebootDelay)
207
208 // transition internal state to deleted
209 if err := o.InternalState.Event("delete"); err != nil {
210 oltLogger.WithFields(log.Fields{
211 "oltId": o.ID,
212 }).Errorf("Error deleting OLT: %v", err)
213 return err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100214 }
215
216 // TODO handle hard poweroff (i.e. no indications sent to Voltha) vs soft poweroff
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800217 time.Sleep(1 * time.Second) // we need to give the OLT the time to respond to all the pending gRPC request before stopping the server
218 if err := o.StopOltServer(); err != nil {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100219 return err
220 }
221
222 // terminate the OLT's processOltMessages go routine
223 close(o.channel)
224 // terminate the OLT's processNniPacketIns go routine
Matteo Scandolo401503a2019-12-11 14:48:14 -0800225 o.nniHandle.Close()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100226 close(o.nniPktInChannel)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100227
Matteo Scandolo401503a2019-12-11 14:48:14 -0800228 for i := range olt.Pons {
229 for _, onu := range olt.Pons[i].Onus {
230 // NOTE while the olt is off, restore the ONU to the initial state
231 onu.InternalState.SetState("created")
232 }
233 }
234
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100235 time.Sleep(time.Duration(rebootDelay) * time.Second)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100236
237 if err := o.InternalState.Event("initialize"); err != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800238 oltLogger.WithFields(log.Fields{
239 "oltId": o.ID,
240 }).Errorf("Error initializing OLT: %v", err)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100241 return err
242 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800243 oltLogger.WithFields(log.Fields{
244 "oltId": o.ID,
245 }).Info("OLT restart completed")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100246 return nil
247}
248
249// newOltServer launches a new grpc server for OpenOLT
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800250func (o *OltDevice) newOltServer() (*grpc.Server, error) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100251 address := common.Options.BBSim.OpenOltAddress
Matteo Scandolo4747d292019-08-05 11:50:18 -0700252 lis, err := net.Listen("tcp", address)
253 if err != nil {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700254 oltLogger.Fatalf("OLT failed to listen: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700255 }
256 grpcServer := grpc.NewServer()
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100257
Matteo Scandolo4747d292019-08-05 11:50:18 -0700258 openolt.RegisterOpenoltServer(grpcServer, o)
259
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100260 reflection.Register(grpcServer)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700261
Matteo Scandolo4747d292019-08-05 11:50:18 -0700262 go grpcServer.Serve(lis)
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100263 oltLogger.Debugf("OLT listening on %v", address)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700264
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100265 return grpcServer, nil
266}
267
268// StopOltServer stops the OpenOLT grpc server
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800269func (o *OltDevice) StopOltServer() error {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100270 // TODO handle poweroff vs graceful shutdown
271 if oltServer != nil {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800272 oltLogger.WithFields(log.Fields{
273 "oltId": o.SerialNumber,
274 }).Warnf("Stopping OLT gRPC server")
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100275 oltServer.Stop()
276 oltServer = nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700277 }
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800278
Matteo Scandolo4747d292019-08-05 11:50:18 -0700279 return nil
280}
281
282// Device Methods
283
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100284// Enable implements the OpenOLT EnableIndicationServer functionality
285func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700286 oltLogger.Debug("Enable OLT called")
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700287
David Bainbridge103cf022019-12-16 20:11:35 +0000288 // If enabled has already been called then an enabled context has
289 // been created. If this is the case then we want to cancel all the
290 // proessing loops associated with that enable before we recreate
291 // new ones
292 o.Lock()
293 if o.enableContext != nil && o.enableContextCancel != nil {
294 o.enableContextCancel()
295 }
296 o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO())
297 o.Unlock()
298
Matteo Scandolo4747d292019-08-05 11:50:18 -0700299 wg := sync.WaitGroup{}
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800300 wg.Add(3)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700301
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100302 // create Go routine to process all OLT events
David Bainbridge103cf022019-12-16 20:11:35 +0000303 go o.processOltMessages(o.enableContext, stream, &wg)
304 go o.processNniPacketIns(o.enableContext, stream, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700305
306 // enable the OLT
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100307 oltMsg := Message{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700308 Type: OltIndication,
309 Data: OltIndicationMessage{
310 OperState: UP,
311 },
312 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100313 o.channel <- oltMsg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700314
315 // send NNI Port Indications
316 for _, nni := range o.Nnis {
317 msg := Message{
318 Type: NniIndication,
319 Data: NniIndicationMessage{
320 OperState: UP,
321 NniPortID: nni.ID,
322 },
323 }
324 o.channel <- msg
325 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100326
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800327 go o.processOmciMessages(o.enableContext, stream, &wg)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100328
Matteo Scandolo4747d292019-08-05 11:50:18 -0700329 // send PON Port indications
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100330 for i, pon := range o.Pons {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700331 msg := Message{
332 Type: PonIndication,
333 Data: PonIndicationMessage{
334 OperState: UP,
335 PonPortID: pon.ID,
336 },
337 }
338 o.channel <- msg
339
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100340 for _, onu := range o.Pons[i].Onus {
David Bainbridge103cf022019-12-16 20:11:35 +0000341 go onu.ProcessOnuMessages(o.enableContext, stream, nil)
342 if onu.InternalState.Current() != "initialized" {
343 continue
344 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100345 if err := onu.InternalState.Event("discover"); err != nil {
346 log.Errorf("Error discover ONU: %v", err)
347 return err
Matteo Scandolo4747d292019-08-05 11:50:18 -0700348 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700349 }
350 }
351
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800352 oltLogger.Debug("Enable OLT Done")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700353 wg.Wait()
354 return nil
355}
356
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800357func (o *OltDevice) processOmciMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400358 ch := omcisim.GetChannel()
359
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100360 oltLogger.Debug("Starting OMCI Indication Channel")
William Kurkian9dadc5b2019-10-22 13:51:57 -0400361
David Bainbridge103cf022019-12-16 20:11:35 +0000362loop:
363 for {
364 select {
365 case <-ctx.Done():
366 oltLogger.Debug("OMCI processing canceled via context")
367 break loop
368 case message, ok := <-ch:
369 if !ok || ctx.Err() != nil {
370 oltLogger.Debug("OMCI processing canceled via channel close")
371 break loop
372 }
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800373
374 oltLogger.WithFields(log.Fields{
375 "messageType": message.Type,
376 "OnuId": message.Data.OnuId,
377 "IntfId": message.Data.IntfId,
378 }).Info("Received message on OMCI Sim channel")
379
David Bainbridge103cf022019-12-16 20:11:35 +0000380 onuId := message.Data.OnuId
381 intfId := message.Data.IntfId
382 onu, err := o.FindOnuById(intfId, onuId)
383 if err != nil {
384 oltLogger.Errorf("Failed to find onu: %v", err)
385 continue
386 }
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800387 go onu.processOmciMessage(message, stream)
William Kurkian9dadc5b2019-10-22 13:51:57 -0400388 }
William Kurkian9dadc5b2019-10-22 13:51:57 -0400389 }
David Bainbridge103cf022019-12-16 20:11:35 +0000390
391 wg.Done()
William Kurkian9dadc5b2019-10-22 13:51:57 -0400392}
393
Matteo Scandolo4747d292019-08-05 11:50:18 -0700394// Helpers method
395
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700396func (o OltDevice) GetPonById(id uint32) (*PonPort, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700397 for _, pon := range o.Pons {
398 if pon.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700399 return pon, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700400 }
401 }
402 return nil, errors.New(fmt.Sprintf("Cannot find PonPort with id %d in OLT %d", id, o.ID))
403}
404
405func (o OltDevice) getNniById(id uint32) (*NniPort, error) {
406 for _, nni := range o.Nnis {
407 if nni.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -0700408 return nni, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700409 }
410 }
411 return nil, errors.New(fmt.Sprintf("Cannot find NniPort with id %d in OLT %d", id, o.ID))
412}
413
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100414func (o *OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700415 data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
416 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700417 oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800418 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700419 }
420
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700421 oltLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700422 "OperState": msg.OperState,
423 }).Debug("Sent Indication_OltInd")
424}
425
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100426func (o *OltDevice) sendNniIndication(msg NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700427 nni, _ := o.getNniById(msg.NniPortID)
Matteo Scandolo401503a2019-12-11 14:48:14 -0800428 if msg.OperState == UP {
429 if err := nni.OperState.Event("enable"); err != nil {
430 log.WithFields(log.Fields{
431 "Type": nni.Type,
432 "IntfId": nni.ID,
433 "OperState": nni.OperState.Current(),
434 }).Errorf("Can't move NNI Port to enabled state: %v", err)
435 }
436 } else if msg.OperState == DOWN {
437 if err := nni.OperState.Event("disable"); err != nil {
438 log.WithFields(log.Fields{
439 "Type": nni.Type,
440 "IntfId": nni.ID,
441 "OperState": nni.OperState.Current(),
442 }).Errorf("Can't move NNI Port to disable state: %v", err)
443 }
444 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700445 // NOTE Operstate may need to be an integer
Matteo Scandolo4747d292019-08-05 11:50:18 -0700446 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700447 Type: nni.Type,
448 IntfId: nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700449 OperState: nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700450 }}
451
452 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700453 oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800454 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700455 }
456
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700457 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700458 "Type": nni.Type,
459 "IntfId": nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700460 "OperState": nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700461 }).Debug("Sent Indication_IntfOperInd for NNI")
462}
463
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100464func (o *OltDevice) sendPonIndication(msg PonIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700465 pon, _ := o.GetPonById(msg.PonPortID)
Matteo Scandolo401503a2019-12-11 14:48:14 -0800466 if msg.OperState == UP {
467 if err := pon.OperState.Event("enable"); err != nil {
468 log.WithFields(log.Fields{
469 "Type": pon.Type,
470 "IntfId": pon.ID,
471 "OperState": pon.OperState.Current(),
472 }).Errorf("Can't move PON Port to enable state: %v", err)
473 }
474 } else if msg.OperState == DOWN {
475 if err := pon.OperState.Event("disable"); err != nil {
476 log.WithFields(log.Fields{
477 "Type": pon.Type,
478 "IntfId": pon.ID,
479 "OperState": pon.OperState.Current(),
480 }).Errorf("Can't move PON Port to disable state: %v", err)
481 }
482 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700483 discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700484 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700485 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700486 }}
487
488 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700489 oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800490 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700491 }
492
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700493 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700494 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700495 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700496 }).Debug("Sent Indication_IntfInd")
497
498 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700499 Type: pon.Type,
500 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700501 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700502 }}
503
504 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700505 oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
Matteo Scandolo08badf42019-12-12 11:21:50 -0800506 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700507 }
508
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700509 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700510 "Type": pon.Type,
511 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700512 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700513 }).Debug("Sent Indication_IntfOperInd for PON")
514}
515
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100516// processOltMessages handles messages received over the OpenOLT interface
David Bainbridge103cf022019-12-16 20:11:35 +0000517func (o *OltDevice) processOltMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100518 oltLogger.Debug("Starting OLT Indication Channel")
David Bainbridge103cf022019-12-16 20:11:35 +0000519 ch := o.channel
Matteo Scandolo4747d292019-08-05 11:50:18 -0700520
David Bainbridge103cf022019-12-16 20:11:35 +0000521loop:
522 for {
523 select {
524 case <-ctx.Done():
525 oltLogger.Debug("OLT Indication processing canceled via context")
526 break loop
527 case message, ok := <-ch:
528 if !ok || ctx.Err() != nil {
529 oltLogger.Debug("OLT Indication processing canceled via closed channel")
530 break loop
Matteo Scandolo4747d292019-08-05 11:50:18 -0700531 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700532
David Bainbridge103cf022019-12-16 20:11:35 +0000533 oltLogger.WithFields(log.Fields{
534 "oltId": o.ID,
535 "messageType": message.Type,
536 }).Trace("Received message")
537
538 switch message.Type {
539 case OltIndication:
540 msg, _ := message.Data.(OltIndicationMessage)
541 if msg.OperState == UP {
542 o.InternalState.Event("enable")
543 o.OperState.Event("enable")
544 } else if msg.OperState == DOWN {
545 o.InternalState.Event("disable")
546 o.OperState.Event("disable")
547 }
548 o.sendOltIndication(msg, stream)
549 case NniIndication:
550 msg, _ := message.Data.(NniIndicationMessage)
551 o.sendNniIndication(msg, stream)
552 case PonIndication:
553 msg, _ := message.Data.(PonIndicationMessage)
554 o.sendPonIndication(msg, stream)
555 default:
556 oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
557 }
558 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700559 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100560 wg.Done()
561 oltLogger.Warn("Stopped handling OLT Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700562}
563
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100564// processNniPacketIns handles messages received over the NNI interface
David Bainbridge103cf022019-12-16 20:11:35 +0000565func (o *OltDevice) processNniPacketIns(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, wg *sync.WaitGroup) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700566 oltLogger.WithFields(log.Fields{
567 "nniChannel": o.nniPktInChannel,
Matteo Scandolo401503a2019-12-11 14:48:14 -0800568 }).Debug("Started Processing Packets arriving from the NNI")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700569 nniId := o.Nnis[0].ID // FIXME we are assuming we have only one NNI
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700570
David Bainbridge103cf022019-12-16 20:11:35 +0000571 ch := o.nniPktInChannel
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700572
David Bainbridge103cf022019-12-16 20:11:35 +0000573loop:
574 for {
575 select {
576 case <-ctx.Done():
577 oltLogger.Debug("NNI Indication processing canceled via context")
578 break loop
579 case message, ok := <-ch:
580 if !ok || ctx.Err() != nil {
581 oltLogger.Debug("NNI Indication processing canceled via channel closed")
582 break loop
583 }
584 oltLogger.Tracef("Received packets on NNI Channel")
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700585
David Bainbridge103cf022019-12-16 20:11:35 +0000586 onuMac, err := packetHandlers.GetDstMacAddressFromPacket(message.Pkt)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700587
David Bainbridge103cf022019-12-16 20:11:35 +0000588 if err != nil {
589 log.WithFields(log.Fields{
590 "IntfType": "nni",
591 "IntfId": nniId,
592 "Pkt": message.Pkt.Data(),
593 }).Error("Can't find Dst MacAddress in packet")
594 return
595 }
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700596
David Bainbridge103cf022019-12-16 20:11:35 +0000597 onu, err := o.FindOnuByMacAddress(onuMac)
598 if err != nil {
599 log.WithFields(log.Fields{
600 "IntfType": "nni",
601 "IntfId": nniId,
602 "Pkt": message.Pkt.Data(),
603 "MacAddress": onuMac.String(),
604 }).Error("Can't find ONU with MacAddress")
605 return
606 }
607
608 doubleTaggedPkt, err := packetHandlers.PushDoubleTag(onu.STag, onu.CTag, message.Pkt)
609 if err != nil {
610 log.Error("Fail to add double tag to packet")
611 }
612
613 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
614 IntfType: "nni",
615 IntfId: nniId,
616 Pkt: doubleTaggedPkt.Data()}}
617 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
618 oltLogger.WithFields(log.Fields{
619 "IntfType": data.PktInd.IntfType,
620 "IntfId": nniId,
621 "Pkt": doubleTaggedPkt.Data(),
622 }).Errorf("Fail to send PktInd indication: %v", err)
623 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700624 oltLogger.WithFields(log.Fields{
625 "IntfType": data.PktInd.IntfType,
626 "IntfId": nniId,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700627 "Pkt": doubleTaggedPkt.Data(),
David Bainbridge103cf022019-12-16 20:11:35 +0000628 "OnuSn": onu.Sn(),
629 }).Tracef("Sent PktInd indication")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700630 }
631 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100632 wg.Done()
633 oltLogger.WithFields(log.Fields{
634 "nniChannel": o.nniPktInChannel,
635 }).Warn("Stopped handling NNI Channel")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700636}
637
Pragya Arya6a708d62020-01-01 17:17:20 +0530638func (o *OltDevice) handleReenableOlt() {
639 // enable OLT
640 oltMsg := Message{
641 Type: OltIndication,
642 Data: OltIndicationMessage{
643 OperState: UP,
644 },
645 }
646 o.channel <- oltMsg
647
648 for i := range olt.Pons {
649 for _, onu := range olt.Pons[i].Onus {
650 if err := onu.InternalState.Event("discover"); err != nil {
651 log.Errorf("Error discover ONU: %v", err)
652 }
653 }
654 }
655
656}
657
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700658// returns an ONU with a given Serial Number
659func (o OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200660 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700661 // memoizing it will remove the bottleneck
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700662 for _, pon := range o.Pons {
663 for _, onu := range pon.Onus {
664 if onu.Sn() == serialNumber {
Matteo Scandolo27428702019-10-11 16:21:16 -0700665 return onu, nil
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700666 }
667 }
668 }
669
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700670 return &Onu{}, errors.New(fmt.Sprintf("cannot-find-onu-by-serial-number-%s", serialNumber))
671}
672
William Kurkian9dadc5b2019-10-22 13:51:57 -0400673// returns an ONU with a given interface/Onu Id
674func (o OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200675 // TODO this function can be a performance bottleneck when we have many ONUs,
William Kurkian9dadc5b2019-10-22 13:51:57 -0400676 // memoizing it will remove the bottleneck
677 for _, pon := range o.Pons {
678 if pon.ID == intfId {
679 for _, onu := range pon.Onus {
680 if onu.ID == onuId {
681 return onu, nil
682 }
683 }
684 }
685 }
686 return &Onu{}, errors.New(fmt.Sprintf("cannot-find-onu-by-id-%v-%v", intfId, onuId))
687}
688
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700689// returns an ONU with a given Mac Address
690func (o OltDevice) FindOnuByMacAddress(mac net.HardwareAddr) (*Onu, error) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200691 // TODO this function can be a performance bottleneck when we have many ONUs,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700692 // memoizing it will remove the bottleneck
693 for _, pon := range o.Pons {
694 for _, onu := range pon.Onus {
695 if onu.HwAddress.String() == mac.String() {
Matteo Scandolo27428702019-10-11 16:21:16 -0700696 return onu, nil
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700697 }
698 }
699 }
700
701 return &Onu{}, errors.New(fmt.Sprintf("cannot-find-onu-by-mac-address-%s", mac))
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700702}
703
Matteo Scandolo4747d292019-08-05 11:50:18 -0700704// GRPC Endpoints
705
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700706func (o OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700707 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700708 "OnuSn": onuSnToString(onu.SerialNumber),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700709 }).Info("Received ActivateOnu call from VOLTHA")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700710
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700711 pon, _ := o.GetPonById(onu.IntfId)
712 _onu, _ := pon.GetOnuBySn(onu.SerialNumber)
William Kurkian0418bc82019-11-06 12:16:24 -0500713 _onu.SetID(onu.OnuId)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700714
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700715 if err := _onu.OperState.Event("enable"); err != nil {
716 oltLogger.WithFields(log.Fields{
717 "IntfId": _onu.PonPortID,
718 "OnuSn": _onu.Sn(),
719 "OnuId": _onu.ID,
720 }).Infof("Failed to transition ONU.OperState to enabled state: %s", err.Error())
Matteo Scandolo4747d292019-08-05 11:50:18 -0700721 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700722 if err := _onu.InternalState.Event("enable"); err != nil {
723 oltLogger.WithFields(log.Fields{
724 "IntfId": _onu.PonPortID,
725 "OnuSn": _onu.Sn(),
726 "OnuId": _onu.ID,
727 }).Infof("Failed to transition ONU to enabled state: %s", err.Error())
728 }
729
730 // NOTE we need to immediately activate the ONU or the OMCI state machine won't start
731
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700732 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700733}
734
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700735func (o OltDevice) DeactivateOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700736 oltLogger.Error("DeactivateOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700737 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700738}
739
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700740func (o OltDevice) DeleteOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700741 oltLogger.Error("DeleteOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700742 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700743}
744
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700745func (o OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700746 // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether?
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800747 oltLogger.WithFields(log.Fields{
748 "oltId": o.ID,
749 }).Info("Disabling OLT")
750
Matteo Scandolo401503a2019-12-11 14:48:14 -0800751 for _, pon := range o.Pons {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800752 // disable PONs
753 msg := Message{
754 Type: PonIndication,
755 Data: PonIndicationMessage{
756 OperState: DOWN,
757 PonPortID: pon.ID,
758 },
759 }
760
761 o.channel <- msg
762 }
763
Matteo Scandolo401503a2019-12-11 14:48:14 -0800764 // Note that we are not disabling the NNI as the real OLT does not.
765 // The reason for that is in-band management
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800766
767 // disable OLT
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100768 oltMsg := Message{
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700769 Type: OltIndication,
770 Data: OltIndicationMessage{
771 OperState: DOWN,
772 },
773 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100774 o.channel <- oltMsg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700775 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700776}
777
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700778func (o OltDevice) DisablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700779 oltLogger.Error("DisablePonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700780 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700781}
782
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100783func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700784 oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700785 o.Enable(stream)
786 return nil
787}
788
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700789func (o OltDevice) EnablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700790 oltLogger.Error("EnablePonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700791 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700792}
793
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700794func (o OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700795 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700796 "IntfId": flow.AccessIntfId,
797 "OnuId": flow.OnuId,
798 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700799 "InnerVlan": flow.Classifier.IVid,
800 "OuterVlan": flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700801 "FlowType": flow.FlowType,
802 "FlowId": flow.FlowId,
803 "UniID": flow.UniId,
804 "PortNo": flow.PortNo,
805 }).Tracef("OLT receives Flow")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700806 // TODO optionally store flows somewhere
807
808 if flow.AccessIntfId == -1 {
809 oltLogger.WithFields(log.Fields{
810 "FlowId": flow.FlowId,
811 }).Debugf("This is an OLT flow")
812 } else {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700813 pon, err := o.GetPonById(uint32(flow.AccessIntfId))
Matteo Scandolo27428702019-10-11 16:21:16 -0700814 if err != nil {
815 oltLogger.WithFields(log.Fields{
816 "OnuId": flow.OnuId,
817 "IntfId": flow.AccessIntfId,
818 "err": err,
819 }).Error("Can't find PonPort")
820 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700821 onu, err := pon.GetOnuById(uint32(flow.OnuId))
Matteo Scandolo27428702019-10-11 16:21:16 -0700822 if err != nil {
823 oltLogger.WithFields(log.Fields{
824 "OnuId": flow.OnuId,
825 "IntfId": flow.AccessIntfId,
826 "err": err,
827 }).Error("Can't find Onu")
828 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700829
830 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700831 Type: FlowUpdate,
832 Data: OnuFlowUpdateMessage{
833 PonPortID: pon.ID,
834 OnuID: onu.ID,
835 Flow: flow,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700836 },
837 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700838 onu.Channel <- msg
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700839 }
840
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700841 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700842}
843
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700844func (o OltDevice) FlowRemove(context.Context, *openolt.Flow) (*openolt.Empty, error) {
845 oltLogger.Tracef("received FlowRemove")
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700846 // TODO store flows somewhere
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700847 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700848}
849
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700850func (o OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo18859852020-01-15 13:33:57 -0800851 res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())}
852 oltLogger.WithFields(log.Fields{
853 "signature": res.HeartbeatSignature,
854 }).Trace("HeartbeatCheck")
855 return &res, nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700856}
857
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700858func (o OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700859
Matteo Scandoloda9cbe22019-08-19 16:05:10 -0700860 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700861 "oltId": o.ID,
Matteo Scandoloda9cbe22019-08-19 16:05:10 -0700862 "PonPorts": o.NumPon,
863 }).Info("OLT receives GetDeviceInfo call from VOLTHA")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700864 devinfo := new(openolt.DeviceInfo)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100865 devinfo.Vendor = common.Options.Olt.Vendor
866 devinfo.Model = common.Options.Olt.Model
867 devinfo.HardwareVersion = common.Options.Olt.HardwareVersion
868 devinfo.FirmwareVersion = common.Options.Olt.FirmwareVersion
869 devinfo.Technology = common.Options.Olt.Technology
Matteo Scandoloda9cbe22019-08-19 16:05:10 -0700870 devinfo.PonPorts = uint32(o.NumPon)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700871 devinfo.OnuIdStart = 1
872 devinfo.OnuIdEnd = 255
873 devinfo.AllocIdStart = 1024
874 devinfo.AllocIdEnd = 16383
875 devinfo.GemportIdStart = 1024
876 devinfo.GemportIdEnd = 65535
877 devinfo.FlowIdStart = 1
878 devinfo.FlowIdEnd = 16383
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700879 devinfo.DeviceSerialNumber = o.SerialNumber
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100880 devinfo.DeviceId = common.Options.Olt.DeviceId
Matteo Scandolo4747d292019-08-05 11:50:18 -0700881
882 return devinfo, nil
883}
884
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700885func (o OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700886 pon, _ := o.GetPonById(omci_msg.IntfId)
887 onu, _ := pon.GetOnuById(omci_msg.OnuId)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700888 oltLogger.WithFields(log.Fields{
889 "IntfId": onu.PonPortID,
890 "OnuId": onu.ID,
891 "OnuSn": onu.Sn(),
892 }).Tracef("Received OmciMsgOut")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700893 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700894 Type: OMCI,
895 Data: OmciMessage{
896 OnuSN: onu.SerialNumber,
897 OnuID: onu.ID,
898 omciMsg: omci_msg,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700899 },
900 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700901 onu.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700902 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700903}
904
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700905func (o OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700906 pon, err := o.GetPonById(onuPkt.IntfId)
Matteo Scandolo27428702019-10-11 16:21:16 -0700907 if err != nil {
908 oltLogger.WithFields(log.Fields{
909 "OnuId": onuPkt.OnuId,
910 "IntfId": onuPkt.IntfId,
911 "err": err,
912 }).Error("Can't find PonPort")
913 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700914 onu, err := pon.GetOnuById(onuPkt.OnuId)
Matteo Scandolo27428702019-10-11 16:21:16 -0700915 if err != nil {
916 oltLogger.WithFields(log.Fields{
917 "OnuId": onuPkt.OnuId,
918 "IntfId": onuPkt.IntfId,
919 "err": err,
920 }).Error("Can't find Onu")
921 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700922
Matteo Scandolo075b1892019-10-07 12:11:07 -0700923 oltLogger.WithFields(log.Fields{
924 "IntfId": onu.PonPortID,
925 "OnuId": onu.ID,
926 "OnuSn": onu.Sn(),
927 }).Tracef("Received OnuPacketOut")
928
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700929 rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700930 pktType, err := packetHandlers.IsEapolOrDhcp(rawpkt)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700931
Matteo Scandolo075b1892019-10-07 12:11:07 -0700932 msg := Message{
933 Type: OnuPacketOut,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700934 Data: OnuPacketMessage{
Matteo Scandolo075b1892019-10-07 12:11:07 -0700935 IntfId: onuPkt.IntfId,
936 OnuId: onuPkt.OnuId,
937 Packet: rawpkt,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700938 Type: pktType,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700939 },
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700940 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700941 onu.Channel <- msg
942
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700943 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700944}
945
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700946func (o OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolod02b79b2019-12-05 16:42:13 -0800947 oltLogger.WithFields(log.Fields{
948 "oltId": o.ID,
949 }).Info("Shutting down")
950 go o.RestartOLT()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700951 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700952}
953
954func (o OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Pragya Arya6a708d62020-01-01 17:17:20 +0530955 oltLogger.WithFields(log.Fields{
956 "oltId": o.ID,
957 }).Info("Received ReenableOlt request from VOLTHA")
958
959 for _, pon := range o.Pons {
960 msg := Message{
961 Type: PonIndication,
962 Data: PonIndicationMessage{
963 OperState: UP,
964 PonPortID: pon.ID,
965 },
966 }
967 o.channel <- msg
968 }
969
970 // Openolt adapter will start processing indications only after success reponse of ReenableOlt
971 // thats why need to send OLT and ONU indications after return of this function
972 go o.handleReenableOlt()
973
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700974 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700975}
976
977func (o OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700978 pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
979
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100980 o.Nnis[0].sendNniPacket(pkt) // FIXME we are assuming we have only one NNI
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700981 // NOTE should we return an error if sendNniPakcet fails?
982 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700983}
984
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700985func (o OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700986 oltLogger.Error("CollectStatistics not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700987 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700988}
989
Matteo Scandolo4747d292019-08-05 11:50:18 -0700990func (o OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700991 oltLogger.Error("GetOnuInfo not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700992 return new(openolt.OnuIndication), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700993}
994
995func (o OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700996 oltLogger.Error("GetPonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700997 return new(openolt.IntfIndication), nil
Matteo Scandolod54283a2019-08-13 16:22:31 -0700998}
999
1000func (s OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001001 oltLogger.Info("received CreateTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001002 return new(openolt.Empty), nil
1003}
1004
1005func (s OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001006 oltLogger.Info("received RemoveTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001007 return new(openolt.Empty), nil
1008}
1009
1010func (s OltDevice) CreateTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001011 oltLogger.Info("received CreateTrafficSchedulers")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001012 return new(openolt.Empty), nil
1013}
1014
1015func (s OltDevice) RemoveTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001016 oltLogger.Info("received RemoveTrafficSchedulers")
Matteo Scandolod54283a2019-08-13 16:22:31 -07001017 return new(openolt.Empty), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001018}