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 | |
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) { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 311 | logger.Debug("updateDevIntState called state:%d", state) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 312 | current := dev.GetIntState() |
| 313 | dev.UpdateIntState(state) |
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{}) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 316 | logger.Debug("OLT State updated to:%d", state) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 317 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 318 | logger.Debug("ONU State updated to:%d", state) |
| 319 | } else { |
| 320 | logger.Error("UpdateDevIntState () doesn't support this device: %s", reflect.TypeOf(dev)) |
| 321 | } |
Keita 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 { |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 325 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 326 | if err != nil { |
| 327 | return err |
| 328 | } |
| 329 | s.updateDevIntState(onu, state) |
| 330 | return nil |
| 331 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 332 | |
| 333 | func (s *Server) activateOnu(onu *device.Onu) { |
| 334 | snKey := stringifySerialNumber(onu.SerialNumber) |
| 335 | s.SNmap.Store(snKey, onu) |
| 336 | device.UpdateOnusOpStatus(onu.IntfID, onu, "up") |
| 337 | |
| 338 | err := sendOnuDiscInd(*s.EnableServer, onu) |
| 339 | if err != nil { |
| 340 | logger.Error(err.Error()) |
| 341 | return |
| 342 | } |
| 343 | logger.Info("OLT id:%d sent ONUDiscInd.", s.Olt.ID) |
| 344 | logger.Debug("activateONUs Entry in SNmap %v", snKey) |
| 345 | } |
| 346 | |
| 347 | func (s *Server) activateONUs(stream openolt.Openolt_EnableIndicationServer, Onumap map[uint32][]*device.Onu) { |
| 348 | // Add all ONUs to SerialNumber Map |
| 349 | for intfid := range Onumap { |
| 350 | for _, onu := range Onumap[intfid] { |
| 351 | s.activateOnu(onu) |
| 352 | } |
| 353 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 354 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 355 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 356 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 357 | defer logger.Debug("activateOLT() Done") |
| 358 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 359 | // Activate OLT |
| 360 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 361 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 362 | return err |
| 363 | } |
| 364 | olt.OperState = "up" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 365 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 366 | |
| 367 | // OLT sends Interface Indication to Adapter |
| 368 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 369 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 370 | return err |
| 371 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 372 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 373 | |
| 374 | // OLT sends Operation Indication to Adapter after activating each interface |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 375 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 376 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 377 | return err |
| 378 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 379 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 380 | return nil |
| 381 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 382 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 383 | // StartPktLoops creates veth pairs and invokes runPktLoops (blocking) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 384 | 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] | 385 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 386 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 387 | RemoveVeths(s.Vethnames) |
| 388 | s.Vethnames = []string{} |
| 389 | s.Ioinfos = []*Ioinfo{} |
| 390 | s.wg.Done() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 391 | s.updateDevIntState(s.Olt, device.OLT_PREACTIVE) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 392 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 393 | }() |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 394 | s.alarmCh = make(chan *openolt.Indication, 10) |
| 395 | go startAlarmLoop(stream, s.alarmCh) |
| 396 | go s.startDeviceActionLoop() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 397 | s.wg.Add(1) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 398 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 399 | if err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 400 | logger.Error("createIoinfos failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 401 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 402 | } |
| 403 | s.Ioinfos = ioinfos |
| 404 | s.Vethnames = veths |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 405 | logger.Debug("Created vethnames: %v", s.Vethnames) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 406 | |
| 407 | parent := ctx |
| 408 | child, cancel := context.WithCancel(parent) |
| 409 | s.cancel = cancel |
| 410 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 411 | if err = s.runPktLoops(child, stream); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 412 | logger.Error("runPktLoops failed: %v", err) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 413 | errorchan <- err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 414 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 415 | errorchan <- nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 416 | } |
| 417 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 418 | // StopPktLoops (non-blocking) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 419 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 420 | if s.cancel != nil { |
| 421 | cancel := s.cancel |
| 422 | cancel() |
| 423 | } |
| 424 | } |
| 425 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 426 | func createIoinfos(oltid uint32, Vethnames []string) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 427 | ioinfos := []*Ioinfo{} |
| 428 | var err error |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 429 | var handler *pcap.Handle |
| 430 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 431 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 432 | logger.Error("setupVethHandler failed for nni: %v", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 433 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 434 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 435 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 436 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 437 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 438 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 439 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 440 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 441 | } |
| 442 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 443 | // Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 444 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 445 | logger.Debug("runPacketPktLoops Start") |
| 446 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 447 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 448 | errchOmci := make(chan error) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 449 | s.RunOmciResponder(ctx, s.omciOut, s.omciIn, errchOmci) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 450 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 451 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 452 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 453 | errchEapol := make(chan error) |
| 454 | RunEapolResponder(ctx, s.eapolOut, s.eapolIn, errchEapol) |
| 455 | |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 456 | errchDhcp := make(chan error) |
| 457 | RunDhcpResponder(ctx, s.dhcpOut, s.dhcpIn, errchDhcp) |
| 458 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 459 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 460 | logger.Debug("runOMCIResponder Start") |
| 461 | defer logger.Debug("runOMCIResponder Done") |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 462 | select { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 463 | case v, ok := <-errchOmci: // Wait for OmciInitialization |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 464 | if ok { // Error |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 465 | logger.Error("Error happend in Omci: %s", v) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 466 | return v |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 467 | } |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 468 | case <-child.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 469 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 470 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 471 | return nil |
| 472 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 473 | |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 474 | eg.Go(func() error { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 475 | logger.Debug("runEapolResponder Start") |
| 476 | defer logger.Debug("runEapolResponder Done") |
| 477 | select { |
| 478 | case v, ok := <-errchEapol: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 479 | if ok { // Error |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 480 | logger.Error("Error happend in Eapol:%s", v) |
| 481 | return v |
| 482 | } |
| 483 | case <-child.Done(): |
| 484 | return nil |
| 485 | } |
| 486 | return nil |
| 487 | }) |
| 488 | |
| 489 | eg.Go(func() error { |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 490 | logger.Debug("runDhcpResponder Start") |
| 491 | defer logger.Debug("runDhcpResponder Done") |
| 492 | select { |
| 493 | case v, ok := <-errchDhcp: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 494 | if ok { // Error |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 495 | logger.Error("Error happend in Dhcp:%s", v) |
| 496 | return v |
| 497 | } |
| 498 | case <-child.Done(): |
| 499 | return nil |
| 500 | } |
| 501 | return nil |
| 502 | }) |
| 503 | |
| 504 | eg.Go(func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 505 | err := s.runMainPktLoop(child, stream) |
| 506 | return err |
| 507 | }) |
| 508 | |
| 509 | if err := eg.Wait(); err != nil { |
| 510 | logger.Error("Error happend in runPacketLoops:%s", err) |
| 511 | cancel() |
| 512 | } |
| 513 | return nil |
| 514 | } |
| 515 | |
| 516 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 517 | logger.Debug("runMainPktLoop Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 518 | defer func() { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 519 | logger.Debug("runMainPktLoop Done") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 520 | }() |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 521 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
| 522 | if err != nil { |
| 523 | return err |
| 524 | } |
| 525 | nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32) |
| 526 | go RecvWorker(ioinfo, nhandler, nnichannel) |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 527 | defer func() { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 528 | close(nnichannel) |
| 529 | }() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 530 | logger.Debug("BEFORE OLT_ACTIVE") |
| 531 | s.updateDevIntState(s.Olt, device.OLT_ACTIVE) |
| 532 | logger.Debug("AFTER OLT_ACTIVE") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 533 | data := &openolt.Indication_PktInd{} |
| 534 | for { |
| 535 | select { |
| 536 | case msg := <-s.omciIn: |
| 537 | logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
| 538 | omci := &openolt.Indication_OmciInd{OmciInd: &msg} |
| 539 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 540 | logger.Error("send omci indication failed: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 541 | continue |
| 542 | } |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 543 | case msg := <-s.eapolIn: |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 544 | intfid := msg.IntfId |
| 545 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 546 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 547 | if err != nil { |
| 548 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 549 | continue |
| 550 | } |
| 551 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 552 | 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] | 553 | |
| 554 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}} |
| 555 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 556 | logger.Error("Fail to send EAPOL PktInd indication. %v", err) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 557 | return err |
| 558 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 559 | case msg := <-s.dhcpIn: // TODO: We should put omciIn, eapolIn, dhcpIn toghether |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 560 | intfid := msg.IntfId |
| 561 | onuid := msg.OnuId |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 562 | gemid, err := s.getGemPortID(intfid, onuid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 563 | bytes := msg.Byte |
| 564 | pkt := gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default) |
| 565 | |
| 566 | if err != nil { |
| 567 | logger.Error("Failed to getGemPortID intfid:%d onuid:%d", intfid, onuid) |
| 568 | continue |
| 569 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 570 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 571 | onu, err := s.GetOnuByID(onuid, intfid) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 572 | if err != nil { |
| 573 | logger.Error("Failed to GetOnuByID:%d", onuid) |
| 574 | continue |
| 575 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 576 | sn := device.ConvB2S(onu.SerialNumber.VendorSpecific) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 577 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 578 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 579 | if err != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 580 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 581 | "gemId": gemid, |
| 582 | }).Error("Fail to tag C-tag") |
| 583 | } else { |
| 584 | pkt = tagpkt |
| 585 | } |
| 586 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 587 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 588 | "gemId": gemid, |
| 589 | "cTagMap": s.CtagMap, |
| 590 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
| 591 | } |
Keita NISHIMOTO | 9a4c4dc | 2019-02-13 09:33:00 +0900 | [diff] [blame] | 592 | |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 593 | 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] | 594 | |
| 595 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: msg.Byte}} |
| 596 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 597 | logger.Error("Fail to send DHCP PktInd indication: %v", err) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 598 | return err |
| 599 | } |
| 600 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 601 | case nnipkt := <-nnichannel: |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 602 | logger.Debug("Received packet from NNI") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 603 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 604 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 605 | continue |
| 606 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 607 | |
| 608 | onuid := nnipkt.Info.onuid |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 609 | intfid := nnipkt.Info.intfid |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 610 | onu, _ := s.GetOnuByID(onuid, intfid) |
| 611 | |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 612 | device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 613 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 614 | pkt := nnipkt.Pkt |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 615 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 616 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 617 | logger.Error("Fail to send PktInd indication: %v", err) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 618 | return err |
| 619 | } |
| 620 | |
| 621 | case <-ctx.Done(): |
| 622 | logger.Debug("Closed nnichannel ") |
| 623 | return nil |
| 624 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 625 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 626 | } |
| 627 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 628 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 629 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 630 | onu, err := s.GetOnuByID(onuid, intfid) |
| 631 | if err != nil { |
| 632 | logger.Error("Failed processing onuPacketOut: %v", err) |
| 633 | return err |
| 634 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 635 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 636 | if layerEth != nil { |
| 637 | pkt, _ := layerEth.(*layers.Ethernet) |
| 638 | ethtype := pkt.EthernetType |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 639 | if ethtype == layers.EthernetTypeEAPOL { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 640 | device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 641 | eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 642 | s.eapolOut <- &eapolPkt |
| 643 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 644 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 645 | device.LoggerWithOnu(onu).WithFields(log.Fields{ |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 646 | "payload": layerDHCP.LayerPayload(), |
| 647 | "type": layerDHCP.LayerType().String(), |
| 648 | }).Info("Received downstream packet is DHCP.") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame^] | 649 | poppkt, _, err := PopVLAN(rawpkt) |
| 650 | if err != nil { |
| 651 | logger.Error("Received untagged packet when expecting single-tagged packet. Dropped.") |
| 652 | return nil |
| 653 | } else { |
| 654 | // check to see if the packet was double-tagged |
| 655 | _, _, err := PopVLAN(poppkt) |
| 656 | if err == nil { |
| 657 | logger.Error("Received double-tagged packet when expecting single-tagged packet. Dropped.") |
| 658 | return nil |
| 659 | } |
| 660 | } |
| 661 | logger.Debug("%s", poppkt.Dump()) |
| 662 | dhcpPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: poppkt.Data()} |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 663 | s.dhcpOut <- &dhcpPkt |
| 664 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 665 | } else { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 666 | device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP") |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame^] | 667 | // 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] | 668 | return nil |
| 669 | } |
| 670 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 671 | if err != nil { |
| 672 | return err |
| 673 | } |
| 674 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 675 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 676 | return nil |
| 677 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 678 | device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 679 | return nil |
| 680 | } |
| 681 | |
| 682 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 683 | poppkt, _, err := PopVLAN(rawpkt) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 684 | if err != nil { |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame^] | 685 | logger.Error("Received untagged packet when expecting single-tagged packet. Dropped") |
| 686 | return nil |
| 687 | } else { |
| 688 | // check to see if the packet was double-tagged |
| 689 | poppktAgain, _, err := PopVLAN(poppkt) |
| 690 | if err == nil { |
| 691 | poppkt = poppktAgain |
| 692 | } else { |
| 693 | logger.Error("Received single-tagged packet when expecting double-tagged packet. Dropped.") |
| 694 | return nil |
| 695 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 696 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 697 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 698 | if err != nil { |
| 699 | return err |
| 700 | } |
| 701 | handle := ioinfo.handler |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 702 | logger.Debug("%s", poppkt.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 703 | SendNni(handle, poppkt) |
| 704 | return nil |
| 705 | } |
| 706 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 707 | // 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] | 708 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 709 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 710 | for _, onu := range onus { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 711 | if onu.GetIntState() != device.ONU_ACTIVE { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 712 | return false |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | return true |
| 717 | } |
| 718 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 719 | func (s *Server) isAllOnuOmciActive() bool { |
| 720 | for _, onus := range s.Onumap { |
Mahir Gunyel | 9897f6e | 2019-05-22 14:54:05 -0700 | [diff] [blame] | 721 | for _, onu := range onus { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 722 | if onu.GetIntState() != device.ONU_OMCIACTIVE { |
| 723 | return false |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | return true |
| 728 | } |
| 729 | |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 730 | func (s *Server) getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 731 | logger.Debug("getGemPortID(intfid:%d, onuid:%d)", intfid, onuid) |
| 732 | gemportid, err := omci.GetGemPortId(intfid, onuid) |
| 733 | if err != nil { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 734 | logger.Warn("Failed to getGemPortID from OMCI lib: %s", err) |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 735 | onu, err := s.GetOnuByID(onuid, intfid) |
| 736 | if err != nil { |
| 737 | logger.Error("Failed to getGemPortID: %s", err) |
| 738 | return 0, err |
| 739 | } |
| 740 | gemportid = onu.GemportID |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame] | 741 | } |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 742 | return uint32(gemportid), nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 743 | } |
| 744 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 745 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 746 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 747 | for _, onu := range onus { |
| 748 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 749 | return onu, nil |
| 750 | } |
| 751 | } |
| 752 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 753 | err := errors.New("no matching serial number found") |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 754 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 755 | return nil, err |
| 756 | } |
| 757 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 758 | // GetOnuByID returns ONU object as per onuID and intfID |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 759 | func (s *Server) GetOnuByID(onuid uint32, intfid uint32) (*device.Onu, error) { |
| 760 | return getOnuByID(s.Onumap, onuid, intfid) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Zdravko Bozakov | edb6532 | 2019-04-11 14:49:32 +0200 | [diff] [blame] | 763 | func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32, intfid uint32) (*device.Onu, error) { |
| 764 | for _, onu := range onumap[intfid] { |
| 765 | if onu.OnuID == onuid { |
| 766 | return onu, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 767 | } |
| 768 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 769 | err := errors.New("no matching OnuID found") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 770 | logger.WithFields(log.Fields{ |
| 771 | "onumap": onumap, |
| 772 | "onuid": onuid, |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 773 | "intfid": intfid, |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 774 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 775 | return nil, err |
| 776 | } |
| 777 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 778 | // getOnuFromSNmap method returns onu object from SNmap if found |
| 779 | func (s *Server) getOnuFromSNmap(serialNumber *openolt.SerialNumber) (*device.Onu, bool) { |
| 780 | snkey := stringifySerialNumber(serialNumber) |
| 781 | |
| 782 | logger.Debug("getOnuFromSNmap received serial number %s", snkey) |
| 783 | |
| 784 | if onu, exist := s.SNmap.Load(snkey); exist { |
| 785 | logger.Info("Serial number found in map") |
| 786 | return onu.(*device.Onu), true |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 787 | } |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 788 | logger.Info("Serial number not found in map") |
| 789 | return nil, false |
| 790 | } |
| 791 | |
| 792 | func stringifySerialNumber(serialNum *openolt.SerialNumber) string { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 793 | return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) { |
| 797 | if len(SerialNumber) != SerialNumberLength { |
| 798 | logger.Error("Invalid serial number %s", SerialNumber) |
| 799 | return nil, errors.New("invalid serial number") |
| 800 | } |
| 801 | // First four characters are vendorId |
| 802 | vendorID := SerialNumber[:VendorIDLength] |
| 803 | vendorSpecific := SerialNumber[VendorIDLength:] |
| 804 | |
| 805 | vsbyte, _ := hex.DecodeString(vendorSpecific) |
| 806 | |
| 807 | // Convert to Openolt serial number |
| 808 | serialNum := new(openolt.SerialNumber) |
| 809 | serialNum.VendorId = []byte(vendorID) |
| 810 | serialNum.VendorSpecific = vsbyte |
| 811 | |
| 812 | return serialNum, nil |
| 813 | } |
| 814 | |
| 815 | // TODO move to device_onu.go |
| 816 | func (s *Server) sendOnuIndicationsOnOltReboot() { |
| 817 | if AutoONUActivate == 1 { |
| 818 | // For auto activate mode, onu indications is sent in Enable() |
| 819 | return |
| 820 | } |
| 821 | |
| 822 | s.SNmap.Range( |
| 823 | func(key, value interface{}) bool { |
| 824 | onu := value.(*device.Onu) |
| 825 | if onu.InternalState == device.ONU_LOS_RAISED { |
| 826 | return true |
| 827 | } |
| 828 | |
| 829 | err := sendOnuDiscInd(*s.EnableServer, onu) |
| 830 | if err != nil { |
| 831 | logger.Error(err.Error()) |
| 832 | } |
| 833 | |
| 834 | return true |
| 835 | }) |
| 836 | } |
| 837 | |
| 838 | // StartServerActionLoop reads on server-action channel, and starts and stops the server as per the value received |
| 839 | func (s *Server) StartServerActionLoop(wg *sync.WaitGroup) { |
| 840 | for { |
| 841 | select { |
| 842 | case Req := <-s.serverActionCh: |
| 843 | logger.Debug("Request Received On serverActionCh: %+v", Req) |
| 844 | switch Req { |
| 845 | case "start": |
| 846 | logger.Debug("Server Start Request Received On ServerActionChannel") |
| 847 | go s.Start() // blocking |
| 848 | case "stop": |
| 849 | logger.Debug("Server Stop Request Received On ServerActionChannel") |
| 850 | s.Stop() |
| 851 | default: |
| 852 | logger.Error("Invalid value received in deviceActionCh") |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | // startDeviceActionLoop reads on the action-channel, and performs onu and olt reboot related actions |
| 859 | // TODO all onu and olt related actions (like alarms) should be handled using this function |
| 860 | func (s *Server) startDeviceActionLoop() { |
| 861 | logger.Debug("startDeviceActionLoop invoked") |
| 862 | s.deviceActionCh = make(chan *pb.DeviceAction, 10) |
| 863 | for { |
| 864 | logger.Debug("Action channel loop started") |
| 865 | select { |
| 866 | case Req := <-s.deviceActionCh: |
| 867 | logger.Debug("Reboot Action Type: %+v", Req.DeviceAction) |
| 868 | switch Req.DeviceType { |
| 869 | case DeviceTypeOnu: |
| 870 | value, _ := s.SNmap.Load(Req.DeviceSerialNumber) |
| 871 | onu := value.(*device.Onu) |
| 872 | if Req.DeviceAction == SoftReboot { |
| 873 | s.handleONUSoftReboot(onu.IntfID, onu.OnuID) |
| 874 | } else if Req.DeviceAction == HardReboot { |
| 875 | s.handleONUHardReboot(onu) |
| 876 | } |
| 877 | case DeviceTypeOlt: |
| 878 | logger.Debug("Reboot For OLT Received") |
| 879 | s.handleOLTReboot() |
| 880 | default: |
| 881 | logger.Error("Invalid value received in deviceActionCh") |
| 882 | } |
| 883 | } |
| 884 | } |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 885 | } |