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