Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame^] | 1 | package devices |
| 2 | |
| 3 | import ( |
| 4 | "gerrit.opencord.org/bbsim/api" |
| 5 | "github.com/looplab/fsm" |
| 6 | log "github.com/sirupsen/logrus" |
| 7 | ) |
| 8 | |
| 9 | func CreateONU(olt OltDevice, pon PonPort, id uint32) Onu { |
| 10 | o := Onu{ |
| 11 | ID: id, |
| 12 | OperState: DOWN, |
| 13 | PonPortID: pon.ID, |
| 14 | PonPort: pon, |
| 15 | } |
| 16 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID) |
| 17 | |
| 18 | o.InternalState = fsm.NewFSM( |
| 19 | "created", |
| 20 | fsm.Events{ |
| 21 | {Name: "discover", Src: []string{"created"}, Dst: "discovered"}, |
| 22 | {Name: "enable", Src: []string{"discovered"}, Dst: "enabled"}, |
| 23 | }, |
| 24 | fsm.Callbacks{ |
| 25 | "enter_state": func(e *fsm.Event) { |
| 26 | olt.stateChange(e) |
| 27 | }, |
| 28 | }, |
| 29 | ) |
| 30 | return o |
| 31 | } |
| 32 | |
| 33 | func (o Onu) stateChange(e *fsm.Event) { |
| 34 | log.WithFields(log.Fields{ |
| 35 | "onuID": o.ID, |
| 36 | "onuSN": o.SerialNumber, |
| 37 | "dstState": e.Dst, |
| 38 | "srcState": e.Src, |
| 39 | }).Debugf("ONU state has changed") |
| 40 | } |
| 41 | |
| 42 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
| 43 | |
| 44 | sn := new(openolt.SerialNumber) |
| 45 | |
| 46 | sn = new(openolt.SerialNumber) |
| 47 | sn.VendorId = []byte("BBSM") |
| 48 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 49 | |
| 50 | return sn |
| 51 | } |
| 52 | |
| 53 | func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 54 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
| 55 | IntfId: msg.Onu.PonPortID, |
| 56 | SerialNumber: msg.Onu.SerialNumber, |
| 57 | }} |
| 58 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
| 59 | log.Error("Failed to send Indication_OnuDiscInd: %v", err) |
| 60 | } |
| 61 | log.WithFields(log.Fields{ |
| 62 | "IntfId": msg.Onu.PonPortID, |
| 63 | "SerialNumber": msg.Onu.SerialNumber, |
| 64 | }).Debug("Sent Indication_OnuDiscInd") |
| 65 | } |
| 66 | |
| 67 | func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 68 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 69 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 70 | // so we're using the internal ID that is 1 |
| 71 | // o.ID = msg.OnuID |
| 72 | o.OperState = msg.OperState |
| 73 | |
| 74 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
| 75 | IntfId: o.PonPortID, |
| 76 | OnuId: o.ID, |
| 77 | OperState: o.OperState.String(), |
| 78 | AdminState: o.OperState.String(), |
| 79 | SerialNumber: o.SerialNumber, |
| 80 | }} |
| 81 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
| 82 | log.Error("Failed to send Indication_OnuInd: %v", err) |
| 83 | } |
| 84 | log.WithFields(log.Fields{ |
| 85 | "IntfId": o.PonPortID, |
| 86 | "OnuId": o.ID, |
| 87 | "OperState": msg.OperState.String(), |
| 88 | "AdminState": msg.OperState.String(), |
| 89 | "SerialNumber": o.SerialNumber, |
| 90 | }).Debug("Sent Indication_OnuInd") |
| 91 | } |