blob: 01aa411b73e7cb3e1a525eea204b39e2a8e6b30c [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -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 devices
18
19import (
20 "context"
21 "errors"
22 "fmt"
Shrey Baid688b4242020-07-10 20:40:10 +053023 "io"
24 "reflect"
25 "time"
26
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027 "github.com/google/gopacket"
28 "github.com/google/gopacket/layers"
29 "github.com/opencord/bbsim/internal/bbsim/devices"
30 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
31 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080032 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070033 log "github.com/sirupsen/logrus"
34 "google.golang.org/grpc"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070035)
36
37type OltMock struct {
Matteo Scandolo583f17d2020-02-13 10:35:17 -080038 LastUsedOnuId map[uint32]uint32
39 Olt *devices.OltDevice
40 BBSimIp string
41 BBSimPort string
42 BBSimApiPort string
Matteo Scandolo40e067f2019-10-16 16:59:41 -070043
44 conn *grpc.ClientConn
45
46 TargetOnus int
47 CompletedOnus int // Number of ONUs that have received a DHCPAck
48}
49
50// trigger an enable call and start the same listeners on the gRPC stream that VOLTHA would create
51// this method is blocking
52func (o *OltMock) Start() {
53 log.Info("Starting Mock OLT")
54
55 for _, pon := range o.Olt.Pons {
56 for _, onu := range pon.Onus {
Matteo Scandolod32c3822019-11-26 15:57:46 -070057 if err := onu.InternalState.Event("initialize"); err != nil {
58 log.Fatalf("Error initializing ONU: %v", err)
59 }
Matteo Scandolo583f17d2020-02-13 10:35:17 -080060 log.Debugf("Created ONU: %s (%d:%d)", onu.Sn(), onu.STag, onu.CTag)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070061 }
62 }
63
64 client, conn := Connect(o.BBSimIp, o.BBSimPort)
65 o.conn = conn
66 defer conn.Close()
67
68 deviceInfo, err := o.getDeviceInfo(client)
69
70 if err != nil {
71 log.WithFields(log.Fields{
72 "error": err,
73 }).Fatal("Can't read device info")
74 }
75
76 log.WithFields(log.Fields{
77 "Vendor": deviceInfo.Vendor,
78 "Model": deviceInfo.Model,
79 "DeviceSerialNumber": deviceInfo.DeviceSerialNumber,
80 "PonPorts": deviceInfo.PonPorts,
81 }).Info("Retrieved device info")
82
83 o.readIndications(client)
84
85}
86
87func (o *OltMock) getDeviceInfo(client openolt.OpenoltClient) (*openolt.DeviceInfo, error) {
88 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
89 defer cancel()
90
91 return client.GetDeviceInfo(ctx, new(openolt.Empty))
92}
93
94func (o *OltMock) getOnuByTags(sTag int, cTag int) (*devices.Onu, error) {
95
96 for _, pon := range o.Olt.Pons {
97 for _, onu := range pon.Onus {
98 if onu.STag == sTag && onu.CTag == cTag {
99 return onu, nil
100 }
101 }
102 }
103
104 return nil, errors.New("cant-find-onu-by-c-s-tags")
105}
106
107func (o *OltMock) readIndications(client openolt.OpenoltClient) {
108 defer func() {
109 log.Info("OLT readIndications done")
110 }()
111
112 // Tell the OLT to start sending indications
113 indications, err := client.EnableIndication(context.Background(), new(openolt.Empty))
114 if err != nil {
115 log.WithFields(log.Fields{
116 "error": err,
117 }).Error("Failed to enable indication stream")
118 return
119 }
120
121 // listen for indications
122 for {
123 indication, err := indications.Recv()
124 if err == io.EOF {
125 break
126 }
127 if err != nil {
128
129 // the connection is closed once we have sent the DHCP_ACK packet to all of the ONUs
130 // it means BBR completed, it's not an error
131
132 log.WithFields(log.Fields{
133 "error": err,
134 }).Debug("Failed to read from indications")
135 break
136 }
137
138 o.handleIndication(client, indication)
139 }
140}
141
142func (o *OltMock) handleIndication(client openolt.OpenoltClient, indication *openolt.Indication) {
143 switch indication.Data.(type) {
144 case *openolt.Indication_OltInd:
145 log.Info("Received Indication_OltInd")
146 case *openolt.Indication_IntfInd:
147 log.Info("Received Indication_IntfInd")
148 case *openolt.Indication_IntfOperInd:
149 log.Info("Received Indication_IntfOperInd")
150 case *openolt.Indication_OnuDiscInd:
151 onuDiscInd := indication.GetOnuDiscInd()
152 o.handleOnuDiscIndication(client, onuDiscInd)
153 case *openolt.Indication_OnuInd:
154 onuInd := indication.GetOnuInd()
155 o.handleOnuIndication(client, onuInd)
156 case *openolt.Indication_OmciInd:
157 omciIndication := indication.GetOmciInd()
158 o.handleOmciIndication(client, omciIndication)
159 case *openolt.Indication_PktInd:
160 pktIndication := indication.GetPktInd()
161 o.handlePktIndication(client, pktIndication)
162 case *openolt.Indication_PortStats:
163 case *openolt.Indication_FlowStats:
164 case *openolt.Indication_AlarmInd:
165 default:
166 log.WithFields(log.Fields{
167 "data": indication.Data,
168 "type": reflect.TypeOf(indication.Data),
169 }).Warn("Indication unsupported")
170 }
171}
172
173func (o *OltMock) handleOnuDiscIndication(client openolt.OpenoltClient, onuDiscInd *openolt.OnuDiscIndication) {
174 log.WithFields(log.Fields{
175 "IntfId": onuDiscInd.IntfId,
176 "SerialNumber": common.OnuSnToString(onuDiscInd.SerialNumber),
177 }).Info("Received Onu discovery indication")
178
179 onu, err := o.Olt.FindOnuBySn(common.OnuSnToString(onuDiscInd.SerialNumber))
180
181 if err != nil {
182 log.WithFields(log.Fields{
183 "IntfId": onuDiscInd.IntfId,
184 "SerialNumber": common.OnuSnToString(onuDiscInd.SerialNumber),
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800185 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700186 }).Fatal("Cannot find ONU")
187 }
188
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800189 // creating and storing ONU IDs
190 id := o.LastUsedOnuId[onuDiscInd.IntfId] + 1
191 o.LastUsedOnuId[onuDiscInd.IntfId] = o.LastUsedOnuId[onuDiscInd.IntfId] + 1
192 onu.SetID(id)
193
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700194 var pir uint32 = 1000000
195 Onu := openolt.Onu{
196 IntfId: onu.PonPortID,
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800197 OnuId: id,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700198 SerialNumber: onu.SerialNumber,
199 Pir: pir,
200 }
201
202 if _, err := client.ActivateOnu(context.Background(), &Onu); err != nil {
203 log.WithFields(log.Fields{
204 "IntfId": onuDiscInd.IntfId,
205 "SerialNumber": common.OnuSnToString(onuDiscInd.SerialNumber),
206 }).Error("Failed to activate ONU")
207 }
208}
209
210func (o *OltMock) handleOnuIndication(client openolt.OpenoltClient, onuInd *openolt.OnuIndication) {
211 log.WithFields(log.Fields{
212 "IntfId": onuInd.IntfId,
213 "SerialNumber": common.OnuSnToString(onuInd.SerialNumber),
214 }).Info("Received Onu indication")
215
216 onu, err := o.Olt.FindOnuBySn(common.OnuSnToString(onuInd.SerialNumber))
217
218 if err != nil {
219 log.WithFields(log.Fields{
220 "IntfId": onuInd.IntfId,
221 "SerialNumber": common.OnuSnToString(onuInd.SerialNumber),
222 }).Fatal("Cannot find ONU")
223 }
224
David Bainbridge103cf022019-12-16 20:11:35 +0000225 ctx, cancel := context.WithCancel(context.TODO())
226 go onu.ProcessOnuMessages(ctx, nil, client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700227
228 go func() {
229
230 defer func() {
231 log.WithFields(log.Fields{
232 "onuSn": common.OnuSnToString(onuInd.SerialNumber),
233 "CompletedOnus": o.CompletedOnus,
234 "TargetOnus": o.TargetOnus,
235 }).Debugf("Onu done")
236
Matteo Scandolo569e7172019-12-20 11:51:51 -0800237 // close the ONU channel
238 cancel()
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700239 }()
240
241 for message := range onu.DoneChannel {
Shrey Baid688b4242020-07-10 20:40:10 +0530242 if message {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700243 o.CompletedOnus++
244 if o.CompletedOnus == o.TargetOnus {
245 // NOTE once all the ONUs are completed, exit
246 // closing the connection is not the most elegant way,
247 // but I haven't found any other way to stop
248 // the indications.Recv() infinite loop
249 log.Info("Simulation Done")
250 ValidateAndClose(o)
251 }
252
253 break
254 }
255 }
256
257 }()
258
259 // TODO change the state instead of calling an ONU method from here
260 onu.StartOmci(client)
261}
262
263func (o *OltMock) handleOmciIndication(client openolt.OpenoltClient, omciInd *openolt.OmciIndication) {
264
265 pon, err := o.Olt.GetPonById(omciInd.IntfId)
266 if err != nil {
267 log.WithFields(log.Fields{
268 "OnuId": omciInd.OnuId,
269 "IntfId": omciInd.IntfId,
270 "err": err,
271 }).Fatal("Can't find PonPort")
272 }
273 onu, _ := pon.GetOnuById(omciInd.OnuId)
274 if err != nil {
275 log.WithFields(log.Fields{
276 "OnuId": omciInd.OnuId,
277 "IntfId": omciInd.IntfId,
278 "err": err,
279 }).Fatal("Can't find Onu")
280 }
281
282 log.WithFields(log.Fields{
283 "IntfId": onu.PonPortID,
284 "OnuId": onu.ID,
285 "OnuSn": onu.Sn(),
286 "Pkt": omciInd.Pkt,
287 }).Trace("Received Onu omci indication")
288
289 msg := devices.Message{
290 Type: devices.OmciIndication,
291 Data: devices.OmciIndicationMessage{
292 OnuSN: onu.SerialNumber,
293 OnuID: onu.ID,
294 OmciInd: omciInd,
295 },
296 }
297 onu.Channel <- msg
298}
299
300func (o *OltMock) handlePktIndication(client openolt.OpenoltClient, pktIndication *openolt.PacketIndication) {
301
302 pkt := gopacket.NewPacket(pktIndication.Pkt, layers.LayerTypeEthernet, gopacket.Default)
303
304 pktType, err := packetHandlers.IsEapolOrDhcp(pkt)
305
306 if err != nil {
307 log.Warnf("Ignoring packet as it's neither EAPOL or DHCP")
308 return
309 }
310
311 log.WithFields(log.Fields{
312 "IntfType": pktIndication.IntfType,
313 "IntfId": pktIndication.IntfId,
314 "GemportId": pktIndication.GemportId,
315 "FlowId": pktIndication.FlowId,
316 "PortNo": pktIndication.PortNo,
317 "Cookie": pktIndication.Cookie,
318 "pktType": pktType,
319 }).Trace("Received PktIndication")
320
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700321 if pktIndication.IntfType == "nni" {
322 // This is an packet that is arriving from the NNI and needs to be sent to an ONU
323 // in this case we need to fin the ONU from the C/S tags
324 // TODO: handle errors in the untagging process
325 sTag, _ := packetHandlers.GetVlanTag(pkt)
326 singleTagPkt, _ := packetHandlers.PopSingleTag(pkt)
327 cTag, _ := packetHandlers.GetVlanTag(singleTagPkt)
328
329 onu, err := o.getOnuByTags(int(sTag), int(cTag))
330
331 if err != nil {
332 log.WithFields(log.Fields{
333 "sTag": sTag,
334 "cTag": cTag,
335 }).Fatalf("Can't find ONU from c/s tags")
336 }
337
Shrey Baid688b4242020-07-10 20:40:10 +0530338 msg := devices.Message{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700339 Type: devices.OnuPacketIn,
340 Data: devices.OnuPacketMessage{
341 IntfId: pktIndication.IntfId,
342 OnuId: onu.ID,
343 Packet: pkt,
344 Type: pktType,
345 },
346 }
347 // NOTE we send it on the ONU channel so that is handled as all the others packets in a separate thread
348 onu.Channel <- msg
349 } else {
350 // TODO a very similar construct is used in many places,
351 // abstract this in an OLT method
352 pon, err := o.Olt.GetPonById(pktIndication.IntfId)
353 if err != nil {
354 log.WithFields(log.Fields{
355 "OnuId": pktIndication.PortNo,
356 "IntfId": pktIndication.IntfId,
357 "err": err,
358 }).Fatal("Can't find PonPort")
359 }
360 onu, err := pon.GetOnuById(pktIndication.PortNo)
361 if err != nil {
362 log.WithFields(log.Fields{
363 "OnuId": pktIndication.PortNo,
364 "IntfId": pktIndication.IntfId,
365 "err": err,
366 }).Fatal("Can't find Onu")
367 }
368 // NOTE when we push the EAPOL flow we set the PortNo = OnuId for convenience sake
369 // BBsim responds setting the port number that was sent with the flow
Shrey Baid688b4242020-07-10 20:40:10 +0530370 msg := devices.Message{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700371 Type: devices.OnuPacketIn,
372 Data: devices.OnuPacketMessage{
373 IntfId: pktIndication.IntfId,
374 OnuId: pktIndication.PortNo,
375 Packet: pkt,
376 Type: pktType,
377 },
378 }
379 onu.Channel <- msg
380 }
381}
382
383// TODO Move in a different file
384func Connect(ip string, port string) (openolt.OpenoltClient, *grpc.ClientConn) {
385 server := fmt.Sprintf("%s:%s", ip, port)
386 conn, err := grpc.Dial(server, grpc.WithInsecure())
387
388 if err != nil {
389 log.Fatalf("did not connect: %v", err)
390 return nil, conn
391 }
392 return openolt.NewOpenoltClient(conn), conn
393}