blob: c75f2d1763f9e10fbd8098cb162db1b57e3c0180 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29: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
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
20 "context"
21 "errors"
22 "fmt"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070023 "github.com/google/gopacket"
24 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070025 "github.com/looplab/fsm"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070026 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070027 "github.com/opencord/voltha-protos/go/openolt"
Matteo Scandolod54283a2019-08-13 16:22:31 -070028 "github.com/opencord/voltha-protos/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070029 log "github.com/sirupsen/logrus"
30 "google.golang.org/grpc"
31 "net"
32 "sync"
33)
34
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070035var oltLogger = log.WithFields(log.Fields{
Matteo Scandolo84f7d482019-08-08 19:00:47 -070036 "module": "OLT",
37})
38
Matteo Scandolo4747d292019-08-05 11:50:18 -070039func init() {
40 //log.SetReportCaller(true)
41 log.SetLevel(log.DebugLevel)
42}
43
Matteo Scandolo84f7d482019-08-08 19:00:47 -070044var olt = OltDevice{}
45
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070046func GetOLT() OltDevice {
Matteo Scandolo84f7d482019-08-08 19:00:47 -070047 return olt
48}
49
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070050func CreateOLT(seq int, nni int, pon int, onuPerPon int, sTag int, cTagInit int, oltDoneChannel *chan bool, apiDoneChannel *chan bool, group *sync.WaitGroup) OltDevice {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070051 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070052 "ID": seq,
53 "NumNni": nni,
54 "NumPon": pon,
55 "NumOnuPerPon": onuPerPon,
Matteo Scandolo4747d292019-08-05 11:50:18 -070056 }).Debug("CreateOLT")
57
Matteo Scandolo84f7d482019-08-08 19:00:47 -070058 olt = OltDevice{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070059 ID: seq,
Matteo Scandolo8df63df2019-09-12 10:34:32 -070060 SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", seq),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070061 OperState: getOperStateFSM(func(e *fsm.Event) {
62 oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst)
63 }),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070064 NumNni: nni,
65 NumPon: pon,
66 NumOnuPerPon: onuPerPon,
67 Pons: []PonPort{},
68 Nnis: []NniPort{},
69 channel: make(chan Message),
70 oltDoneChannel: oltDoneChannel,
71 apiDoneChannel: apiDoneChannel,
72 nniPktInChannel: make(chan *bbsim.PacketMsg, 1024), // packets coming in from the NNI and going to VOLTHA
Matteo Scandolo4747d292019-08-05 11:50:18 -070073 }
74
75 // OLT State machine
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070076 // NOTE do we need 2 state machines for the OLT? (InternalState and OperState)
Matteo Scandolo4747d292019-08-05 11:50:18 -070077 olt.InternalState = fsm.NewFSM(
78 "created",
79 fsm.Events{
80 {Name: "enable", Src: []string{"created"}, Dst: "enabled"},
81 {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"},
82 },
83 fsm.Callbacks{
84 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070085 oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -070086 },
87 },
88 )
89
90 // create NNI Port
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070091 nniPort, err := CreateNNI(&olt)
92
93 if err != nil {
94 oltLogger.Fatalf("Couldn't create NNI Port: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -070095 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070096
Matteo Scandolo4747d292019-08-05 11:50:18 -070097 olt.Nnis = append(olt.Nnis, nniPort)
98
99 // create PON ports
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700100 availableCTag := cTagInit
Matteo Scandolo4747d292019-08-05 11:50:18 -0700101 for i := 0; i < pon; i++ {
102 p := PonPort{
103 NumOnu: olt.NumOnuPerPon,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700104 ID: uint32(i),
105 Type: "pon",
Matteo Scandolo4747d292019-08-05 11:50:18 -0700106 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700107 p.OperState = getOperStateFSM(func(e *fsm.Event) {
108 oltLogger.WithFields(log.Fields{
109 "ID": p.ID,
110 }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst)
111 })
Matteo Scandolo4747d292019-08-05 11:50:18 -0700112
113 // create ONU devices
114 for j := 0; j < onuPerPon; j++ {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700115 //o := CreateONU(olt, p, uint32(onuId))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700116 o := CreateONU(olt, p, uint32(j+1), sTag, availableCTag)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700117 p.Onus = append(p.Onus, o)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700118 availableCTag = availableCTag + 1
Matteo Scandolo4747d292019-08-05 11:50:18 -0700119 }
120
121 olt.Pons = append(olt.Pons, p)
122 }
123
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700124 newOltServer(olt)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700125
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700126 group.Done()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700127 return olt
128}
129
130func newOltServer(o OltDevice) error {
131 // TODO make configurable
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700132 address := "0.0.0.0:50060"
Matteo Scandolo4747d292019-08-05 11:50:18 -0700133 lis, err := net.Listen("tcp", address)
134 if err != nil {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700135 oltLogger.Fatalf("OLT failed to listen: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700136 }
137 grpcServer := grpc.NewServer()
138 openolt.RegisterOpenoltServer(grpcServer, o)
139
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700140 wg := sync.WaitGroup{}
141 wg.Add(1)
142
Matteo Scandolo4747d292019-08-05 11:50:18 -0700143 go grpcServer.Serve(lis)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700144 oltLogger.Debugf("OLT Listening on: %v", address)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700145
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700146 for {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700147 _, ok := <-*o.oltDoneChannel
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700148 if !ok {
149 // if the olt channel is closed, stop the gRPC server
150 log.Warnf("Stopping OLT gRPC server")
151 grpcServer.Stop()
152 wg.Done()
153 break
154 }
155 }
156
157 wg.Wait()
158
Matteo Scandolo4747d292019-08-05 11:50:18 -0700159 return nil
160}
161
162// Device Methods
163
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700164func (o OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700165
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700166 oltLogger.Debug("Enable OLT called")
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700167
Matteo Scandolo4747d292019-08-05 11:50:18 -0700168 wg := sync.WaitGroup{}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700169 wg.Add(2)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700170
171 // create a channel for all the OLT events
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700172 go o.processOltMessages(stream)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700173 go o.processNniPacketIns(stream)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700174
175 // enable the OLT
176 olt_msg := Message{
177 Type: OltIndication,
178 Data: OltIndicationMessage{
179 OperState: UP,
180 },
181 }
182 o.channel <- olt_msg
183
184 // send NNI Port Indications
185 for _, nni := range o.Nnis {
186 msg := Message{
187 Type: NniIndication,
188 Data: NniIndicationMessage{
189 OperState: UP,
190 NniPortID: nni.ID,
191 },
192 }
193 o.channel <- msg
194 }
195
196 // send PON Port indications
197 for _, pon := range o.Pons {
198 msg := Message{
199 Type: PonIndication,
200 Data: PonIndicationMessage{
201 OperState: UP,
202 PonPortID: pon.ID,
203 },
204 }
205 o.channel <- msg
206
207 for _, onu := range pon.Onus {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700208 go onu.processOnuMessages(stream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700209 go onu.processOmciMessages(stream)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700210 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700211 Type: OnuDiscIndication,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700212 Data: OnuDiscIndicationMessage{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700213 Onu: onu,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700214 OperState: UP,
215 },
216 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700217 onu.channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700218 }
219 }
220
221 wg.Wait()
222 return nil
223}
224
225// Helpers method
226
227func (o OltDevice) getPonById(id uint32) (*PonPort, error) {
228 for _, pon := range o.Pons {
229 if pon.ID == id {
230 return &pon, nil
231 }
232 }
233 return nil, errors.New(fmt.Sprintf("Cannot find PonPort with id %d in OLT %d", id, o.ID))
234}
235
236func (o OltDevice) getNniById(id uint32) (*NniPort, error) {
237 for _, nni := range o.Nnis {
238 if nni.ID == id {
239 return &nni, nil
240 }
241 }
242 return nil, errors.New(fmt.Sprintf("Cannot find NniPort with id %d in OLT %d", id, o.ID))
243}
244
Matteo Scandolo4747d292019-08-05 11:50:18 -0700245func (o OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
246 data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
247 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700248 oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700249 }
250
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700251 oltLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700252 "OperState": msg.OperState,
253 }).Debug("Sent Indication_OltInd")
254}
255
256func (o OltDevice) sendNniIndication(msg NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
257 nni, _ := o.getNniById(msg.NniPortID)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700258 nni.OperState.Event("enable")
259 // NOTE Operstate may need to be an integer
Matteo Scandolo4747d292019-08-05 11:50:18 -0700260 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700261 Type: nni.Type,
262 IntfId: nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700263 OperState: nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700264 }}
265
266 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700267 oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700268 }
269
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700270 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700271 "Type": nni.Type,
272 "IntfId": nni.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700273 "OperState": nni.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700274 }).Debug("Sent Indication_IntfOperInd for NNI")
275}
276
277func (o OltDevice) sendPonIndication(msg PonIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
278 pon, _ := o.getPonById(msg.PonPortID)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700279 pon.OperState.Event("enable")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700280 discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700281 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700282 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700283 }}
284
285 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700286 oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700287 }
288
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700289 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700290 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700291 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700292 }).Debug("Sent Indication_IntfInd")
293
294 operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700295 Type: pon.Type,
296 IntfId: pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700297 OperState: pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700298 }}
299
300 if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700301 oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700302 }
303
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700304 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700305 "Type": pon.Type,
306 "IntfId": pon.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700307 "OperState": pon.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700308 }).Debug("Sent Indication_IntfOperInd for PON")
309}
310
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700311func (o OltDevice) processOltMessages(stream openolt.Openolt_EnableIndicationServer) {
312 oltLogger.Debug("Started OLT Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700313 for message := range o.channel {
314
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700315 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700316 "oltId": o.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700317 "messageType": message.Type,
318 }).Trace("Received message")
319
320 switch message.Type {
321 case OltIndication:
322 msg, _ := message.Data.(OltIndicationMessage)
323 if msg.OperState == UP {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700324 o.InternalState.Event("enable")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700325 o.OperState.Event("enable")
326 } else if msg.OperState == DOWN {
327 o.InternalState.Event("disable")
328 o.OperState.Event("disable")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700329 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700330 o.sendOltIndication(msg, stream)
331 case NniIndication:
332 msg, _ := message.Data.(NniIndicationMessage)
333 o.sendNniIndication(msg, stream)
334 case PonIndication:
335 msg, _ := message.Data.(PonIndicationMessage)
336 o.sendPonIndication(msg, stream)
337 default:
338 oltLogger.Warnf("Received unknown message data %v for type %v in OLT channel", message.Data, message.Type)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700339 }
340
341 }
342}
343
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700344func (o OltDevice) processNniPacketIns(stream openolt.Openolt_EnableIndicationServer) {
345 oltLogger.WithFields(log.Fields{
346 "nniChannel": o.nniPktInChannel,
347 }).Debug("Started NNI Channel")
348 nniId := o.Nnis[0].ID // FIXME we are assuming we have only one NNI
349 for message := range o.nniPktInChannel {
350 oltLogger.Debug("Received packets on NNI Channel")
351 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
352 IntfType: "nni",
353 IntfId: nniId,
354 Pkt: message.Pkt.Data()}}
355 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
356 oltLogger.WithFields(log.Fields{
357 "IntfType": data.PktInd.IntfType,
358 "IntfId": nniId,
359 "Pkt": message.Pkt.Data(),
360 }).Errorf("Fail to send PktInd indication: %v", err)
361 }
362 }
363}
364
Matteo Scandolo4747d292019-08-05 11:50:18 -0700365// GRPC Endpoints
366
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700367func (o OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700368 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700369 "OnuSn": onuSnToString(onu.SerialNumber),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700370 }).Info("Received ActivateOnu call from VOLTHA")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700371
372 pon, _ := o.getPonById(onu.IntfId)
373 _onu, _ := pon.getOnuBySn(onu.SerialNumber)
374
375 // NOTE we need to immediately activate the ONU or the OMCI state machine won't start
Matteo Scandolo4747d292019-08-05 11:50:18 -0700376 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700377 Type: OnuIndication,
378 Data: OnuIndicationMessage{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700379 OnuSN: onu.SerialNumber,
380 PonPortID: onu.IntfId,
381 OperState: UP,
382 },
383 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700384 _onu.channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700385 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700386}
387
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700388func (o OltDevice) DeactivateOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700389 oltLogger.Error("DeactivateOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700390 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700391}
392
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700393func (o OltDevice) DeleteOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700394 oltLogger.Error("DeleteOnu not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700395 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700396}
397
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700398func (o OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700399 // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether?
400 olt_msg := Message{
401 Type: OltIndication,
402 Data: OltIndicationMessage{
403 OperState: DOWN,
404 },
405 }
406 o.channel <- olt_msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700407 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700408}
409
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700410func (o OltDevice) DisablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700411 oltLogger.Error("DisablePonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700412 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700413}
414
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700415func (o OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700416 oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700417 o.Enable(stream)
418 return nil
419}
420
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700421func (o OltDevice) EnablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700422 oltLogger.Error("EnablePonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700423 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700424}
425
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700426func (o OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700427 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700428 "IntfId": flow.AccessIntfId,
429 "OnuId": flow.OnuId,
430 "EthType": fmt.Sprintf("%x", flow.Classifier.EthType),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700431 "InnerVlan": flow.Classifier.IVid,
432 "OuterVlan": flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700433 "FlowType": flow.FlowType,
434 "FlowId": flow.FlowId,
435 "UniID": flow.UniId,
436 "PortNo": flow.PortNo,
437 }).Tracef("OLT receives Flow")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700438 // TODO optionally store flows somewhere
439
440 if flow.AccessIntfId == -1 {
441 oltLogger.WithFields(log.Fields{
442 "FlowId": flow.FlowId,
443 }).Debugf("This is an OLT flow")
444 } else {
445 pon, _ := o.getPonById(uint32(flow.AccessIntfId))
446 onu, _ := pon.getOnuById(uint32(flow.OnuId))
447
448 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700449 Type: FlowUpdate,
450 Data: OnuFlowUpdateMessage{
451 PonPortID: pon.ID,
452 OnuID: onu.ID,
453 Flow: flow,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700454 },
455 }
456 onu.channel <- msg
457 }
458
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700459 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700460}
461
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700462func (o OltDevice) FlowRemove(context.Context, *openolt.Flow) (*openolt.Empty, error) {
463 oltLogger.Tracef("received FlowRemove")
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700464 // TODO store flows somewhere
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700465 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700466}
467
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700468func (o OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700469 oltLogger.Error("HeartbeatCheck not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700470 return new(openolt.Heartbeat), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700471}
472
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700473func (o OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700474
Matteo Scandoloda9cbe22019-08-19 16:05:10 -0700475 oltLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700476 "oltId": o.ID,
Matteo Scandoloda9cbe22019-08-19 16:05:10 -0700477 "PonPorts": o.NumPon,
478 }).Info("OLT receives GetDeviceInfo call from VOLTHA")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700479 devinfo := new(openolt.DeviceInfo)
480 devinfo.Vendor = "BBSim"
481 devinfo.Model = "asfvolt16"
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700482 devinfo.HardwareVersion = "emulated"
Matteo Scandolo4747d292019-08-05 11:50:18 -0700483 devinfo.FirmwareVersion = ""
484 devinfo.Technology = "xgspon"
Matteo Scandoloda9cbe22019-08-19 16:05:10 -0700485 devinfo.PonPorts = uint32(o.NumPon)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700486 devinfo.OnuIdStart = 1
487 devinfo.OnuIdEnd = 255
488 devinfo.AllocIdStart = 1024
489 devinfo.AllocIdEnd = 16383
490 devinfo.GemportIdStart = 1024
491 devinfo.GemportIdEnd = 65535
492 devinfo.FlowIdStart = 1
493 devinfo.FlowIdEnd = 16383
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700494 devinfo.DeviceSerialNumber = o.SerialNumber
Matteo Scandolo4747d292019-08-05 11:50:18 -0700495
496 return devinfo, nil
497}
498
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700499func (o OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700500 pon, _ := o.getPonById(omci_msg.IntfId)
501 onu, _ := pon.getOnuById(omci_msg.OnuId)
502 msg := Message{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700503 Type: OMCI,
504 Data: OmciMessage{
505 OnuSN: onu.SerialNumber,
506 OnuID: onu.ID,
507 omciMsg: omci_msg,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700508 },
509 }
510 onu.channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700511 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700512}
513
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700514func (o OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700515 pon, _ := o.getPonById(onuPkt.IntfId)
516 onu, _ := pon.getOnuById(onuPkt.OnuId)
517
518 rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default)
519
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700520 etherType := rawpkt.Layer(layers.LayerTypeEthernet).(*layers.Ethernet).EthernetType
521
522 if etherType == layers.EthernetTypeEAPOL {
523 eapolPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()}
524 onu.eapolPktOutCh <- &eapolPkt
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700525 } else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
526 // TODO use IsDhcpPacket
527 // TODO we need to untag the packets
528 // NOTE here we receive packets going from the DHCP Server to the ONU
529 // for now we expect them to be double-tagged, but ideally the should be single tagged
530 dhcpPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()}
531 onu.dhcpPktOutCh <- &dhcpPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700532 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700533 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700534}
535
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700536func (o OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700537 oltLogger.Info("Shutting Down")
538 close(*o.oltDoneChannel)
539 close(*o.apiDoneChannel)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700540 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700541}
542
543func (o OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700544 oltLogger.Error("ReenableOlt not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700545 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700546}
547
548func (o OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700549 pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
550
551 sendNniPacket(pkt)
552 // NOTE should we return an error if sendNniPakcet fails?
553 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700554}
555
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700556func (o OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700557 oltLogger.Error("CollectStatistics not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700558 return new(openolt.Empty), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700559}
560
Matteo Scandolo4747d292019-08-05 11:50:18 -0700561func (o OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700562 oltLogger.Error("GetOnuInfo not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700563 return new(openolt.OnuIndication), nil
Matteo Scandolo4747d292019-08-05 11:50:18 -0700564}
565
566func (o OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700567 oltLogger.Error("GetPonIf not implemented")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700568 return new(openolt.IntfIndication), nil
Matteo Scandolod54283a2019-08-13 16:22:31 -0700569}
570
571func (s OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700572 oltLogger.Info("received CreateTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -0700573 return new(openolt.Empty), nil
574}
575
576func (s OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700577 oltLogger.Info("received RemoveTrafficQueues")
Matteo Scandolod54283a2019-08-13 16:22:31 -0700578 return new(openolt.Empty), nil
579}
580
581func (s OltDevice) CreateTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700582 oltLogger.Info("received CreateTrafficSchedulers")
Matteo Scandolod54283a2019-08-13 16:22:31 -0700583 return new(openolt.Empty), nil
584}
585
586func (s OltDevice) RemoveTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700587 oltLogger.Info("received RemoveTrafficSchedulers")
Matteo Scandolod54283a2019-08-13 16:22:31 -0700588 return new(openolt.Empty), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700589}