Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -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 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | package devices |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 21 | "encoding/hex" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 22 | "fmt" |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 23 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 24 | "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 25 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v4/go/ext/config" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 27 | "net" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 28 | "strconv" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 29 | "sync" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 30 | "time" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 31 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 32 | "github.com/google/gopacket" |
| 33 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 34 | "github.com/looplab/fsm" |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 35 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 36 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 37 | common_protos "github.com/opencord/voltha-protos/v4/go/common" |
| 38 | "github.com/opencord/voltha-protos/v4/go/openolt" |
| 39 | "github.com/opencord/voltha-protos/v4/go/tech_profile" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 40 | log "github.com/sirupsen/logrus" |
| 41 | "google.golang.org/grpc" |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 42 | "google.golang.org/grpc/codes" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 43 | "google.golang.org/grpc/reflection" |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 44 | "google.golang.org/grpc/status" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 47 | var oltLogger = log.WithFields(log.Fields{ |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 48 | "module": "OLT", |
| 49 | }) |
| 50 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 51 | const ( |
| 52 | onuIdStart = 1 |
| 53 | onuIdEnd = 127 |
| 54 | allocIdStart = 1024 |
| 55 | allocIdEnd = 16383 |
| 56 | gemportIdStart = 1024 |
| 57 | gemportIdEnd = 65535 |
| 58 | flowIdStart = 1 |
| 59 | flowIdEnd = 65535 |
| 60 | ) |
| 61 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 62 | type OltDevice struct { |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 63 | sync.Mutex |
Hardik Windlass | efdb4b6 | 2021-03-18 10:33:24 +0000 | [diff] [blame] | 64 | OltServer *grpc.Server |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 65 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 66 | // BBSIM Internals |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 67 | ID int |
| 68 | SerialNumber string |
| 69 | NumNni int |
| 70 | NumPon int |
| 71 | NumOnuPerPon int |
| 72 | InternalState *fsm.FSM |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 73 | channel chan types.Message |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 74 | dhcpServer dhcp.DHCPServerIf |
Andrea Campanella | be8e12f | 2020-12-14 18:43:41 +0100 | [diff] [blame] | 75 | Flows sync.Map |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 76 | Delay int |
| 77 | ControlledActivation mode |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 78 | EventChannel chan common.Event |
| 79 | PublishEvents bool |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 80 | PortStatsInterval int |
Matteo Scandolo | 96f8919 | 2021-03-12 13:17:26 -0800 | [diff] [blame] | 81 | PreviouslyConnected bool |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 83 | Pons []*PonPort |
| 84 | Nnis []*NniPort |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 85 | |
| 86 | // OLT Attributes |
| 87 | OperState *fsm.FSM |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 88 | |
| 89 | enableContext context.Context |
| 90 | enableContextCancel context.CancelFunc |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 91 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 92 | OpenoltStream openolt.Openolt_EnableIndicationServer |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 93 | enablePerf bool |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 94 | |
| 95 | // Allocated Resources |
| 96 | // this data are to verify that the openolt adapter does not duplicate resources |
| 97 | AllocIDsLock sync.RWMutex |
| 98 | AllocIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[AllocIds]map[FlowId]bool |
| 99 | GemPortIDsLock sync.RWMutex |
| 100 | GemPortIDs map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool // map[ponPortId]map[OnuId]map[PortNo]map[GemPortIDs]map[FlowId]bool |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 103 | var olt OltDevice |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 104 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 105 | func GetOLT() *OltDevice { |
| 106 | return &olt |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 109 | func CreateOLT(options common.GlobalConfig, services []common.ServiceYaml, isMock bool) *OltDevice { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 110 | oltLogger.WithFields(log.Fields{ |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 111 | "ID": options.Olt.ID, |
| 112 | "NumNni": options.Olt.NniPorts, |
| 113 | "NumPon": options.Olt.PonPorts, |
| 114 | "NumOnuPerPon": options.Olt.OnusPonPort, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 115 | }).Debug("CreateOLT") |
| 116 | |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 117 | olt = OltDevice{ |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 118 | ID: options.Olt.ID, |
| 119 | SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", options.Olt.ID), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 120 | OperState: getOperStateFSM(func(e *fsm.Event) { |
| 121 | oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst) |
| 122 | }), |
Matteo Scandolo | 96f8919 | 2021-03-12 13:17:26 -0800 | [diff] [blame] | 123 | NumNni: int(options.Olt.NniPorts), |
| 124 | NumPon: int(options.Olt.PonPorts), |
| 125 | NumOnuPerPon: int(options.Olt.OnusPonPort), |
| 126 | Pons: []*PonPort{}, |
| 127 | Nnis: []*NniPort{}, |
| 128 | Delay: options.BBSim.Delay, |
| 129 | enablePerf: options.BBSim.EnablePerf, |
| 130 | PublishEvents: options.BBSim.Events, |
| 131 | PortStatsInterval: options.Olt.PortStatsInterval, |
| 132 | dhcpServer: dhcp.NewDHCPServer(), |
| 133 | PreviouslyConnected: false, |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 134 | AllocIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool), |
| 135 | GemPortIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 138 | if val, ok := ControlledActivationModes[options.BBSim.ControlledActivation]; ok { |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 139 | olt.ControlledActivation = val |
| 140 | } else { |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 141 | // FIXME throw an error if the ControlledActivation is not valid |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 142 | oltLogger.Warn("Unknown ControlledActivation Mode given, running in Default mode") |
| 143 | olt.ControlledActivation = Default |
| 144 | } |
| 145 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 146 | // OLT State machine |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 147 | // NOTE do we need 2 state machines for the OLT? (InternalState and OperState) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 148 | olt.InternalState = fsm.NewFSM( |
| 149 | "created", |
| 150 | fsm.Events{ |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 151 | {Name: "initialize", Src: []string{"created", "deleted"}, Dst: "initialized"}, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 152 | {Name: "enable", Src: []string{"initialized", "disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 153 | {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"}, |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 154 | // delete event in enabled state below is for reboot OLT case. |
Mahir Gunyel | 6dad445 | 2020-01-06 12:59:04 -0800 | [diff] [blame] | 155 | {Name: "delete", Src: []string{"disabled", "enabled"}, Dst: "deleted"}, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 156 | }, |
| 157 | fsm.Callbacks{ |
| 158 | "enter_state": func(e *fsm.Event) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 159 | oltLogger.Debugf("Changing OLT InternalState from %s to %s", e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 160 | }, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 161 | "enter_initialized": func(e *fsm.Event) { olt.InitOlt() }, |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 162 | "enter_deleted": func(e *fsm.Event) { |
| 163 | // remove all the resource allocations |
| 164 | olt.clearAllResources() |
| 165 | }, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 166 | }, |
| 167 | ) |
| 168 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 169 | if !isMock { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 170 | // create NNI Port |
| 171 | nniPort, err := CreateNNI(&olt) |
| 172 | if err != nil { |
| 173 | oltLogger.Fatalf("Couldn't create NNI Port: %v", err) |
| 174 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 175 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 176 | olt.Nnis = append(olt.Nnis, &nniPort) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 177 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 178 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 179 | // Create device and Services |
| 180 | |
| 181 | nextCtag := map[string]int{} |
| 182 | nextStag := map[string]int{} |
| 183 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 184 | // create PON ports |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 185 | for i := 0; i < olt.NumPon; i++ { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 186 | |
| 187 | // initialize the resource maps for every PON Ports |
| 188 | olt.AllocIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 189 | olt.GemPortIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 190 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 191 | p := CreatePonPort(&olt, uint32(i)) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 192 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 193 | // create ONU devices |
| 194 | for j := 0; j < olt.NumOnuPerPon; j++ { |
| 195 | delay := time.Duration(olt.Delay*j) * time.Millisecond |
| 196 | o := CreateONU(&olt, p, uint32(j+1), delay, isMock) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 197 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 198 | for k, s := range common.Services { |
| 199 | |
| 200 | // find the correct cTag for this service |
| 201 | if _, ok := nextCtag[s.Name]; !ok { |
| 202 | // it's the first time we iterate over this service, |
| 203 | // so we start from the config value |
| 204 | nextCtag[s.Name] = s.CTag |
| 205 | } else { |
| 206 | // we have a previous value, so we check it |
| 207 | // if Allocation is unique, we increment, |
| 208 | // otherwise (shared) we do nothing |
| 209 | if s.CTagAllocation == common.TagAllocationUnique.String() { |
| 210 | nextCtag[s.Name] = nextCtag[s.Name] + 1 |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // find the correct sTag for this service |
| 215 | if _, ok := nextStag[s.Name]; !ok { |
| 216 | nextStag[s.Name] = s.STag |
| 217 | } else { |
| 218 | if s.STagAllocation == common.TagAllocationUnique.String() { |
| 219 | nextStag[s.Name] = nextStag[s.Name] + 1 |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(p.ID), byte(o.ID), byte(k)} |
| 224 | service, err := NewService(s.Name, mac, o, nextCtag[s.Name], nextStag[s.Name], |
| 225 | s.NeedsEapol, s.NeedsDchp, s.NeedsIgmp, s.TechnologyProfileID, s.UniTagMatch, |
| 226 | s.ConfigureMacAddress, s.UsPonCTagPriority, s.UsPonSTagPriority, s.DsPonCTagPriority, s.DsPonSTagPriority) |
| 227 | |
| 228 | if err != nil { |
| 229 | oltLogger.WithFields(log.Fields{ |
| 230 | "Err": err.Error(), |
| 231 | }).Fatal("Can't create Service") |
| 232 | } |
| 233 | |
| 234 | o.Services = append(o.Services, service) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 235 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 236 | p.Onus = append(p.Onus, o) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 237 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 238 | olt.Pons = append(olt.Pons, p) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 239 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 240 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 241 | if !isMock { |
Matteo Scandolo | d32c382 | 2019-11-26 15:57:46 -0700 | [diff] [blame] | 242 | if err := olt.InternalState.Event("initialize"); err != nil { |
| 243 | log.Errorf("Error initializing OLT: %v", err) |
| 244 | return nil |
| 245 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 248 | if olt.PublishEvents { |
| 249 | log.Debugf("BBSim event publishing is enabled") |
| 250 | // Create a channel to write event messages |
| 251 | olt.EventChannel = make(chan common.Event, 100) |
| 252 | } |
| 253 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 254 | return &olt |
| 255 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 256 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 257 | func (o *OltDevice) InitOlt() { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 258 | |
Hardik Windlass | efdb4b6 | 2021-03-18 10:33:24 +0000 | [diff] [blame] | 259 | if o.OltServer == nil { |
| 260 | o.OltServer, _ = o.StartOltServer() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 261 | } else { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 262 | oltLogger.Fatal("OLT server already running.") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // create new channel for processOltMessages Go routine |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 266 | o.channel = make(chan types.Message) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 267 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 268 | // FIXME we are assuming we have only one NNI |
| 269 | if o.Nnis[0] != nil { |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 270 | // NOTE we want to make sure the state is down when we initialize the OLT, |
| 271 | // the NNI may be in a bad state after a disable/reboot as we are not disabling it for |
| 272 | // in-band management |
| 273 | o.Nnis[0].OperState.SetState("down") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 274 | } |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 275 | |
| 276 | for ponId := range o.Pons { |
| 277 | // initialize the resource maps for every PON Ports |
| 278 | olt.AllocIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 279 | olt.GemPortIDs[uint32(ponId)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 280 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 283 | func (o *OltDevice) RestartOLT() error { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 284 | |
Matteo Scandolo | 96f8919 | 2021-03-12 13:17:26 -0800 | [diff] [blame] | 285 | o.PreviouslyConnected = false |
| 286 | |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 287 | softReboot := false |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 288 | rebootDelay := common.Config.Olt.OltRebootDelay |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 289 | |
| 290 | oltLogger.WithFields(log.Fields{ |
| 291 | "oltId": o.ID, |
| 292 | }).Infof("Simulating OLT restart... (%ds)", rebootDelay) |
| 293 | |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 294 | if o.InternalState.Is("enabled") { |
| 295 | oltLogger.WithFields(log.Fields{ |
| 296 | "oltId": o.ID, |
| 297 | }).Info("This is an OLT soft reboot") |
| 298 | softReboot = true |
| 299 | } |
| 300 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 301 | // transition internal state to deleted |
| 302 | if err := o.InternalState.Event("delete"); err != nil { |
| 303 | oltLogger.WithFields(log.Fields{ |
| 304 | "oltId": o.ID, |
| 305 | }).Errorf("Error deleting OLT: %v", err) |
| 306 | return err |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 309 | time.Sleep(1 * time.Second) // we need to give the OLT the time to respond to all the pending gRPC request before stopping the server |
Matteo Scandolo | 88c204a | 2020-11-03 10:34:24 -0800 | [diff] [blame] | 310 | o.StopOltServer() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 311 | |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 312 | if softReboot { |
| 313 | for _, pon := range o.Pons { |
| 314 | if pon.InternalState.Current() == "enabled" { |
| 315 | // disable PONs |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 316 | msg := types.Message{ |
| 317 | Type: types.PonIndication, |
| 318 | Data: types.PonIndicationMessage{ |
| 319 | OperState: types.DOWN, |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 320 | PonPortID: pon.ID, |
| 321 | }, |
| 322 | } |
| 323 | o.channel <- msg |
| 324 | } |
| 325 | |
| 326 | for _, onu := range pon.Onus { |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 327 | _ = onu.InternalState.Event(OnuTxDisable) |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } else { |
| 331 | // PONs are already handled in the Disable call |
| 332 | for _, pon := range olt.Pons { |
| 333 | // ONUs are not automatically disabled when a PON goes down |
| 334 | // as it's possible that it's an admin down and in that case the ONUs need to keep their state |
| 335 | for _, onu := range pon.Onus { |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 336 | _ = onu.InternalState.Event(OnuTxDisable) |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 337 | } |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 341 | // terminate the OLT's processOltMessages go routine |
| 342 | close(o.channel) |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 343 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 344 | o.enableContextCancel() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 345 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 346 | time.Sleep(time.Duration(rebootDelay) * time.Second) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 347 | |
| 348 | if err := o.InternalState.Event("initialize"); err != nil { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 349 | oltLogger.WithFields(log.Fields{ |
| 350 | "oltId": o.ID, |
| 351 | }).Errorf("Error initializing OLT: %v", err) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 352 | return err |
| 353 | } |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 354 | oltLogger.WithFields(log.Fields{ |
| 355 | "oltId": o.ID, |
| 356 | }).Info("OLT restart completed") |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 357 | return nil |
| 358 | } |
| 359 | |
| 360 | // newOltServer launches a new grpc server for OpenOLT |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 361 | func (o *OltDevice) newOltServer() (*grpc.Server, error) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 362 | address := common.Config.BBSim.OpenOltAddress |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 363 | lis, err := net.Listen("tcp", address) |
| 364 | if err != nil { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 365 | oltLogger.Fatalf("OLT failed to listen: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 366 | } |
| 367 | grpcServer := grpc.NewServer() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 368 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 369 | openolt.RegisterOpenoltServer(grpcServer, o) |
| 370 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 371 | reflection.Register(grpcServer) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 372 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 373 | go func() { _ = grpcServer.Serve(lis) }() |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 374 | oltLogger.Debugf("OLT listening on %v", address) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 375 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 376 | return grpcServer, nil |
| 377 | } |
| 378 | |
Matteo Scandolo | 88c204a | 2020-11-03 10:34:24 -0800 | [diff] [blame] | 379 | // StartOltServer will create the grpc server that VOLTHA uses |
| 380 | // to communicate with the device |
| 381 | func (o *OltDevice) StartOltServer() (*grpc.Server, error) { |
| 382 | oltServer, err := o.newOltServer() |
| 383 | if err != nil { |
| 384 | oltLogger.WithFields(log.Fields{ |
| 385 | "err": err, |
| 386 | }).Error("Cannot OLT gRPC server") |
| 387 | return nil, err |
| 388 | } |
| 389 | return oltServer, nil |
| 390 | } |
| 391 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 392 | // StopOltServer stops the OpenOLT grpc server |
Matteo Scandolo | 88c204a | 2020-11-03 10:34:24 -0800 | [diff] [blame] | 393 | func (o *OltDevice) StopOltServer() { |
Hardik Windlass | efdb4b6 | 2021-03-18 10:33:24 +0000 | [diff] [blame] | 394 | if o.OltServer != nil { |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 395 | oltLogger.WithFields(log.Fields{ |
| 396 | "oltId": o.SerialNumber, |
| 397 | }).Warnf("Stopping OLT gRPC server") |
Hardik Windlass | efdb4b6 | 2021-03-18 10:33:24 +0000 | [diff] [blame] | 398 | o.OltServer.Stop() |
| 399 | o.OltServer = nil |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 400 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Device Methods |
| 404 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 405 | // Enable implements the OpenOLT EnableIndicationServer functionality |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 406 | func (o *OltDevice) Enable(stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 407 | oltLogger.Debug("Enable OLT called") |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 408 | rebootFlag := false |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 409 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 410 | // If enabled has already been called then an enabled context has |
| 411 | // been created. If this is the case then we want to cancel all the |
| 412 | // proessing loops associated with that enable before we recreate |
| 413 | // new ones |
| 414 | o.Lock() |
| 415 | if o.enableContext != nil && o.enableContextCancel != nil { |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 416 | oltLogger.Info("This is an OLT reboot or a reconcile") |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 417 | o.enableContextCancel() |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 418 | rebootFlag = true |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 419 | time.Sleep(1 * time.Second) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 420 | } |
| 421 | o.enableContext, o.enableContextCancel = context.WithCancel(context.TODO()) |
| 422 | o.Unlock() |
| 423 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 424 | wg := sync.WaitGroup{} |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 425 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 426 | o.OpenoltStream = stream |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 427 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 428 | // create Go routine to process all OLT events |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 429 | wg.Add(1) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 430 | go o.processOltMessages(o.enableContext, stream, &wg) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 431 | |
| 432 | // enable the OLT |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 433 | oltMsg := types.Message{ |
| 434 | Type: types.OltIndication, |
| 435 | Data: types.OltIndicationMessage{ |
| 436 | OperState: types.UP, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 437 | }, |
| 438 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 439 | o.channel <- oltMsg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 440 | |
| 441 | // send NNI Port Indications |
| 442 | for _, nni := range o.Nnis { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 443 | msg := types.Message{ |
| 444 | Type: types.NniIndication, |
| 445 | Data: types.NniIndicationMessage{ |
| 446 | OperState: types.UP, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 447 | NniPortID: nni.ID, |
| 448 | }, |
| 449 | } |
| 450 | o.channel <- msg |
| 451 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 452 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 453 | if rebootFlag { |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 454 | for _, pon := range o.Pons { |
| 455 | if pon.InternalState.Current() == "disabled" { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 456 | msg := types.Message{ |
| 457 | Type: types.PonIndication, |
| 458 | Data: types.PonIndicationMessage{ |
| 459 | OperState: types.UP, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 460 | PonPortID: pon.ID, |
| 461 | }, |
| 462 | } |
| 463 | o.channel <- msg |
Matteo Scandolo | e60a505 | 2020-02-07 00:31:14 +0000 | [diff] [blame] | 464 | } |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 465 | // when the enableContext was canceled the ONUs stopped listening on the channel |
| 466 | for _, onu := range pon.Onus { |
| 467 | go onu.ProcessOnuMessages(o.enableContext, stream, nil) |
| 468 | |
| 469 | // update the stream on all the services |
| 470 | for _, service := range onu.Services { |
| 471 | service.UpdateStream(stream) |
| 472 | } |
| 473 | } |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 474 | } |
| 475 | } else { |
| 476 | |
| 477 | // 1. controlledActivation == Default: Send both PON and ONUs indications |
| 478 | // 2. controlledActivation == only-onu: that means only ONUs will be controlled activated, so auto send PON indications |
| 479 | |
| 480 | if o.ControlledActivation == Default || o.ControlledActivation == OnlyONU { |
| 481 | // send PON Port indications |
| 482 | for _, pon := range o.Pons { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 483 | msg := types.Message{ |
| 484 | Type: types.PonIndication, |
| 485 | Data: types.PonIndicationMessage{ |
| 486 | OperState: types.UP, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 487 | PonPortID: pon.ID, |
| 488 | }, |
| 489 | } |
| 490 | o.channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 491 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 495 | if !o.enablePerf { |
| 496 | // Start a go routine to send periodic port stats to openolt adapter |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 497 | wg.Add(1) |
| 498 | go o.periodicPortStats(o.enableContext, &wg, stream) |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 499 | } |
| 500 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 501 | wg.Wait() |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 502 | oltLogger.WithFields(log.Fields{ |
| 503 | "stream": stream, |
| 504 | }).Debug("OpenOLT Stream closed") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 507 | func (o *OltDevice) periodicPortStats(ctx context.Context, wg *sync.WaitGroup, stream openolt.Openolt_EnableIndicationServer) { |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 508 | var portStats *openolt.PortStatistics |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 509 | |
| 510 | loop: |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 511 | for { |
| 512 | select { |
| 513 | case <-time.After(time.Duration(o.PortStatsInterval) * time.Second): |
| 514 | // send NNI port stats |
| 515 | for _, port := range o.Nnis { |
| 516 | incrementStat := true |
| 517 | if port.OperState.Current() == "down" { |
| 518 | incrementStat = false |
| 519 | } |
| 520 | portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat) |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 521 | o.sendPortStatsIndication(portStats, port.ID, port.Type, stream) |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | // send PON port stats |
| 525 | for _, port := range o.Pons { |
| 526 | incrementStat := true |
| 527 | // do not increment port stats if PON port is down or no ONU is activated on PON port |
| 528 | if port.OperState.Current() == "down" || port.GetNumOfActiveOnus() < 1 { |
| 529 | incrementStat = false |
| 530 | } |
| 531 | portStats, port.PacketCount = getPortStats(port.PacketCount, incrementStat) |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 532 | o.sendPortStatsIndication(portStats, port.ID, port.Type, stream) |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 533 | } |
| 534 | case <-ctx.Done(): |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 535 | oltLogger.Debug("Stop sending port stats") |
| 536 | break loop |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 537 | } |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 538 | } |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 539 | wg.Done() |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 540 | } |
| 541 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 542 | // Helpers method |
| 543 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 544 | func (o *OltDevice) SetAlarm(interfaceId uint32, interfaceType string, alarmStatus string) error { |
| 545 | |
| 546 | switch interfaceType { |
| 547 | case "nni": |
| 548 | if !o.HasNni(interfaceId) { |
| 549 | return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" NNI not present in olt") |
| 550 | } |
| 551 | |
| 552 | case "pon": |
| 553 | if !o.HasPon(interfaceId) { |
| 554 | return status.Errorf(codes.NotFound, strconv.Itoa(int(interfaceId))+" PON not present in olt") |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | alarmIndication := &openolt.AlarmIndication{ |
| 559 | Data: &openolt.AlarmIndication_LosInd{LosInd: &openolt.LosIndication{ |
| 560 | Status: alarmStatus, |
| 561 | IntfId: InterfaceIDToPortNo(interfaceId, interfaceType), |
| 562 | }}, |
| 563 | } |
| 564 | |
| 565 | msg := types.Message{ |
| 566 | Type: types.AlarmIndication, |
| 567 | Data: alarmIndication, |
| 568 | } |
| 569 | |
| 570 | o.channel <- msg |
| 571 | |
| 572 | return nil |
| 573 | } |
| 574 | |
| 575 | func (o *OltDevice) HasNni(id uint32) bool { |
| 576 | for _, intf := range o.Nnis { |
| 577 | if intf.ID == id { |
| 578 | return true |
| 579 | } |
| 580 | } |
| 581 | return false |
| 582 | } |
| 583 | |
| 584 | func (o *OltDevice) HasPon(id uint32) bool { |
| 585 | for _, intf := range o.Pons { |
| 586 | if intf.ID == id { |
| 587 | return true |
| 588 | } |
| 589 | } |
| 590 | return false |
| 591 | } |
| 592 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 593 | func (o *OltDevice) GetPonById(id uint32) (*PonPort, error) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 594 | for _, pon := range o.Pons { |
| 595 | if pon.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 596 | return pon, nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 599 | return nil, fmt.Errorf("Cannot find PonPort with id %d in OLT %d", id, o.ID) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 602 | func (o *OltDevice) getNniById(id uint32) (*NniPort, error) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 603 | for _, nni := range o.Nnis { |
| 604 | if nni.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 605 | return nni, nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 608 | return nil, fmt.Errorf("Cannot find NniPort with id %d in OLT %d", id, o.ID) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 611 | func (o *OltDevice) sendAlarmIndication(alarmInd *openolt.AlarmIndication, stream openolt.Openolt_EnableIndicationServer) { |
| 612 | data := &openolt.Indication_AlarmInd{AlarmInd: alarmInd} |
| 613 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 614 | oltLogger.Errorf("Failed to send Alarm Indication: %v", err) |
| 615 | return |
| 616 | } |
| 617 | |
| 618 | oltLogger.WithFields(log.Fields{ |
| 619 | "AlarmIndication": alarmInd, |
| 620 | }).Debug("Sent Indication_AlarmInd") |
| 621 | } |
| 622 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 623 | func (o *OltDevice) sendOltIndication(msg types.OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 624 | data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}} |
| 625 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 626 | oltLogger.Errorf("Failed to send Indication_OltInd: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 627 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 630 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 631 | "OperState": msg.OperState, |
| 632 | }).Debug("Sent Indication_OltInd") |
| 633 | } |
| 634 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 635 | func (o *OltDevice) sendNniIndication(msg types.NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 636 | nni, _ := o.getNniById(msg.NniPortID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 637 | if msg.OperState == types.UP { |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 638 | if err := nni.OperState.Event("enable"); err != nil { |
| 639 | log.WithFields(log.Fields{ |
| 640 | "Type": nni.Type, |
| 641 | "IntfId": nni.ID, |
| 642 | "OperState": nni.OperState.Current(), |
| 643 | }).Errorf("Can't move NNI Port to enabled state: %v", err) |
| 644 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 645 | } else if msg.OperState == types.DOWN { |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 646 | if err := nni.OperState.Event("disable"); err != nil { |
| 647 | log.WithFields(log.Fields{ |
| 648 | "Type": nni.Type, |
| 649 | "IntfId": nni.ID, |
| 650 | "OperState": nni.OperState.Current(), |
| 651 | }).Errorf("Can't move NNI Port to disable state: %v", err) |
| 652 | } |
| 653 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 654 | // NOTE Operstate may need to be an integer |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 655 | operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 656 | Type: nni.Type, |
| 657 | IntfId: nni.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 658 | OperState: nni.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 659 | }} |
| 660 | |
| 661 | if err := stream.Send(&openolt.Indication{Data: operData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 662 | oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 663 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 666 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 667 | "Type": nni.Type, |
| 668 | "IntfId": nni.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 669 | "OperState": nni.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 670 | }).Debug("Sent Indication_IntfOperInd for NNI") |
| 671 | } |
| 672 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 673 | func (o *OltDevice) sendPonIndication(ponPortID uint32) { |
| 674 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 675 | stream := o.OpenoltStream |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 676 | pon, _ := o.GetPonById(ponPortID) |
| 677 | // Send IntfIndication for PON port |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 678 | discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 679 | IntfId: pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 680 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 681 | }} |
| 682 | |
| 683 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 684 | oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 685 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 688 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 689 | "IntfId": pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 690 | "OperState": pon.OperState.Current(), |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 691 | }).Debug("Sent Indication_IntfInd for PON") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 692 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 693 | // Send IntfOperIndication for PON port |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 694 | operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 695 | Type: pon.Type, |
| 696 | IntfId: pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 697 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 698 | }} |
| 699 | |
| 700 | if err := stream.Send(&openolt.Indication{Data: operData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 701 | oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err) |
Matteo Scandolo | 08badf4 | 2019-12-12 11:21:50 -0800 | [diff] [blame] | 702 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 705 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 706 | "Type": pon.Type, |
| 707 | "IntfId": pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 708 | "OperState": pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 709 | }).Debug("Sent Indication_IntfOperInd for PON") |
| 710 | } |
| 711 | |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 712 | func (o *OltDevice) sendPortStatsIndication(stats *openolt.PortStatistics, portID uint32, portType string, stream openolt.Openolt_EnableIndicationServer) { |
Shrey Baid | 55f328c | 2020-07-07 19:20:42 +0530 | [diff] [blame] | 713 | if o.InternalState.Current() == "enabled" { |
| 714 | oltLogger.WithFields(log.Fields{ |
| 715 | "Type": portType, |
| 716 | "IntfId": portID, |
| 717 | }).Trace("Sending port stats") |
| 718 | stats.IntfId = InterfaceIDToPortNo(portID, portType) |
| 719 | data := &openolt.Indication_PortStats{ |
| 720 | PortStats: stats, |
| 721 | } |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 722 | |
Shrey Baid | 55f328c | 2020-07-07 19:20:42 +0530 | [diff] [blame] | 723 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 724 | oltLogger.Errorf("Failed to send PortStats: %v", err) |
| 725 | return |
| 726 | } |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 730 | // processOltMessages handles messages received over the OpenOLT interface |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 731 | func (o *OltDevice) processOltMessages(ctx context.Context, stream types.Stream, wg *sync.WaitGroup) { |
| 732 | oltLogger.WithFields(log.Fields{ |
| 733 | "stream": stream, |
| 734 | }).Debug("Starting OLT Indication Channel") |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 735 | ch := o.channel |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 736 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 737 | loop: |
| 738 | for { |
| 739 | select { |
| 740 | case <-ctx.Done(): |
| 741 | oltLogger.Debug("OLT Indication processing canceled via context") |
| 742 | break loop |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 743 | case <-stream.Context().Done(): |
| 744 | oltLogger.Debug("OLT Indication processing canceled via stream context") |
| 745 | break loop |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 746 | case message, ok := <-ch: |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 747 | if !ok { |
| 748 | if ctx.Err() != nil { |
| 749 | oltLogger.WithField("err", ctx.Err()).Error("OLT EnableContext error") |
| 750 | } |
| 751 | oltLogger.Warn("OLT Indication processing canceled via closed channel") |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 752 | break loop |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 753 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 754 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 755 | oltLogger.WithFields(log.Fields{ |
| 756 | "oltId": o.ID, |
| 757 | "messageType": message.Type, |
| 758 | }).Trace("Received message") |
| 759 | |
| 760 | switch message.Type { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 761 | case types.OltIndication: |
| 762 | msg, _ := message.Data.(types.OltIndicationMessage) |
| 763 | if msg.OperState == types.UP { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 764 | _ = o.InternalState.Event("enable") |
| 765 | _ = o.OperState.Event("enable") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 766 | } else if msg.OperState == types.DOWN { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 767 | _ = o.InternalState.Event("disable") |
| 768 | _ = o.OperState.Event("disable") |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 769 | } |
| 770 | o.sendOltIndication(msg, stream) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 771 | case types.AlarmIndication: |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 772 | alarmInd, _ := message.Data.(*openolt.AlarmIndication) |
| 773 | o.sendAlarmIndication(alarmInd, stream) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 774 | case types.NniIndication: |
| 775 | msg, _ := message.Data.(types.NniIndicationMessage) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 776 | o.sendNniIndication(msg, stream) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 777 | case types.PonIndication: |
| 778 | msg, _ := message.Data.(types.PonIndicationMessage) |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 779 | pon, _ := o.GetPonById(msg.PonPortID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 780 | if msg.OperState == types.UP { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 781 | if err := pon.OperState.Event("enable"); err != nil { |
| 782 | oltLogger.WithFields(log.Fields{ |
| 783 | "IntfId": msg.PonPortID, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 784 | "Err": err, |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 785 | }).Error("Can't Enable Oper state for PON Port") |
| 786 | } |
| 787 | if err := pon.InternalState.Event("enable"); err != nil { |
| 788 | oltLogger.WithFields(log.Fields{ |
| 789 | "IntfId": msg.PonPortID, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 790 | "Err": err, |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 791 | }).Error("Can't Enable Internal state for PON Port") |
| 792 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 793 | } else if msg.OperState == types.DOWN { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 794 | if err := pon.OperState.Event("disable"); err != nil { |
| 795 | oltLogger.WithFields(log.Fields{ |
| 796 | "IntfId": msg.PonPortID, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 797 | "Err": err, |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 798 | }).Error("Can't Disable Oper state for PON Port") |
| 799 | } |
| 800 | if err := pon.InternalState.Event("disable"); err != nil { |
| 801 | oltLogger.WithFields(log.Fields{ |
| 802 | "IntfId": msg.PonPortID, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 803 | "Err": err, |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 804 | }).Error("Can't Disable Internal state for PON Port") |
| 805 | } |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 806 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 807 | default: |
| 808 | oltLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type) |
| 809 | } |
| 810 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 811 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 812 | wg.Done() |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame^] | 813 | oltLogger.WithFields(log.Fields{ |
| 814 | "stream": stream, |
| 815 | }).Warn("Stopped handling OLT Indication Channel") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 818 | // returns an ONU with a given Serial Number |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 819 | func (o *OltDevice) FindOnuBySn(serialNumber string) (*Onu, error) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 820 | // TODO this function can be a performance bottleneck when we have many ONUs, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 821 | // memoizing it will remove the bottleneck |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 822 | for _, pon := range o.Pons { |
| 823 | for _, onu := range pon.Onus { |
| 824 | if onu.Sn() == serialNumber { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 825 | return onu, nil |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 830 | return &Onu{}, fmt.Errorf("cannot-find-onu-by-serial-number-%s", serialNumber) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 831 | } |
| 832 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 833 | // returns an ONU with a given interface/Onu Id |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 834 | func (o *OltDevice) FindOnuById(intfId uint32, onuId uint32) (*Onu, error) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 835 | // TODO this function can be a performance bottleneck when we have many ONUs, |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 836 | // memoizing it will remove the bottleneck |
| 837 | for _, pon := range o.Pons { |
| 838 | if pon.ID == intfId { |
| 839 | for _, onu := range pon.Onus { |
| 840 | if onu.ID == onuId { |
| 841 | return onu, nil |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 846 | return &Onu{}, fmt.Errorf("cannot-find-onu-by-id-%v-%v", intfId, onuId) |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 847 | } |
| 848 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 849 | // returns a Service with a given Mac Address |
| 850 | func (o *OltDevice) FindServiceByMacAddress(mac net.HardwareAddr) (ServiceIf, error) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 851 | // TODO this function can be a performance bottleneck when we have many ONUs, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 852 | // memoizing it will remove the bottleneck |
| 853 | for _, pon := range o.Pons { |
| 854 | for _, onu := range pon.Onus { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 855 | s, err := onu.findServiceByMacAddress(mac) |
| 856 | if err == nil { |
| 857 | return s, nil |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 862 | return nil, fmt.Errorf("cannot-find-service-by-mac-address-%s", mac) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 865 | // GRPC Endpoints |
| 866 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 867 | func (o *OltDevice) ActivateOnu(context context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 868 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 869 | "OnuSn": onuSnToString(onu.SerialNumber), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 870 | }).Info("Received ActivateOnu call from VOLTHA") |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 871 | publishEvent("ONU-activate-indication-received", int32(onu.IntfId), int32(onu.OnuId), onuSnToString(onu.SerialNumber)) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 872 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 873 | pon, _ := o.GetPonById(onu.IntfId) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 874 | |
| 875 | // Initialize the resource maps for this ONU |
| 876 | olt.AllocIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool) |
| 877 | olt.GemPortIDs[onu.IntfId][onu.OnuId] = make(map[uint32]map[int32]map[uint64]bool) |
| 878 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 879 | _onu, _ := pon.GetOnuBySn(onu.SerialNumber) |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 880 | _onu.SetID(onu.OnuId) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 881 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 882 | if err := _onu.InternalState.Event(OnuTxEnable); err != nil { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 883 | oltLogger.WithFields(log.Fields{ |
| 884 | "IntfId": _onu.PonPortID, |
| 885 | "OnuSn": _onu.Sn(), |
| 886 | "OnuId": _onu.ID, |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 887 | }).Infof("Failed to transition ONU to %s state: %s", OnuStateEnabled, err.Error()) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // NOTE we need to immediately activate the ONU or the OMCI state machine won't start |
| 891 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 892 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 895 | func (o *OltDevice) DeactivateOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 896 | oltLogger.Error("DeactivateOnu not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 897 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 900 | func (o *OltDevice) DeleteOnu(_ context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 901 | oltLogger.WithFields(log.Fields{ |
| 902 | "IntfId": onu.IntfId, |
| 903 | "OnuId": onu.OnuId, |
| 904 | }).Info("Received DeleteOnu call from VOLTHA") |
| 905 | |
| 906 | pon, err := o.GetPonById(onu.IntfId) |
| 907 | if err != nil { |
| 908 | oltLogger.WithFields(log.Fields{ |
| 909 | "OnuId": onu.OnuId, |
| 910 | "IntfId": onu.IntfId, |
| 911 | "err": err, |
| 912 | }).Error("Can't find PonPort") |
| 913 | } |
| 914 | _onu, err := pon.GetOnuById(onu.OnuId) |
| 915 | if err != nil { |
| 916 | oltLogger.WithFields(log.Fields{ |
| 917 | "OnuId": onu.OnuId, |
| 918 | "IntfId": onu.IntfId, |
| 919 | "err": err, |
| 920 | }).Error("Can't find Onu") |
| 921 | } |
| 922 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 923 | if err := _onu.InternalState.Event(OnuTxDisable); err != nil { |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 924 | oltLogger.WithFields(log.Fields{ |
| 925 | "IntfId": _onu.PonPortID, |
| 926 | "OnuSn": _onu.Sn(), |
| 927 | "OnuId": _onu.ID, |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 928 | }).Infof("Failed to transition ONU to %s state: %s", OnuStateDisabled, err.Error()) |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 929 | } |
| 930 | |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 931 | // ONU Re-Discovery |
| 932 | if o.InternalState.Current() == "enabled" && pon.InternalState.Current() == "enabled" { |
Hardik Windlass | 7b3405b | 2020-07-08 15:10:05 +0530 | [diff] [blame] | 933 | go _onu.ReDiscoverOnu() |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 934 | } |
| 935 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 936 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 939 | func (o *OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 940 | // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether? |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 941 | oltLogger.WithFields(log.Fields{ |
| 942 | "oltId": o.ID, |
| 943 | }).Info("Disabling OLT") |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 944 | publishEvent("OLT-disable-received", -1, -1, "") |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 945 | |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 946 | for _, pon := range o.Pons { |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 947 | if pon.InternalState.Current() == "enabled" { |
| 948 | // disable PONs |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 949 | msg := types.Message{ |
| 950 | Type: types.PonIndication, |
| 951 | Data: types.PonIndicationMessage{ |
| 952 | OperState: types.DOWN, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 953 | PonPortID: pon.ID, |
| 954 | }, |
| 955 | } |
| 956 | o.channel <- msg |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 957 | } |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 960 | // Note that we are not disabling the NNI as the real OLT does not. |
| 961 | // The reason for that is in-band management |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 962 | |
| 963 | // disable OLT |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 964 | oltMsg := types.Message{ |
| 965 | Type: types.OltIndication, |
| 966 | Data: types.OltIndicationMessage{ |
| 967 | OperState: types.DOWN, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 968 | }, |
| 969 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 970 | o.channel <- oltMsg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 971 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 974 | func (o *OltDevice) DisablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 975 | oltLogger.Infof("DisablePonIf request received for PON %d", intf.IntfId) |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 976 | ponID := intf.GetIntfId() |
| 977 | pon, _ := o.GetPonById(intf.IntfId) |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 978 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 979 | msg := types.Message{ |
| 980 | Type: types.PonIndication, |
| 981 | Data: types.PonIndicationMessage{ |
| 982 | OperState: types.DOWN, |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 983 | PonPortID: ponID, |
| 984 | }, |
| 985 | } |
| 986 | o.channel <- msg |
| 987 | |
| 988 | for _, onu := range pon.Onus { |
| 989 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 990 | onuIndication := types.OnuIndicationMessage{ |
| 991 | OperState: types.DOWN, |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 992 | PonPortID: ponID, |
| 993 | OnuID: onu.ID, |
| 994 | OnuSN: onu.SerialNumber, |
| 995 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 996 | onu.sendOnuIndication(onuIndication, o.OpenoltStream) |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 997 | |
| 998 | } |
| 999 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1000 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 1003 | func (o *OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1004 | oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA") |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 1005 | publishEvent("OLT-enable-received", -1, -1, "") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1006 | o.Enable(stream) |
| 1007 | return nil |
| 1008 | } |
| 1009 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1010 | func (o *OltDevice) EnablePonIf(_ context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 1011 | oltLogger.Infof("EnablePonIf request received for PON %d", intf.IntfId) |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1012 | ponID := intf.GetIntfId() |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 1013 | pon, _ := o.GetPonById(intf.IntfId) |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 1014 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1015 | msg := types.Message{ |
| 1016 | Type: types.PonIndication, |
| 1017 | Data: types.PonIndicationMessage{ |
| 1018 | OperState: types.UP, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1019 | PonPortID: ponID, |
| 1020 | }, |
| 1021 | } |
| 1022 | o.channel <- msg |
| 1023 | |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 1024 | for _, onu := range pon.Onus { |
| 1025 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1026 | onuIndication := types.OnuIndicationMessage{ |
| 1027 | OperState: types.UP, |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 1028 | PonPortID: ponID, |
| 1029 | OnuID: onu.ID, |
| 1030 | OnuSN: onu.SerialNumber, |
| 1031 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1032 | onu.sendOnuIndication(onuIndication, o.OpenoltStream) |
Andrea Campanella | 340b9ea | 2020-04-28 18:34:01 +0200 | [diff] [blame] | 1033 | |
| 1034 | } |
| 1035 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1036 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1039 | func (o *OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1040 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1041 | "IntfId": flow.AccessIntfId, |
| 1042 | "OnuId": flow.OnuId, |
| 1043 | "EthType": fmt.Sprintf("%x", flow.Classifier.EthType), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1044 | "InnerVlan": flow.Classifier.IVid, |
| 1045 | "OuterVlan": flow.Classifier.OVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1046 | "FlowType": flow.FlowType, |
| 1047 | "FlowId": flow.FlowId, |
| 1048 | "UniID": flow.UniId, |
| 1049 | "PortNo": flow.PortNo, |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1050 | }).Tracef("OLT receives FlowAdd") |
| 1051 | |
| 1052 | flowKey := FlowKey{} |
| 1053 | if !o.enablePerf { |
| 1054 | flowKey = FlowKey{ID: flow.FlowId, Direction: flow.FlowType} |
Andrea Campanella | be8e12f | 2020-12-14 18:43:41 +0100 | [diff] [blame] | 1055 | olt.Flows.Store(flowKey, *flow) |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1056 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1057 | |
| 1058 | if flow.AccessIntfId == -1 { |
| 1059 | oltLogger.WithFields(log.Fields{ |
| 1060 | "FlowId": flow.FlowId, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1061 | }).Debug("Adding OLT flow") |
Jonathan Hart | b5fc46a | 2020-03-31 16:42:31 -0700 | [diff] [blame] | 1062 | } else if flow.FlowType == "multicast" { |
| 1063 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 1064 | "Cookie": flow.Cookie, |
| 1065 | "DstPort": flow.Classifier.DstPort, |
| 1066 | "EthType": fmt.Sprintf("%x", flow.Classifier.EthType), |
| 1067 | "FlowId": flow.FlowId, |
| 1068 | "FlowType": flow.FlowType, |
| 1069 | "GemportId": flow.GemportId, |
| 1070 | "InnerVlan": flow.Classifier.IVid, |
| 1071 | "IntfId": flow.AccessIntfId, |
| 1072 | "IpProto": flow.Classifier.IpProto, |
| 1073 | "OnuId": flow.OnuId, |
| 1074 | "OuterVlan": flow.Classifier.OVid, |
| 1075 | "PortNo": flow.PortNo, |
| 1076 | "SrcPort": flow.Classifier.SrcPort, |
| 1077 | "UniID": flow.UniId, |
| 1078 | "ClassifierOPbits": flow.Classifier.OPbits, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1079 | }).Debug("Adding OLT multicast flow") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1080 | } else { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1081 | pon, err := o.GetPonById(uint32(flow.AccessIntfId)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1082 | if err != nil { |
| 1083 | oltLogger.WithFields(log.Fields{ |
| 1084 | "OnuId": flow.OnuId, |
| 1085 | "IntfId": flow.AccessIntfId, |
| 1086 | "err": err, |
| 1087 | }).Error("Can't find PonPort") |
| 1088 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1089 | onu, err := pon.GetOnuById(uint32(flow.OnuId)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1090 | if err != nil { |
| 1091 | oltLogger.WithFields(log.Fields{ |
| 1092 | "OnuId": flow.OnuId, |
| 1093 | "IntfId": flow.AccessIntfId, |
| 1094 | "err": err, |
| 1095 | }).Error("Can't find Onu") |
Jonathan Hart | b5fc46a | 2020-03-31 16:42:31 -0700 | [diff] [blame] | 1096 | return nil, err |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1097 | } |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1098 | if !o.enablePerf { |
| 1099 | onu.Flows = append(onu.Flows, flowKey) |
Pragya Arya | 1d5ffb8 | 2020-03-20 18:51:37 +0530 | [diff] [blame] | 1100 | // Generate event on first flow for ONU |
| 1101 | if len(onu.Flows) == 1 { |
| 1102 | publishEvent("Flow-add-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber)) |
| 1103 | } |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1104 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1105 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1106 | // validate that the flow reference correct IDs (Alloc, Gem) |
| 1107 | if err := o.validateFlow(flow); err != nil { |
| 1108 | oltLogger.WithFields(log.Fields{ |
| 1109 | "OnuId": flow.OnuId, |
| 1110 | "IntfId": flow.AccessIntfId, |
| 1111 | "Flow": flow, |
| 1112 | "SerialNumber": onu.Sn(), |
| 1113 | "err": err, |
| 1114 | }).Error("invalid-flow-for-onu") |
| 1115 | return nil, err |
| 1116 | } |
| 1117 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 1118 | o.storeGemPortIdByFlow(flow) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1119 | o.storeAllocId(flow) |
| 1120 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1121 | msg := types.Message{ |
| 1122 | Type: types.FlowAdd, |
| 1123 | Data: types.OnuFlowUpdateMessage{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1124 | PonPortID: pon.ID, |
| 1125 | OnuID: onu.ID, |
| 1126 | Flow: flow, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1127 | }, |
| 1128 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 1129 | onu.Channel <- msg |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1132 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1135 | // FlowRemove request from VOLTHA |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1136 | func (o *OltDevice) FlowRemove(_ context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1137 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1138 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1139 | "AllocId": flow.AllocId, |
| 1140 | "Cookie": flow.Cookie, |
| 1141 | "FlowId": flow.FlowId, |
| 1142 | "FlowType": flow.FlowType, |
| 1143 | "GemportId": flow.GemportId, |
| 1144 | "IntfId": flow.AccessIntfId, |
| 1145 | "OnuId": flow.OnuId, |
| 1146 | "PortNo": flow.PortNo, |
| 1147 | "UniID": flow.UniId, |
| 1148 | "ReplicateFlow": flow.ReplicateFlow, |
| 1149 | "PbitToGemport": flow.PbitToGemport, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1150 | }).Debug("OLT receives FlowRemove") |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1151 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1152 | olt.freeGemPortId(flow) |
| 1153 | olt.freeAllocId(flow) |
| 1154 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1155 | if !o.enablePerf { // remove only if flow were stored |
| 1156 | flowKey := FlowKey{ |
| 1157 | ID: flow.FlowId, |
| 1158 | Direction: flow.FlowType, |
| 1159 | } |
| 1160 | |
| 1161 | // Check if flow exists |
Andrea Campanella | be8e12f | 2020-12-14 18:43:41 +0100 | [diff] [blame] | 1162 | storedFlowIntf, ok := o.Flows.Load(flowKey) |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1163 | if !ok { |
| 1164 | oltLogger.Errorf("Flow %v not found", flow) |
| 1165 | return new(openolt.Empty), status.Errorf(codes.NotFound, "Flow not found") |
| 1166 | } |
| 1167 | |
Andrea Campanella | be8e12f | 2020-12-14 18:43:41 +0100 | [diff] [blame] | 1168 | storedFlow := storedFlowIntf.(openolt.Flow) |
| 1169 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1170 | // if its ONU flow remove it from ONU also |
| 1171 | if storedFlow.AccessIntfId != -1 { |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 1172 | pon, err := o.GetPonById(uint32(storedFlow.AccessIntfId)) |
| 1173 | if err != nil { |
| 1174 | oltLogger.WithFields(log.Fields{ |
| 1175 | "OnuId": storedFlow.OnuId, |
| 1176 | "IntfId": storedFlow.AccessIntfId, |
| 1177 | "PONs": olt.Pons, |
| 1178 | "err": err, |
| 1179 | }).Error("PON-port-not-found") |
| 1180 | return new(openolt.Empty), nil |
| 1181 | } |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1182 | onu, err := pon.GetOnuById(uint32(storedFlow.OnuId)) |
| 1183 | if err != nil { |
| 1184 | oltLogger.WithFields(log.Fields{ |
| 1185 | "OnuId": storedFlow.OnuId, |
| 1186 | "IntfId": storedFlow.AccessIntfId, |
| 1187 | "err": err, |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 1188 | }).Error("ONU-not-found") |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1189 | return new(openolt.Empty), nil |
| 1190 | } |
| 1191 | onu.DeleteFlow(flowKey) |
Pragya Arya | 1d5ffb8 | 2020-03-20 18:51:37 +0530 | [diff] [blame] | 1192 | publishEvent("Flow-remove-received", int32(onu.PonPortID), int32(onu.ID), onuSnToString(onu.SerialNumber)) |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | // delete from olt flows |
Andrea Campanella | be8e12f | 2020-12-14 18:43:41 +0100 | [diff] [blame] | 1196 | o.Flows.Delete(flowKey) |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1197 | } |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1198 | |
| 1199 | if flow.AccessIntfId == -1 { |
| 1200 | oltLogger.WithFields(log.Fields{ |
| 1201 | "FlowId": flow.FlowId, |
| 1202 | }).Debug("Removing OLT flow") |
| 1203 | } else if flow.FlowType == "multicast" { |
| 1204 | oltLogger.WithFields(log.Fields{ |
| 1205 | "FlowId": flow.FlowId, |
| 1206 | }).Debug("Removing OLT multicast flow") |
| 1207 | } else { |
| 1208 | |
| 1209 | onu, err := o.GetOnuByFlowId(flow.FlowId) |
| 1210 | if err != nil { |
| 1211 | oltLogger.WithFields(log.Fields{ |
| 1212 | "OnuId": flow.OnuId, |
| 1213 | "IntfId": flow.AccessIntfId, |
| 1214 | "err": err, |
| 1215 | }).Error("Can't find Onu") |
| 1216 | return nil, err |
| 1217 | } |
| 1218 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1219 | msg := types.Message{ |
| 1220 | Type: types.FlowRemoved, |
| 1221 | Data: types.OnuFlowUpdateMessage{ |
Shrey Baid | 55f328c | 2020-07-07 19:20:42 +0530 | [diff] [blame] | 1222 | Flow: flow, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1223 | }, |
| 1224 | } |
| 1225 | onu.Channel <- msg |
| 1226 | } |
| 1227 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1228 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1231 | func (o *OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) { |
Matteo Scandolo | 1885985 | 2020-01-15 13:33:57 -0800 | [diff] [blame] | 1232 | res := openolt.Heartbeat{HeartbeatSignature: uint32(time.Now().Unix())} |
| 1233 | oltLogger.WithFields(log.Fields{ |
| 1234 | "signature": res.HeartbeatSignature, |
| 1235 | }).Trace("HeartbeatCheck") |
| 1236 | return &res, nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 1239 | func (o *OltDevice) GetOnuByFlowId(flowId uint64) (*Onu, error) { |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1240 | for _, pon := range o.Pons { |
| 1241 | for _, onu := range pon.Onus { |
| 1242 | for _, fId := range onu.FlowIds { |
| 1243 | if fId == flowId { |
| 1244 | return onu, nil |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | } |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1249 | return nil, fmt.Errorf("Cannot find Onu by flowId %d", flowId) |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1252 | func (o *OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1253 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 1254 | intfIDs := []uint32{} |
| 1255 | for i := 0; i < o.NumPon; i++ { |
| 1256 | intfIDs = append(intfIDs, uint32(i)) |
| 1257 | } |
| 1258 | |
| 1259 | devinfo := &openolt.DeviceInfo{ |
| 1260 | Vendor: common.Config.Olt.Vendor, |
| 1261 | Model: common.Config.Olt.Model, |
| 1262 | HardwareVersion: common.Config.Olt.HardwareVersion, |
| 1263 | FirmwareVersion: common.Config.Olt.FirmwareVersion, |
| 1264 | Technology: common.Config.Olt.Technology, |
| 1265 | PonPorts: uint32(o.NumPon), |
| 1266 | OnuIdStart: onuIdStart, |
| 1267 | OnuIdEnd: onuIdEnd, |
| 1268 | AllocIdStart: allocIdStart, |
| 1269 | AllocIdEnd: allocIdEnd, |
| 1270 | GemportIdStart: gemportIdStart, |
| 1271 | GemportIdEnd: gemportIdEnd, |
| 1272 | FlowIdStart: flowIdStart, |
| 1273 | FlowIdEnd: flowIdEnd, |
| 1274 | DeviceSerialNumber: o.SerialNumber, |
| 1275 | DeviceId: common.Config.Olt.DeviceId, |
| 1276 | PreviouslyConnected: o.PreviouslyConnected, |
| 1277 | Ranges: []*openolt.DeviceInfo_DeviceResourceRanges{ |
| 1278 | { |
| 1279 | IntfIds: intfIDs, |
| 1280 | Technology: common.Config.Olt.Technology, |
| 1281 | Pools: []*openolt.DeviceInfo_DeviceResourceRanges_Pool{ |
| 1282 | { |
| 1283 | Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID, |
| 1284 | Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, |
| 1285 | Start: onuIdStart, |
| 1286 | End: onuIdEnd, |
| 1287 | }, |
| 1288 | { |
| 1289 | Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID, |
| 1290 | Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, |
| 1291 | Start: allocIdStart, |
| 1292 | End: allocIdEnd, |
| 1293 | }, |
| 1294 | { |
| 1295 | Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID, |
| 1296 | Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, |
| 1297 | Start: gemportIdStart, |
| 1298 | End: gemportIdEnd, |
| 1299 | }, |
| 1300 | { |
| 1301 | Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID, |
| 1302 | Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH, |
| 1303 | Start: flowIdStart, |
| 1304 | End: flowIdEnd, |
| 1305 | }, |
| 1306 | }, |
| 1307 | }, |
| 1308 | }, |
| 1309 | } |
Matteo Scandolo | 96f8919 | 2021-03-12 13:17:26 -0800 | [diff] [blame] | 1310 | |
| 1311 | oltLogger.WithFields(log.Fields{ |
| 1312 | "Vendor": devinfo.Vendor, |
| 1313 | "Model": devinfo.Model, |
| 1314 | "HardwareVersion": devinfo.HardwareVersion, |
| 1315 | "FirmwareVersion": devinfo.FirmwareVersion, |
| 1316 | "Technology": devinfo.Technology, |
| 1317 | "PonPorts": devinfo.PonPorts, |
| 1318 | "OnuIdStart": devinfo.OnuIdStart, |
| 1319 | "OnuIdEnd": devinfo.OnuIdEnd, |
| 1320 | "AllocIdStart": devinfo.AllocIdStart, |
| 1321 | "AllocIdEnd": devinfo.AllocIdEnd, |
| 1322 | "GemportIdStart": devinfo.GemportIdStart, |
| 1323 | "GemportIdEnd": devinfo.GemportIdEnd, |
| 1324 | "FlowIdStart": devinfo.FlowIdStart, |
| 1325 | "FlowIdEnd": devinfo.FlowIdEnd, |
| 1326 | "DeviceSerialNumber": devinfo.DeviceSerialNumber, |
| 1327 | "DeviceId": devinfo.DeviceId, |
| 1328 | "PreviouslyConnected": devinfo.PreviouslyConnected, |
| 1329 | }).Info("OLT receives GetDeviceInfo call from VOLTHA") |
| 1330 | |
| 1331 | // once we connect, set the flag |
| 1332 | o.PreviouslyConnected = true |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1333 | |
| 1334 | return devinfo, nil |
| 1335 | } |
| 1336 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1337 | func (o *OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) { |
Jonathan Hart | acfa20e | 2020-03-31 15:20:14 -0700 | [diff] [blame] | 1338 | pon, err := o.GetPonById(omci_msg.IntfId) |
| 1339 | if err != nil { |
| 1340 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 1341 | "error": err, |
Jonathan Hart | acfa20e | 2020-03-31 15:20:14 -0700 | [diff] [blame] | 1342 | "onu_id": omci_msg.OnuId, |
| 1343 | "pon_id": omci_msg.IntfId, |
| 1344 | }).Error("pon ID not found") |
| 1345 | return nil, err |
| 1346 | } |
| 1347 | |
| 1348 | onu, err := pon.GetOnuById(omci_msg.OnuId) |
| 1349 | if err != nil { |
| 1350 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 1351 | "error": err, |
Jonathan Hart | acfa20e | 2020-03-31 15:20:14 -0700 | [diff] [blame] | 1352 | "onu_id": omci_msg.OnuId, |
| 1353 | "pon_id": omci_msg.IntfId, |
| 1354 | }).Error("onu ID not found") |
| 1355 | return nil, err |
| 1356 | } |
| 1357 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 1358 | oltLogger.WithFields(log.Fields{ |
| 1359 | "IntfId": onu.PonPortID, |
| 1360 | "OnuId": onu.ID, |
| 1361 | "OnuSn": onu.Sn(), |
| 1362 | }).Tracef("Received OmciMsgOut") |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 1363 | omciPkt, omciMsg, err := omcilib.ParseOpenOltOmciPacket(omci_msg.Pkt) |
| 1364 | if err != nil { |
| 1365 | log.WithFields(log.Fields{ |
| 1366 | "IntfId": onu.PonPortID, |
| 1367 | "SerialNumber": onu.Sn(), |
| 1368 | "omciPacket": omcilib.HexDecode(omci_msg.Pkt), |
| 1369 | "err": err.Error(), |
| 1370 | }).Error("cannot-parse-OMCI-packet") |
| 1371 | return nil, fmt.Errorf("olt-received-malformed-omci-packet") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1372 | } |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 1373 | if onu.InternalState.Current() == OnuStateDisabled { |
| 1374 | // if the ONU is disabled just drop the message |
| 1375 | log.WithFields(log.Fields{ |
| 1376 | "IntfId": onu.PonPortID, |
| 1377 | "SerialNumber": onu.Sn(), |
| 1378 | "omciBytes": hex.EncodeToString(omciPkt.Data()), |
| 1379 | "omciPkt": omciPkt, |
| 1380 | "omciMsgType": omciMsg.MessageType, |
| 1381 | }).Warn("dropping-omci-message") |
| 1382 | } else { |
| 1383 | msg := types.Message{ |
| 1384 | Type: types.OMCI, |
| 1385 | Data: types.OmciMessage{ |
| 1386 | OnuSN: onu.SerialNumber, |
| 1387 | OnuID: onu.ID, |
| 1388 | OmciMsg: omciMsg, |
| 1389 | OmciPkt: omciPkt, |
| 1390 | }, |
| 1391 | } |
| 1392 | onu.Channel <- msg |
| 1393 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1394 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1397 | func (o *OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1398 | pon, err := o.GetPonById(onuPkt.IntfId) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1399 | if err != nil { |
| 1400 | oltLogger.WithFields(log.Fields{ |
| 1401 | "OnuId": onuPkt.OnuId, |
| 1402 | "IntfId": onuPkt.IntfId, |
| 1403 | "err": err, |
| 1404 | }).Error("Can't find PonPort") |
| 1405 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1406 | onu, err := pon.GetOnuById(onuPkt.OnuId) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1407 | if err != nil { |
| 1408 | oltLogger.WithFields(log.Fields{ |
| 1409 | "OnuId": onuPkt.OnuId, |
| 1410 | "IntfId": onuPkt.IntfId, |
| 1411 | "err": err, |
| 1412 | }).Error("Can't find Onu") |
| 1413 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 1414 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 1415 | oltLogger.WithFields(log.Fields{ |
| 1416 | "IntfId": onu.PonPortID, |
| 1417 | "OnuId": onu.ID, |
| 1418 | "OnuSn": onu.Sn(), |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 1419 | "Packet": hex.EncodeToString(onuPkt.Pkt), |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 1420 | }).Trace("Received OnuPacketOut") |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 1421 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 1422 | rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 1423 | |
| 1424 | pktType, err := packetHandlers.GetPktType(rawpkt) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 1425 | if err != nil { |
| 1426 | onuLogger.WithFields(log.Fields{ |
| 1427 | "IntfId": onu.PonPortID, |
| 1428 | "OnuId": onu.ID, |
| 1429 | "OnuSn": onu.Sn(), |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 1430 | "Pkt": hex.EncodeToString(rawpkt.Data()), |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 1431 | }).Error("Can't find pktType in packet, droppint it") |
| 1432 | return new(openolt.Empty), nil |
| 1433 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 1434 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1435 | pktMac, err := packetHandlers.GetDstMacAddressFromPacket(rawpkt) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1436 | if err != nil { |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 1437 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1438 | "IntfId": onu.PonPortID, |
| 1439 | "OnuId": onu.ID, |
| 1440 | "OnuSn": onu.Sn(), |
| 1441 | "Pkt": rawpkt.Data(), |
| 1442 | }).Error("Can't find Dst MacAddress in packet, droppint it") |
| 1443 | return new(openolt.Empty), nil |
| 1444 | } |
| 1445 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1446 | msg := types.Message{ |
| 1447 | Type: types.OnuPacketOut, |
| 1448 | Data: types.OnuPacketMessage{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1449 | IntfId: onuPkt.IntfId, |
| 1450 | OnuId: onuPkt.OnuId, |
| 1451 | Packet: rawpkt, |
| 1452 | Type: pktType, |
| 1453 | MacAddress: pktMac, |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 1454 | }, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 1455 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1456 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 1457 | onu.Channel <- msg |
| 1458 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1459 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1462 | func (o *OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 635b2bf | 2020-09-04 10:23:40 -0700 | [diff] [blame] | 1463 | |
| 1464 | // OLT Reboot is called in two cases: |
| 1465 | // - when an OLT is being removed (voltctl device disable -> voltctl device delete are called, then a new voltctl device create -> voltctl device enable will be issued) |
| 1466 | // - when an OLT needs to be rebooted (voltcl device reboot) |
| 1467 | |
Matteo Scandolo | d02b79b | 2019-12-05 16:42:13 -0800 | [diff] [blame] | 1468 | oltLogger.WithFields(log.Fields{ |
| 1469 | "oltId": o.ID, |
| 1470 | }).Info("Shutting down") |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 1471 | publishEvent("OLT-reboot-received", -1, -1, "") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1472 | go func() { _ = o.RestartOLT() }() |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1473 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1476 | func (o *OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1477 | oltLogger.WithFields(log.Fields{ |
| 1478 | "oltId": o.ID, |
| 1479 | }).Info("Received ReenableOlt request from VOLTHA") |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 1480 | publishEvent("OLT-reenable-received", -1, -1, "") |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1481 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1482 | // enable OLT |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1483 | oltMsg := types.Message{ |
| 1484 | Type: types.OltIndication, |
| 1485 | Data: types.OltIndicationMessage{ |
| 1486 | OperState: types.UP, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1487 | }, |
Pragya Arya | 1881df0 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1488 | } |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1489 | o.channel <- oltMsg |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame] | 1490 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1491 | for _, pon := range o.Pons { |
| 1492 | if pon.InternalState.Current() == "disabled" { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1493 | msg := types.Message{ |
| 1494 | Type: types.PonIndication, |
| 1495 | Data: types.PonIndicationMessage{ |
| 1496 | OperState: types.UP, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 1497 | PonPortID: pon.ID, |
| 1498 | }, |
| 1499 | } |
| 1500 | o.channel <- msg |
| 1501 | } |
| 1502 | } |
Matteo Scandolo | e60a505 | 2020-02-07 00:31:14 +0000 | [diff] [blame] | 1503 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1504 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1507 | func (o *OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1508 | pkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 1509 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 1510 | err := o.Nnis[0].handleNniPacket(pkt) // FIXME we are assuming we have only one NNI |
| 1511 | |
| 1512 | if err != nil { |
| 1513 | return nil, err |
| 1514 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1515 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1518 | func (o *OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1519 | oltLogger.Error("CollectStatistics not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1520 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1523 | func (o *OltDevice) GetOnuInfo(context context.Context, packet *openolt.Onu) (*openolt.OnuIndication, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1524 | oltLogger.Error("GetOnuInfo not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1525 | return new(openolt.OnuIndication), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1528 | func (o *OltDevice) GetPonIf(context context.Context, packet *openolt.Interface) (*openolt.IntfIndication, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 1529 | oltLogger.Error("GetPonIf not implemented") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1530 | return new(openolt.IntfIndication), nil |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1533 | func (s *OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 1534 | oltLogger.Info("received CreateTrafficQueues") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1535 | return new(openolt.Empty), nil |
| 1536 | } |
| 1537 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1538 | func (s *OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 1539 | oltLogger.Info("received RemoveTrafficQueues") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1540 | return new(openolt.Empty), nil |
| 1541 | } |
| 1542 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1543 | func (s *OltDevice) CreateTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) { |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 1544 | oltLogger.WithFields(log.Fields{ |
| 1545 | "OnuId": trafficSchedulers.OnuId, |
| 1546 | "IntfId": trafficSchedulers.IntfId, |
| 1547 | "OnuPortNo": trafficSchedulers.PortNo, |
| 1548 | }).Info("received CreateTrafficSchedulers") |
| 1549 | |
| 1550 | if !s.enablePerf { |
| 1551 | pon, err := s.GetPonById(trafficSchedulers.IntfId) |
| 1552 | if err != nil { |
| 1553 | oltLogger.Errorf("Error retrieving PON by IntfId: %v", err) |
| 1554 | return new(openolt.Empty), err |
| 1555 | } |
| 1556 | onu, err := pon.GetOnuById(trafficSchedulers.OnuId) |
| 1557 | if err != nil { |
| 1558 | oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err) |
| 1559 | return new(openolt.Empty), err |
| 1560 | } |
| 1561 | onu.TrafficSchedulers = trafficSchedulers |
| 1562 | } |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1563 | return new(openolt.Empty), nil |
| 1564 | } |
| 1565 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 1566 | func (s *OltDevice) RemoveTrafficSchedulers(context context.Context, trafficSchedulers *tech_profile.TrafficSchedulers) (*openolt.Empty, error) { |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 1567 | oltLogger.WithFields(log.Fields{ |
| 1568 | "OnuId": trafficSchedulers.OnuId, |
| 1569 | "IntfId": trafficSchedulers.IntfId, |
| 1570 | "OnuPortNo": trafficSchedulers.PortNo, |
| 1571 | }).Info("received RemoveTrafficSchedulers") |
| 1572 | if !s.enablePerf { |
| 1573 | pon, err := s.GetPonById(trafficSchedulers.IntfId) |
| 1574 | if err != nil { |
| 1575 | oltLogger.Errorf("Error retrieving PON by IntfId: %v", err) |
| 1576 | return new(openolt.Empty), err |
| 1577 | } |
| 1578 | onu, err := pon.GetOnuById(trafficSchedulers.OnuId) |
| 1579 | if err != nil { |
| 1580 | oltLogger.Errorf("Error retrieving ONU from pon by OnuId: %v", err) |
| 1581 | return new(openolt.Empty), err |
| 1582 | } |
| 1583 | |
| 1584 | onu.TrafficSchedulers = nil |
| 1585 | } |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 1586 | return new(openolt.Empty), nil |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1587 | } |
Scott Baker | 41724b8 | 2020-01-21 19:54:53 -0800 | [diff] [blame] | 1588 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 1589 | func (o *OltDevice) PerformGroupOperation(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) { |
| 1590 | oltLogger.WithFields(log.Fields{ |
| 1591 | "GroupId": group.GroupId, |
| 1592 | "Command": group.Command, |
| 1593 | "Members": group.Members, |
| 1594 | "Action": group.Action, |
| 1595 | }).Debug("received PerformGroupOperation") |
| 1596 | return &openolt.Empty{}, nil |
| 1597 | } |
| 1598 | |
| 1599 | func (o *OltDevice) DeleteGroup(ctx context.Context, group *openolt.Group) (*openolt.Empty, error) { |
| 1600 | oltLogger.WithFields(log.Fields{ |
| 1601 | "GroupId": group.GroupId, |
| 1602 | "Command": group.Command, |
| 1603 | "Members": group.Members, |
| 1604 | "Action": group.Action, |
| 1605 | }).Debug("received PerformGroupOperation") |
| 1606 | return &openolt.Empty{}, nil |
| 1607 | } |
| 1608 | |
| 1609 | func (o *OltDevice) GetExtValue(ctx context.Context, in *openolt.ValueParam) (*common_protos.ReturnValues, error) { |
| 1610 | return &common_protos.ReturnValues{}, nil |
| 1611 | } |
| 1612 | |
| 1613 | func (o *OltDevice) OnuItuPonAlarmSet(ctx context.Context, in *config.OnuItuPonAlarm) (*openolt.Empty, error) { |
| 1614 | return &openolt.Empty{}, nil |
| 1615 | } |
| 1616 | |
| 1617 | func (o *OltDevice) GetLogicalOnuDistanceZero(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) { |
| 1618 | return &openolt.OnuLogicalDistance{}, nil |
| 1619 | } |
| 1620 | |
| 1621 | func (o *OltDevice) GetLogicalOnuDistance(ctx context.Context, in *openolt.Onu) (*openolt.OnuLogicalDistance, error) { |
| 1622 | return &openolt.OnuLogicalDistance{}, nil |
| 1623 | } |
Matteo Scandolo | 96f8919 | 2021-03-12 13:17:26 -0800 | [diff] [blame] | 1624 | |
| 1625 | func (o *OltDevice) GetGemPortStatistics(ctx context.Context, in *openolt.OnuPacket) (*openolt.GemPortStatistics, error) { |
| 1626 | return &openolt.GemPortStatistics{}, nil |
| 1627 | } |
| 1628 | |
| 1629 | func (o *OltDevice) GetOnuStatistics(ctx context.Context, in *openolt.Onu) (*openolt.OnuStatistics, error) { |
| 1630 | return &openolt.OnuStatistics{}, nil |
| 1631 | } |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1632 | |
| 1633 | func (o *OltDevice) storeAllocId(flow *openolt.Flow) { |
| 1634 | o.AllocIDsLock.Lock() |
| 1635 | defer o.AllocIDsLock.Unlock() |
| 1636 | |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 1637 | if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)]; !ok { |
| 1638 | oltLogger.WithFields(log.Fields{ |
| 1639 | "IntfId": flow.AccessIntfId, |
| 1640 | "OnuId": flow.OnuId, |
| 1641 | "PortNo": flow.PortNo, |
| 1642 | "GemportId": flow.GemportId, |
| 1643 | "FlowId": flow.FlowId, |
| 1644 | }).Error("trying-to-store-alloc-id-for-unknown-onu") |
| 1645 | } |
| 1646 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1647 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 1648 | "IntfId": flow.AccessIntfId, |
| 1649 | "OnuId": flow.OnuId, |
| 1650 | "PortNo": flow.PortNo, |
| 1651 | "GemportId": flow.GemportId, |
| 1652 | "FlowId": flow.FlowId, |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1653 | }).Trace("storing-alloc-id-via-flow") |
| 1654 | |
| 1655 | if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo]; !ok { |
| 1656 | o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo] = make(map[int32]map[uint64]bool) |
| 1657 | } |
| 1658 | if _, ok := o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId]; !ok { |
| 1659 | o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId] = make(map[uint64]bool) |
| 1660 | } |
| 1661 | o.AllocIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.AllocId][flow.FlowId] = true |
| 1662 | } |
| 1663 | |
| 1664 | func (o *OltDevice) freeAllocId(flow *openolt.Flow) { |
| 1665 | // if this is the last flow referencing the AllocId then remove it |
| 1666 | o.AllocIDsLock.Lock() |
| 1667 | defer o.AllocIDsLock.Unlock() |
| 1668 | |
| 1669 | oltLogger.WithFields(log.Fields{ |
| 1670 | "IntfId": flow.AccessIntfId, |
| 1671 | "OnuId": flow.OnuId, |
| 1672 | "PortNo": flow.PortNo, |
| 1673 | "GemportId": flow.GemportId, |
| 1674 | }).Trace("freeing-alloc-id-via-flow") |
| 1675 | |
| 1676 | // NOTE look at the freeGemPortId implementation for comments and context |
| 1677 | for ponId, ponValues := range o.AllocIDs { |
| 1678 | for onuId, onuValues := range ponValues { |
| 1679 | for uniId, uniValues := range onuValues { |
| 1680 | for allocId, flows := range uniValues { |
| 1681 | for flowId := range flows { |
| 1682 | // if the flow matches, remove it from the map. |
| 1683 | if flow.FlowId == flowId { |
| 1684 | delete(o.AllocIDs[ponId][onuId][uniId][allocId], flow.FlowId) |
| 1685 | } |
| 1686 | // if that was the last flow for a particular allocId, remove the entire allocId |
| 1687 | if len(o.AllocIDs[ponId][onuId][uniId][allocId]) == 0 { |
| 1688 | delete(o.AllocIDs[ponId][onuId][uniId], allocId) |
| 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 1697 | func (o *OltDevice) storeGemPortId(ponId uint32, onuId uint32, portNo uint32, gemId int32, flowId uint64) { |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 1698 | o.GemPortIDsLock.Lock() |
| 1699 | defer o.GemPortIDsLock.Unlock() |
| 1700 | |
| 1701 | if _, ok := o.GemPortIDs[ponId][onuId]; !ok { |
| 1702 | oltLogger.WithFields(log.Fields{ |
| 1703 | "IntfId": ponId, |
| 1704 | "OnuId": onuId, |
| 1705 | "PortNo": portNo, |
| 1706 | "GemportId": gemId, |
| 1707 | "FlowId": flowId, |
| 1708 | }).Error("trying-to-store-gemport-for-unknown-onu") |
| 1709 | } |
| 1710 | |
| 1711 | oltLogger.WithFields(log.Fields{ |
| 1712 | "IntfId": ponId, |
| 1713 | "OnuId": onuId, |
| 1714 | "PortNo": portNo, |
| 1715 | "GemportId": gemId, |
| 1716 | "FlowId": flowId, |
| 1717 | }).Trace("storing-alloc-id-via-flow") |
| 1718 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 1719 | if _, ok := o.GemPortIDs[ponId][onuId][portNo]; !ok { |
| 1720 | o.GemPortIDs[ponId][onuId][portNo] = make(map[int32]map[uint64]bool) |
| 1721 | } |
| 1722 | if _, ok := o.GemPortIDs[ponId][onuId][portNo][gemId]; !ok { |
| 1723 | o.GemPortIDs[ponId][onuId][portNo][gemId] = make(map[uint64]bool) |
| 1724 | } |
| 1725 | o.GemPortIDs[ponId][onuId][portNo][gemId][flowId] = true |
| 1726 | } |
| 1727 | |
| 1728 | func (o *OltDevice) storeGemPortIdByFlow(flow *openolt.Flow) { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1729 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 1730 | "IntfId": flow.AccessIntfId, |
| 1731 | "OnuId": flow.OnuId, |
| 1732 | "PortNo": flow.PortNo, |
| 1733 | "GemportId": flow.GemportId, |
| 1734 | "FlowId": flow.FlowId, |
| 1735 | "ReplicateFlow": flow.ReplicateFlow, |
| 1736 | "PbitToGemport": flow.PbitToGemport, |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1737 | }).Trace("storing-gem-port-id-via-flow") |
| 1738 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 1739 | if flow.ReplicateFlow { |
| 1740 | for _, gem := range flow.PbitToGemport { |
| 1741 | o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, int32(gem), flow.FlowId) |
| 1742 | } |
| 1743 | } else { |
| 1744 | o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, flow.GemportId, flow.FlowId) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1745 | } |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | func (o *OltDevice) freeGemPortId(flow *openolt.Flow) { |
| 1749 | // if this is the last flow referencing the GemPort then remove it |
| 1750 | o.GemPortIDsLock.Lock() |
| 1751 | defer o.GemPortIDsLock.Unlock() |
| 1752 | |
| 1753 | oltLogger.WithFields(log.Fields{ |
| 1754 | "IntfId": flow.AccessIntfId, |
| 1755 | "OnuId": flow.OnuId, |
| 1756 | "PortNo": flow.PortNo, |
| 1757 | "GemportId": flow.GemportId, |
| 1758 | }).Trace("freeing-gem-port-id-via-flow") |
| 1759 | |
| 1760 | // NOTE that this loop is not very performant, it would be better if the flow carries |
| 1761 | // the same information that it carries during a FlowAdd. If so we can directly remove |
| 1762 | // items from the map |
| 1763 | |
| 1764 | //delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId], flow.FlowId) |
| 1765 | //if len(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo][flow.GemportId]) == 0 { |
| 1766 | // delete(o.GemPortIDs[uint32(flow.AccessIntfId)][uint32(flow.OnuId)][flow.PortNo], flow.GemportId) |
| 1767 | //} |
| 1768 | |
| 1769 | // NOTE this loop assumes that flow IDs are unique per device |
| 1770 | for ponId, ponValues := range o.GemPortIDs { |
| 1771 | for onuId, onuValues := range ponValues { |
| 1772 | for uniId, uniValues := range onuValues { |
| 1773 | for gemId, flows := range uniValues { |
| 1774 | for flowId := range flows { |
| 1775 | // if the flow matches, remove it from the map. |
| 1776 | if flow.FlowId == flowId { |
| 1777 | delete(o.GemPortIDs[ponId][onuId][uniId][gemId], flow.FlowId) |
| 1778 | } |
| 1779 | // if that was the last flow for a particular gem, remove the entire gem |
| 1780 | if len(o.GemPortIDs[ponId][onuId][uniId][gemId]) == 0 { |
| 1781 | delete(o.GemPortIDs[ponId][onuId][uniId], gemId) |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | // validateFlow checks that: |
| 1791 | // - the AllocId is not used in any flow referencing other ONUs/UNIs on the same PON |
| 1792 | // - the GemPortId is not used in any flow referencing other ONUs/UNIs on the same PON |
| 1793 | func (o *OltDevice) validateFlow(flow *openolt.Flow) error { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1794 | // validate gemPort |
| 1795 | o.GemPortIDsLock.RLock() |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 1796 | defer o.GemPortIDsLock.RUnlock() |
| 1797 | for onuId, onu := range o.GemPortIDs[uint32(flow.AccessIntfId)] { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1798 | if onuId == uint32(flow.OnuId) { |
| 1799 | continue |
| 1800 | } |
| 1801 | for uniId, uni := range onu { |
| 1802 | for gem := range uni { |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 1803 | if flow.ReplicateFlow { |
| 1804 | for _, flowGem := range flow.PbitToGemport { |
| 1805 | if gem == int32(flowGem) { |
| 1806 | return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-replicated-flow-%d", gem, uniId, onuId, flow.FlowId) |
| 1807 | } |
| 1808 | } |
| 1809 | } else { |
| 1810 | if gem == flow.GemportId { |
| 1811 | return fmt.Errorf("gem-%d-already-in-use-on-uni-%d-onu-%d-flow-%d", gem, uniId, onuId, flow.FlowId) |
| 1812 | } |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | o.AllocIDsLock.RLock() |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 1819 | defer o.AllocIDsLock.RUnlock() |
| 1820 | for onuId, onu := range o.AllocIDs[uint32(flow.AccessIntfId)] { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1821 | if onuId == uint32(flow.OnuId) { |
| 1822 | continue |
| 1823 | } |
| 1824 | for uniId, uni := range onu { |
| 1825 | for allocId := range uni { |
| 1826 | if allocId == flow.AllocId { |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 1827 | return fmt.Errorf("allocId-%d-already-in-use-on-uni-%d-onu-%d-flow-%d", allocId, uniId, onuId, flow.FlowId) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | return nil |
| 1834 | } |
| 1835 | |
| 1836 | // clearAllResources is invoked up OLT Reboot to remove all the allocated |
| 1837 | // GemPorts, AllocId and ONU-IDs across the PONs |
| 1838 | func (o *OltDevice) clearAllResources() { |
| 1839 | |
| 1840 | // remove the resources received via flows |
| 1841 | o.GemPortIDsLock.Lock() |
| 1842 | o.GemPortIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 1843 | o.GemPortIDsLock.Unlock() |
| 1844 | o.AllocIDsLock.Lock() |
| 1845 | o.AllocIDs = make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 1846 | o.AllocIDsLock.Unlock() |
| 1847 | |
| 1848 | // remove the resources received via OMCI |
| 1849 | for _, pon := range o.Pons { |
| 1850 | pon.removeAllAllocIds() |
| 1851 | pon.removeAllGemPorts() |
| 1852 | pon.removeAllOnuIds() |
| 1853 | } |
| 1854 | } |