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