Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 20 | "context" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 21 | "encoding/hex" |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 22 | "errors" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 23 | "reflect" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 24 | "strconv" |
| 25 | "sync" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 26 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 27 | "github.com/google/gopacket" |
| 28 | "github.com/google/gopacket/layers" |
| 29 | "github.com/google/gopacket/pcap" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 30 | omci "github.com/opencord/omci-sim" |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame^] | 31 | pb "github.com/opencord/voltha-bbsim/api" |
| 32 | "github.com/opencord/voltha-bbsim/common/logger" |
| 33 | "github.com/opencord/voltha-bbsim/device" |
| 34 | flowHandler "github.com/opencord/voltha-bbsim/flow" |
| 35 | openolt "github.com/opencord/voltha-bbsim/protos" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 36 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 37 | "golang.org/x/sync/errgroup" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 38 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 39 | ) |
| 40 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 41 | // Constants |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 42 | const ( |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 43 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | ) |
| 52 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 53 | // Server structure consists of all the params required for BBsim. |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 54 | type Server struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 55 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 86 | // Packet structure |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 87 | type Packet struct { |
| 88 | Info *Ioinfo |
| 89 | Pkt gopacket.Packet |
| 90 | } |
| 91 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 92 | type byteMsg struct { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 93 | IntfId uint32 |
| 94 | OnuId uint32 |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 95 | Byte []byte |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 98 | type stateReport struct { |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 99 | device device.Device |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 100 | current device.DeviceState |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 101 | next device.DeviceState |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 102 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 103 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 104 | // FlowKey used for FlowMap key |
| 105 | type FlowKey struct { |
| 106 | FlowID uint32 |
| 107 | FlowDirection string |
| 108 | } |
| 109 | |
Anjali Thontakudi | 6665920 | 2019-07-03 23:14:02 +0000 | [diff] [blame] | 110 | //Has options (OLT id, number onu ports) from mediator |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 111 | // NewCore initialize OLT and ONU objects |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 112 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 113 | // TODO: make it decent |
| 114 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 115 | npon := opt.npon |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 116 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 120 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 121 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 151 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 152 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 153 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 154 | logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber) |
| 155 | // Creating Onu Map |
| 156 | for intfid := uint32(0); intfid < npon; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 157 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 158 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 159 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 160 | 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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 168 | s.CtagMap = make(map[string]uint32) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 169 | for i := 0; i < MaxOnusPerPon; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 170 | oltid := s.Olt.ID |
| 171 | intfid := uint32(1) |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 172 | sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 173 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 174 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 175 | |
| 176 | flowHandler.InitializeFlowManager(s.Olt.ID) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 177 | return &s |
| 178 | } |
| 179 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 180 | // Start starts the openolt gRPC server (blocking) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 181 | func (s *Server) Start() error { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 182 | logger.Debug("Starting OpenOLT gRPC Server") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 183 | defer func() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 184 | logger.Debug("OpenOLT gRPC Server Stopped") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 185 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 186 | |
| 187 | // Start Openolt gRPC server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 188 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 189 | listener, gserver, err := NewGrpcServer(addressport) |
| 190 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 191 | logger.Error("Failed to create gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 192 | return err |
| 193 | } |
| 194 | s.gRPCserver = gserver |
| 195 | openolt.RegisterOpenoltServer(gserver, s) |
| 196 | if err := gserver.Serve(listener); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 197 | logger.Error("Failed to run gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 198 | return err |
| 199 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 200 | return nil |
| 201 | } |
| 202 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 203 | // Stop stops the openolt gRPC servers (non-blocking). |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 204 | func (s *Server) Stop() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 205 | logger.Debug("Stopping OpenOLT gRPC Server & PktLoops") |
| 206 | defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped") |
| 207 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 208 | if s.gRPCserver != nil { |
| 209 | s.gRPCserver.Stop() |
| 210 | logger.Debug("gRPCserver.Stop()") |
| 211 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 212 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 213 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 214 | return |
| 215 | } |
| 216 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 217 | func (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 | |
| 241 | func (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 Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 250 | // Enable invokes methods for activation of OLT and ONU (blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 251 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 252 | olt := s.Olt |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 253 | defer func() { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 254 | olt.Initialize() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 255 | // 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 NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 261 | s.updateDevIntState(olt, device.OLT_INACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 262 | 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 Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 269 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 270 | s.updateDevIntState(olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 271 | |
| 272 | coreCtx := context.Background() |
| 273 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 274 | s.cancel = corecancel |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 275 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 276 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 288 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 289 | |
| 290 | select { |
| 291 | case err := <-errorchan: |
| 292 | if err != nil { |
| 293 | logger.Debug("Error: %v", err) |
| 294 | return err |
| 295 | } |
| 296 | } |
| 297 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 298 | return nil |
| 299 | } |
| 300 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 301 | // Disable stops packet loops (non-blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 302 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 303 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 304 | logger.Debug("Disable() Done") |
| 305 | }() |
| 306 | logger.Debug("Disable() Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 307 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 310 | func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) { |
| 311 | current := dev.GetIntState() |
| 312 | dev.UpdateIntState(state) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 313 | logger.Debug("updateDevIntState called state: current %s, next %s", device.ONUState[current], device.ONUState[dev.GetIntState()]) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 314 | s.stateRepCh <- stateReport{device: dev, current: current, next: state} |
| 315 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 316 | logger.Debug("OLT State updated to:%s", device.ONUState[state]) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 317 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 318 | logger.Debug("ONU State updated to:%s", device.ONUState[state]) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 319 | } else { |
| 320 | logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev)) |
| 321 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 324 | func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 325 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 326 | onu, err := s.GetOnuByID(onuid, intfid) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 327 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 328 | if err != nil { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 329 | logger.Warn("Failed to get ONU %d: %v", onuid, err) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 330 | return err |
| 331 | } |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 332 | |
| 333 | if state == onu.GetIntState(){ |
| 334 | logger.WithFields(log.Fields{ |
| 335 | "toState": device.ONUState[state], |
| 336 | "currentState": device.ONUState[onu.GetIntState()], |
| 337 | }).Warn("Trying to update the state with the same state, ignoring this request") |
| 338 | return nil |
| 339 | } |
| 340 | |
| 341 | logger.Debug("Transitioning ONU state from %s to %s", device.ONUState[onu.GetIntState()], device.ONUState[state]) |
| 342 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 343 | s.updateDevIntState(onu, state) |
| 344 | return nil |
| 345 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 346 | |
| 347 | func (s *Server) activateOnu(onu *device.Onu) { |
| 348 | snKey := stringifySerialNumber(onu.SerialNumber) |
| 349 | s.SNmap.Store(snKey, onu) |
| 350 | device.UpdateOnusOpStatus(onu.IntfID, onu, "up") |
| 351 | |
| 352 | err := sendOnuDiscInd(*s.EnableServer, onu) |
| 353 | if err != nil { |
| 354 | logger.Error(err.Error()) |
| 355 | return |
| 356 | } |
| 357 | logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID) |
| 358 | logger.Debug("activateONUs Entry in SNmap %v", snKey) |
| 359 | } |
| 360 | |
| 361 | func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) { |
| 362 | // Add all ONUs to SerialNumber Map |
| 363 | for intfid := range Onumap { |
| 364 | for _, onu := range Onumap[intfid] { |
| 365 | s.activateOnu(onu) |
| 366 | } |
| 367 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 368 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 369 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 370 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 371 | defer logger.Debug("activateOLT() Done") |
| 372 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 373 | // Activate OLT |
| 374 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 375 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 376 | return err |
| 377 | } |
| 378 | olt.OperState = "up" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 379 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 380 | |
| 381 | // OLT sends Interface Indication to Adapter |
| 382 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 383 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 384 | return err |
| 385 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 386 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 387 | |
| 388 | // OLT sends Operation Indication to Adapter after activating each interface |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 389 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 390 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 391 | return err |
| 392 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 393 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 394 | return nil |
| 395 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 396 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 397 | // StartPktLoops creates veth pairs and invokes runPktLoops (blocking) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 398 | func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, errorchan chan error) { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 399 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 400 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 401 | RemoveVeths(s.Vethnames) |
| 402 | s.Vethnames = []string{} |
| 403 | s.Ioinfos = []*Ioinfo{} |
| 404 | s.wg.Done() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 405 | s.updateDevIntState(s.Olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 406 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 407 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 408 | s.alarmCh = make(chan *openolt.Indication, 10) |
| 409 | go startAlarmLoop(stream, s.alarmCh) |
| 410 | go s.startDeviceActionLoop() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 411 | s.wg.Add(1) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 412 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 413 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 414 | logger.Error("createIoinfos failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 415 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 416 | } |
| 417 | s.Ioinfos = ioinfos |
| 418 | s.Vethnames = veths |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 419 | logger.Debug("Created vethnames: %v", s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 420 | |
| 421 | parent := ctx |
| 422 | child, cancel := context.WithCancel(parent) |
| 423 | s.cancel = cancel |
| 424 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 425 | if err = s.runPktLoops(child, stream); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 426 | logger.Error("runPktLoops failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 427 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 428 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 429 | errorchan <- nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 430 | } |
| 431 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 432 | // StopPktLoops (non-blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 433 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 434 | if s.cancel != nil { |
| 435 | cancel := s.cancel |
| 436 | cancel() |
| 437 | } |
| 438 | } |
| 439 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 440 | func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 441 | ioinfos := []*Ioinfo{} |
| 442 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 443 | var handler *pcap.Handle |
| 444 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 445 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 446 | logger.Error("setupVethHandler failed for nni: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 447 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 448 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 449 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 450 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 451 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 452 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 453 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 454 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 455 | } |
| 456 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 457 | // Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 458 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 459 | logger.Debug("runPacketPktLoops Start") |
| 460 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 461 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 462 | errchOmci := make(chan error) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 463 | s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 464 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 465 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 466 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 467 | errchEapol := make(chan error) |
| 468 | RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol) |
| 469 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 470 | errchDhcp := make(chan error) |
| 471 | RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp) |
| 472 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 473 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 474 | logger.Debug("runOMCIResponder Start") |
| 475 | defer logger.Debug("runOMCIResponder Done") |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 476 | select { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 477 | case v, ok := <-errchOmci: // Wait for OmciInitialization |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 478 | if ok { // Error |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 479 | logger.Error("Error happend in Omci: %s", v) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 480 | return v |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 481 | } |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 482 | case <-child.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 483 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 484 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 485 | return nil |
| 486 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 487 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 488 | eg.Go(func() error { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 489 | logger.Debug("runEapolResponder Start") |
| 490 | defer logger.Debug("runEapolResponder Done") |
| 491 | select { |
| 492 | case v, ok := <-errchEapol: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 493 | if ok { // Error |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 494 | logger.Error("Error happend in Eapol:%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 NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 504 | logger.Debug("runDhcpResponder Start") |
| 505 | defer logger.Debug("runDhcpResponder Done") |
| 506 | select { |
| 507 | case v, ok := <-errchDhcp: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 508 | if ok { // Error |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 509 | logger.Error("Error happend in Dhcp:%s", v) |
| 510 | return v |
| 511 | } |
| 512 | case <-child.Done(): |
| 513 | return nil |
| 514 | } |
| 515 | return nil |
| 516 | }) |
| 517 | |
| 518 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 519 | err := s.runMainPktLoop(child, stream) |
| 520 | return err |
| 521 | }) |
| 522 | |
| 523 | if err := eg.Wait(); err != nil { |
| 524 | logger.Error("Error happend in runPacketLoops:%s", err) |
| 525 | cancel() |
| 526 | } |
| 527 | return nil |
| 528 | } |
| 529 | |
| 530 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 531 | logger.Debug("runMainPktLoop Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 532 | defer func() { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 533 | logger.Debug("runMainPktLoop Done") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 534 | }() |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 535 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
| 536 | if err != nil { |
| 537 | return err |
| 538 | } |
| 539 | nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32) |
| 540 | go RecvWorker(ioinfo, nhandler, nnichannel) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 541 | defer func() { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 542 | close(nnichannel) |
| 543 | }() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 544 | logger.Debug("BEFORE OLT_ACTIVE") |
| 545 | s.updateDevIntState(s.Olt, device.OLT_ACTIVE) |
| 546 | logger.Debug("AFTER OLT_ACTIVE") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 547 | data := &openolt.Indication_PktInd{} |
| 548 | for { |
| 549 | select { |
| 550 | case msg := <-s.omciIn: |
| 551 | logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
| 552 | omci := &openolt.Indication_OmciInd{OmciInd: &msg} |
| 553 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 554 | logger.Error("send omci indication failed: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 555 | continue |
| 556 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 557 | case msg := <-s.eapolIn: |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 558 | intfid := msg.IntfId |
| 559 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 560 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 561 | if err != nil { |
| 562 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 563 | continue |
| 564 | } |
| 565 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 566 | logger.Debug("OLT %d send eapol packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, msg.Byte) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 567 | |
| 568 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}} |
| 569 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 570 | logger.Error("Fail to send EAPOL PktInd indication. %v", err) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 571 | return err |
| 572 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 573 | case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 574 | intfid := msg.IntfId |
| 575 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 576 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 577 | bytes := msg.Byte |
| 578 | pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default) |
| 579 | |
| 580 | if err != nil { |
| 581 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 582 | continue |
| 583 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 584 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 585 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 586 | if err != nil { |
| 587 | logger.Error("Failed to GetOnuByID:%d", onuid) |
| 588 | continue |
| 589 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 590 | sn := device.ConvB2S(onu.SerialNumber.VendorSpecific) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 591 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 592 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 593 | if err != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 594 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 595 | "gemId": gemid, |
| 596 | }).Error("Fail to tag C-tag") |
| 597 | } else { |
| 598 | pkt = tagpkt |
| 599 | } |
| 600 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 601 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 602 | "gemId": gemid, |
| 603 | "cTagMap": s.CtagMap, |
| 604 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
| 605 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 606 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 607 | logger.Debug("OLT %d send dhcp packet in (upstream), IF %v (ONU-ID: %v) pkt: %x", s.Olt.ID, intfid, onuid, pkt.Dump()) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 608 | |
| 609 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}} |
| 610 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 611 | logger.Error("Fail to send DHCP PktInd indication: %v", err) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 612 | return err |
| 613 | } |
| 614 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 615 | case nnipkt := <-nnichannel: |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 616 | logger.Debug("Received packet from NNI") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 617 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 618 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 619 | continue |
| 620 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 621 | |
| 622 | onuid := nnipkt.Info.onuid |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 623 | intfid := nnipkt.Info.intfid |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 624 | onu, _ := s.GetOnuByID(onuid, intfid) |
| 625 | |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 626 | device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 627 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 628 | pkt := nnipkt.Pkt |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 629 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 630 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 631 | logger.Error("Fail to send PktInd indication: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 632 | return err |
| 633 | } |
| 634 | |
| 635 | case <-ctx.Done(): |
| 636 | logger.Debug("Closed nnichannel ") |
| 637 | return nil |
| 638 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 639 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 640 | } |
| 641 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 642 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 643 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 644 | onu, err := s.GetOnuByID(onuid, intfid) |
| 645 | if err != nil { |
| 646 | logger.Error("Failed processing onuPacketOut: %v", err) |
| 647 | return err |
| 648 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 649 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 650 | if layerEth != nil { |
| 651 | pkt, _ := layerEth.(*layers.Ethernet) |
| 652 | ethtype := pkt.EthernetType |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 653 | if ethtype == layers.EthernetTypeEAPOL { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 654 | device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 655 | eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 656 | s.eapolOut <- &eapolPkt |
| 657 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 658 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 659 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 660 | "payload": layerDHCP.LayerPayload(), |
| 661 | "type": layerDHCP.LayerType().String(), |
| 662 | }).Info("Received downstream packet is DHCP.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 663 | poppkt, _, err := PopVLAN(rawpkt) |
| 664 | if err != nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 665 | logger.Warn("Received untagged packet when expecting single-tagged packet.") |
| 666 | poppkt = rawpkt |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 667 | } else { |
| 668 | // check to see if the packet was double-tagged |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 669 | poppktAgain, _, err := PopVLAN(poppkt) |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 670 | if err == nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 671 | logger.Warn("Received double-tagged packet when expecting single-tagged packet.") |
| 672 | poppkt = poppktAgain |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | logger.Debug("%s", poppkt.Dump()) |
| 676 | dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: poppkt.Data()} |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 677 | s.dhcpOut <- &dhcpPkt |
| 678 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 679 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 680 | device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 681 | // TODO(smbaker): Clarify if this return is correct. There is SendUni() dead code that follows. |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 682 | return nil |
| 683 | } |
| 684 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 685 | if err != nil { |
| 686 | return err |
| 687 | } |
| 688 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 689 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 690 | return nil |
| 691 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 692 | device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 693 | return nil |
| 694 | } |
| 695 | |
| 696 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 697 | poppkt, _, err := PopVLAN(rawpkt) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 698 | if err != nil { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 699 | logger.Warn("Received untagged packet when expecting double-tagged packet.") |
| 700 | poppkt = rawpkt |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 701 | } else { |
| 702 | // check to see if the packet was double-tagged |
| 703 | poppktAgain, _, err := PopVLAN(poppkt) |
| 704 | if err == nil { |
| 705 | poppkt = poppktAgain |
| 706 | } else { |
Scott Baker | 07fe4dc | 2019-08-05 16:36:37 -0700 | [diff] [blame] | 707 | logger.Warn("Received single-tagged packet when expecting double-tagged packet.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 708 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 709 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 710 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 711 | if err != nil { |
| 712 | return err |
| 713 | } |
| 714 | handle := ioinfo.handler |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 715 | logger.Debug("%s", poppkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 716 | SendNni(handle, poppkt) |
| 717 | return nil |
| 718 | } |
| 719 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 720 | // IsAllOnuActive checks for ONU_ACTIVE state for all the onus in the map |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 721 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 722 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 723 | for _, onu := range onus { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 724 | if onu.GetIntState() != device.ONU_ACTIVE { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 725 | return false |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | return true |
| 730 | } |
| 731 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 732 | func (s *Server) isAllOnuOmciActive() bool { |
| 733 | for _, onus := range s.Onumap { |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 734 | for _, onu := range onus { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 735 | if onu.GetIntState() != device.ONU_OMCIACTIVE { |
| 736 | return false |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | return true |
| 741 | } |
| 742 | |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 743 | func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 744 | logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid) |
| 745 | gemportid, err := omci.GetGemPortId(intfid, onuid) |
| 746 | if err != nil { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 747 | logger.Warn("Failed to getGemPortID from OMCI lib: %s", err) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 748 | onu, err := s.GetOnuByID(onuid, intfid) |
| 749 | if err != nil { |
| 750 | logger.Error("Failed to getGemPortID: %s", err) |
| 751 | return 0, err |
| 752 | } |
| 753 | gemportid = onu.GemportID |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 754 | } |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 755 | return uint32(gemportid), nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 756 | } |
| 757 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 758 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 759 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 760 | for _, onu := range onus { |
| 761 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 762 | return onu, nil |
| 763 | } |
| 764 | } |
| 765 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 766 | err := errors.New("no matching serial number found") |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 767 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 768 | return nil, err |
| 769 | } |
| 770 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 771 | // GetOnuByID returns ONU object as per onuID and intfID |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 772 | func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) { |
| 773 | return getOnuByID(s.Onumap, onuid, intfid) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 776 | func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) { |
| 777 | for _, onu := range onumap[intfid] { |
| 778 | if onu.OnuID == onuid { |
| 779 | return onu, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 780 | } |
| 781 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 782 | err := errors.New("no matching OnuID found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 783 | logger.WithFields(log.Fields{ |
| 784 | "onumap": onumap, |
| 785 | "onuid": onuid, |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 786 | "intfid": intfid, |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 787 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 788 | return nil, err |
| 789 | } |
| 790 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 791 | // getOnuFromSNmap method returns onu object from SNmap if found |
| 792 | func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) { |
| 793 | snkey := stringifySerialNumber(serialNumber) |
| 794 | |
| 795 | logger.Debug("getOnuFromSNmap received serial number %s", snkey) |
| 796 | |
| 797 | if onu, exist := s.SNmap.Load(snkey); exist { |
| 798 | logger.Info("Serial number found in map") |
| 799 | return onu.(*device.Onu), true |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 800 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 801 | logger.Info("Serial number not found in map") |
| 802 | return nil, false |
| 803 | } |
| 804 | |
| 805 | func stringifySerialNumber(serialNum *openolt.SerialNumber) string { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 806 | return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) { |
| 810 | if len(SerialNumber) != SerialNumberLength { |
| 811 | logger.Error("Invalid serial number %s", SerialNumber) |
| 812 | return nil, errors.New("invalid serial number") |
| 813 | } |
| 814 | // First four characters are vendorId |
| 815 | vendorID := SerialNumber[:VendorIDLength] |
| 816 | vendorSpecific := SerialNumber[VendorIDLength:] |
| 817 | |
| 818 | vsbyte, _ := hex.DecodeString(vendorSpecific) |
| 819 | |
| 820 | // Convert to Openolt serial number |
| 821 | serialNum := new(openolt.SerialNumber) |
| 822 | serialNum.VendorId = []byte(vendorID) |
| 823 | serialNum.VendorSpecific = vsbyte |
| 824 | |
| 825 | return serialNum, nil |
| 826 | } |
| 827 | |
| 828 | // TODO move to device_onu.go |
| 829 | func (s *Server) sendOnuIndicationsOnOltReboot() { |
| 830 | if AutoONUActivate == 1 { |
| 831 | // For auto activate mode, onu indications is sent in Enable() |
| 832 | return |
| 833 | } |
| 834 | |
| 835 | s.SNmap.Range( |
| 836 | func(key, value interface{}) bool { |
| 837 | onu := value.(*device.Onu) |
| 838 | if onu.InternalState == device.ONU_LOS_RAISED { |
| 839 | return true |
| 840 | } |
| 841 | |
| 842 | err := sendOnuDiscInd(*s.EnableServer, onu) |
| 843 | if err != nil { |
| 844 | logger.Error(err.Error()) |
| 845 | } |
| 846 | |
| 847 | return true |
| 848 | }) |
| 849 | } |
| 850 | |
| 851 | // StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received |
| 852 | func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) { |
| 853 | for { |
| 854 | select { |
| 855 | case Req := <-s.serverActionCh: |
| 856 | logger.Debug("Request Received On serverActionCh: %+v", Req) |
| 857 | switch Req { |
| 858 | case "start": |
| 859 | logger.Debug("Server Start Request Received On ServerActionChannel") |
| 860 | go s.Start() // blocking |
| 861 | case "stop": |
| 862 | logger.Debug("Server Stop Request Received On ServerActionChannel") |
| 863 | s.Stop() |
| 864 | default: |
| 865 | logger.Error("Invalid value received in deviceActionCh") |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | // startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions |
| 872 | // TODO all onu and olt related actions (like alarms) should be handled using this function |
| 873 | func (s *Server) startDeviceActionLoop() { |
| 874 | logger.Debug("startDeviceActionLoop invoked") |
| 875 | s.deviceActionCh = make(chan *pb.DeviceAction, 10) |
| 876 | for { |
| 877 | logger.Debug("Action channel loop started") |
| 878 | select { |
| 879 | case Req := <-s.deviceActionCh: |
| 880 | logger.Debug("Reboot Action Type: %+v", Req.DeviceAction) |
| 881 | switch Req.DeviceType { |
| 882 | case DeviceTypeOnu: |
| 883 | value, _ := s.SNmap.Load(Req.DeviceSerialNumber) |
| 884 | onu := value.(*device.Onu) |
| 885 | if Req.DeviceAction == SoftReboot { |
| 886 | s.handleONUSoftReboot(onu.IntfID, onu.OnuID) |
| 887 | } else if Req.DeviceAction == HardReboot { |
| 888 | s.handleONUHardReboot(onu) |
| 889 | } |
| 890 | case DeviceTypeOlt: |
| 891 | logger.Debug("Reboot For OLT Received") |
| 892 | s.handleOLTReboot() |
| 893 | default: |
| 894 | logger.Error("Invalid value received in deviceActionCh") |
| 895 | } |
| 896 | } |
| 897 | } |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 898 | } |