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" |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 21 | "errors" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | "strconv" |
| 23 | "sync" |
| 24 | |
| 25 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 26 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 27 | "gerrit.opencord.org/voltha-bbsim/device" |
| 28 | "gerrit.opencord.org/voltha-bbsim/protos" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | "github.com/google/gopacket" |
| 30 | "github.com/google/gopacket/layers" |
| 31 | "github.com/google/gopacket/pcap" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 32 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 33 | "google.golang.org/grpc" |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 34 | "golang.org/x/sync/errgroup" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 35 | ) |
| 36 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 37 | const ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 38 | UNI_VETH_UP_PFX = "sim_uu" |
| 39 | UNI_VETH_DW_PFX = "sim_ud" |
| 40 | NNI_VETH_UP_PFX = "sim_nu" |
| 41 | NNI_VETH_DW_PFX = "sim_nd" |
| 42 | MAX_ONUS_PER_PON = 64 // This value should be the same with the value in AdapterPlatrorm class |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | type Server struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 46 | wg *sync.WaitGroup |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 47 | Olt *device.Olt |
| 48 | Onumap map[uint32][]*device.Onu |
| 49 | Ioinfos []*Ioinfo |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 50 | gRPCserver *grpc.Server |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 51 | gRPCAddress string |
| 52 | gRPCPort uint32 |
| 53 | Vethnames []string |
| 54 | IndInterval int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 55 | Processes []string |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 56 | EnableServer *openolt.Openolt_EnableIndicationServer |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 57 | CtagMap map[string]uint32 |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 58 | cancel context.CancelFunc |
| 59 | state coreState |
| 60 | stateChan chan coreState |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 61 | omciIn chan openolt.OmciIndication |
| 62 | omciOut chan openolt.OmciMsg |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | type Packet struct { |
| 66 | Info *Ioinfo |
| 67 | Pkt gopacket.Packet |
| 68 | } |
| 69 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 70 | type coreState int |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 71 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 72 | const ( |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 73 | INACTIVE = iota // OLT/ONUs are not instantiated |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 74 | PRE_ACTIVE // Before running MainPacketLoop |
| 75 | ACTIVE // After running MainPacketLoop |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 76 | ) |
| 77 | |
| 78 | /* coreState |
| 79 | INACTIVE -> PRE_ACTIVE -> ACTIVE |
| 80 | (ActivateOLT) (Enable) |
| 81 | <- <- |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 82 | */ |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 83 | |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 84 | func NewCore(opt *option) *Server { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 85 | // TODO: make it decent |
| 86 | oltid := opt.oltid |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 87 | npon := opt.npon |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 88 | nonus := opt.nonus |
| 89 | s := Server{ |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 90 | Olt: device.NewOlt(oltid, npon, 1), |
| 91 | Onumap: make(map[uint32][]*device.Onu), |
| 92 | Ioinfos: []*Ioinfo{}, |
| 93 | gRPCAddress: opt.address, |
| 94 | gRPCPort: opt.port, |
| 95 | Vethnames: []string{}, |
| 96 | IndInterval: opt.intvl, |
| 97 | Processes: []string{}, |
Shad Ansari | 704c6f2 | 2018-11-13 14:57:53 -0800 | [diff] [blame] | 98 | EnableServer: nil, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 99 | state: INACTIVE, |
| 100 | stateChan: make(chan coreState, 8), |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 101 | omciIn: make(chan openolt.OmciIndication, 1024), |
| 102 | omciOut: make(chan openolt.OmciMsg, 1024), |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 105 | nnni := s.Olt.NumNniIntf |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 106 | logger.Info("OLT ID: %d was retrieved.", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 107 | for intfid := nnni; intfid < npon+nnni; intfid++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 108 | s.Onumap[intfid] = device.NewOnus(oltid, intfid, nonus, nnni) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 109 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 110 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 111 | //TODO: To be fixed because it is hardcoded |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 112 | s.CtagMap = make(map[string]uint32) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 113 | for i := 0; i < MAX_ONUS_PER_PON; i++ { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 114 | oltid := s.Olt.ID |
| 115 | intfid := uint32(1) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 116 | sn := convB2S(device.NewSN(oltid, intfid, uint32(i))) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 117 | s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 118 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 119 | return &s |
| 120 | } |
| 121 | |
| 122 | //Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 123 | func (s *Server) Start() error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 124 | s.wg = &sync.WaitGroup{} |
| 125 | logger.Debug("Start() Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 126 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 127 | close(s.stateChan) |
| 128 | logger.Debug("Start() Done") |
| 129 | }() |
| 130 | addressport := s.gRPCAddress + ":" + strconv.Itoa(int(s.gRPCPort)) |
| 131 | listener, gserver, err := NewGrpcServer(addressport) |
| 132 | if err != nil { |
| 133 | logger.Error("Failed to create gRPC server", err) |
| 134 | return err |
| 135 | } |
| 136 | s.gRPCserver = gserver |
| 137 | openolt.RegisterOpenoltServer(gserver, s) |
| 138 | if err := gserver.Serve(listener); err != nil { |
| 139 | logger.Error("Failed to run gRPC server", err) |
| 140 | return err |
| 141 | } |
| 142 | s.wg.Wait() |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | //Non-Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 147 | func (s *Server) Stop() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 148 | logger.Debug("Stop() Start") |
| 149 | defer logger.Debug("Stop() Done") |
| 150 | if s.gRPCserver != nil { |
| 151 | s.gRPCserver.Stop() |
| 152 | logger.Debug("gRPCserver.Stop()") |
| 153 | } |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 154 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 155 | return |
| 156 | } |
| 157 | |
| 158 | // Blocking |
| 159 | func (s *Server) Enable(sv *openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 160 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 161 | olt := s.Olt |
| 162 | olt.InitializeStatus() |
| 163 | for intfid, _ := range s.Onumap { |
| 164 | for _, onu := range s.Onumap[intfid] { |
| 165 | onu.InitializeStatus() |
| 166 | } |
| 167 | } |
| 168 | s.updateState(INACTIVE) |
| 169 | logger.Debug("Enable() Done") |
| 170 | }() |
| 171 | logger.Debug("Enable() Start") |
| 172 | s.EnableServer = sv |
| 173 | if err := s.activateOLT(*sv); err != nil { |
| 174 | return err |
| 175 | } |
| 176 | s.updateState(PRE_ACTIVE) |
| 177 | |
| 178 | coreCtx := context.Background() |
| 179 | coreCtx, corecancel := context.WithCancel(coreCtx) |
| 180 | s.cancel = corecancel |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 181 | if err := s.StartPktLoops(coreCtx, *sv); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 182 | return err |
| 183 | } |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | //Non-Blocking |
| 188 | func (s *Server) Disable() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 189 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 190 | logger.Debug("Disable() Done") |
| 191 | }() |
| 192 | logger.Debug("Disable() Start") |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 193 | s.StopPktLoops() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 196 | func (s *Server) updateState(state coreState) { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 197 | s.state = state |
| 198 | s.stateChan <- state |
| 199 | logger.Debug("State updated to:%d", state) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | func (s *Server) activateOLT(stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 203 | defer logger.Debug("activateOLT() Done") |
| 204 | logger.Debug("activateOLT() Start") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 205 | // Activate OLT |
| 206 | olt := s.Olt |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 207 | if err := sendOltIndUp(stream, olt); err != nil { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 208 | return err |
| 209 | } |
| 210 | olt.OperState = "up" |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 211 | *olt.InternalState = device.OLT_UP |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 212 | logger.Info("OLT %s sent OltInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 213 | |
| 214 | // OLT sends Interface Indication to Adapter |
| 215 | if err := sendIntfInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 216 | logger.Error("Fail to sendIntfInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 217 | return err |
| 218 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 219 | logger.Info("OLT %s sent IntfInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 220 | |
| 221 | // OLT sends Operation Indication to Adapter after activating each interface |
| 222 | //time.Sleep(IF_UP_TIME * time.Second) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 223 | *olt.InternalState = device.PONIF_UP |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 224 | if err := sendOperInd(stream, olt); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 225 | logger.Error("Fail to sendOperInd: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 226 | return err |
| 227 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 228 | logger.Info("OLT %s sent OperInd.", olt.Name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 229 | |
| 230 | // OLT sends ONU Discover Indication to Adapter after ONU discovery |
| 231 | for intfid, _ := range s.Onumap { |
| 232 | device.UpdateOnusOpStatus(intfid, s.Onumap[intfid], "up") |
| 233 | } |
| 234 | |
| 235 | for intfid, _ := range s.Onumap { |
| 236 | sendOnuDiscInd(stream, s.Onumap[intfid]) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 237 | logger.Info("OLT id:%d sent ONUDiscInd.", olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // OLT Sends OnuInd after waiting all of those ONUs up |
| 241 | for { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 242 | if IsAllOnuActive(s.Onumap) { |
| 243 | logger.Debug("All the Onus are Activated.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 244 | break |
| 245 | } |
| 246 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 247 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 248 | for intfid, _ := range s.Onumap { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 249 | sendOnuInd(stream, s.Onumap[intfid], s.IndInterval) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 250 | logger.Info("OLT id:%d sent ONUInd.", olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 251 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 252 | return nil |
| 253 | } |
| 254 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 255 | // Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 256 | func (s *Server) StartPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 257 | logger.Debug("StartPktLoops () Start") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 258 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 259 | RemoveVeths(s.Vethnames) |
| 260 | s.Vethnames = []string{} |
| 261 | s.Ioinfos = []*Ioinfo{} |
| 262 | s.wg.Done() |
| 263 | s.updateState(PRE_ACTIVE) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 264 | logger.Debug("StartPktLoops () Done") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 265 | }() |
| 266 | s.wg.Add(1) |
| 267 | ioinfos, veths, err := createIoinfos(s.Olt.ID, s.Vethnames, s.Onumap) |
| 268 | if err != nil { |
Shad Ansari | 6a93ee2 | 2018-11-16 13:26:47 -0800 | [diff] [blame] | 269 | logger.Error("createIoinfos failed.", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 270 | return err |
| 271 | } |
| 272 | s.Ioinfos = ioinfos |
| 273 | s.Vethnames = veths |
| 274 | logger.Debug("Created vethnames:%v", s.Vethnames) |
| 275 | |
| 276 | parent := ctx |
| 277 | child, cancel := context.WithCancel(parent) |
| 278 | s.cancel = cancel |
| 279 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 280 | if err = s.runPktLoops(child, stream); err != nil { |
| 281 | logger.Error("runPktLoops failed.", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 282 | return err |
| 283 | } |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | //Non-Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 288 | func (s *Server) StopPktLoops() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 289 | if s.cancel != nil { |
| 290 | cancel := s.cancel |
| 291 | cancel() |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | func createIoinfos(oltid uint32, Vethnames []string, onumap map[uint32][]*device.Onu) ([]*Ioinfo, []string, error) { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 296 | ioinfos := []*Ioinfo{} |
| 297 | var err error |
| 298 | for intfid, _ := range onumap { |
| 299 | for i := 0; i < len(onumap[intfid]); i++ { |
| 300 | var handler *pcap.Handle |
| 301 | onuid := onumap[intfid][i].OnuID |
| 302 | uniup, unidw := makeUniName(oltid, intfid, onuid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 303 | if handler, Vethnames, err = setupVethHandler(uniup, unidw, Vethnames); err != nil { |
Shad Ansari | 6a93ee2 | 2018-11-16 13:26:47 -0800 | [diff] [blame] | 304 | logger.Error("setupVethHandler failed (onuid: %d)", onuid, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 305 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 306 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 307 | iinfo := Ioinfo{Name: uniup, iotype: "uni", ioloc: "inside", intfid: intfid, onuid: onuid, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 308 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 309 | oinfo := Ioinfo{Name: unidw, iotype: "uni", ioloc: "outside", intfid: intfid, onuid: onuid, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 310 | ioinfos = append(ioinfos, &oinfo) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | var handler *pcap.Handle |
| 315 | nniup, nnidw := makeNniName(oltid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 316 | if handler, Vethnames, err = setupVethHandler(nniup, nnidw, Vethnames); err != nil { |
Shad Ansari | 6a93ee2 | 2018-11-16 13:26:47 -0800 | [diff] [blame] | 317 | logger.Error("setupVethHandler failed for nni", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 318 | return ioinfos, Vethnames, err |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 319 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 320 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 321 | iinfo := Ioinfo{Name: nnidw, iotype: "nni", ioloc: "inside", intfid: 1, handler: handler} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 322 | ioinfos = append(ioinfos, &iinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 323 | oinfo := Ioinfo{Name: nniup, iotype: "nni", ioloc: "outside", intfid: 1, handler: nil} |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 324 | ioinfos = append(ioinfos, &oinfo) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 325 | return ioinfos, Vethnames, nil |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 326 | } |
| 327 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 328 | //Blocking |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 329 | func (s *Server) runPktLoops(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 330 | logger.Debug("runPacketPktLoops Start") |
| 331 | defer logger.Debug("runPacketLoops Done") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 332 | |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 333 | errch := make(chan error) |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 334 | RunOmciResponder(ctx, s.omciOut, s.omciIn, s.Onumap, errch) |
| 335 | eg, child := errgroup.WithContext(ctx) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 336 | child, cancel := context.WithCancel(child) |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 337 | |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 338 | eg.Go (func() error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 339 | logger.Debug("runOMCIResponder Start") |
| 340 | defer logger.Debug("runOMCIResponder Done") |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 341 | select{ |
| 342 | case v, ok := <- errch: // Wait for OmciInitialization |
| 343 | if ok { //Error |
| 344 | logger.Error("Error happend in Omci:%s", v) |
| 345 | return v |
| 346 | } else { //Close |
| 347 | s.updateState(ACTIVE) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 348 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 349 | case <- child.Done(): |
| 350 | return nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 351 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 352 | return nil |
| 353 | }) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 354 | |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 355 | eg.Go (func () error { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 356 | err := s.runMainPktLoop(child, stream) |
| 357 | return err |
| 358 | }) |
| 359 | |
| 360 | if err := eg.Wait(); err != nil { |
| 361 | logger.Error("Error happend in runPacketLoops:%s", err) |
| 362 | cancel() |
| 363 | } |
| 364 | return nil |
| 365 | } |
| 366 | |
| 367 | func (s *Server) runMainPktLoop(ctx context.Context, stream openolt.Openolt_EnableIndicationServer) error { |
| 368 | unichannel := make(chan Packet, 2048) |
| 369 | defer func() { |
| 370 | close(unichannel) |
| 371 | logger.Debug("Closed unichannel ") |
| 372 | }() |
| 373 | for intfid, _ := range s.Onumap { |
| 374 | for _, onu := range s.Onumap[intfid] { |
| 375 | onuid := onu.OnuID |
| 376 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 377 | if err != nil { |
| 378 | utils.LoggerWithOnu(onu).Error("Fail to identifyUniIoinfo (onuid: %d): %v", onuid, err) |
| 379 | return err |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame] | 380 | } |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 381 | uhandler := ioinfo.handler |
| 382 | go RecvWorker(ioinfo, uhandler, unichannel) |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 383 | } |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 384 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 385 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 386 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
| 387 | if err != nil { |
| 388 | return err |
| 389 | } |
| 390 | nhandler, nnichannel := ioinfo.handler, make(chan Packet, 32) |
| 391 | go RecvWorker(ioinfo, nhandler, nnichannel) |
| 392 | defer func(){ |
| 393 | close(nnichannel) |
| 394 | }() |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 395 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 396 | data := &openolt.Indication_PktInd{} |
| 397 | for { |
| 398 | select { |
| 399 | case msg := <-s.omciIn: |
| 400 | logger.Debug("OLT %d send omci indication, IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
| 401 | omci := &openolt.Indication_OmciInd{OmciInd: &msg} |
| 402 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
| 403 | logger.Error("send omci indication failed.", err) |
| 404 | continue |
| 405 | } |
| 406 | case unipkt := <-unichannel: |
| 407 | onuid := unipkt.Info.onuid |
| 408 | onu, _ := s.GetOnuByID(onuid) |
| 409 | utils.LoggerWithOnu(onu).Debug("Received packet from UNI in grpc Server") |
| 410 | if unipkt.Info == nil || unipkt.Info.iotype != "uni" { |
| 411 | logger.Debug("WARNING: This packet does not come from UNI ") |
| 412 | continue |
| 413 | } |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 414 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 415 | intfid := unipkt.Info.intfid |
| 416 | gemid, _ := getGemPortID(intfid, onuid) |
| 417 | pkt := unipkt.Pkt |
| 418 | layerEth := pkt.Layer(layers.LayerTypeEthernet) |
| 419 | le, _ := layerEth.(*layers.Ethernet) |
| 420 | ethtype := le.EthernetType |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 421 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 422 | if ethtype == 0x888e { |
| 423 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 424 | "gemId": gemid, |
| 425 | }).Info("Received upstream packet is EAPOL.") |
| 426 | } else if layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
| 427 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 428 | "gemId": gemid, |
| 429 | }).Info("Received upstream packet is DHCP.") |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 430 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 431 | //C-TAG |
| 432 | sn := convB2S(onu.SerialNumber.VendorSpecific) |
| 433 | if ctag, ok := s.CtagMap[sn]; ok == true { |
| 434 | tagpkt, err := PushVLAN(pkt, uint16(ctag), onu) |
| 435 | if err != nil { |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 436 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 437 | "gemId": gemid, |
| 438 | }).Error("Fail to tag C-tag") |
| 439 | } else { |
| 440 | pkt = tagpkt |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 441 | } |
| 442 | } else { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 443 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 444 | "gemId": gemid, |
| 445 | "cTagMap": s.CtagMap, |
| 446 | }).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 447 | } |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 448 | } else { |
| 449 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 450 | "gemId": gemid, |
| 451 | }).Info("Received upstream packet is of unknow type, skipping.") |
| 452 | continue |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 453 | } |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 454 | |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame^] | 455 | utils.LoggerWithOnu(onu).Info("sendPktInd - UNI Packet") |
| 456 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "pon", IntfId: intfid, GemportId: gemid, Pkt: pkt.Data()}} |
| 457 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 458 | logger.Error("Fail to send PktInd indication.", err) |
| 459 | return err |
| 460 | } |
| 461 | |
| 462 | case nnipkt := <-nnichannel: |
| 463 | if nnipkt.Info == nil || nnipkt.Info.iotype != "nni" { |
| 464 | logger.Debug("WARNING: This packet does not come from NNI ") |
| 465 | continue |
| 466 | } |
| 467 | onuid := nnipkt.Info.onuid |
| 468 | onu, _ := s.GetOnuByID(onuid) |
| 469 | |
| 470 | utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.") |
| 471 | intfid := nnipkt.Info.intfid |
| 472 | pkt := nnipkt.Pkt |
| 473 | utils.LoggerWithOnu(onu).Info("sendPktInd - NNI Packet") |
| 474 | data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}} |
| 475 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 476 | logger.Error("Fail to send PktInd indication.", err) |
| 477 | return err |
| 478 | } |
| 479 | |
| 480 | case <-ctx.Done(): |
| 481 | logger.Debug("Closed nnichannel ") |
| 482 | return nil |
| 483 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 484 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 485 | return nil |
| 486 | } |
| 487 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 488 | func (s *Server) onuPacketOut(intfid uint32, onuid uint32, rawpkt gopacket.Packet) error { |
| 489 | layerEth := rawpkt.Layer(layers.LayerTypeEthernet) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 490 | onu, _ := s.GetOnuByID(onuid) |
| 491 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 492 | if layerEth != nil { |
| 493 | pkt, _ := layerEth.(*layers.Ethernet) |
| 494 | ethtype := pkt.EthernetType |
| 495 | if ethtype == 0x888e { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 496 | utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 497 | } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil { |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 498 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 499 | "payload": layerDHCP.LayerPayload(), |
| 500 | "type": layerDHCP.LayerType().String(), |
| 501 | }).Info("Received downstream packet is DHCP.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 502 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 503 | rawpkt, _, _ = PopVLAN(rawpkt) |
| 504 | } else { |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 505 | utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 506 | return nil |
| 507 | } |
| 508 | ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid) |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | handle := ioinfo.handler |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 513 | SendUni(handle, rawpkt, onu) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 514 | return nil |
| 515 | } |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 516 | utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 517 | return nil |
| 518 | } |
| 519 | |
| 520 | func (s *Server) uplinkPacketOut(rawpkt gopacket.Packet) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 521 | poppkt, _, err := PopVLAN(rawpkt) |
| 522 | poppkt, _, err = PopVLAN(poppkt) |
| 523 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 524 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 525 | return err |
| 526 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 527 | ioinfo, err := s.IdentifyNniIoinfo("inside") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 528 | if err != nil { |
| 529 | return err |
| 530 | } |
| 531 | handle := ioinfo.handler |
| 532 | SendNni(handle, poppkt) |
| 533 | return nil |
| 534 | } |
| 535 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 536 | func IsAllOnuActive(onumap map[uint32][]*device.Onu) bool { |
| 537 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 538 | for _, onu := range onus { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 539 | if onu.GetIntStatus() != device.ONU_ACTIVATED { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 540 | return false |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | return true |
| 545 | } |
| 546 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 547 | func getGemPortID(intfid uint32, onuid uint32) (uint32, error) { |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 548 | key := OnuKey{intfid, onuid} |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 549 | if onuState, ok := OnuOmciStateMap[key]; !ok { |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 550 | idx := uint32(0) |
| 551 | // Backwards compatible with bbsim_olt adapter |
| 552 | return 1024 + (((MAX_ONUS_PER_PON*intfid + onuid - 1) * 7) + idx), nil |
| 553 | } else { |
| 554 | // FIXME - Gem Port ID is 2 bytes - fix openolt.proto |
| 555 | return uint32(onuState.gemPortId), nil |
| 556 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 557 | } |
| 558 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 559 | func getOnuBySN(onumap map[uint32][]*device.Onu, sn *openolt.SerialNumber) (*device.Onu, error) { |
| 560 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 561 | for _, onu := range onus { |
| 562 | if device.ValidateSN(*sn, *onu.SerialNumber) { |
| 563 | return onu, nil |
| 564 | } |
| 565 | } |
| 566 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 567 | err := errors.New("No mathced SN is found ") |
| 568 | logger.Error("%s", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 569 | return nil, err |
| 570 | } |
| 571 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 572 | func (s *Server) GetOnuByID(onuid uint32) (*device.Onu, error) { |
| 573 | return getOnuByID(s.Onumap, onuid) |
| 574 | } |
| 575 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 576 | func getOnuByID(onumap map[uint32][]*device.Onu, onuid uint32) (*device.Onu, error) { |
| 577 | for _, onus := range onumap { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 578 | for _, onu := range onus { |
| 579 | if onu.OnuID == onuid { |
| 580 | return onu, nil |
| 581 | } |
| 582 | } |
| 583 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 584 | err := errors.New("No matched OnuID is found ") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 585 | logger.WithFields(log.Fields{ |
| 586 | "onumap": onumap, |
| 587 | "onuid": onuid, |
| 588 | }).Error(err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 589 | return nil, err |
| 590 | } |
| 591 | |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 592 | func convB2S(b []byte) string { |
| 593 | s := "" |
| 594 | for _, i := range b { |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 595 | s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 596 | } |
| 597 | return s |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 598 | } |