blob: cec88037f0e88b741835b54cfea934a948701ae5 [file] [log] [blame]
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001/*
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
17package api
18
19import (
20 "context"
21 "fmt"
Pragya Arya1cbefa42020-01-13 12:15:29 +053022
Matteo Scandolo10f965c2019-09-24 10:40:46 -070023 "github.com/opencord/bbsim/api/bbsim"
24 "github.com/opencord/bbsim/internal/bbsim/devices"
25 log "github.com/sirupsen/logrus"
26 "google.golang.org/grpc/codes"
27)
28
29func (s BBSimServer) GetONUs(ctx context.Context, req *bbsim.Empty) (*bbsim.ONUs, error) {
30 olt := devices.GetOLT()
31 onus := bbsim.ONUs{
32 Items: []*bbsim.ONU{},
33 }
34
35 for _, pon := range olt.Pons {
36 for _, o := range pon.Onus {
37 onu := bbsim.ONU{
38 ID: int32(o.ID),
39 SerialNumber: o.Sn(),
40 OperState: o.OperState.Current(),
41 InternalState: o.InternalState.Current(),
42 PonPortID: int32(o.PonPortID),
43 STag: int32(o.STag),
44 CTag: int32(o.CTag),
45 HwAddress: o.HwAddress.String(),
Matteo Scandolo27428702019-10-11 16:21:16 -070046 PortNo: int32(o.PortNo),
Matteo Scandolo10f965c2019-09-24 10:40:46 -070047 }
48 onus.Items = append(onus.Items, &onu)
49 }
50 }
51 return &onus, nil
52}
53
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070054func (s BBSimServer) GetONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.ONU, error) {
55 olt := devices.GetOLT()
56
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070057 onu, err := olt.FindOnuBySn(req.SerialNumber)
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070058
59 if err != nil {
60 res := bbsim.ONU{}
61 return &res, err
62 }
63
64 res := bbsim.ONU{
65 ID: int32(onu.ID),
66 SerialNumber: onu.Sn(),
67 OperState: onu.OperState.Current(),
68 InternalState: onu.InternalState.Current(),
69 PonPortID: int32(onu.PonPortID),
70 STag: int32(onu.STag),
71 CTag: int32(onu.CTag),
72 HwAddress: onu.HwAddress.String(),
Matteo Scandolo27428702019-10-11 16:21:16 -070073 PortNo: int32(onu.PortNo),
Matteo Scandolod2ca2c72019-10-04 16:50:22 -070074 }
75 return &res, nil
76}
77
Matteo Scandolo10f965c2019-09-24 10:40:46 -070078func (s BBSimServer) ShutdownONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
79 // NOTE this method is now sendying a Dying Gasp and then disabling the device (operState: down, adminState: up),
80 // is this the only way to do? Should we address other cases?
81 // Investigate what happens when:
82 // - a fiber is pulled
83 // - ONU malfunction
84 // - ONU shutdown
85 res := &bbsim.Response{}
86
87 logger.WithFields(log.Fields{
88 "OnuSn": req.SerialNumber,
89 }).Infof("Received request to shutdown ONU")
90
91 olt := devices.GetOLT()
92
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070093 onu, err := olt.FindOnuBySn(req.SerialNumber)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070094
95 if err != nil {
96 res.StatusCode = int32(codes.NotFound)
97 res.Message = err.Error()
98 return res, err
99 }
100
101 dyingGasp := devices.Message{
102 Type: devices.DyingGaspIndication,
103 Data: devices.DyingGaspIndicationMessage{
104 OnuID: onu.ID,
105 PonPortID: onu.PonPortID,
106 Status: "on", // TODO do we need a type for Dying Gasp Indication?
107 },
108 }
109
110 onu.Channel <- dyingGasp
111
112 if err := onu.InternalState.Event("disable"); err != nil {
113 logger.WithFields(log.Fields{
114 "OnuId": onu.ID,
115 "IntfId": onu.PonPortID,
116 "OnuSn": onu.Sn(),
117 }).Errorf("Cannot shutdown ONU: %s", err.Error())
118 res.StatusCode = int32(codes.FailedPrecondition)
119 res.Message = err.Error()
120 return res, err
121 }
122
123 res.StatusCode = int32(codes.OK)
124 res.Message = fmt.Sprintf("ONU %s successfully shut down.", onu.Sn())
125
126 return res, nil
127}
128
129func (s BBSimServer) PoweronONU(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
130 res := &bbsim.Response{}
131
132 logger.WithFields(log.Fields{
133 "OnuSn": req.SerialNumber,
134 }).Infof("Received request to poweron ONU")
135
136 olt := devices.GetOLT()
137
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700138 onu, err := olt.FindOnuBySn(req.SerialNumber)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700139
140 if err != nil {
141 res.StatusCode = int32(codes.NotFound)
142 res.Message = err.Error()
143 return res, err
144 }
145
Pragya Arya1cbefa42020-01-13 12:15:29 +0530146 if err := onu.InternalState.Event("discover"); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700147 logger.WithFields(log.Fields{
148 "OnuId": onu.ID,
149 "IntfId": onu.PonPortID,
150 "OnuSn": onu.Sn(),
151 }).Errorf("Cannot poweron ONU: %s", err.Error())
152 res.StatusCode = int32(codes.FailedPrecondition)
153 res.Message = err.Error()
154 return res, err
155 }
156
157 res.StatusCode = int32(codes.OK)
158 res.Message = fmt.Sprintf("ONU %s successfully powered on.", onu.Sn())
159
160 return res, nil
161}
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700162
Arjun E K57a7fcb2020-01-30 06:44:45 +0000163func (s BBSimServer) ChangeIgmpState(ctx context.Context, req *bbsim.IgmpRequest) (*bbsim.Response, error) {
164 res := &bbsim.Response{}
165
166 logger.WithFields(log.Fields{
167 "OnuSn": req.OnuReq.SerialNumber,
168 "subAction": req.SubActionVal,
169 }).Infof("Received igmp request for ONU")
170
171 olt := devices.GetOLT()
172 onu, err := olt.FindOnuBySn(req.OnuReq.SerialNumber)
173
174 if err != nil {
175 res.StatusCode = int32(codes.NotFound)
176 res.Message = err.Error()
177 fmt.Println("ONU not found for sending igmp packet.")
178 return res, err
179 } else {
180 event := ""
181 switch req.SubActionVal {
182 case bbsim.SubActionTypes_JOIN:
183 event = "igmp_join_start"
184 case bbsim.SubActionTypes_LEAVE:
185 event = "igmp_leave"
186 }
187
188 if igmpErr := onu.InternalState.Event(event); igmpErr != nil {
189 logger.WithFields(log.Fields{
190 "OnuId": onu.ID,
191 "IntfId": onu.PonPortID,
192 "OnuSn": onu.Sn(),
193 }).Errorf("IGMP request failed: %s", igmpErr.Error())
194 res.StatusCode = int32(codes.FailedPrecondition)
195 res.Message = err.Error()
196 return res, igmpErr
197 }
198 }
199
200 return res, nil
201}
202
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700203func (s BBSimServer) RestartEapol(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
204 res := &bbsim.Response{}
205
206 logger.WithFields(log.Fields{
207 "OnuSn": req.SerialNumber,
208 }).Infof("Received request to restart authentication ONU")
209
210 olt := devices.GetOLT()
211
212 onu, err := olt.FindOnuBySn(req.SerialNumber)
213
214 if err != nil {
215 res.StatusCode = int32(codes.NotFound)
216 res.Message = err.Error()
217 return res, err
218 }
219
220 if err := onu.InternalState.Event("start_auth"); err != nil {
221 logger.WithFields(log.Fields{
222 "OnuId": onu.ID,
223 "IntfId": onu.PonPortID,
224 "OnuSn": onu.Sn(),
225 }).Errorf("Cannot restart authenticaton for ONU: %s", err.Error())
226 res.StatusCode = int32(codes.FailedPrecondition)
227 res.Message = err.Error()
228 return res, err
229 }
230
231 res.StatusCode = int32(codes.OK)
232 res.Message = fmt.Sprintf("Authentication restarted for ONU %s.", onu.Sn())
233
234 return res, nil
235}
236
237func (s BBSimServer) RestartDhcp(ctx context.Context, req *bbsim.ONURequest) (*bbsim.Response, error) {
238 res := &bbsim.Response{}
239
240 logger.WithFields(log.Fields{
241 "OnuSn": req.SerialNumber,
242 }).Infof("Received request to restart DHCP on ONU")
243
244 olt := devices.GetOLT()
245
246 onu, err := olt.FindOnuBySn(req.SerialNumber)
247
248 if err != nil {
249 res.StatusCode = int32(codes.NotFound)
250 res.Message = err.Error()
251 return res, err
252 }
253
254 if err := onu.InternalState.Event("start_dhcp"); err != nil {
255 logger.WithFields(log.Fields{
256 "OnuId": onu.ID,
257 "IntfId": onu.PonPortID,
258 "OnuSn": onu.Sn(),
259 }).Errorf("Cannot restart DHCP for ONU: %s", err.Error())
260 res.StatusCode = int32(codes.FailedPrecondition)
261 res.Message = err.Error()
262 return res, err
263 }
264
265 res.StatusCode = int32(codes.OK)
266 res.Message = fmt.Sprintf("DHCP restarted for ONU %s.", onu.Sn())
267
268 return res, nil
269}