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 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 27 | pb "gerrit.opencord.org/voltha-bbsim/api" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 28 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | "gerrit.opencord.org/voltha-bbsim/device" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 30 | flowHandler "gerrit.opencord.org/voltha-bbsim/flow" |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 31 | openolt "gerrit.opencord.org/voltha-bbsim/protos" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 32 | "github.com/google/gopacket" |
| 33 | "github.com/google/gopacket/layers" |
| 34 | "github.com/google/gopacket/pcap" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 35 | omci "github.com/opencord/omci-sim" |
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 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 110 | // NewCore initialize OLT and ONU objects |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 111 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 112 | // TODO: make it decent |
| 113 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 114 | npon := opt.npon |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 115 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 119 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 120 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 150 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 151 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 152 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 153 | logger.Info("OLT Serial-Number: %v", s.Olt.SerialNumber) |
| 154 | // Creating Onu Map |
| 155 | for intfid := uint32(0); intfid < npon; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 156 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 157 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 158 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 159 | 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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 167 | s.CtagMap = make(map[string]uint32) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 168 | for i := 0; i < MaxOnusPerPon; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 169 | oltid := s.Olt.ID |
| 170 | intfid := uint32(1) |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 171 | sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 172 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 173 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 174 | |
| 175 | flowHandler.InitializeFlowManager(s.Olt.ID) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 176 | return &s |
| 177 | } |
| 178 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 179 | // Start starts the openolt gRPC server (blocking) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 180 | func (s *Server) Start() error { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 181 | logger.Debug("Starting OpenOLT gRPC Server") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 182 | defer func() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 183 | logger.Debug("OpenOLT gRPC Server Stopped") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 184 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 185 | |
| 186 | // Start Openolt gRPC server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 187 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 188 | listener, gserver, err := NewGrpcServer(addressport) |
| 189 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 190 | logger.Error("Failed to create gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 191 | return err |
| 192 | } |
| 193 | s.gRPCserver = gserver |
| 194 | openolt.RegisterOpenoltServer(gserver, s) |
| 195 | if err := gserver.Serve(listener); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 196 | logger.Error("Failed to run gRPC server: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 197 | return err |
| 198 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 199 | return nil |
| 200 | } |
| 201 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 202 | // Stop stops the openolt gRPC servers (non-blocking). |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 203 | func (s *Server) Stop() { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 204 | logger.Debug("Stopping OpenOLT gRPC Server & PktLoops") |
| 205 | defer logger.Debug("OpenOLT gRPC Server & PktLoops Stopped") |
| 206 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 207 | if s.gRPCserver != nil { |
| 208 | s.gRPCserver.Stop() |
| 209 | logger.Debug("gRPCserver.Stop()") |
| 210 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 211 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 212 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 213 | return |
| 214 | } |
| 215 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 216 | func (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 | |
| 240 | func (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 Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 249 | // Enable invokes methods for activation of OLT and ONU (blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 250 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 251 | olt := s.Olt |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 252 | defer func() { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 253 | olt.Initialize() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 254 | // 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 NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 260 | s.updateDevIntState(olt, device.OLT_INACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 261 | 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 Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 268 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 269 | s.updateDevIntState(olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 270 | |
| 271 | coreCtx := context.Background() |
| 272 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 273 | s.cancel = corecancel |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 274 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 275 | 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 287 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 288 | |
| 289 | select { |
| 290 | case err := <-errorchan: |
| 291 | if err != nil { |
| 292 | logger.Debug("Error: %v", err) |
| 293 | return err |
| 294 | } |
| 295 | } |
| 296 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 297 | return nil |
| 298 | } |
| 299 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 300 | // Disable stops packet loops (non-blocking) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 301 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 302 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 303 | logger.Debug("Disable() Done") |
| 304 | }() |
| 305 | logger.Debug("Disable() Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 306 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 309 | func (s *Server) updateDevIntState(dev device.Device, state device.DeviceState) { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 310 | logger.Debug("updateDevIntState called state:%d", state) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 311 | current := dev.GetIntState() |
| 312 | dev.UpdateIntState(state) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 313 | s.stateRepCh <- stateReport{device: dev, current: current, next: state} |
| 314 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 315 | logger.Debug("OLT State updated to:%d", state) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 316 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 317 | 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 321 | } |
| 322 | |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 323 | func (s *Server) updateOnuIntState(intfid uint32, onuid uint32, state device.DeviceState) error { |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 324 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 325 | if err != nil { |
| 326 | return err |
| 327 | } |
| 328 | s.updateDevIntState(onu, state) |
| 329 | return nil |
| 330 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 331 | |
| 332 | func (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 | |
| 346 | func (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 Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 353 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 354 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 355 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 356 | defer logger.Debug("activateOLT() Done") |
| 357 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 358 | // Activate OLT |
| 359 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 360 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 361 | return err |
| 362 | } |
| 363 | olt.OperState = "up" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 364 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 365 | |
| 366 | // OLT sends Interface Indication to Adapter |
| 367 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 368 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 369 | return err |
| 370 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 371 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 372 | |
| 373 | // OLT sends Operation Indication to Adapter after activating each interface |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 374 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 375 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 376 | return err |
| 377 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 378 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 379 | return nil |
| 380 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 381 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 382 | // StartPktLoops creates veth pairs and invokes runPktLoops (blocking) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 383 | 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] | 384 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 385 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 386 | RemoveVeths(s.Vethnames) |
| 387 | s.Vethnames = []string{} |
| 388 | s.Ioinfos = []*Ioinfo{} |
| 389 | s.wg.Done() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 390 | s.updateDevIntState(s.Olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 391 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 392 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 393 | s.alarmCh = make(chan *openolt.Indication, 10) |
| 394 | go startAlarmLoop(stream, s.alarmCh) |
| 395 | go s.startDeviceActionLoop() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 396 | s.wg.Add(1) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 397 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 398 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 399 | logger.Error("createIoinfos failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 400 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 401 | } |
| 402 | s.Ioinfos = ioinfos |
| 403 | s.Vethnames = veths |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 404 | logger.Debug("Created vethnames: %v", s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 405 | |
| 406 | parent := ctx |
| 407 | child, cancel := context.WithCancel(parent) |
| 408 | s.cancel = cancel |
| 409 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 410 | if err = s.runPktLoops(child, stream); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 411 | logger.Error("runPktLoops failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 412 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 413 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 414 | errorchan <- nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 415 | } |
| 416 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 417 | // StopPktLoops (non-blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 418 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 419 | if s.cancel != nil { |
| 420 | cancel := s.cancel |
| 421 | cancel() |
| 422 | } |
| 423 | } |
| 424 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 425 | func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 426 | ioinfos := []*Ioinfo{} |
| 427 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 428 | var handler *pcap.Handle |
| 429 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 430 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 431 | logger.Error("setupVethHandler failed for nni: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 432 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 433 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 434 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 435 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 436 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 437 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 438 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 439 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 440 | } |
| 441 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 442 | // Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 443 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 444 | logger.Debug("runPacketPktLoops Start") |
| 445 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 446 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 447 | errchOmci := make(chan error) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 448 | s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 449 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 450 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 451 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 452 | errchEapol := make(chan error) |
| 453 | RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol) |
| 454 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 455 | errchDhcp := make(chan error) |
| 456 | RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp) |
| 457 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 458 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 459 | logger.Debug("runOMCIResponder Start") |
| 460 | defer logger.Debug("runOMCIResponder Done") |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 461 | select { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 462 | case v, ok := <-errchOmci: // Wait for OmciInitialization |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 463 | if ok { // Error |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 464 | logger.Error("Error happend in Omci: %s", v) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 465 | return v |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 466 | } |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 467 | case <-child.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 468 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 469 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 470 | return nil |
| 471 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 472 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 473 | eg.Go(func() error { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 474 | logger.Debug("runEapolResponder Start") |
| 475 | defer logger.Debug("runEapolResponder Done") |
| 476 | select { |
| 477 | case v, ok := <-errchEapol: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 478 | if ok { // Error |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 479 | 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 NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 489 | logger.Debug("runDhcpResponder Start") |
| 490 | defer logger.Debug("runDhcpResponder Done") |
| 491 | select { |
| 492 | case v, ok := <-errchDhcp: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 493 | if ok { // Error |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 494 | 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 NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 504 | 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 | |
| 515 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 516 | logger.Debug("runMainPktLoop Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 517 | defer func() { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 518 | logger.Debug("runMainPktLoop Done") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 519 | }() |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 520 | 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 Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 526 | defer func() { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 527 | close(nnichannel) |
| 528 | }() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 529 | logger.Debug("BEFORE OLT_ACTIVE") |
| 530 | s.updateDevIntState(s.Olt, device.OLT_ACTIVE) |
| 531 | logger.Debug("AFTER OLT_ACTIVE") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 532 | 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 539 | logger.Error("send omci indication failed: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 540 | continue |
| 541 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 542 | case msg := <-s.eapolIn: |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 543 | intfid := msg.IntfId |
| 544 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 545 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 546 | if err != nil { |
| 547 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 548 | continue |
| 549 | } |
| 550 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 551 | 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] | 552 | |
| 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 555 | logger.Error("Fail to send EAPOL PktInd indication. %v", err) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 556 | return err |
| 557 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 558 | case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 559 | intfid := msg.IntfId |
| 560 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 561 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 562 | 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 NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 569 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 570 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 571 | if err != nil { |
| 572 | logger.Error("Failed to GetOnuByID:%d", onuid) |
| 573 | continue |
| 574 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 575 | sn := device.ConvB2S(onu.SerialNumber.VendorSpecific) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 576 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 577 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 578 | if err != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 579 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 580 | "gemId": gemid, |
| 581 | }).Error("Fail to tag C-tag") |
| 582 | } else { |
| 583 | pkt = tagpkt |
| 584 | } |
| 585 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 586 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 587 | "gemId": gemid, |
| 588 | "cTagMap": s.CtagMap, |
| 589 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
| 590 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 591 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 592 | 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] | 593 | |
| 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 596 | logger.Error("Fail to send DHCP PktInd indication: %v", err) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 597 | return err |
| 598 | } |
| 599 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 600 | case nnipkt := <-nnichannel: |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 601 | logger.Debug("Received packet from NNI") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 602 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 603 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 604 | continue |
| 605 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 606 | |
| 607 | onuid := nnipkt.Info.onuid |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 608 | intfid := nnipkt.Info.intfid |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 609 | onu, _ := s.GetOnuByID(onuid, intfid) |
| 610 | |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 611 | device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 612 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 613 | pkt := nnipkt.Pkt |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 614 | 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 Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 616 | logger.Error("Fail to send PktInd indication: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 617 | return err |
| 618 | } |
| 619 | |
| 620 | case <-ctx.Done(): |
| 621 | logger.Debug("Closed nnichannel ") |
| 622 | return nil |
| 623 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 624 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 625 | } |
| 626 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 627 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 628 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 629 | onu, err := s.GetOnuByID(onuid, intfid) |
| 630 | if err != nil { |
| 631 | logger.Error("Failed processing onuPacketOut: %v", err) |
| 632 | return err |
| 633 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 634 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 635 | if layerEth != nil { |
| 636 | pkt, _ := layerEth.(*layers.Ethernet) |
| 637 | ethtype := pkt.EthernetType |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 638 | if ethtype == layers.EthernetTypeEAPOL { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 639 | device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 640 | eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 641 | s.eapolOut <- &eapolPkt |
| 642 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 643 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 644 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 645 | "payload": layerDHCP.LayerPayload(), |
| 646 | "type": layerDHCP.LayerType().String(), |
| 647 | }).Info("Received downstream packet is DHCP.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 648 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 649 | rawpkt, _, _ = PopVLAN(rawpkt) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 650 | logger.Debug("%s", rawpkt.Dump()) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 651 | dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 652 | s.dhcpOut <- &dhcpPkt |
| 653 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 654 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 655 | device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 656 | return nil |
| 657 | } |
| 658 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 659 | if err != nil { |
| 660 | return err |
| 661 | } |
| 662 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 663 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 664 | return nil |
| 665 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 666 | device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 667 | return nil |
| 668 | } |
| 669 | |
| 670 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 671 | poppkt, _, err := PopVLAN(rawpkt) |
| 672 | poppkt, _, err = PopVLAN(poppkt) |
| 673 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 674 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 675 | return err |
| 676 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 677 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 678 | if err != nil { |
| 679 | return err |
| 680 | } |
| 681 | handle := ioinfo.handler |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 682 | logger.Debug("%s", poppkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 683 | SendNni(handle, poppkt) |
| 684 | return nil |
| 685 | } |
| 686 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 687 | // 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] | 688 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 689 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 690 | for _, onu := range onus { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 691 | if onu.GetIntState() != device.ONU_ACTIVE { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 692 | return false |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | return true |
| 697 | } |
| 698 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 699 | func (s *Server) isAllOnuOmciActive() bool { |
| 700 | for _, onus := range s.Onumap { |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 701 | for _, onu := range onus { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 702 | if onu.GetIntState() != device.ONU_OMCIACTIVE { |
| 703 | return false |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | return true |
| 708 | } |
| 709 | |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 710 | func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 711 | logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid) |
| 712 | gemportid, err := omci.GetGemPortId(intfid, onuid) |
| 713 | if err != nil { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 714 | logger.Warn("Failed to getGemPortID from OMCI lib: %s", err) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 715 | 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 NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 721 | } |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 722 | return uint32(gemportid), nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 723 | } |
| 724 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 725 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 726 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 727 | for _, onu := range onus { |
| 728 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 729 | return onu, nil |
| 730 | } |
| 731 | } |
| 732 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 733 | err := errors.New("no matching serial number found") |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 734 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 735 | return nil, err |
| 736 | } |
| 737 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 738 | // GetOnuByID returns ONU object as per onuID and intfID |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 739 | func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) { |
| 740 | return getOnuByID(s.Onumap, onuid, intfid) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 743 | func 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 NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 747 | } |
| 748 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 749 | err := errors.New("no matching OnuID found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 750 | logger.WithFields(log.Fields{ |
| 751 | "onumap": onumap, |
| 752 | "onuid": onuid, |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 753 | "intfid": intfid, |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 754 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 755 | return nil, err |
| 756 | } |
| 757 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 758 | // getOnuFromSNmap method returns onu object from SNmap if found |
| 759 | func (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 NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 767 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 768 | logger.Info("Serial number not found in map") |
| 769 | return nil, false |
| 770 | } |
| 771 | |
| 772 | func stringifySerialNumber(serialNum *openolt.SerialNumber) string { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame^] | 773 | return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | func 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 |
| 796 | func (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 |
| 819 | func (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 |
| 840 | func (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 Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 865 | } |