Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [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 | "encoding/hex" |
| 21 | "errors" |
| 22 | "net" |
| 23 | "strconv" |
| 24 | |
| 25 | "github.com/opencord/bbsim/api/legacy" |
| 26 | api "github.com/opencord/bbsim/api/legacy" |
| 27 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 28 | "github.com/opencord/voltha-protos/go/openolt" |
| 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/status" |
| 31 | ) |
| 32 | |
| 33 | // Constants for reboot delays |
| 34 | const ( |
| 35 | OltRebootDelay = 40 |
| 36 | OnuSoftRebootDelay = 10 |
| 37 | OnuHardRebootDelay = 30 |
| 38 | ) |
| 39 | |
| 40 | // handleONUStatusRequest process ONU status request |
| 41 | func (s BBSimLegacyServer) handleONUStatusRequest(in *api.ONUInfo) (*api.ONUs, error) { |
| 42 | logger.Trace("handleONUStatusRequest() invoked") |
| 43 | onuInfo := &api.ONUs{} |
| 44 | olt := devices.GetOLT() |
| 45 | |
| 46 | if in.OnuSerial != "" { // Get status of a single ONU by SerialNumber |
| 47 | onu, err := olt.FindOnuBySn(in.OnuSerial) |
| 48 | if err != nil { |
| 49 | logger.Errorf("ONU error: %+v", err) |
| 50 | return onuInfo, status.Errorf(codes.NotFound, "Unable to retrieve ONU %s", in.OnuSerial) |
| 51 | } |
| 52 | onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(onu)) |
| 53 | } else { |
| 54 | if in.OnuId != 0 { // Get status of a single ONU by ONU-ID |
| 55 | onu, err := olt.FindOnuById(in.PonPortId, in.OnuId) |
| 56 | if err != nil { |
| 57 | logger.Errorf("ONU error: %+v", err) |
| 58 | return onuInfo, status.Errorf(codes.NotFound, "Unable to retrieve ONU with ID %d", in.OnuId) |
| 59 | } |
| 60 | |
| 61 | onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(onu)) |
| 62 | } else { // Get status of all ONUs |
| 63 | for _, p := range olt.Pons { |
| 64 | for _, o := range p.Onus { |
| 65 | onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(o)) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return onuInfo, nil |
| 72 | } |
| 73 | |
| 74 | func copyONUInfo(onu *devices.Onu) *api.ONUInfo { |
| 75 | onuData := &api.ONUInfo{ |
| 76 | OnuId: onu.ID, |
| 77 | PonPortId: onu.PonPortID, |
| 78 | OnuSerial: onu.Sn(), |
| 79 | OnuState: onu.InternalState.Current(), |
| 80 | OperState: onu.OperState.Current(), |
| 81 | } |
| 82 | |
| 83 | return onuData |
| 84 | } |
| 85 | |
| 86 | func (s BBSimLegacyServer) fetchPortDetail(intfID uint32, portType string) (*api.PortInfo, error) { |
| 87 | logger.Tracef("fetchPortDetail() invoked %s-%d", portType, intfID) |
| 88 | olt := devices.GetOLT() |
| 89 | |
| 90 | switch portType { |
| 91 | case "pon": |
| 92 | for _, pon := range olt.Pons { |
| 93 | if pon.ID == intfID { |
| 94 | ponPortInfo := &api.PortInfo{ |
| 95 | PortType: "pon", |
| 96 | PortId: uint32(pon.ID), |
| 97 | PonPortMaxOnus: uint32(olt.NumOnuPerPon), |
| 98 | PonPortActiveOnus: uint32(olt.NumPon), |
| 99 | PortState: pon.OperState.Current(), |
| 100 | } |
| 101 | return ponPortInfo, nil |
| 102 | } |
| 103 | } |
| 104 | case "nni": |
| 105 | for _, nni := range olt.Nnis { |
| 106 | if nni.ID == intfID { |
| 107 | nniPortInfo := &legacy.PortInfo{ |
| 108 | PortType: "nni", |
| 109 | PortId: uint32(nni.ID), |
| 110 | PortState: nni.OperState.Current(), |
| 111 | } |
| 112 | return nniPortInfo, nil |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | portInfo := &api.PortInfo{} |
| 118 | return portInfo, status.Errorf(codes.NotFound, "Invalid port %s-%d", portType, intfID) |
| 119 | } |
| 120 | |
| 121 | func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) { |
| 122 | var VendorIDLength = 4 |
| 123 | var SerialNumberLength = 12 |
| 124 | |
| 125 | if len(SerialNumber) != SerialNumberLength { |
| 126 | logger.Error("Invalid serial number %s", SerialNumber) |
| 127 | return nil, errors.New("invalid serial number") |
| 128 | } |
| 129 | // First four characters are vendorId |
| 130 | vendorID := SerialNumber[:VendorIDLength] |
| 131 | vendorSpecific := SerialNumber[VendorIDLength:] |
| 132 | |
| 133 | vsbyte, _ := hex.DecodeString(vendorSpecific) |
| 134 | |
| 135 | // Convert to Openolt serial number |
| 136 | serialNum := new(openolt.SerialNumber) |
| 137 | serialNum.VendorId = []byte(vendorID) |
| 138 | serialNum.VendorSpecific = vsbyte |
| 139 | |
| 140 | return serialNum, nil |
| 141 | } |
| 142 | |
| 143 | // ConvB2S converts byte array to string |
| 144 | func ConvB2S(b []byte) string { |
| 145 | s := "" |
| 146 | for _, i := range b { |
| 147 | s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16) |
| 148 | } |
| 149 | return s |
| 150 | } |
| 151 | |
| 152 | func getOltIP() net.IP { |
| 153 | // TODO make this better |
| 154 | conn, err := net.Dial("udp", "8.8.8.8:80") |
| 155 | if err != nil { |
| 156 | logger.Error(err.Error()) |
| 157 | return net.IP{} |
| 158 | } |
| 159 | defer func() { |
| 160 | err := conn.Close() |
| 161 | if err != nil { |
| 162 | logger.Error(err.Error()) |
| 163 | } |
| 164 | }() |
| 165 | |
| 166 | localAddr := conn.LocalAddr().(*net.UDPAddr) |
| 167 | |
| 168 | return localAddr.IP |
| 169 | } |