blob: 69c0bc803341e8aeb591eebf53fcb0378568b0e8 [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
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200110// NewCore initialize OLT and ONU objects
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900111func NewCore(opt *option) *Server {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900112 // TODO: make it decent
113 oltid := opt.oltid
Matteo Scandolo88e91892018-11-06 16:29:19 -0800114 npon := opt.npon
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200115 if npon > MaxPonPorts {
116 logger.Warn("Provided number of PON ports exceeds limit of %d", MaxPonPorts)
117 logger.Info("Setting number of PON ports to %d", MaxPonPorts)
118 npon = MaxPonPorts
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900119 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200120 nonus := opt.nonus
121 if nonus > MaxOnusPerPon {
122 logger.Warn("Provided number of ONUs per PON port exceeds limit of %d", MaxOnusPerPon)
123 logger.Info("Setting number of ONUs per PON port to %d", MaxOnusPerPon)
124 nonus = MaxOnusPerPon
125 }
126 s := Server{
127 Olt: device.NewOlt(oltid, npon, 1), // TODO nnni is to be taken from options
128 Onumap: make(map[uint32][]*device.Onu),
129 Ioinfos: []*Ioinfo{},
130 gRPCAddress: opt.address,
131 gRPCPort: opt.port,
132 Vethnames: []string{},
133 IndInterval: opt.intvl,
134 AutoONUActivate: !opt.interactiveOnuActivation,
135 Processes: []string{},
136 mgmtGrpcPort: opt.mgmtGrpcPort,
137 mgmtRestPort: opt.mgmtRestPort,
138 EnableServer: nil,
139 stateRepCh: make(chan stateReport, 8),
140 omciIn: make(chan openolt.OmciIndication, 1024),
141 omciOut: make(chan openolt.OmciMsg, 1024),
142 eapolIn: make(chan *byteMsg, 1024),
143 eapolOut: make(chan *byteMsg, 1024),
144 dhcpIn: make(chan *byteMsg, 1024),
145 dhcpOut: make(chan *byteMsg, 1024),
146 FlowMap: make(map[FlowKey]*openolt.Flow),
147 serverActionCh: make(chan string),
148 }
149 logger.Info("OLT %d created: %v", s.Olt.ID, s.Olt)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900150
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900151 nnni := s.Olt.NumNniIntf
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800152 logger.Info("OLT ID: %d was retrieved.", s.Olt.ID)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200153 logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber)
154 // Creating Onu Map
155 for intfid := uint32(0); intfid < npon; intfid++ {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900156 s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900157 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900158
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200159 logger.Debug("Onu Map:")
160 for _, onus := range s.Onumap {
161 for _, onu := range onus {
162 logger.Debug("%+v", *onu)
163 }
164 }
165
166 // TODO: To be fixed because it is hardcoded
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900167 s.CtagMap = make(map[string]uint32)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200168 for i := 0; i < MaxOnusPerPon; i++ {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900169 oltid := s.Olt.ID
170 intfid := uint32(1)
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900171 sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700172 s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900173 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200174
175 flowHandler.InitializeFlowManager(s.Olt.ID)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900176 return &s
177}
178
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200179// Start starts the openolt gRPC server (blocking)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800180func (s *Server) Start() error {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200181 logger.Debug("Starting OpenOLT gRPC Server")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800182 defer func() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200183 logger.Debug("OpenOLT gRPC Server Stopped")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900184 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200185
186 // Start Openolt gRPC server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900187 addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort))
188 listener, gserver, err := NewGrpcServer(addressport)
189 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700190 logger.Error("Failed to create gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900191 return err
192 }
193 s.gRPCserver = gserver
194 openolt.RegisterOpenoltServer(gserver, s)
195 if err := gserver.Serve(listener); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700196 logger.Error("Failed to run gRPC server: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197 return err
198 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900199 return nil
200}
201
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200202// Stop stops the openolt gRPC servers (non-blocking).
Matteo Scandolo88e91892018-11-06 16:29:19 -0800203func (s *Server) Stop() {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200204 logger.Debug("Stopping OpenOLT gRPC Server & PktLoops")
205 defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped")
206
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900207 if s.gRPCserver != nil {
208 s.gRPCserver.Stop()
209 logger.Debug("gRPCserver.Stop()")
210 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200211
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900212 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900213 return
214}
215
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200216func (s *Server) startMgmtServer(wg *sync.WaitGroup) {
217 defer logger.Debug("Management api server exited")
218
219 grpcAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
220 restAddressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtRestPort))
221 // Start rest gateway for BBSim server
222 go StartRestGatewayService(grpcAddressPort, restAddressPort, wg)
223 addressPort := s.gRPCAddress + ":" + strconv.Itoa(int(s.mgmtGrpcPort))
224
225 listener, apiserver, err := NewMgmtAPIServer(addressPort)
226 if err != nil {
227 logger.Error("Unable to create management api server %v", err)
228 return
229 }
230
231 s.mgmtServer = apiserver
232 pb.RegisterBBSimServiceServer(apiserver, s)
233 if e := apiserver.Serve(listener); e != nil {
234 logger.Error("Failed to run management api server %v", e)
235 return
236 }
237
238}
239
240func (s *Server) stopMgmtServer() error {
241 if s.mgmtServer != nil {
242 s.mgmtServer.GracefulStop()
243 logger.Debug("Management server stopped")
244 return nil
245 }
246 return errors.New("can not stop management server, server not created")
247}
248
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200249// Enable invokes methods for activation of OLT and ONU (blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900250func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900251 olt := s.Olt
Matteo Scandolo88e91892018-11-06 16:29:19 -0800252 defer func() {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900253 olt.Initialize()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200254 // Below lines commented as we dont want to change the onu state on restart
255 // for intfid := range s.Onumap {
256 // for _, onu := range s.Onumap[intfid] {
257 // onu.Initialize()
258 // }
259 // }
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900260 s.updateDevIntState(olt, device.OLT_INACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900261 logger.Debug("Enable() Done")
262 }()
263 logger.Debug("Enable() Start")
264 s.EnableServer = sv
265 if err := s.activateOLT(*sv); err != nil {
266 return err
267 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700268
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900269 s.updateDevIntState(olt, device.OLT_PREACTIVE)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900270
271 coreCtx := context.Background()
272 coreCtx, corecancel := context.WithCancel(coreCtx)
273 s.cancel = corecancel
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700274
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200275 errorchan := make(chan error, 5)
276 go s.StartPktLoops(coreCtx, *sv, errorchan)
277
278 if s.AutoONUActivate == true {
279 // Initialize all ONUs
280 for intfid := range s.Onumap {
281 for _, onu := range s.Onumap[intfid] {
282 onu.Initialize()
283 }
284 }
285 // Activate all ONUs
286 s.activateONUs(*sv, s.Onumap)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900287 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200288
289 select {
290 case err := <-errorchan:
291 if err != nil {
292 logger.Debug("Error: %v", err)
293 return err
294 }
295 }
296
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900297 return nil
298}
299
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200300// Disable stops packet loops (non-blocking)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900301func (s *Server) Disable() {
Matteo Scandolo88e91892018-11-06 16:29:19 -0800302 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900303 logger.Debug("Disable() Done")
304 }()
305 logger.Debug("Disable() Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900306 s.StopPktLoops()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900307}
308
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900309func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900310 logger.Debug("updateDevIntState called state:%d", state)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900311 current := dev.GetIntState()
312 dev.UpdateIntState(state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800313 s.stateRepCh <- stateReport{device: dev, current: current, next: state}
314 if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900315 logger.Debug("OLT State updated to:%d", state)
Shad Ansarib744bf22019-01-17 11:36:46 -0800316 } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900317 logger.Debug("ONU State updated to:%d", state)
318 } else {
319 logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev))
320 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900321}
322
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700323func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200324 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900325 if err != nil {
326 return err
327 }
328 s.updateDevIntState(onu, state)
329 return nil
330}
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200331
332func (s *Server) activateOnu(onu *device.Onu) {
333 snKey := stringifySerialNumber(onu.SerialNumber)
334 s.SNmap.Store(snKey, onu)
335 device.UpdateOnusOpStatus(onu.IntfID, onu, "up")
336
337 err := sendOnuDiscInd(*s.EnableServer, onu)
338 if err != nil {
339 logger.Error(err.Error())
340 return
341 }
342 logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID)
343 logger.Debug("activateONUs Entry in SNmap %v", snKey)
344}
345
346func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) {
347 // Add all ONUs to SerialNumber Map
348 for intfid := range Onumap {
349 for _, onu := range Onumap[intfid] {
350 s.activateOnu(onu)
351 }
352 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700353}
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900354
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900355func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900356 defer logger.Debug("activateOLT() Done")
357 logger.Debug("activateOLT() Start")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900358 // Activate OLT
359 olt := s.Olt
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900360 if err := sendOltIndUp(stream, olt); err != nil {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900361 return err
362 }
363 olt.OperState = "up"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800364 logger.Info("OLT %s sent OltInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900365
366 // OLT sends Interface Indication to Adapter
367 if err := sendIntfInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800368 logger.Error("Fail to sendIntfInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900369 return err
370 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800371 logger.Info("OLT %s sent IntfInd.", olt.Name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900372
373 // OLT sends Operation Indication to Adapter after activating each interface
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900374 if err := sendOperInd(stream, olt); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800375 logger.Error("Fail to sendOperInd: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900376 return err
377 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800378 logger.Info("OLT %s sent OperInd.", olt.Name)
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700379 return nil
380}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900381
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200382// StartPktLoops creates veth pairs and invokes runPktLoops (blocking)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200383func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900384 logger.Debug("StartPktLoops () Start")
Matteo Scandolo88e91892018-11-06 16:29:19 -0800385 defer func() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900386 RemoveVeths(s.Vethnames)
387 s.Vethnames = []string{}
388 s.Ioinfos = []*Ioinfo{}
389 s.wg.Done()
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900390 s.updateDevIntState(s.Olt, device.OLT_PREACTIVE)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900391 logger.Debug("StartPktLoops () Done")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900392 }()
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200393 s.alarmCh = make(chan *openolt.Indication, 10)
394 go startAlarmLoop(stream, s.alarmCh)
395 go s.startDeviceActionLoop()
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900396 s.wg.Add(1)
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900397 ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900398 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700399 logger.Error("createIoinfos failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200400 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900401 }
402 s.Ioinfos = ioinfos
403 s.Vethnames = veths
Zack Williamsb85f5932019-05-10 16:21:35 -0700404 logger.Debug("Created vethnames: %v", s.Vethnames)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900405
406 parent := ctx
407 child, cancel := context.WithCancel(parent)
408 s.cancel = cancel
409
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900410 if err = s.runPktLoops(child, stream); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700411 logger.Error("runPktLoops failed: %v", err)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200412 errorchan <- err
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900413 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200414 errorchan <- nil
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900415}
416
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200417// StopPktLoops (non-blocking)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900418func (s *Server) StopPktLoops() {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900419 if s.cancel != nil {
420 cancel := s.cancel
421 cancel()
422 }
423}
424
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900425func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900426 ioinfos := []*Ioinfo{}
427 var err error
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900428 var handler *pcap.Handle
429 nniup, nnidw := makeNniName(oltid)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900430 if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700431 logger.Error("setupVethHandler failed for nni: %v", err)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900432 return ioinfos, Vethnames, err
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900433 }
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900434
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900435 iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900436 ioinfos = append(ioinfos, &iinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900437 oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900438 ioinfos = append(ioinfos, &oinfo)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900439 return ioinfos, Vethnames, nil
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900440}
441
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200442// Blocking
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900443func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
444 logger.Debug("runPacketPktLoops Start")
445 defer logger.Debug("runPacketLoops Done")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900446
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900447 errchOmci := make(chan error)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200448 s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900449 eg, child := errgroup.WithContext(ctx)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900450 child, cancel := context.WithCancel(child)
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900451
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900452 errchEapol := make(chan error)
453 RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol)
454
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900455 errchDhcp := make(chan error)
456 RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp)
457
Shad Ansarib744bf22019-01-17 11:36:46 -0800458 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900459 logger.Debug("runOMCIResponder Start")
460 defer logger.Debug("runOMCIResponder Done")
Shad Ansarib744bf22019-01-17 11:36:46 -0800461 select {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900462 case v, ok := <-errchOmci: // Wait for OmciInitialization
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200463 if ok { // Error
Zack Williamsb85f5932019-05-10 16:21:35 -0700464 logger.Error("Error happend in Omci: %s", v)
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900465 return v
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900466 }
Shad Ansarib744bf22019-01-17 11:36:46 -0800467 case <-child.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900468 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900469 }
Keita NISHIMOTO2b694202018-12-18 07:30:55 +0900470 return nil
471 })
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900472
Shad Ansarib744bf22019-01-17 11:36:46 -0800473 eg.Go(func() error {
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900474 logger.Debug("runEapolResponder Start")
475 defer logger.Debug("runEapolResponder Done")
476 select {
477 case v, ok := <-errchEapol:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200478 if ok { // Error
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900479 logger.Error("Error happend in Eapol:%s", v)
480 return v
481 }
482 case <-child.Done():
483 return nil
484 }
485 return nil
486 })
487
488 eg.Go(func() error {
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900489 logger.Debug("runDhcpResponder Start")
490 defer logger.Debug("runDhcpResponder Done")
491 select {
492 case v, ok := <-errchDhcp:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200493 if ok { // Error
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900494 logger.Error("Error happend in Dhcp:%s", v)
495 return v
496 }
497 case <-child.Done():
498 return nil
499 }
500 return nil
501 })
502
503 eg.Go(func() error {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900504 err := s.runMainPktLoop(child, stream)
505 return err
506 })
507
508 if err := eg.Wait(); err != nil {
509 logger.Error("Error happend in runPacketLoops:%s", err)
510 cancel()
511 }
512 return nil
513}
514
515func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900516 logger.Debug("runMainPktLoop Start")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900517 defer func() {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900518 logger.Debug("runMainPktLoop Done")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900519 }()
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900520 ioinfo, err := s.IdentifyNniIoinfo("inside")
521 if err != nil {
522 return err
523 }
524 nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32)
525 go RecvWorker(ioinfo, nhandler, nnichannel)
Shad Ansarib744bf22019-01-17 11:36:46 -0800526 defer func() {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900527 close(nnichannel)
528 }()
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900529 logger.Debug("BEFORE OLT_ACTIVE")
530 s.updateDevIntState(s.Olt, device.OLT_ACTIVE)
531 logger.Debug("AFTER OLT_ACTIVE")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900532 data := &openolt.Indication_PktInd{}
533 for {
534 select {
535 case msg := <-s.omciIn:
536 logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
537 omci := &openolt.Indication_OmciInd{OmciInd: &msg}
538 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700539 logger.Error("send omci indication failed: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900540 continue
541 }
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700542 case msg := <-s.eapolIn:
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900543 intfid := msg.IntfId
544 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900545 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900546 if err != nil {
547 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
548 continue
549 }
550
Zack Williamsb85f5932019-05-10 16:21:35 -0700551 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 +0900552
553 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
554 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700555 logger.Error("Fail to send EAPOL PktInd indication. %v", err)
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900556 return err
557 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200558 case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900559 intfid := msg.IntfId
560 onuid := msg.OnuId
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900561 gemid, err := s.getGemPortID(intfid, onuid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900562 bytes := msg.Byte
563 pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
564
565 if err != nil {
566 logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid)
567 continue
568 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900569
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200570 onu, err := s.GetOnuByID(onuid, intfid)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900571 if err != nil {
572 logger.Error("Failed to GetOnuByID:%d", onuid)
573 continue
574 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900575 sn := device.ConvB2S(onu.SerialNumber.VendorSpecific)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900576 if ctag, ok := s.CtagMap[sn]; ok == true {
577 tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
578 if err != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900579 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900580 "gemId": gemid,
581 }).Error("Fail to tag C-tag")
582 } else {
583 pkt = tagpkt
584 }
585 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900586 device.LoggerWithOnu(onu).WithFields(log.Fields{
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900587 "gemId": gemid,
588 "cTagMap": s.CtagMap,
589 }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
590 }
Keita NISHIMOTO9a4c4dc2019-02-13 09:33:00 +0900591
Zack Williamsb85f5932019-05-10 16:21:35 -0700592 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 +0900593
594 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}}
595 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700596 logger.Error("Fail to send DHCP PktInd indication: %v", err)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900597 return err
598 }
599
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900600 case nnipkt := <-nnichannel:
Keita NISHIMOTOdad44cb2019-02-08 09:45:40 +0900601 logger.Debug("Received packet from NNI")
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900602 if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" {
603 logger.Debug("WARNING: This packet does not come from NNI ")
604 continue
605 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200606
607 onuid := nnipkt.Info.onuid
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200608 intfid := nnipkt.Info.intfid
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200609 onu, _ := s.GetOnuByID(onuid, intfid)
610
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900611 device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200612
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900613 pkt := nnipkt.Pkt
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900614 data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
615 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700616 logger.Error("Fail to send PktInd indication: %v", err)
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +0900617 return err
618 }
619
620 case <-ctx.Done():
621 logger.Debug("Closed nnichannel ")
622 return nil
623 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900624 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900625}
626
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900627func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error {
628 layerEth := rawpkt.Layer(layers.LayerTypeEthernet)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200629 onu, err := s.GetOnuByID(onuid, intfid)
630 if err != nil {
631 logger.Error("Failed processing onuPacketOut: %v", err)
632 return err
633 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800634
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900635 if layerEth != nil {
636 pkt, _ := layerEth.(*layers.Ethernet)
637 ethtype := pkt.EthernetType
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900638 if ethtype == layers.EthernetTypeEAPOL {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900639 device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700640 eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +0900641 s.eapolOut <- &eapolPkt
642 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900643 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900644 device.LoggerWithOnu(onu).WithFields(log.Fields{
Matteo Scandoloa286c742018-11-20 08:10:04 -0800645 "payload": layerDHCP.LayerPayload(),
646 "type": layerDHCP.LayerType().String(),
647 }).Info("Received downstream packet is DHCP.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900648 rawpkt, _, _ = PopVLAN(rawpkt)
649 rawpkt, _, _ = PopVLAN(rawpkt)
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900650 logger.Debug("%s", rawpkt.Dump())
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700651 dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900652 s.dhcpOut <- &dhcpPkt
653 return nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900654 } else {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900655 device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900656 return nil
657 }
658 ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
659 if err != nil {
660 return err
661 }
662 handle := ioinfo.handler
Matteo Scandoloa286c742018-11-20 08:10:04 -0800663 SendUni(handle, rawpkt, onu)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900664 return nil
665 }
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900666 device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900667 return nil
668}
669
670func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900671 poppkt, _, err := PopVLAN(rawpkt)
672 poppkt, _, err = PopVLAN(poppkt)
673 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900674 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900675 return err
676 }
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900677 ioinfo, err := s.IdentifyNniIoinfo("inside")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900678 if err != nil {
679 return err
680 }
681 handle := ioinfo.handler
Keita NISHIMOTO2f8a6a42019-02-08 09:47:07 +0900682 logger.Debug("%s", poppkt.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900683 SendNni(handle, poppkt)
684 return nil
685}
686
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200687// IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900688func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool {
689 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900690 for _, onu := range onus {
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900691 if onu.GetIntState() != device.ONU_ACTIVE {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900692 return false
693 }
694 }
695 }
696 return true
697}
698
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900699func (s *Server) isAllOnuOmciActive() bool {
700 for _, onus := range s.Onumap {
Mahir Gunyel9897f6e2019-05-22 14:54:05 -0700701 for _, onu := range onus {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900702 if onu.GetIntState() != device.ONU_OMCIACTIVE {
703 return false
704 }
705 }
706 }
707 return true
708}
709
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900710func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900711 logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid)
712 gemportid, err := omci.GetGemPortId(intfid, onuid)
713 if err != nil {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900714 logger.Warn("Failed to getGemPortID from OMCI lib: %s", err)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200715 onu, err := s.GetOnuByID(onuid, intfid)
716 if err != nil {
717 logger.Error("Failed to getGemPortID: %s", err)
718 return 0, err
719 }
720 gemportid = onu.GemportID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900721 }
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900722 return uint32(gemportid), nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900723}
724
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900725func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) {
726 for _, onus := range onumap {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900727 for _, onu := range onus {
728 if device.ValidateSN(*sn, *onu.SerialNumber) {
729 return onu, nil
730 }
731 }
732 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200733 err := errors.New("no matching serial number found")
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900734 logger.Error("%s", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900735 return nil, err
736}
737
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200738// GetOnuByID returns ONU object as per onuID and intfID
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200739func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) {
740 return getOnuByID(s.Onumap, onuid, intfid)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800741}
742
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200743func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) {
744 for _, onu := range onumap[intfid] {
745 if onu.OnuID == onuid {
746 return onu, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900747 }
748 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200749 err := errors.New("no matching OnuID found")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800750 logger.WithFields(log.Fields{
751 "onumap": onumap,
752 "onuid": onuid,
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200753 "intfid": intfid,
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800754 }).Error(err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900755 return nil, err
756}
757
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200758// getOnuFromSNmap method returns onu object from SNmap if found
759func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) {
760 snkey := stringifySerialNumber(serialNumber)
761
762 logger.Debug("getOnuFromSNmap received serial number %s", snkey)
763
764 if onu, exist := s.SNmap.Load(snkey); exist {
765 logger.Info("Serial number found in map")
766 return onu.(*device.Onu), true
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900767 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200768 logger.Info("Serial number not found in map")
769 return nil, false
770}
771
772func stringifySerialNumber(serialNum *openolt.SerialNumber) string {
Keita NISHIMOTO9617c852019-06-17 21:46:44 +0900773 return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200774}
775
776func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) {
777 if len(SerialNumber) != SerialNumberLength {
778 logger.Error("Invalid serial number %s", SerialNumber)
779 return nil, errors.New("invalid serial number")
780 }
781 // First four characters are vendorId
782 vendorID := SerialNumber[:VendorIDLength]
783 vendorSpecific := SerialNumber[VendorIDLength:]
784
785 vsbyte, _ := hex.DecodeString(vendorSpecific)
786
787 // Convert to Openolt serial number
788 serialNum := new(openolt.SerialNumber)
789 serialNum.VendorId = []byte(vendorID)
790 serialNum.VendorSpecific = vsbyte
791
792 return serialNum, nil
793}
794
795// TODO move to device_onu.go
796func (s *Server) sendOnuIndicationsOnOltReboot() {
797 if AutoONUActivate == 1 {
798 // For auto activate mode, onu indications is sent in Enable()
799 return
800 }
801
802 s.SNmap.Range(
803 func(key, value interface{}) bool {
804 onu := value.(*device.Onu)
805 if onu.InternalState == device.ONU_LOS_RAISED {
806 return true
807 }
808
809 err := sendOnuDiscInd(*s.EnableServer, onu)
810 if err != nil {
811 logger.Error(err.Error())
812 }
813
814 return true
815 })
816}
817
818// StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received
819func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) {
820 for {
821 select {
822 case Req := <-s.serverActionCh:
823 logger.Debug("Request Received On serverActionCh: %+v", Req)
824 switch Req {
825 case "start":
826 logger.Debug("Server Start Request Received On ServerActionChannel")
827 go s.Start() // blocking
828 case "stop":
829 logger.Debug("Server Stop Request Received On ServerActionChannel")
830 s.Stop()
831 default:
832 logger.Error("Invalid value received in deviceActionCh")
833 }
834 }
835 }
836}
837
838// startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions
839// TODO all onu and olt related actions (like alarms) should be handled using this function
840func (s *Server) startDeviceActionLoop() {
841 logger.Debug("startDeviceActionLoop invoked")
842 s.deviceActionCh = make(chan *pb.DeviceAction, 10)
843 for {
844 logger.Debug("Action channel loop started")
845 select {
846 case Req := <-s.deviceActionCh:
847 logger.Debug("Reboot Action Type: %+v", Req.DeviceAction)
848 switch Req.DeviceType {
849 case DeviceTypeOnu:
850 value, _ := s.SNmap.Load(Req.DeviceSerialNumber)
851 onu := value.(*device.Onu)
852 if Req.DeviceAction == SoftReboot {
853 s.handleONUSoftReboot(onu.IntfID, onu.OnuID)
854 } else if Req.DeviceAction == HardReboot {
855 s.handleONUHardReboot(onu)
856 }
857 case DeviceTypeOlt:
858 logger.Debug("Reboot For OLT Received")
859 s.handleOLTReboot()
860 default:
861 logger.Error("Invalid value received in deviceActionCh")
862 }
863 }
864 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700865}