Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [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 api |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 22 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 23 | "github.com/opencord/bbsim/api/bbsim" |
| 24 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | "google.golang.org/grpc/codes" |
| 27 | ) |
| 28 | |
| 29 | func (s BBSimServer) GetONUs(ctx context.Context, req *bbsim.Empty) (*bbsim.ONUs, error) { |
| 30 | olt := devices.GetOLT() |
| 31 | onus := bbsim.ONUs{ |
| 32 | Items: []*bbsim.ONU{}, |
| 33 | } |
| 34 | |
| 35 | for _, pon := range olt.Pons { |
| 36 | for _, o := range pon.Onus { |
| 37 | onu := bbsim.ONU{ |
| 38 | ID: int32(o.ID), |
| 39 | SerialNumber: o.Sn(), |
| 40 | OperState: o.OperState.Current(), |
| 41 | InternalState: o.InternalState.Current(), |
| 42 | PonPortID: int32(o.PonPortID), |
| 43 | STag: int32(o.STag), |
| 44 | CTag: int32(o.CTag), |
| 45 | HwAddress: o.HwAddress.String(), |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 46 | PortNo: int32(o.PortNo), |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 47 | } |
| 48 | onus.Items = append(onus.Items, &onu) |
| 49 | } |
| 50 | } |
| 51 | return &onus, nil |
| 52 | } |
| 53 | |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 54 | func (s BBSimServer) GetONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.ONU, error) { |
| 55 | olt := devices.GetOLT() |
| 56 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 57 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 58 | |
| 59 | if err != nil { |
| 60 | res := bbsim.ONU{} |
| 61 | return &res, err |
| 62 | } |
| 63 | |
| 64 | res := bbsim.ONU{ |
| 65 | ID: int32(onu.ID), |
| 66 | SerialNumber: onu.Sn(), |
| 67 | OperState: onu.OperState.Current(), |
| 68 | InternalState: onu.InternalState.Current(), |
| 69 | PonPortID: int32(onu.PonPortID), |
| 70 | STag: int32(onu.STag), |
| 71 | CTag: int32(onu.CTag), |
| 72 | HwAddress: onu.HwAddress.String(), |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 73 | PortNo: int32(onu.PortNo), |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 74 | } |
| 75 | return &res, nil |
| 76 | } |
| 77 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 78 | func (s BBSimServer) ShutdownONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 79 | // NOTE this method is now sendying a Dying Gasp and then disabling the device (operState: down, adminState: up), |
| 80 | // is this the only way to do? Should we address other cases? |
| 81 | // Investigate what happens when: |
| 82 | // - a fiber is pulled |
| 83 | // - ONU malfunction |
| 84 | // - ONU shutdown |
| 85 | res := &bbsim.Response{} |
| 86 | |
| 87 | logger.WithFields(log.Fields{ |
| 88 | "OnuSn": req.SerialNumber, |
| 89 | }).Infof("Received request to shutdown ONU") |
| 90 | |
| 91 | olt := devices.GetOLT() |
| 92 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 93 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 94 | |
| 95 | if err != nil { |
| 96 | res.StatusCode = int32(codes.NotFound) |
| 97 | res.Message = err.Error() |
| 98 | return res, err |
| 99 | } |
| 100 | |
| 101 | dyingGasp := devices.Message{ |
| 102 | Type: devices.DyingGaspIndication, |
| 103 | Data: devices.DyingGaspIndicationMessage{ |
| 104 | OnuID: onu.ID, |
| 105 | PonPortID: onu.PonPortID, |
| 106 | Status: "on", // TODO do we need a type for Dying Gasp Indication? |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | onu.Channel <- dyingGasp |
| 111 | |
| 112 | if err := onu.InternalState.Event("disable"); err != nil { |
| 113 | logger.WithFields(log.Fields{ |
| 114 | "OnuId": onu.ID, |
| 115 | "IntfId": onu.PonPortID, |
| 116 | "OnuSn": onu.Sn(), |
| 117 | }).Errorf("Cannot shutdown ONU: %s", err.Error()) |
| 118 | res.StatusCode = int32(codes.FailedPrecondition) |
| 119 | res.Message = err.Error() |
| 120 | return res, err |
| 121 | } |
| 122 | |
| 123 | res.StatusCode = int32(codes.OK) |
| 124 | res.Message = fmt.Sprintf("ONU %s successfully shut down.", onu.Sn()) |
| 125 | |
| 126 | return res, nil |
| 127 | } |
| 128 | |
| 129 | func (s BBSimServer) PoweronONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 130 | res := &bbsim.Response{} |
| 131 | |
| 132 | logger.WithFields(log.Fields{ |
| 133 | "OnuSn": req.SerialNumber, |
| 134 | }).Infof("Received request to poweron ONU") |
| 135 | |
| 136 | olt := devices.GetOLT() |
| 137 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 138 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
Matteo Scandolo | e60a505 | 2020-02-07 00:31:14 +0000 | [diff] [blame^] | 139 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 140 | if err != nil { |
| 141 | res.StatusCode = int32(codes.NotFound) |
| 142 | res.Message = err.Error() |
| 143 | return res, err |
| 144 | } |
| 145 | |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 146 | if err := onu.InternalState.Event("discover"); err != nil { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 147 | logger.WithFields(log.Fields{ |
| 148 | "OnuId": onu.ID, |
| 149 | "IntfId": onu.PonPortID, |
| 150 | "OnuSn": onu.Sn(), |
| 151 | }).Errorf("Cannot poweron ONU: %s", err.Error()) |
| 152 | res.StatusCode = int32(codes.FailedPrecondition) |
| 153 | res.Message = err.Error() |
| 154 | return res, err |
| 155 | } |
| 156 | |
| 157 | res.StatusCode = int32(codes.OK) |
| 158 | res.Message = fmt.Sprintf("ONU %s successfully powered on.", onu.Sn()) |
| 159 | |
| 160 | return res, nil |
| 161 | } |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 162 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 163 | func (s BBSimServer) ChangeIgmpState(ctx context.Context, req *bbsim.IgmpRequest) (*bbsim.Response, error) { |
| 164 | res := &bbsim.Response{} |
| 165 | |
| 166 | logger.WithFields(log.Fields{ |
| 167 | "OnuSn": req.OnuReq.SerialNumber, |
| 168 | "subAction": req.SubActionVal, |
| 169 | }).Infof("Received igmp request for ONU") |
| 170 | |
| 171 | olt := devices.GetOLT() |
| 172 | onu, err := olt.FindOnuBySn(req.OnuReq.SerialNumber) |
| 173 | |
| 174 | if err != nil { |
| 175 | res.StatusCode = int32(codes.NotFound) |
| 176 | res.Message = err.Error() |
| 177 | fmt.Println("ONU not found for sending igmp packet.") |
| 178 | return res, err |
| 179 | } else { |
| 180 | event := "" |
| 181 | switch req.SubActionVal { |
| 182 | case bbsim.SubActionTypes_JOIN: |
| 183 | event = "igmp_join_start" |
| 184 | case bbsim.SubActionTypes_LEAVE: |
| 185 | event = "igmp_leave" |
| 186 | } |
| 187 | |
| 188 | if igmpErr := onu.InternalState.Event(event); igmpErr != nil { |
| 189 | logger.WithFields(log.Fields{ |
| 190 | "OnuId": onu.ID, |
| 191 | "IntfId": onu.PonPortID, |
| 192 | "OnuSn": onu.Sn(), |
| 193 | }).Errorf("IGMP request failed: %s", igmpErr.Error()) |
| 194 | res.StatusCode = int32(codes.FailedPrecondition) |
| 195 | res.Message = err.Error() |
| 196 | return res, igmpErr |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return res, nil |
| 201 | } |
| 202 | |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 203 | func (s BBSimServer) RestartEapol(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 204 | res := &bbsim.Response{} |
| 205 | |
| 206 | logger.WithFields(log.Fields{ |
| 207 | "OnuSn": req.SerialNumber, |
| 208 | }).Infof("Received request to restart authentication ONU") |
| 209 | |
| 210 | olt := devices.GetOLT() |
| 211 | |
| 212 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
| 213 | |
| 214 | if err != nil { |
| 215 | res.StatusCode = int32(codes.NotFound) |
| 216 | res.Message = err.Error() |
| 217 | return res, err |
| 218 | } |
| 219 | |
| 220 | if err := onu.InternalState.Event("start_auth"); err != nil { |
| 221 | logger.WithFields(log.Fields{ |
| 222 | "OnuId": onu.ID, |
| 223 | "IntfId": onu.PonPortID, |
| 224 | "OnuSn": onu.Sn(), |
| 225 | }).Errorf("Cannot restart authenticaton for ONU: %s", err.Error()) |
| 226 | res.StatusCode = int32(codes.FailedPrecondition) |
| 227 | res.Message = err.Error() |
| 228 | return res, err |
| 229 | } |
| 230 | |
| 231 | res.StatusCode = int32(codes.OK) |
| 232 | res.Message = fmt.Sprintf("Authentication restarted for ONU %s.", onu.Sn()) |
| 233 | |
| 234 | return res, nil |
| 235 | } |
| 236 | |
| 237 | func (s BBSimServer) RestartDhcp(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 238 | res := &bbsim.Response{} |
| 239 | |
| 240 | logger.WithFields(log.Fields{ |
| 241 | "OnuSn": req.SerialNumber, |
| 242 | }).Infof("Received request to restart DHCP on ONU") |
| 243 | |
| 244 | olt := devices.GetOLT() |
| 245 | |
| 246 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
| 247 | |
| 248 | if err != nil { |
| 249 | res.StatusCode = int32(codes.NotFound) |
| 250 | res.Message = err.Error() |
| 251 | return res, err |
| 252 | } |
| 253 | |
| 254 | if err := onu.InternalState.Event("start_dhcp"); err != nil { |
| 255 | logger.WithFields(log.Fields{ |
| 256 | "OnuId": onu.ID, |
| 257 | "IntfId": onu.PonPortID, |
| 258 | "OnuSn": onu.Sn(), |
| 259 | }).Errorf("Cannot restart DHCP for ONU: %s", err.Error()) |
| 260 | res.StatusCode = int32(codes.FailedPrecondition) |
| 261 | res.Message = err.Error() |
| 262 | return res, err |
| 263 | } |
| 264 | |
| 265 | res.StatusCode = int32(codes.OK) |
| 266 | res.Message = fmt.Sprintf("DHCP restarted for ONU %s.", onu.Sn()) |
| 267 | |
| 268 | return res, nil |
| 269 | } |