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 | |
| 52 | func (s BBSimServer) ShutdownONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 53 | // NOTE this method is now sendying a Dying Gasp and then disabling the device (operState: down, adminState: up), |
| 54 | // is this the only way to do? Should we address other cases? |
| 55 | // Investigate what happens when: |
| 56 | // - a fiber is pulled |
| 57 | // - ONU malfunction |
| 58 | // - ONU shutdown |
| 59 | res := &bbsim.Response{} |
| 60 | |
| 61 | logger.WithFields(log.Fields{ |
| 62 | "OnuSn": req.SerialNumber, |
| 63 | }).Infof("Received request to shutdown ONU") |
| 64 | |
| 65 | olt := devices.GetOLT() |
| 66 | |
| 67 | onu, err := olt.FindOnu(req.SerialNumber) |
| 68 | |
| 69 | if err != nil { |
| 70 | res.StatusCode = int32(codes.NotFound) |
| 71 | res.Message = err.Error() |
| 72 | return res, err |
| 73 | } |
| 74 | |
| 75 | dyingGasp := devices.Message{ |
| 76 | Type: devices.DyingGaspIndication, |
| 77 | Data: devices.DyingGaspIndicationMessage{ |
| 78 | OnuID: onu.ID, |
| 79 | PonPortID: onu.PonPortID, |
| 80 | Status: "on", // TODO do we need a type for Dying Gasp Indication? |
| 81 | }, |
| 82 | } |
| 83 | |
| 84 | onu.Channel <- dyingGasp |
| 85 | |
| 86 | if err := onu.InternalState.Event("disable"); err != nil { |
| 87 | logger.WithFields(log.Fields{ |
| 88 | "OnuId": onu.ID, |
| 89 | "IntfId": onu.PonPortID, |
| 90 | "OnuSn": onu.Sn(), |
| 91 | }).Errorf("Cannot shutdown ONU: %s", err.Error()) |
| 92 | res.StatusCode = int32(codes.FailedPrecondition) |
| 93 | res.Message = err.Error() |
| 94 | return res, err |
| 95 | } |
| 96 | |
| 97 | res.StatusCode = int32(codes.OK) |
| 98 | res.Message = fmt.Sprintf("ONU %s successfully shut down.", onu.Sn()) |
| 99 | |
| 100 | return res, nil |
| 101 | } |
| 102 | |
| 103 | func (s BBSimServer) PoweronONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) { |
| 104 | res := &bbsim.Response{} |
| 105 | |
| 106 | logger.WithFields(log.Fields{ |
| 107 | "OnuSn": req.SerialNumber, |
| 108 | }).Infof("Received request to poweron ONU") |
| 109 | |
| 110 | olt := devices.GetOLT() |
| 111 | |
| 112 | onu, err := olt.FindOnu(req.SerialNumber) |
| 113 | |
| 114 | if err != nil { |
| 115 | res.StatusCode = int32(codes.NotFound) |
| 116 | res.Message = err.Error() |
| 117 | return res, err |
| 118 | } |
| 119 | |
| 120 | if err := onu.InternalState.Event("enable"); err != nil { |
| 121 | logger.WithFields(log.Fields{ |
| 122 | "OnuId": onu.ID, |
| 123 | "IntfId": onu.PonPortID, |
| 124 | "OnuSn": onu.Sn(), |
| 125 | }).Errorf("Cannot poweron ONU: %s", err.Error()) |
| 126 | res.StatusCode = int32(codes.FailedPrecondition) |
| 127 | res.Message = err.Error() |
| 128 | return res, err |
| 129 | } |
| 130 | |
| 131 | res.StatusCode = int32(codes.OK) |
| 132 | res.Message = fmt.Sprintf("ONU %s successfully powered on.", onu.Sn()) |
| 133 | |
| 134 | return res, nil |
| 135 | } |