blob: 24b541b7a2810bc3467212bf92d299697d1d5fff [file] [log] [blame]
Matteo Scandolo86e8ce62019-10-11 12:03:10 -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 "bytes"
21 "errors"
22 "fmt"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020023
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070024 "github.com/looplab/fsm"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080025 "github.com/opencord/voltha-protos/v2/go/openolt"
Pragya Arya6a708d62020-01-01 17:17:20 +053026 log "github.com/sirupsen/logrus"
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070027)
28
29type PonPort struct {
30 // BBSIM Internals
31 ID uint32
32 NumOnu int
Matteo Scandolo27428702019-10-11 16:21:16 -070033 Onus []*Onu
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070034 Olt OltDevice
35
36 // PON Attributes
37 OperState *fsm.FSM
38 Type string
39
40 // NOTE do we need a state machine for the PON Ports?
41}
42
Pragya Arya6a708d62020-01-01 17:17:20 +053043// CreatePonPort creates pon port object
44func CreatePonPort(olt OltDevice, id uint32) *PonPort {
45
46 ponPort := PonPort{
47 NumOnu: olt.NumOnuPerPon,
48 ID: id,
49 Type: "pon",
50 Olt: olt,
51 Onus: []*Onu{},
52 }
53
54 ponPort.OperState = fsm.NewFSM(
55 "down",
56 fsm.Events{
57 {Name: "enable", Src: []string{"down"}, Dst: "up"},
58 {Name: "disable", Src: []string{"up"}, Dst: "down"},
59 },
60 fsm.Callbacks{
61 "enter_up": func(e *fsm.Event) {
62 oltLogger.WithFields(log.Fields{
63 "ID": ponPort.ID,
64 }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst)
65 },
66 "enter_down": func(e *fsm.Event) {
67 oltLogger.WithFields(log.Fields{
68 "ID": ponPort.ID,
69 }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst)
70
71 for _, onu := range ponPort.Onus {
72 if err := onu.InternalState.Event("pon_disabled"); err != nil {
73 oltLogger.Errorf("Failed to move ONU in pon_disabled states: %v", err)
74 }
75 }
76 },
77 },
78 )
79 return &ponPort
80}
81
Matteo Scandolo40e067f2019-10-16 16:59:41 -070082func (p PonPort) GetOnuBySn(sn *openolt.SerialNumber) (*Onu, error) {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070083 for _, onu := range p.Onus {
84 if bytes.Equal(onu.SerialNumber.VendorSpecific, sn.VendorSpecific) {
Matteo Scandolo27428702019-10-11 16:21:16 -070085 return onu, nil
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070086 }
87 }
88 return nil, errors.New(fmt.Sprintf("Cannot find Onu with serial number %d in PonPort %d", sn, p.ID))
89}
90
Matteo Scandolo40e067f2019-10-16 16:59:41 -070091func (p PonPort) GetOnuById(id uint32) (*Onu, error) {
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070092 for _, onu := range p.Onus {
93 if onu.ID == id {
Matteo Scandolo27428702019-10-11 16:21:16 -070094 return onu, nil
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070095 }
96 }
97 return nil, errors.New(fmt.Sprintf("Cannot find Onu with id %d in PonPort %d", id, p.ID))
98}