Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "errors" |
| 22 | "fmt" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 23 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 24 | "github.com/looplab/fsm" |
Matteo Scandolo | 3de9de0 | 2019-11-14 13:40:03 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v2/go/openolt" |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame^] | 26 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type PonPort struct { |
| 30 | // BBSIM Internals |
| 31 | ID uint32 |
| 32 | NumOnu int |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 33 | Onus []*Onu |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 34 | Olt OltDevice |
| 35 | |
| 36 | // PON Attributes |
| 37 | OperState *fsm.FSM |
| 38 | Type string |
| 39 | |
| 40 | // NOTE do we need a state machine for the PON Ports? |
| 41 | } |
| 42 | |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame^] | 43 | // CreatePonPort creates pon port object |
| 44 | func CreatePonPort(olt OltDevice, id uint32) *PonPort { |
| 45 | |
| 46 | ponPort := PonPort{ |
| 47 | NumOnu: olt.NumOnuPerPon, |
| 48 | ID: id, |
| 49 | Type: "pon", |
| 50 | Olt: olt, |
| 51 | Onus: []*Onu{}, |
| 52 | } |
| 53 | |
| 54 | ponPort.OperState = fsm.NewFSM( |
| 55 | "down", |
| 56 | fsm.Events{ |
| 57 | {Name: "enable", Src: []string{"down"}, Dst: "up"}, |
| 58 | {Name: "disable", Src: []string{"up"}, Dst: "down"}, |
| 59 | }, |
| 60 | fsm.Callbacks{ |
| 61 | "enter_up": func(e *fsm.Event) { |
| 62 | oltLogger.WithFields(log.Fields{ |
| 63 | "ID": ponPort.ID, |
| 64 | }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst) |
| 65 | }, |
| 66 | "enter_down": func(e *fsm.Event) { |
| 67 | oltLogger.WithFields(log.Fields{ |
| 68 | "ID": ponPort.ID, |
| 69 | }).Debugf("Changing PON Port OperState from %s to %s", e.Src, e.Dst) |
| 70 | |
| 71 | for _, onu := range ponPort.Onus { |
| 72 | if err := onu.InternalState.Event("pon_disabled"); err != nil { |
| 73 | oltLogger.Errorf("Failed to move ONU in pon_disabled states: %v", err) |
| 74 | } |
| 75 | } |
| 76 | }, |
| 77 | }, |
| 78 | ) |
| 79 | return &ponPort |
| 80 | } |
| 81 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 82 | func (p PonPort) GetOnuBySn(sn *openolt.SerialNumber) (*Onu, error) { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 83 | for _, onu := range p.Onus { |
| 84 | if bytes.Equal(onu.SerialNumber.VendorSpecific, sn.VendorSpecific) { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 85 | return onu, nil |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return nil, errors.New(fmt.Sprintf("Cannot find Onu with serial number %d in PonPort %d", sn, p.ID)) |
| 89 | } |
| 90 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 91 | func (p PonPort) GetOnuById(id uint32) (*Onu, error) { |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 92 | for _, onu := range p.Onus { |
| 93 | if onu.ID == id { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 94 | return onu, nil |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | return nil, errors.New(fmt.Sprintf("Cannot find Onu with id %d in PonPort %d", id, p.ID)) |
| 98 | } |