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 { |
| 41 | Enable() error |
| 42 | Disable() error |
| 43 | } |
| 44 | |
| 45 | type PotsPort struct { |
| 46 | ID uint32 |
| 47 | MeId omcilib.EntityID |
| 48 | PortNo uint32 |
| 49 | OperState *fsm.FSM |
| 50 | Onu *Onu |
| 51 | logger *log.Entry |
| 52 | } |
| 53 | |
| 54 | func NewPotsPort(ID uint32, onu *Onu) (*PotsPort, error) { |
| 55 | pots := PotsPort{ |
| 56 | ID: ID, |
| 57 | Onu: onu, |
| 58 | MeId: omcilib.GenerateUniPortEntityId(ID + 1), |
| 59 | } |
| 60 | |
| 61 | pots.logger = potsLogger.WithFields(log.Fields{ |
| 62 | "PotsUniId": pots.ID, |
| 63 | "OnuSn": onu.Sn(), |
| 64 | }) |
| 65 | |
| 66 | pots.OperState = fsm.NewFSM( |
| 67 | PotsStateDown, |
| 68 | fsm.Events{ |
| 69 | {Name: potsTxEnable, Src: []string{PotsStateDown}, Dst: PotsStateUp}, |
| 70 | {Name: potsTxDisable, Src: []string{PotsStateUp}, Dst: PotsStateDown}, |
| 71 | }, |
| 72 | fsm.Callbacks{ |
| 73 | "enter_state": func(e *fsm.Event) { |
| 74 | pots.logger.Debugf("changing-pots-operstate-from-%s-to-%s", e.Src, e.Dst) |
| 75 | }, |
| 76 | fmt.Sprintf("enter_%s", PotsStateUp): func(e *fsm.Event) { |
| 77 | msg := bbsimTypes.Message{ |
| 78 | Type: bbsimTypes.UniStatusAlarm, |
| 79 | Data: bbsimTypes.UniStatusAlarmMessage{ |
| 80 | OnuSN: pots.Onu.SerialNumber, |
| 81 | OnuID: pots.Onu.ID, |
| 82 | AdminState: 0, |
| 83 | EntityID: pots.MeId.ToUint16(), |
| 84 | RaiseOMCIAlarm: false, // never raise an LOS when enabling a UNI |
| 85 | }, |
| 86 | } |
| 87 | pots.Onu.Channel <- msg |
| 88 | }, |
| 89 | fmt.Sprintf("enter_%s", PotsStateDown): func(e *fsm.Event) { |
| 90 | msg := bbsimTypes.Message{ |
| 91 | Type: bbsimTypes.UniStatusAlarm, |
| 92 | Data: bbsimTypes.UniStatusAlarmMessage{ |
| 93 | OnuSN: pots.Onu.SerialNumber, |
| 94 | OnuID: pots.Onu.ID, |
| 95 | AdminState: 1, |
| 96 | EntityID: pots.MeId.ToUint16(), |
| 97 | RaiseOMCIAlarm: true, // raise an LOS when disabling a UNI |
| 98 | }, |
| 99 | } |
| 100 | pots.Onu.Channel <- msg |
| 101 | }, |
| 102 | }, |
| 103 | ) |
| 104 | |
| 105 | return &pots, nil |
| 106 | } |
| 107 | |
| 108 | func (u *PotsPort) StorePortNo(portNo uint32) { |
| 109 | u.PortNo = portNo |
| 110 | u.logger.WithFields(log.Fields{ |
| 111 | "PortNo": portNo, |
| 112 | }).Debug("logical-port-number-added-to-pots") |
| 113 | } |
| 114 | |
| 115 | func (u *PotsPort) Enable() error { |
| 116 | return u.OperState.Event(potsTxEnable) |
| 117 | } |
| 118 | |
| 119 | func (u *PotsPort) Disable() error { |
| 120 | if u.OperState.Is(PotsStateDown) { |
| 121 | return nil |
| 122 | } |
| 123 | return u.OperState.Event(potsTxDisable) |
| 124 | } |