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(), |
| 45 | } |
| 46 | onus.Items = append(onus.Items, &onu) |
| 47 | } |
| 48 | } |
| 49 | return &onus, nil |
| 50 | } |
| 51 | |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 52 | func (s BBSimServer) GetONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.ONU, error) { |
| 53 | olt := devices.GetOLT() |
| 54 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame^] | 55 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
Matteo Scandolo | d2ca2c7 | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 56 | |
| 57 | if err != nil { |
| 58 | res := bbsim.ONU{} |
| 59 | return &res, err |
| 60 | } |
| 61 | |
| 62 | res := bbsim.ONU{ |
| 63 | ID: int32(onu.ID), |
| 64 | SerialNumber: onu.Sn(), |
| 65 | OperState: onu.OperState.Current(), |
| 66 | InternalState: onu.InternalState.Current(), |
| 67 | PonPortID: int32(onu.PonPortID), |
| 68 | STag: int32(onu.STag), |
| 69 | CTag: int32(onu.CTag), |
| 70 | HwAddress: onu.HwAddress.String(), |
| 71 | } |
| 72 | return &res, nil |
| 73 | } |
| 74 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 75 | func (s BBSimServer) ShutdownONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 76 | // NOTE this method is now sendying a Dying Gasp and then disabling the device (operState: down, adminState: up), |
| 77 | // is this the only way to do? Should we address other cases? |
| 78 | // Investigate what happens when: |
| 79 | // - a fiber is pulled |
| 80 | // - ONU malfunction |
| 81 | // - ONU shutdown |
| 82 | res := &bbsim.Response{} |
| 83 | |
| 84 | logger.WithFields(log.Fields{ |
| 85 | "OnuSn": req.SerialNumber, |
| 86 | }).Infof("Received request to shutdown ONU") |
| 87 | |
| 88 | olt := devices.GetOLT() |
| 89 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame^] | 90 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 91 | |
| 92 | if err != nil { |
| 93 | res.StatusCode = int32(codes.NotFound) |
| 94 | res.Message = err.Error() |
| 95 | return res, err |
| 96 | } |
| 97 | |
| 98 | dyingGasp := devices.Message{ |
| 99 | Type: devices.DyingGaspIndication, |
| 100 | Data: devices.DyingGaspIndicationMessage{ |
| 101 | OnuID: onu.ID, |
| 102 | PonPortID: onu.PonPortID, |
| 103 | Status: "on", // TODO do we need a type for Dying Gasp Indication? |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | onu.Channel <- dyingGasp |
| 108 | |
| 109 | if err := onu.InternalState.Event("disable"); err != nil { |
| 110 | logger.WithFields(log.Fields{ |
| 111 | "OnuId": onu.ID, |
| 112 | "IntfId": onu.PonPortID, |
| 113 | "OnuSn": onu.Sn(), |
| 114 | }).Errorf("Cannot shutdown ONU: %s", err.Error()) |
| 115 | res.StatusCode = int32(codes.FailedPrecondition) |
| 116 | res.Message = err.Error() |
| 117 | return res, err |
| 118 | } |
| 119 | |
| 120 | res.StatusCode = int32(codes.OK) |
| 121 | res.Message = fmt.Sprintf("ONU %s successfully shut down.", onu.Sn()) |
| 122 | |
| 123 | return res, nil |
| 124 | } |
| 125 | |
| 126 | func (s BBSimServer) PoweronONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 127 | res := &bbsim.Response{} |
| 128 | |
| 129 | logger.WithFields(log.Fields{ |
| 130 | "OnuSn": req.SerialNumber, |
| 131 | }).Infof("Received request to poweron ONU") |
| 132 | |
| 133 | olt := devices.GetOLT() |
| 134 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame^] | 135 | onu, err := olt.FindOnuBySn(req.SerialNumber) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 136 | |
| 137 | if err != nil { |
| 138 | res.StatusCode = int32(codes.NotFound) |
| 139 | res.Message = err.Error() |
| 140 | return res, err |
| 141 | } |
| 142 | |
| 143 | if err := onu.InternalState.Event("enable"); err != nil { |
| 144 | logger.WithFields(log.Fields{ |
| 145 | "OnuId": onu.ID, |
| 146 | "IntfId": onu.PonPortID, |
| 147 | "OnuSn": onu.Sn(), |
| 148 | }).Errorf("Cannot poweron ONU: %s", err.Error()) |
| 149 | res.StatusCode = int32(codes.FailedPrecondition) |
| 150 | res.Message = err.Error() |
| 151 | return res, err |
| 152 | } |
| 153 | |
| 154 | res.StatusCode = int32(codes.OK) |
| 155 | res.Message = fmt.Sprintf("ONU %s successfully powered on.", onu.Sn()) |
| 156 | |
| 157 | return res, nil |
| 158 | } |