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