Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "errors" |
| 22 | "fmt" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 23 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 24 | "github.com/looplab/fsm" |
Matteo Scandolo | 3de9de0 | 2019-11-14 13:40:03 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v2/go/openolt" |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 26 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type PonPort struct { |
| 30 | // BBSIM Internals |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 31 | ID uint32 |
| 32 | NumOnu int |
| 33 | Onus []*Onu |
| 34 | Olt *OltDevice |
| 35 | PacketCount uint64 |
| 36 | InternalState *fsm.FSM |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 37 | |
| 38 | // PON Attributes |
| 39 | OperState *fsm.FSM |
| 40 | Type string |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 43 | // CreatePonPort creates pon port object |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 44 | func CreatePonPort(olt *OltDevice, id uint32) *PonPort { |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 45 | |
| 46 | ponPort := PonPort{ |
| 47 | NumOnu: olt.NumOnuPerPon, |
| 48 | ID: id, |
| 49 | Type: "pon", |
| 50 | Olt: olt, |
| 51 | Onus: []*Onu{}, |
| 52 | } |
| 53 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 54 | ponPort.InternalState = fsm.NewFSM( |
| 55 | "created", |
| 56 | fsm.Events{ |
| 57 | {Name: "enable", Src: []string{"created", "disabled"}, Dst: "enabled"}, |
| 58 | {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"}, |
| 59 | }, |
| 60 | fsm.Callbacks{ |
| 61 | "enter_enabled": func(e *fsm.Event) { |
| 62 | oltLogger.WithFields(log.Fields{ |
| 63 | "ID": ponPort.ID, |
| 64 | }).Debugf("Changing PON Port InternalState from %s to %s", e.Src, e.Dst) |
| 65 | |
| 66 | if e.Src == "created" { |
| 67 | if olt.ControlledActivation == Default || olt.ControlledActivation == OnlyPON { |
| 68 | for _, onu := range ponPort.Onus { |
| 69 | if err := onu.InternalState.Event("initialize"); err != nil { |
| 70 | log.Errorf("Error initializing ONU: %v", err) |
| 71 | continue |
| 72 | } |
| 73 | if err := onu.InternalState.Event("discover"); err != nil { |
| 74 | log.Errorf("Error discover ONU: %v", err) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } else if e.Src == "disabled" { |
| 79 | for _, onu := range ponPort.Onus { |
| 80 | if onu.InternalState.Current() == "pon_disabled" { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 81 | if err := onu.InternalState.Event("enable"); err != nil { |
| 82 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame^] | 83 | "Err": err, |
| 84 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 85 | "IntfId": onu.PonPortID, |
| 86 | }).Error("Error enabling ONU") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 87 | } |
| 88 | } else if onu.InternalState.Current() == "disabled" { |
| 89 | if err := onu.InternalState.Event("initialize"); err != nil { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 90 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame^] | 91 | "Err": err, |
| 92 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 93 | "IntfId": onu.PonPortID, |
| 94 | }).Error("Error initializing ONU") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 95 | continue |
| 96 | } |
| 97 | if err := onu.InternalState.Event("discover"); err != nil { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 98 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame^] | 99 | "Err": err, |
| 100 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 101 | "IntfId": onu.PonPortID, |
| 102 | }).Error("Error discovering ONU") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 103 | } |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 104 | } else if onu.InternalState.Current() == "initialized" { |
| 105 | if err := onu.InternalState.Event("discover"); err != nil { |
| 106 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame^] | 107 | "Err": err, |
| 108 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 109 | "IntfId": onu.PonPortID, |
| 110 | }).Error("Error discovering ONU") |
| 111 | } |
| 112 | } else { |
| 113 | // this is to loudly report unexpected states in order to address them |
| 114 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame^] | 115 | "OnuSn": onu.Sn(), |
| 116 | "IntfId": onu.PonPortID, |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 117 | "InternalState": onu.InternalState.Current(), |
| 118 | }).Error("Unexpected ONU state in PON enabling") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | }, |
| 123 | "enter_disabled": func(e *fsm.Event) { |
| 124 | for _, onu := range ponPort.Onus { |
| 125 | if onu.InternalState.Current() == "initialized" { |
| 126 | continue |
| 127 | } |
| 128 | if err := onu.InternalState.Event("pon_disabled"); err != nil { |
| 129 | oltLogger.Errorf("Failed to move ONU in pon_disabled states: %v", err) |
| 130 | } |
| 131 | } |
| 132 | }, |
| 133 | }, |
| 134 | ) |
| 135 | |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 136 | ponPort.OperState = fsm.NewFSM( |
| 137 | "down", |
| 138 | fsm.Events{ |
| 139 | {Name: "enable", Src: []string{"down"}, Dst: "up"}, |
| 140 | {Name: "disable", Src: []string{"up"}, Dst: "down"}, |
| 141 | }, |
| 142 | fsm.Callbacks{ |
| 143 | "enter_up": func(e *fsm.Event) { |
| 144 | oltLogger.WithFields(log.Fields{ |
| 145 | "ID": ponPort.ID, |
| 146 | }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst) |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 147 | olt.sendPonIndication(ponPort.ID) |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 148 | }, |
| 149 | "enter_down": func(e *fsm.Event) { |
| 150 | oltLogger.WithFields(log.Fields{ |
| 151 | "ID": ponPort.ID, |
| 152 | }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst) |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 153 | olt.sendPonIndication(ponPort.ID) |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 154 | }, |
| 155 | }, |
| 156 | ) |
| 157 | return &ponPort |
| 158 | } |
| 159 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 160 | func (p PonPort) GetOnuBySn(sn *openolt.SerialNumber) (*Onu, error) { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 161 | for _, onu := range p.Onus { |
| 162 | if bytes.Equal(onu.SerialNumber.VendorSpecific, sn.VendorSpecific) { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 163 | return onu, nil |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | return nil, errors.New(fmt.Sprintf("Cannot find Onu with serial number %d in PonPort %d", sn, p.ID)) |
| 167 | } |
| 168 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 169 | func (p PonPort) GetOnuById(id uint32) (*Onu, error) { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 170 | for _, onu := range p.Onus { |
| 171 | if onu.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 172 | return onu, nil |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | return nil, errors.New(fmt.Sprintf("Cannot find Onu with id %d in PonPort %d", id, p.ID)) |
| 176 | } |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 177 | |
| 178 | // GetNumOfActiveOnus returns number of active ONUs for PON port |
| 179 | func (p PonPort) GetNumOfActiveOnus() uint32 { |
| 180 | var count uint32 = 0 |
| 181 | for _, onu := range p.Onus { |
| 182 | if onu.InternalState.Current() == "initialized" || onu.InternalState.Current() == "created" || onu.InternalState.Current() == "disabled" { |
| 183 | continue |
| 184 | } |
| 185 | count++ |
| 186 | } |
| 187 | return count |
| 188 | } |