Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [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 | "fmt" |
| 21 | |
| 22 | "github.com/looplab/fsm" |
| 23 | bbsimTypes "github.com/opencord/bbsim/internal/bbsim/types" |
| 24 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | ) |
| 27 | |
| 28 | var potsLogger = log.WithFields(log.Fields{ |
| 29 | "module": "POTS", |
| 30 | }) |
| 31 | |
| 32 | const ( |
| 33 | PotsStateUp = "up" |
| 34 | PotsStateDown = "down" |
| 35 | |
| 36 | potsTxEnable = "enable" |
| 37 | potsTxDisable = "disable" |
| 38 | ) |
| 39 | |
| 40 | type PotsPortIf interface { |
Matteo Scandolo | fbb94ae | 2022-04-14 14:17:47 -0700 | [diff] [blame^] | 41 | GetID() uint32 |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 42 | Enable() error |
| 43 | Disable() error |
| 44 | } |
| 45 | |
| 46 | type PotsPort struct { |
| 47 | ID uint32 |
| 48 | MeId omcilib.EntityID |
| 49 | PortNo uint32 |
| 50 | OperState *fsm.FSM |
| 51 | Onu *Onu |
| 52 | logger *log.Entry |
| 53 | } |
| 54 | |
| 55 | func NewPotsPort(ID uint32, onu *Onu) (*PotsPort, error) { |
| 56 | pots := PotsPort{ |
| 57 | ID: ID, |
| 58 | Onu: onu, |
| 59 | MeId: omcilib.GenerateUniPortEntityId(ID + 1), |
| 60 | } |
| 61 | |
| 62 | pots.logger = potsLogger.WithFields(log.Fields{ |
| 63 | "PotsUniId": pots.ID, |
| 64 | "OnuSn": onu.Sn(), |
| 65 | }) |
| 66 | |
| 67 | pots.OperState = fsm.NewFSM( |
| 68 | PotsStateDown, |
| 69 | fsm.Events{ |
| 70 | {Name: potsTxEnable, Src: []string{PotsStateDown}, Dst: PotsStateUp}, |
| 71 | {Name: potsTxDisable, Src: []string{PotsStateUp}, Dst: PotsStateDown}, |
| 72 | }, |
| 73 | fsm.Callbacks{ |
| 74 | "enter_state": func(e *fsm.Event) { |
| 75 | pots.logger.Debugf("changing-pots-operstate-from-%s-to-%s", e.Src, e.Dst) |
| 76 | }, |
| 77 | fmt.Sprintf("enter_%s", PotsStateUp): func(e *fsm.Event) { |
| 78 | msg := bbsimTypes.Message{ |
| 79 | Type: bbsimTypes.UniStatusAlarm, |
| 80 | Data: bbsimTypes.UniStatusAlarmMessage{ |
| 81 | OnuSN: pots.Onu.SerialNumber, |
| 82 | OnuID: pots.Onu.ID, |
| 83 | AdminState: 0, |
| 84 | EntityID: pots.MeId.ToUint16(), |
| 85 | RaiseOMCIAlarm: false, // never raise an LOS when enabling a UNI |
| 86 | }, |
| 87 | } |
| 88 | pots.Onu.Channel <- msg |
| 89 | }, |
| 90 | fmt.Sprintf("enter_%s", PotsStateDown): func(e *fsm.Event) { |
| 91 | msg := bbsimTypes.Message{ |
| 92 | Type: bbsimTypes.UniStatusAlarm, |
| 93 | Data: bbsimTypes.UniStatusAlarmMessage{ |
| 94 | OnuSN: pots.Onu.SerialNumber, |
| 95 | OnuID: pots.Onu.ID, |
| 96 | AdminState: 1, |
| 97 | EntityID: pots.MeId.ToUint16(), |
| 98 | RaiseOMCIAlarm: true, // raise an LOS when disabling a UNI |
| 99 | }, |
| 100 | } |
| 101 | pots.Onu.Channel <- msg |
| 102 | }, |
| 103 | }, |
| 104 | ) |
| 105 | |
| 106 | return &pots, nil |
| 107 | } |
| 108 | |
Matteo Scandolo | fbb94ae | 2022-04-14 14:17:47 -0700 | [diff] [blame^] | 109 | func (p *PotsPort) GetID() uint32 { |
| 110 | return p.ID |
| 111 | } |
| 112 | |
| 113 | func (p *PotsPort) StorePortNo(portNo uint32) { |
| 114 | p.PortNo = portNo |
| 115 | p.logger.WithFields(log.Fields{ |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 116 | "PortNo": portNo, |
| 117 | }).Debug("logical-port-number-added-to-pots") |
| 118 | } |
| 119 | |
Matteo Scandolo | fbb94ae | 2022-04-14 14:17:47 -0700 | [diff] [blame^] | 120 | func (p *PotsPort) Enable() error { |
| 121 | return p.OperState.Event(potsTxEnable) |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Matteo Scandolo | fbb94ae | 2022-04-14 14:17:47 -0700 | [diff] [blame^] | 124 | func (p *PotsPort) Disable() error { |
| 125 | if p.OperState.Is(PotsStateDown) { |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 126 | return nil |
| 127 | } |
Matteo Scandolo | fbb94ae | 2022-04-14 14:17:47 -0700 | [diff] [blame^] | 128 | return p.OperState.Event(potsTxDisable) |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 129 | } |