blob: c03545ad2b5f45deb8306abf9730699c7c9bc70d [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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
17package core
18
19import (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090020 "context"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020021 "encoding/hex"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070022 "errors"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020023 "reflect"
Matteo Scandolo88e91892018-11-06 16:29:19 -080024 "strconv"
25 "sync"
Shad Ansarib744bf22019-01-17 11:36:46 -080026
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020027 pb "gerrit.opencord.org/voltha-bbsim/api"
Matteo Scandolo88e91892018-11-06 16:29:19 -080028 "gerrit.opencord.org/voltha-bbsim/common/logger"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029 "gerrit.opencord.org/voltha-bbsim/device"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020030 flowHandler "gerrit.opencord.org/voltha-bbsim/flow"
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070031 openolt "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032 "github.com/google/gopacket"
33 "github.com/google/gopacket/layers"
34 "github.com/google/gopacket/pcap"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020035 omci "github.com/opencord/omci-sim"
Matteo Scandolo88e91892018-11-06 16:29:19 -080036 log "github.com/sirupsen/logrus"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090037 "golang.org/x/sync/errgroup"
Shad Ansarib744bf22019-01-17 11:36:46 -080038 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039)
40
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020041// Constants
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042const (
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020043 NniVethNorthPfx = "nni_north"
44 NniVethSouthPfx = "nni_south"
45 MaxPonPorts = 64
46 MaxOnusPerPon = 64 // This value should be the same with the value in AdapterPlatform class
47 VendorIDLength = 4
48 SerialNumberLength = 12
49 OpenOltStart = "start"
50 OpenOltStop = "stop"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051)
52
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020053// Server structure consists of all the params required for BBsim.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054type Server struct {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020055 wg *sync.WaitGroup
56 Olt *device.Olt
57 Onumap map[uint32][]*device.Onu
58 SNmap sync.Map
59 AutoONUActivate bool
60 Ioinfos []*Ioinfo
61 gRPCserver *grpc.Server
62 gRPCAddress string
63 gRPCPort uint32
64 mgmtServer *grpc.Server
65 mgmtGrpcPort uint32
66 mgmtRestPort uint32
67 Vethnames []string
68 IndInterval int
69 Processes []string
70 EnableServer *openolt.Openolt_EnableIndicationServer
71 CtagMap map[string]uint32
72 cancel context.CancelFunc
73 stateRepCh chan stateReport
74 omciIn chan openolt.OmciIndication
75 omciOut chan openolt.OmciMsg
76 eapolIn chan *byteMsg
77 eapolOut chan *byteMsg
78 dhcpIn chan *byteMsg
79 dhcpOut chan *byteMsg
80 FlowMap map[FlowKey]*openolt.Flow
81 alarmCh chan *openolt.Indication
82 deviceActionCh chan *pb.DeviceAction
83 serverActionCh chan string
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090084}
85
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020086// Packet structure
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090087type Packet struct {
88 Info *Ioinfo
89 Pkt gopacket.Packet
90}
91
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +090092type byteMsg struct {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090093 IntfId uint32
94 OnuId uint32
Mahir Gunyel9897f6e2019-05-22 14:54:05 -070095 Byte []byte
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090096}
97
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090098type stateReport struct {
Shad Ansarib744bf22019-01-17 11:36:46 -080099 device device.Device
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900100 current device.DeviceState
Shad Ansarib744bf22019-01-17 11:36:46 -0800101 next device.DeviceState
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900102}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900103
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200104// FlowKey used for FlowMap key
105type FlowKey struct {
106 FlowID uint32
107 FlowDirection string
108}
109
Anjali Thontakudi66659202019-07-03 23:14:02 +0000110//Has options (OLT id, number onu ports) from mediator
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200111// NewCore initialize OLT and ONU objects
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900112func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900113 // TODO: make it decent
114 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -0800115 npon := opt.npon
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200116 if npon > MaxPonPorts {
117 logger.Warn("Provided number of PON ports exceeds limit of %d", MaxPonPorts)
118 logger.Info("Setting number of PON ports to %d", MaxPonPorts)
119 npon = MaxPonPorts
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200121 nonus := opt.nonus
122 if nonus > MaxOnusPerPon {
123 logger.Warn("Provided number of ONUs per PON port exceeds limit of %d", MaxOnusPerPon)
124 logger.Info("Setting number of ONUs per PON port to %d", MaxOnusPerPon)
125 nonus = MaxOnusPerPon
126 }
127 s := Server{
128 Olt: device.NewOlt(oltid, npon, 1), // TODO nnni is to be taken from options
129 Onumap: make(map[uint32][]*device.Onu),
130 Ioinfos: []*Ioinfo{},
131 gRPCAddress: opt.address,
132 gRPCPort: opt.port,
133 Vethnames: []string{},
134 IndInterval: opt.intvl,
135 AutoONUActivate: !opt.interactiveOnuActivation,
136 Processes: []string{},
137 mgmtGrpcPort: opt.mgmtGrpcPort,
138 mgmtRestPort: opt.mgmtRestPort,
139 EnableServer: nil,
140 stateRepCh: make(chan stateReport, 8),
141 omciIn: make(chan openolt.OmciIndication, 1024),
142 omciOut: make(chan openolt.OmciMsg, 1024),
143 eapolIn: make(chan *byteMsg, 1024),
144 eapolOut: make(chan *byteMsg, 1024),
145 dhcpIn: make(chan *byteMsg, 1024),
146 dhcpOut: make(chan *byteMsg, 1024),
147 FlowMap: make(map[FlowKey]*openolt.Flow),
148 serverActionCh: make(chan string),
149 }
150 logger.Info("OLT %d created: %v", s.Olt.ID, s.Olt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900151
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900152 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800153 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200154 logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber)
155 // Creating Onu Map
156 for intfid := uint32(0); intfid < npon; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900157 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900158 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900159
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200160 logger.Debug("Onu Map:")
161 for _, onus := range s.Onumap {
162 for _, onu := range onus {
163 logger.Debug("%+v", *onu)
164 }
165 }
166
167 // TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900168 s.CtagMap = make(map[string]uint32)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200169 for i := 0; i < MaxOnusPerPon; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900170 oltid := s.Olt.ID
171 intfid := uint32(1)
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900172 sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700173 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900174 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200175
176 flowHandler.InitializeFlowManager(s.Olt.ID)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900177 return &s
178}
179
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200180// Start starts the openolt gRPC server (blocking)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800181func (s *Server) Start() error {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200182 logger.Debug("Starting OpenOLT gRPC Server")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800183 defer func() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200184 logger.Debug("OpenOLT gRPC Server Stopped")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900185 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200186
187 // Start Openolt gRPC server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900188 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
189 listener, gserver, err := NewGrpcServer(addressport)
190 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700191 logger.Error("Failed to create gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900192 return err
193 }
194 s.gRPCserver = gserver
195 openolt.RegisterOpenoltServer(gserver, s)
196 if err := gserver.Serve(listener); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700197 logger.Error("Failed to run gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900198 return err
199 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200 return nil
201}
202
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200203// Stop stops the openolt gRPC servers (non-blocking).
Matteo Scandolo88e91892018-11-06 16:29:19 -0800204func (s *Server) Stop() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200205 logger.Debug("Stopping OpenOLT gRPC Server & PktLoops")
206 defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped")
207
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900208 if s.gRPCserver != nil {
209 s.gRPCserver.Stop()
210 logger.Debug("gRPCserver.Stop()")
211 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200212
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900213 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900214 return
215}
216
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200217func (s *Server) startMgmtServer(wg *sync.WaitGroup) {
218 defer logger.Debug("Management api server exited")
219
220 grpcAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
221 restAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtRestPort))
222 // Start rest gateway for BBSim server
223 go StartRestGatewayService(grpcAddressPort, restAddressPort, wg)
224 addressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
225
226 listener, apiserver, err := NewMgmtAPIServer(addressPort)
227 if err != nil {
228 logger.Error("Unable to create management api server %v", err)
229 return
230 }
231
232 s.mgmtServer = apiserver
233 pb.RegisterBBSimServiceServer(apiserver, s)
234 if e := apiserver.Serve(listener); e != nil {
235 logger.Error("Failed to run management api server %v", e)
236 return
237 }
238
239}
240
241func (s *Server) stopMgmtServer() error {
242 if s.mgmtServer != nil {
243 s.mgmtServer.GracefulStop()
244 logger.Debug("Management server stopped")
245 return nil
246 }
247 return errors.New("can not stop management server, server not created")
248}
249
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200250// Enable invokes methods for activation of OLT and ONU (blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900251func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900252 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800253 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900254 olt.Initialize()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200255 // Below lines commented as we dont want to change the onu state on restart
256 // for intfid := range s.Onumap {
257 // for _, onu := range s.Onumap[intfid] {
258 // onu.Initialize()
259 // }
260 // }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900261 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900262 logger.Debug("Enable() Done")
263 }()
264 logger.Debug("Enable() Start")
265 s.EnableServer = sv
266 if err := s.activateOLT(*sv); err != nil {
267 return err
268 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700269
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900270 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900271
272 coreCtx := context.Background()
273 coreCtx, corecancel := context.WithCancel(coreCtx)
274 s.cancel = corecancel
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700275
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200276 errorchan := make(chan error, 5)
277 go s.StartPktLoops(coreCtx, *sv, errorchan)
278
279 if s.AutoONUActivate == true {
280 // Initialize all ONUs
281 for intfid := range s.Onumap {
282 for _, onu := range s.Onumap[intfid] {
283 onu.Initialize()
284 }
285 }
286 // Activate all ONUs
287 s.activateONUs(*sv, s.Onumap)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900288 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200289
290 select {
291 case err := <-errorchan:
292 if err != nil {
293 logger.Debug("Error: %v", err)
294 return err
295 }
296 }
297
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900298 return nil
299}
300
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200301// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900302func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800303 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900304 logger.Debug("Disable() Done")
305 }()
306 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900307 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900308}
309
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900310func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900311 logger.Debug("updateDevIntState called state:%d", state)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900312 current := dev.GetIntState()
313 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800314 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
315 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900316 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800317 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900318 logger.Debug("ONU State updated to:%d", state)
319 } else {
320 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
321 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900322}
323
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700324func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200325 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900326 if err != nil {
327 return err
328 }
329 s.updateDevIntState(onu, state)
330 return nil
331}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200332
333func (s *Server) activateOnu(onu *device.Onu) {
334 snKey := stringifySerialNumber(onu.SerialNumber)
335 s.SNmap.Store(snKey, onu)
336 device.UpdateOnusOpStatus(onu.IntfID, onu, "up")
337
338 err := sendOnuDiscInd(*s.EnableServer, onu)
339 if err != nil {
340 logger.Error(err.Error())
341 return
342 }
343 logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID)
344 logger.Debug("activateONUs Entry in SNmap %v", snKey)
345}
346
347func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) {
348 // Add all ONUs to SerialNumber Map
349 for intfid := range Onumap {
350 for _, onu := range Onumap[intfid] {
351 s.activateOnu(onu)
352 }
353 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700354}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900355
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900356func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900357 defer logger.Debug("activateOLT() Done")
358 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900359 // Activate OLT
360 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900361 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900362 return err
363 }
364 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800365 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900366
367 // OLT sends Interface Indication to Adapter
368 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800369 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900370 return err
371 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800372 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900373
374 // OLT sends Operation Indication to Adapter after activating each interface
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900375 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800376 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900377 return err
378 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800379 logger.Info("OLT %s sent OperInd.", olt.Name)
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700380 return nil
381}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900382
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200383// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200384func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900385 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800386 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900387 RemoveVeths(s.Vethnames)
388 s.Vethnames = []string{}
389 s.Ioinfos = []*Ioinfo{}
390 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900391 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900392 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900393 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200394 s.alarmCh = make(chan *openolt.Indication, 10)
395 go startAlarmLoop(stream, s.alarmCh)
396 go s.startDeviceActionLoop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900397 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900398 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900399 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700400 logger.Error("createIoinfos failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200401 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900402 }
403 s.Ioinfos = ioinfos
404 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700405 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900406
407 parent := ctx
408 child, cancel := context.WithCancel(parent)
409 s.cancel = cancel
410
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900411 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700412 logger.Error("runPktLoops failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200413 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900414 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200415 errorchan <- nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900416}
417
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200418// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900419func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900420 if s.cancel != nil {
421 cancel := s.cancel
422 cancel()
423 }
424}
425
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900426func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900427 ioinfos := []*Ioinfo{}
428 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900429 var handler *pcap.Handle
430 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900431 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700432 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900433 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900434 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900435
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900436 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900437 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900438 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900439 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900440 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900441}
442
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200443// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900444func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
445 logger.Debug("runPacketPktLoops Start")
446 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900447
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900448 errchOmci := make(chan error)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200449 s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900450 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900451 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900452
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900453 errchEapol := make(chan error)
454 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
455
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900456 errchDhcp := make(chan error)
457 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
458
Shad Ansarib744bf22019-01-17 11:36:46 -0800459 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900460 logger.Debug("runOMCIResponder Start")
461 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800462 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900463 case v, ok := <-errchOmci: // Wait for OmciInitialization
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200464 if ok { // Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700465 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900466 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900467 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800468 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900469 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900470 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900471 return nil
472 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900473
Shad Ansarib744bf22019-01-17 11:36:46 -0800474 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900475 logger.Debug("runEapolResponder Start")
476 defer logger.Debug("runEapolResponder Done")
477 select {
478 case v, ok := <-errchEapol:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200479 if ok { // Error
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900480 logger.Error("Error happend in Eapol:%s", v)
481 return v
482 }
483 case <-child.Done():
484 return nil
485 }
486 return nil
487 })
488
489 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900490 logger.Debug("runDhcpResponder Start")
491 defer logger.Debug("runDhcpResponder Done")
492 select {
493 case v, ok := <-errchDhcp:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200494 if ok { // Error
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900495 logger.Error("Error happend in Dhcp:%s", v)
496 return v
497 }
498 case <-child.Done():
499 return nil
500 }
501 return nil
502 })
503
504 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900505 err := s.runMainPktLoop(child, stream)
506 return err
507 })
508
509 if err := eg.Wait(); err != nil {
510 logger.Error("Error happend in runPacketLoops:%s", err)
511 cancel()
512 }
513 return nil
514}
515
516func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900517 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900518 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900519 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900520 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900521 ioinfo, err := s.IdentifyNniIoinfo("inside")
522 if err != nil {
523 return err
524 }
525 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
526 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800527 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900528 close(nnichannel)
529 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900530 logger.Debug("BEFORE OLT_ACTIVE")
531 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
532 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900533 data := &openolt.Indication_PktInd{}
534 for {
535 select {
536 case msg := <-s.omciIn:
537 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
538 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
539 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700540 logger.Error("send omci indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900541 continue
542 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700543 case msg := <-s.eapolIn:
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900544 intfid := msg.IntfId
545 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900546 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900547 if err != nil {
548 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
549 continue
550 }
551
Zack Williamsb85f5932019-05-10 16:21:35 -0700552 logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, msg.Byte)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900553
554 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
555 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700556 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900557 return err
558 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200559 case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900560 intfid := msg.IntfId
561 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900562 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900563 bytes := msg.Byte
564 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
565
566 if err != nil {
567 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
568 continue
569 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900570
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200571 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900572 if err != nil {
573 logger.Error("Failed to GetOnuByID:%d", onuid)
574 continue
575 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900576 sn := device.ConvB2S(onu.SerialNumber.VendorSpecific)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900577 if ctag, ok := s.CtagMap[sn]; ok == true {
578 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
579 if err != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900580 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900581 "gemId": gemid,
582 }).Error("Fail to tag C-tag")
583 } else {
584 pkt = tagpkt
585 }
586 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900587 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900588 "gemId": gemid,
589 "cTagMap": s.CtagMap,
590 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
591 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900592
Zack Williamsb85f5932019-05-10 16:21:35 -0700593 logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, pkt.Dump())
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900594
595 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
596 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700597 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900598 return err
599 }
600
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900601 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900602 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900603 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
604 logger.Debug("WARNING: This packet does not come from NNI ")
605 continue
606 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200607
608 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200609 intfid := nnipkt.Info.intfid
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200610 onu, _ := s.GetOnuByID(onuid, intfid)
611
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900612 device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200613
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900614 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900615 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
616 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700617 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900618 return err
619 }
620
621 case <-ctx.Done():
622 logger.Debug("Closed nnichannel ")
623 return nil
624 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900625 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900626}
627
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900628func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
629 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200630 onu, err := s.GetOnuByID(onuid, intfid)
631 if err != nil {
632 logger.Error("Failed processing onuPacketOut: %v", err)
633 return err
634 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800635
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900636 if layerEth != nil {
637 pkt, _ := layerEth.(*layers.Ethernet)
638 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900639 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900640 device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700641 eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900642 s.eapolOut <- &eapolPkt
643 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900644 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900645 device.LoggerWithOnu(onu).WithFields(log.Fields{
Matteo Scandoloa286c742018-11-20 08:10:04 -0800646 "payload": layerDHCP.LayerPayload(),
647 "type": layerDHCP.LayerType().String(),
648 }).Info("Received downstream packet is DHCP.")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700649 poppkt, _, err := PopVLAN(rawpkt)
650 if err != nil {
651 logger.Error("Received untagged packet when expecting single-tagged packet. Dropped.")
652 return nil
653 } else {
654 // check to see if the packet was double-tagged
655 _, _, err := PopVLAN(poppkt)
656 if err == nil {
657 logger.Error("Received double-tagged packet when expecting single-tagged packet. Dropped.")
658 return nil
659 }
660 }
661 logger.Debug("%s", poppkt.Dump())
662 dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: poppkt.Data()}
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900663 s.dhcpOut <- &dhcpPkt
664 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900665 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900666 device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP")
Scott Baker2b4ffb72019-08-02 16:20:47 -0700667 // TODO(smbaker): Clarify if this return is correct. There is SendUni() dead code that follows.
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900668 return nil
669 }
670 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
671 if err != nil {
672 return err
673 }
674 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800675 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900676 return nil
677 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900678 device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900679 return nil
680}
681
682func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900683 poppkt, _, err := PopVLAN(rawpkt)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900684 if err != nil {
Scott Baker2b4ffb72019-08-02 16:20:47 -0700685 logger.Error("Received untagged packet when expecting single-tagged packet. Dropped")
686 return nil
687 } else {
688 // check to see if the packet was double-tagged
689 poppktAgain, _, err := PopVLAN(poppkt)
690 if err == nil {
691 poppkt = poppktAgain
692 } else {
693 logger.Error("Received single-tagged packet when expecting double-tagged packet. Dropped.")
694 return nil
695 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900696 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900697 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900698 if err != nil {
699 return err
700 }
701 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900702 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900703 SendNni(handle, poppkt)
704 return nil
705}
706
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200707// IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900708func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
709 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900710 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900711 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900712 return false
713 }
714 }
715 }
716 return true
717}
718
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900719func (s *Server) isAllOnuOmciActive() bool {
720 for _, onus := range s.Onumap {
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700721 for _, onu := range onus {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900722 if onu.GetIntState() != device.ONU_OMCIACTIVE {
723 return false
724 }
725 }
726 }
727 return true
728}
729
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900730func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900731 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
732 gemportid, err := omci.GetGemPortId(intfid, onuid)
733 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900734 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200735 onu, err := s.GetOnuByID(onuid, intfid)
736 if err != nil {
737 logger.Error("Failed to getGemPortID: %s", err)
738 return 0, err
739 }
740 gemportid = onu.GemportID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900741 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900742 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900743}
744
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900745func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
746 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900747 for _, onu := range onus {
748 if device.ValidateSN(*sn, *onu.SerialNumber) {
749 return onu, nil
750 }
751 }
752 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200753 err := errors.New("no matching serial number found")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900754 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900755 return nil, err
756}
757
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200758// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200759func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
760 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800761}
762
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200763func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
764 for _, onu := range onumap[intfid] {
765 if onu.OnuID == onuid {
766 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900767 }
768 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200769 err := errors.New("no matching OnuID found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800770 logger.WithFields(log.Fields{
771 "onumap": onumap,
772 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200773 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800774 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900775 return nil, err
776}
777
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200778// getOnuFromSNmap method returns onu object from SNmap if found
779func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) {
780 snkey := stringifySerialNumber(serialNumber)
781
782 logger.Debug("getOnuFromSNmap received serial number %s", snkey)
783
784 if onu, exist := s.SNmap.Load(snkey); exist {
785 logger.Info("Serial number found in map")
786 return onu.(*device.Onu), true
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900787 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200788 logger.Info("Serial number not found in map")
789 return nil, false
790}
791
792func stringifySerialNumber(serialNum *openolt.SerialNumber) string {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900793 return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200794}
795
796func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) {
797 if len(SerialNumber) != SerialNumberLength {
798 logger.Error("Invalid serial number %s", SerialNumber)
799 return nil, errors.New("invalid serial number")
800 }
801 // First four characters are vendorId
802 vendorID := SerialNumber[:VendorIDLength]
803 vendorSpecific := SerialNumber[VendorIDLength:]
804
805 vsbyte, _ := hex.DecodeString(vendorSpecific)
806
807 // Convert to Openolt serial number
808 serialNum := new(openolt.SerialNumber)
809 serialNum.VendorId = []byte(vendorID)
810 serialNum.VendorSpecific = vsbyte
811
812 return serialNum, nil
813}
814
815// TODO move to device_onu.go
816func (s *Server) sendOnuIndicationsOnOltReboot() {
817 if AutoONUActivate == 1 {
818 // For auto activate mode, onu indications is sent in Enable()
819 return
820 }
821
822 s.SNmap.Range(
823 func(key, value interface{}) bool {
824 onu := value.(*device.Onu)
825 if onu.InternalState == device.ONU_LOS_RAISED {
826 return true
827 }
828
829 err := sendOnuDiscInd(*s.EnableServer, onu)
830 if err != nil {
831 logger.Error(err.Error())
832 }
833
834 return true
835 })
836}
837
838// StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received
839func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) {
840 for {
841 select {
842 case Req := <-s.serverActionCh:
843 logger.Debug("Request Received On serverActionCh: %+v", Req)
844 switch Req {
845 case "start":
846 logger.Debug("Server Start Request Received On ServerActionChannel")
847 go s.Start() // blocking
848 case "stop":
849 logger.Debug("Server Stop Request Received On ServerActionChannel")
850 s.Stop()
851 default:
852 logger.Error("Invalid value received in deviceActionCh")
853 }
854 }
855 }
856}
857
858// startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions
859// TODO all onu and olt related actions (like alarms) should be handled using this function
860func (s *Server) startDeviceActionLoop() {
861 logger.Debug("startDeviceActionLoop invoked")
862 s.deviceActionCh = make(chan *pb.DeviceAction, 10)
863 for {
864 logger.Debug("Action channel loop started")
865 select {
866 case Req := <-s.deviceActionCh:
867 logger.Debug("Reboot Action Type: %+v", Req.DeviceAction)
868 switch Req.DeviceType {
869 case DeviceTypeOnu:
870 value, _ := s.SNmap.Load(Req.DeviceSerialNumber)
871 onu := value.(*device.Onu)
872 if Req.DeviceAction == SoftReboot {
873 s.handleONUSoftReboot(onu.IntfID, onu.OnuID)
874 } else if Req.DeviceAction == HardReboot {
875 s.handleONUHardReboot(onu)
876 }
877 case DeviceTypeOlt:
878 logger.Debug("Reboot For OLT Received")
879 s.handleOLTReboot()
880 default:
881 logger.Error("Invalid value received in deviceActionCh")
882 }
883 }
884 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700885}