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" |
| 21 | "errors" |
| 22 | "fmt" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 23 | bbsim "github.com/opencord/bbsim/internal/bbsim/types" |
| 24 | "github.com/google/gopacket" |
| 25 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 26 | "github.com/looplab/fsm" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/go/tech_profile" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 29 | log "github.com/sirupsen/logrus" |
| 30 | "google.golang.org/grpc" |
| 31 | "net" |
| 32 | "sync" |
| 33 | ) |
| 34 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 35 | var oltLogger = log.WithFields(log.Fields{ |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 36 | "module": "OLT", |
| 37 | }) |
| 38 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 39 | func init() { |
| 40 | //log.SetReportCaller(true) |
| 41 | log.SetLevel(log.DebugLevel) |
| 42 | } |
| 43 | |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 44 | var olt = OltDevice{} |
| 45 | |
| 46 | func GetOLT() OltDevice { |
| 47 | return olt |
| 48 | } |
| 49 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 50 | func CreateOLT(seq int, nni int, pon int, onuPerPon int, oltDoneChannel *chan bool, apiDoneChannel *chan bool, group *sync.WaitGroup) OltDevice { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 51 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 52 | "ID": seq, |
| 53 | "NumNni":nni, |
| 54 | "NumPon":pon, |
| 55 | "NumOnuPerPon":onuPerPon, |
| 56 | }).Debug("CreateOLT") |
| 57 | |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 58 | olt = OltDevice{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 59 | ID: seq, |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame^] | 60 | SerialNumber: fmt.Sprintf("BBSIM_OLT_%d", seq), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 61 | OperState: getOperStateFSM(func(e *fsm.Event) { |
| 62 | oltLogger.Debugf("Changing OLT OperState from %s to %s", e.Src, e.Dst) |
| 63 | }), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 64 | NumNni:nni, |
| 65 | NumPon:pon, |
| 66 | NumOnuPerPon:onuPerPon, |
| 67 | Pons: []PonPort{}, |
| 68 | Nnis: []NniPort{}, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 69 | channel: make(chan Message), |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 70 | oltDoneChannel: oltDoneChannel, |
| 71 | apiDoneChannel: apiDoneChannel, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // OLT State machine |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 75 | // 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] | 76 | olt.InternalState = fsm.NewFSM( |
| 77 | "created", |
| 78 | fsm.Events{ |
| 79 | {Name: "enable", Src: []string{"created"}, Dst: "enabled"}, |
| 80 | {Name: "disable", Src: []string{"enabled"}, Dst: "disabled"}, |
| 81 | }, |
| 82 | fsm.Callbacks{ |
| 83 | "enter_state": func(e *fsm.Event) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 84 | 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] | 85 | }, |
| 86 | }, |
| 87 | ) |
| 88 | |
| 89 | // create NNI Port |
| 90 | nniPort := NniPort{ |
| 91 | ID: uint32(0), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 92 | OperState: getOperStateFSM(func(e *fsm.Event) { |
| 93 | oltLogger.Debugf("Changing NNI OperState from %s to %s", e.Src, e.Dst) |
| 94 | }), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 95 | Type: "nni", |
| 96 | } |
| 97 | olt.Nnis = append(olt.Nnis, nniPort) |
| 98 | |
| 99 | // create PON ports |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 100 | //onuId := 1 |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 101 | for i := 0; i < pon; i++ { |
| 102 | p := PonPort{ |
| 103 | NumOnu: olt.NumOnuPerPon, |
| 104 | ID: uint32(i), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 105 | Type: "pon", |
| 106 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 107 | p.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 108 | oltLogger.WithFields(log.Fields{ |
| 109 | "ID": p.ID, |
| 110 | }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst) |
| 111 | }) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 112 | |
| 113 | // create ONU devices |
| 114 | for j := 0; j < onuPerPon; j++ { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 115 | //o := CreateONU(olt, p, uint32(onuId)) |
| 116 | o := CreateONU(olt, p, uint32(j + 1)) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 117 | p.Onus = append(p.Onus, o) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 118 | //onuId = onuId + 1 |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | olt.Pons = append(olt.Pons, p) |
| 122 | } |
| 123 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 124 | newOltServer(olt) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 125 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 126 | group.Done() |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 127 | return olt |
| 128 | } |
| 129 | |
| 130 | func newOltServer(o OltDevice) error { |
| 131 | // TODO make configurable |
| 132 | address := "0.0.0.0:50060" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 133 | lis, err := net.Listen("tcp", address) |
| 134 | if err != nil { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 135 | oltLogger.Fatalf("OLT failed to listen: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 136 | } |
| 137 | grpcServer := grpc.NewServer() |
| 138 | openolt.RegisterOpenoltServer(grpcServer, o) |
| 139 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 140 | wg := sync.WaitGroup{} |
| 141 | wg.Add(1) |
| 142 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 143 | go grpcServer.Serve(lis) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 144 | oltLogger.Debugf("OLT Listening on: %v", address) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 145 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 146 | for { |
| 147 | _, ok := <- *o.oltDoneChannel |
| 148 | if !ok { |
| 149 | // if the olt channel is closed, stop the gRPC server |
| 150 | log.Warnf("Stopping OLT gRPC server") |
| 151 | grpcServer.Stop() |
| 152 | wg.Done() |
| 153 | break |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | wg.Wait() |
| 158 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // Device Methods |
| 163 | |
| 164 | func (o OltDevice) Enable (stream openolt.Openolt_EnableIndicationServer) error { |
| 165 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 166 | oltLogger.Debug("Enable OLT called") |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 167 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 168 | wg := sync.WaitGroup{} |
| 169 | wg.Add(1) |
| 170 | |
| 171 | // create a channel for all the OLT events |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 172 | go o.processOltMessages(stream) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 173 | |
| 174 | // enable the OLT |
| 175 | olt_msg := Message{ |
| 176 | Type: OltIndication, |
| 177 | Data: OltIndicationMessage{ |
| 178 | OperState: UP, |
| 179 | }, |
| 180 | } |
| 181 | o.channel <- olt_msg |
| 182 | |
| 183 | // send NNI Port Indications |
| 184 | for _, nni := range o.Nnis { |
| 185 | msg := Message{ |
| 186 | Type: NniIndication, |
| 187 | Data: NniIndicationMessage{ |
| 188 | OperState: UP, |
| 189 | NniPortID: nni.ID, |
| 190 | }, |
| 191 | } |
| 192 | o.channel <- msg |
| 193 | } |
| 194 | |
| 195 | // send PON Port indications |
| 196 | for _, pon := range o.Pons { |
| 197 | msg := Message{ |
| 198 | Type: PonIndication, |
| 199 | Data: PonIndicationMessage{ |
| 200 | OperState: UP, |
| 201 | PonPortID: pon.ID, |
| 202 | }, |
| 203 | } |
| 204 | o.channel <- msg |
| 205 | |
| 206 | for _, onu := range pon.Onus { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 207 | go onu.processOnuMessages(stream) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 208 | go onu.processOmciMessages(stream) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 209 | msg := Message{ |
| 210 | Type: OnuDiscIndication, |
| 211 | Data: OnuDiscIndicationMessage{ |
| 212 | Onu: onu, |
| 213 | OperState: UP, |
| 214 | }, |
| 215 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 216 | onu.channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | wg.Wait() |
| 221 | return nil |
| 222 | } |
| 223 | |
| 224 | // Helpers method |
| 225 | |
| 226 | func (o OltDevice) getPonById(id uint32) (*PonPort, error) { |
| 227 | for _, pon := range o.Pons { |
| 228 | if pon.ID == id { |
| 229 | return &pon, nil |
| 230 | } |
| 231 | } |
| 232 | return nil, errors.New(fmt.Sprintf("Cannot find PonPort with id %d in OLT %d", id, o.ID)) |
| 233 | } |
| 234 | |
| 235 | func (o OltDevice) getNniById(id uint32) (*NniPort, error) { |
| 236 | for _, nni := range o.Nnis { |
| 237 | if nni.ID == id { |
| 238 | return &nni, nil |
| 239 | } |
| 240 | } |
| 241 | return nil, errors.New(fmt.Sprintf("Cannot find NniPort with id %d in OLT %d", id, o.ID)) |
| 242 | } |
| 243 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 244 | func (o OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 245 | data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}} |
| 246 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 247 | oltLogger.Errorf("Failed to send Indication_OltInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 250 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 251 | "OperState": msg.OperState, |
| 252 | }).Debug("Sent Indication_OltInd") |
| 253 | } |
| 254 | |
| 255 | func (o OltDevice) sendNniIndication(msg NniIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 256 | nni, _ := o.getNniById(msg.NniPortID) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 257 | nni.OperState.Event("enable") |
| 258 | // NOTE Operstate may need to be an integer |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 259 | operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{ |
| 260 | Type: nni.Type, |
| 261 | IntfId: nni.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 262 | OperState: nni.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 263 | }} |
| 264 | |
| 265 | if err := stream.Send(&openolt.Indication{Data: operData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 266 | oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 269 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 270 | "Type": nni.Type, |
| 271 | "IntfId": nni.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 272 | "OperState": nni.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 273 | }).Debug("Sent Indication_IntfOperInd for NNI") |
| 274 | } |
| 275 | |
| 276 | func (o OltDevice) sendPonIndication(msg PonIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 277 | pon, _ := o.getPonById(msg.PonPortID) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 278 | pon.OperState.Event("enable") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 279 | discoverData := &openolt.Indication_IntfInd{IntfInd: &openolt.IntfIndication{ |
| 280 | IntfId: pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 281 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 282 | }} |
| 283 | |
| 284 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 285 | oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 288 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 289 | "IntfId": pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 290 | "OperState": pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 291 | }).Debug("Sent Indication_IntfInd") |
| 292 | |
| 293 | operData := &openolt.Indication_IntfOperInd{IntfOperInd: &openolt.IntfOperIndication{ |
| 294 | Type: pon.Type, |
| 295 | IntfId: pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 296 | OperState: pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 297 | }} |
| 298 | |
| 299 | if err := stream.Send(&openolt.Indication{Data: operData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 300 | oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 303 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 304 | "Type": pon.Type, |
| 305 | "IntfId": pon.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 306 | "OperState": pon.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 307 | }).Debug("Sent Indication_IntfOperInd for PON") |
| 308 | } |
| 309 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 310 | func (o OltDevice) processOltMessages(stream openolt.Openolt_EnableIndicationServer) { |
| 311 | oltLogger.Debug("Started OLT Indication Channel") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 312 | for message := range o.channel { |
| 313 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 314 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 315 | oltLogger.WithFields(log.Fields{ |
| 316 | "oltId": o.ID, |
| 317 | "messageType": message.Type, |
| 318 | }).Trace("Received message") |
| 319 | |
| 320 | switch message.Type { |
| 321 | case OltIndication: |
| 322 | msg, _ := message.Data.(OltIndicationMessage) |
| 323 | if msg.OperState == UP { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 324 | o.InternalState.Event("enable") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 325 | o.OperState.Event("enable") |
| 326 | } else if msg.OperState == DOWN { |
| 327 | o.InternalState.Event("disable") |
| 328 | o.OperState.Event("disable") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 329 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 330 | o.sendOltIndication(msg, stream) |
| 331 | case NniIndication: |
| 332 | msg, _ := message.Data.(NniIndicationMessage) |
| 333 | o.sendNniIndication(msg, stream) |
| 334 | case PonIndication: |
| 335 | msg, _ := message.Data.(PonIndicationMessage) |
| 336 | o.sendPonIndication(msg, stream) |
| 337 | default: |
| 338 | oltLogger.Warnf("Received unknown message data %v for type %v in OLT channel", message.Data, message.Type) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // GRPC Endpoints |
| 345 | |
| 346 | 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] | 347 | oltLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 348 | "onuSerialNumber": onu.SerialNumber, |
| 349 | }).Info("Received ActivateOnu call from VOLTHA") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 350 | |
| 351 | pon, _ := o.getPonById(onu.IntfId) |
| 352 | _onu, _ := pon.getOnuBySn(onu.SerialNumber) |
| 353 | |
| 354 | // NOTE we need to immediately activate the ONU or the OMCI state machine won't start |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 355 | msg := Message{ |
| 356 | Type: OnuIndication, |
| 357 | Data: OnuIndicationMessage{ |
| 358 | OnuSN: onu.SerialNumber, |
| 359 | PonPortID: onu.IntfId, |
| 360 | OperState: UP, |
| 361 | }, |
| 362 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 363 | _onu.channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 364 | return new(openolt.Empty) , nil |
| 365 | } |
| 366 | |
| 367 | func (o OltDevice) DeactivateOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 368 | oltLogger.Error("DeactivateOnu not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 369 | return new(openolt.Empty) , nil |
| 370 | } |
| 371 | |
| 372 | func (o OltDevice) DeleteOnu(context.Context, *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 373 | oltLogger.Error("DeleteOnu not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 374 | return new(openolt.Empty) , nil |
| 375 | } |
| 376 | |
| 377 | func (o OltDevice) DisableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 378 | // NOTE when we disable the OLT should we disable NNI, PONs and ONUs altogether? |
| 379 | olt_msg := Message{ |
| 380 | Type: OltIndication, |
| 381 | Data: OltIndicationMessage{ |
| 382 | OperState: DOWN, |
| 383 | }, |
| 384 | } |
| 385 | o.channel <- olt_msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 386 | return new(openolt.Empty) , nil |
| 387 | } |
| 388 | |
| 389 | func (o OltDevice) DisablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 390 | oltLogger.Error("DisablePonIf not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 391 | return new(openolt.Empty) , nil |
| 392 | } |
| 393 | |
| 394 | func (o OltDevice) EnableIndication(_ *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 395 | oltLogger.WithField("oltId", o.ID).Info("OLT receives EnableIndication call from VOLTHA") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 396 | o.Enable(stream) |
| 397 | return nil |
| 398 | } |
| 399 | |
| 400 | func (o OltDevice) EnablePonIf(context.Context, *openolt.Interface) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 401 | oltLogger.Error("EnablePonIf not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 402 | return new(openolt.Empty) , nil |
| 403 | } |
| 404 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 405 | func (o OltDevice) FlowAdd(ctx context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
| 406 | oltLogger.WithFields(log.Fields{ |
| 407 | "IntfId": flow.AccessIntfId, |
| 408 | "OnuId": flow.OnuId, |
| 409 | "EthType": fmt.Sprintf("%x", flow.Classifier.EthType), |
| 410 | "InnerVlan": flow.Classifier.IVid, |
| 411 | "OuterVlan": flow.Classifier.OVid, |
| 412 | "FlowType": flow.FlowType, |
| 413 | "FlowId": flow.FlowId, |
| 414 | "UniID": flow.UniId, |
| 415 | "PortNo": flow.PortNo, |
| 416 | }).Infof("OLT receives Flow") |
| 417 | // TODO optionally store flows somewhere |
| 418 | |
| 419 | if flow.AccessIntfId == -1 { |
| 420 | oltLogger.WithFields(log.Fields{ |
| 421 | "FlowId": flow.FlowId, |
| 422 | }).Debugf("This is an OLT flow") |
| 423 | } else { |
| 424 | pon, _ := o.getPonById(uint32(flow.AccessIntfId)) |
| 425 | onu, _ := pon.getOnuById(uint32(flow.OnuId)) |
| 426 | |
| 427 | msg := Message{ |
| 428 | Type: FlowUpdate, |
| 429 | Data: OnuFlowUpdateMessage{ |
| 430 | PonPortID: pon.ID, |
| 431 | OnuID: onu.ID, |
| 432 | Flow: flow, |
| 433 | }, |
| 434 | } |
| 435 | onu.channel <- msg |
| 436 | } |
| 437 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 438 | return new(openolt.Empty) , nil |
| 439 | } |
| 440 | |
| 441 | func (o OltDevice) FlowRemove(context.Context, *openolt.Flow) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 442 | oltLogger.Info("received FlowRemove") |
| 443 | // TODO store flows somewhere |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 444 | return new(openolt.Empty) , nil |
| 445 | } |
| 446 | |
| 447 | func (o OltDevice) HeartbeatCheck(context.Context, *openolt.Empty) (*openolt.Heartbeat, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 448 | oltLogger.Error("HeartbeatCheck not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 449 | return new(openolt.Heartbeat) , nil |
| 450 | } |
| 451 | |
| 452 | func (o OltDevice) GetDeviceInfo(context.Context, *openolt.Empty) (*openolt.DeviceInfo, error) { |
| 453 | |
Matteo Scandolo | da9cbe2 | 2019-08-19 16:05:10 -0700 | [diff] [blame] | 454 | oltLogger.WithFields(log.Fields{ |
| 455 | "oltId": o.ID, |
| 456 | "PonPorts": o.NumPon, |
| 457 | }).Info("OLT receives GetDeviceInfo call from VOLTHA") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 458 | devinfo := new(openolt.DeviceInfo) |
| 459 | devinfo.Vendor = "BBSim" |
| 460 | devinfo.Model = "asfvolt16" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 461 | devinfo.HardwareVersion = "emulated" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 462 | devinfo.FirmwareVersion = "" |
| 463 | devinfo.Technology = "xgspon" |
Matteo Scandolo | da9cbe2 | 2019-08-19 16:05:10 -0700 | [diff] [blame] | 464 | devinfo.PonPorts = uint32(o.NumPon) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 465 | devinfo.OnuIdStart = 1 |
| 466 | devinfo.OnuIdEnd = 255 |
| 467 | devinfo.AllocIdStart = 1024 |
| 468 | devinfo.AllocIdEnd = 16383 |
| 469 | devinfo.GemportIdStart = 1024 |
| 470 | devinfo.GemportIdEnd = 65535 |
| 471 | devinfo.FlowIdStart = 1 |
| 472 | devinfo.FlowIdEnd = 16383 |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame^] | 473 | devinfo.DeviceSerialNumber = o.SerialNumber |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 474 | |
| 475 | return devinfo, nil |
| 476 | } |
| 477 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 478 | func (o OltDevice) OmciMsgOut(ctx context.Context, omci_msg *openolt.OmciMsg) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 479 | pon, _ := o.getPonById(omci_msg.IntfId) |
| 480 | onu, _ := pon.getOnuById(omci_msg.OnuId) |
| 481 | msg := Message{ |
| 482 | Type: OMCI, |
| 483 | Data: OmciMessage{ |
| 484 | OnuSN: onu.SerialNumber, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 485 | OnuID: onu.ID, |
| 486 | omciMsg: omci_msg, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 487 | }, |
| 488 | } |
| 489 | onu.channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 490 | return new(openolt.Empty) , nil |
| 491 | } |
| 492 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 493 | func (o OltDevice) OnuPacketOut(ctx context.Context, onuPkt *openolt.OnuPacket) (*openolt.Empty, error) { |
| 494 | pon, _ := o.getPonById(onuPkt.IntfId) |
| 495 | onu, _ := pon.getOnuById(onuPkt.OnuId) |
| 496 | |
| 497 | rawpkt := gopacket.NewPacket(onuPkt.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 498 | |
| 499 | // NOTE is this the best way to the to the ethertype? |
| 500 | etherType := rawpkt.Layer(layers.LayerTypeEthernet).(*layers.Ethernet).EthernetType |
| 501 | |
| 502 | if etherType == layers.EthernetTypeEAPOL { |
| 503 | eapolPkt := bbsim.ByteMsg{IntfId: onuPkt.IntfId, OnuId: onuPkt.OnuId, Bytes: rawpkt.Data()} |
| 504 | onu.eapolPktOutCh <- &eapolPkt |
| 505 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 506 | return new(openolt.Empty) , nil |
| 507 | } |
| 508 | |
| 509 | func (o OltDevice) Reboot(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 510 | oltLogger.Info("Shutting Down") |
| 511 | close(*o.oltDoneChannel) |
| 512 | close(*o.apiDoneChannel) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 513 | return new(openolt.Empty) , nil |
| 514 | } |
| 515 | |
| 516 | func (o OltDevice) ReenableOlt(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 517 | oltLogger.Error("ReenableOlt not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 518 | return new(openolt.Empty) , nil |
| 519 | } |
| 520 | |
| 521 | func (o OltDevice) UplinkPacketOut(context context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 522 | oltLogger.Warn("UplinkPacketOut not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 523 | return new(openolt.Empty) , nil |
| 524 | } |
| 525 | |
| 526 | func (o OltDevice) CollectStatistics(context.Context, *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 527 | oltLogger.Error("CollectStatistics not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 528 | return new(openolt.Empty) , nil |
| 529 | } |
| 530 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 531 | 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] | 532 | oltLogger.Error("GetOnuInfo not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 533 | return new(openolt.OnuIndication) , nil |
| 534 | } |
| 535 | |
| 536 | 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] | 537 | oltLogger.Error("GetPonIf not implemented") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 538 | return new(openolt.IntfIndication) , nil |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | func (s OltDevice) CreateTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 542 | oltLogger.Info("received CreateTrafficQueues") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 543 | return new(openolt.Empty), nil |
| 544 | } |
| 545 | |
| 546 | func (s OltDevice) RemoveTrafficQueues(context.Context, *tech_profile.TrafficQueues) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 547 | oltLogger.Info("received RemoveTrafficQueues") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 548 | return new(openolt.Empty), nil |
| 549 | } |
| 550 | |
| 551 | func (s OltDevice) CreateTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 552 | oltLogger.Info("received CreateTrafficSchedulers") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 553 | return new(openolt.Empty), nil |
| 554 | } |
| 555 | |
| 556 | func (s OltDevice) RemoveTrafficSchedulers(context.Context, *tech_profile.TrafficSchedulers) (*openolt.Empty, error) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 557 | oltLogger.Info("received RemoveTrafficSchedulers") |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 558 | return new(openolt.Empty), nil |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 559 | } |