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 | 3de9de0 | 2019-11-14 13:40:03 -0800 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v2/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" { |
| 78 | for _, onu := range ponPort.Onus { |
| 79 | if onu.InternalState.Current() == "pon_disabled" { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 80 | if err := onu.InternalState.Event("enable"); err != nil { |
| 81 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 82 | "Err": err, |
| 83 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 84 | "IntfId": onu.PonPortID, |
| 85 | }).Error("Error enabling ONU") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 86 | } |
| 87 | } else if onu.InternalState.Current() == "disabled" { |
| 88 | if err := onu.InternalState.Event("initialize"); err != nil { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 89 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 90 | "Err": err, |
| 91 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 92 | "IntfId": onu.PonPortID, |
| 93 | }).Error("Error initializing ONU") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 94 | continue |
| 95 | } |
| 96 | if err := onu.InternalState.Event("discover"); err != nil { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 97 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 98 | "Err": err, |
| 99 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 100 | "IntfId": onu.PonPortID, |
| 101 | }).Error("Error discovering ONU") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 102 | } |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 103 | } else if onu.InternalState.Current() == "initialized" { |
| 104 | if err := onu.InternalState.Event("discover"); err != nil { |
| 105 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 106 | "Err": err, |
| 107 | "OnuSn": onu.Sn(), |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 108 | "IntfId": onu.PonPortID, |
| 109 | }).Error("Error discovering ONU") |
| 110 | } |
| 111 | } else { |
| 112 | // this is to loudly report unexpected states in order to address them |
| 113 | log.WithFields(log.Fields{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 114 | "OnuSn": onu.Sn(), |
| 115 | "IntfId": onu.PonPortID, |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 116 | "InternalState": onu.InternalState.Current(), |
| 117 | }).Error("Unexpected ONU state in PON enabling") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | }, |
| 122 | "enter_disabled": func(e *fsm.Event) { |
| 123 | for _, onu := range ponPort.Onus { |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame^] | 124 | if onu.InternalState.Current() == "initialized" || onu.InternalState.Current() == "disabled" { |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 125 | continue |
| 126 | } |
| 127 | if err := onu.InternalState.Event("pon_disabled"); err != nil { |
| 128 | oltLogger.Errorf("Failed to move ONU in pon_disabled states: %v", err) |
| 129 | } |
| 130 | } |
| 131 | }, |
| 132 | }, |
| 133 | ) |
| 134 | |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 135 | ponPort.OperState = fsm.NewFSM( |
| 136 | "down", |
| 137 | fsm.Events{ |
| 138 | {Name: "enable", Src: []string{"down"}, Dst: "up"}, |
| 139 | {Name: "disable", Src: []string{"up"}, Dst: "down"}, |
| 140 | }, |
| 141 | fsm.Callbacks{ |
| 142 | "enter_up": func(e *fsm.Event) { |
| 143 | oltLogger.WithFields(log.Fields{ |
| 144 | "ID": ponPort.ID, |
| 145 | }).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] | 146 | olt.sendPonIndication(ponPort.ID) |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 147 | }, |
| 148 | "enter_down": func(e *fsm.Event) { |
| 149 | oltLogger.WithFields(log.Fields{ |
| 150 | "ID": ponPort.ID, |
| 151 | }).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] | 152 | olt.sendPonIndication(ponPort.ID) |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 153 | }, |
| 154 | }, |
| 155 | ) |
| 156 | return &ponPort |
| 157 | } |
| 158 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 159 | func (p PonPort) GetOnuBySn(sn *openolt.SerialNumber) (*Onu, error) { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 160 | for _, onu := range p.Onus { |
| 161 | if bytes.Equal(onu.SerialNumber.VendorSpecific, sn.VendorSpecific) { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 162 | return onu, nil |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 165 | 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] | 166 | } |
| 167 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 168 | func (p PonPort) GetOnuById(id uint32) (*Onu, error) { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 169 | for _, onu := range p.Onus { |
| 170 | if onu.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 171 | return onu, nil |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 174 | 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] | 175 | } |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 176 | |
| 177 | // GetNumOfActiveOnus returns number of active ONUs for PON port |
| 178 | func (p PonPort) GetNumOfActiveOnus() uint32 { |
| 179 | var count uint32 = 0 |
| 180 | for _, onu := range p.Onus { |
| 181 | if onu.InternalState.Current() == "initialized" || onu.InternalState.Current() == "created" || onu.InternalState.Current() == "disabled" { |
| 182 | continue |
| 183 | } |
| 184 | count++ |
| 185 | } |
| 186 | return count |
| 187 | } |