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