blob: 535679013d3d29a78e7bebb83b9bb88679a4393e [file] [log] [blame]
Zdravko Bozakov2da76342019-10-21 09:47:35 +02001/*
Joey Armstrong14628cd2023-01-10 08:38:31 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
Zdravko Bozakov2da76342019-10-21 09:47:35 +02003
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
17package api
18
19import (
Zdravko Bozakov2da76342019-10-21 09:47:35 +020020 "net"
21 "strconv"
22
23 "github.com/opencord/bbsim/api/legacy"
24 api "github.com/opencord/bbsim/api/legacy"
25 "github.com/opencord/bbsim/internal/bbsim/devices"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020026 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
28)
29
30// Constants for reboot delays
31const (
32 OltRebootDelay = 40
33 OnuSoftRebootDelay = 10
34 OnuHardRebootDelay = 30
35)
36
37// handleONUStatusRequest process ONU status request
38func (s BBSimLegacyServer) handleONUStatusRequest(in *api.ONUInfo) (*api.ONUs, error) {
39 logger.Trace("handleONUStatusRequest() invoked")
40 onuInfo := &api.ONUs{}
41 olt := devices.GetOLT()
42
43 if in.OnuSerial != "" { // Get status of a single ONU by SerialNumber
44 onu, err := olt.FindOnuBySn(in.OnuSerial)
45 if err != nil {
46 logger.Errorf("ONU error: %+v", err)
47 return onuInfo, status.Errorf(codes.NotFound, "Unable to retrieve ONU %s", in.OnuSerial)
48 }
49 onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(onu))
50 } else {
51 if in.OnuId != 0 { // Get status of a single ONU by ONU-ID
52 onu, err := olt.FindOnuById(in.PonPortId, in.OnuId)
53 if err != nil {
54 logger.Errorf("ONU error: %+v", err)
55 return onuInfo, status.Errorf(codes.NotFound, "Unable to retrieve ONU with ID %d", in.OnuId)
56 }
57
58 onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(onu))
59 } else { // Get status of all ONUs
60 for _, p := range olt.Pons {
61 for _, o := range p.Onus {
62 onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(o))
63 }
64 }
65 }
66 }
67
68 return onuInfo, nil
69}
70
71func copyONUInfo(onu *devices.Onu) *api.ONUInfo {
72 onuData := &api.ONUInfo{
73 OnuId: onu.ID,
74 PonPortId: onu.PonPortID,
75 OnuSerial: onu.Sn(),
76 OnuState: onu.InternalState.Current(),
77 OperState: onu.OperState.Current(),
78 }
79
80 return onuData
81}
82
83func (s BBSimLegacyServer) fetchPortDetail(intfID uint32, portType string) (*api.PortInfo, error) {
84 logger.Tracef("fetchPortDetail() invoked %s-%d", portType, intfID)
85 olt := devices.GetOLT()
86
87 switch portType {
88 case "pon":
89 for _, pon := range olt.Pons {
90 if pon.ID == intfID {
91 ponPortInfo := &api.PortInfo{
92 PortType: "pon",
93 PortId: uint32(pon.ID),
94 PonPortMaxOnus: uint32(olt.NumOnuPerPon),
95 PonPortActiveOnus: uint32(olt.NumPon),
96 PortState: pon.OperState.Current(),
97 }
98 return ponPortInfo, nil
99 }
100 }
101 case "nni":
102 for _, nni := range olt.Nnis {
103 if nni.ID == intfID {
104 nniPortInfo := &legacy.PortInfo{
105 PortType: "nni",
106 PortId: uint32(nni.ID),
107 PortState: nni.OperState.Current(),
108 }
109 return nniPortInfo, nil
110 }
111 }
112 }
113
114 portInfo := &api.PortInfo{}
115 return portInfo, status.Errorf(codes.NotFound, "Invalid port %s-%d", portType, intfID)
116}
117
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200118// ConvB2S converts byte array to string
119func ConvB2S(b []byte) string {
120 s := ""
121 for _, i := range b {
122 s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
123 }
124 return s
125}
126
127func getOltIP() net.IP {
128 // TODO make this better
129 conn, err := net.Dial("udp", "8.8.8.8:80")
130 if err != nil {
131 logger.Error(err.Error())
132 return net.IP{}
133 }
134 defer func() {
135 err := conn.Close()
136 if err != nil {
137 logger.Error(err.Error())
138 }
139 }()
140
141 localAddr := conn.LocalAddr().(*net.UDPAddr)
142
143 return localAddr.IP
144}