Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 1 | package devices |
| 2 | |
| 3 | import ( |
Matteo Scandolo | d54283a | 2019-08-13 16:22:31 -0700 | [diff] [blame] | 4 | "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 5 | "github.com/looplab/fsm" |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame^] | 6 | omci "github.com/opencord/omci-sim" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 7 | log "github.com/sirupsen/logrus" |
| 8 | ) |
| 9 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 10 | var onuLogger = log.WithFields(log.Fields{ |
| 11 | "module": "ONU", |
| 12 | }) |
| 13 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 14 | func CreateONU(olt OltDevice, pon PonPort, id uint32) Onu { |
| 15 | o := Onu{ |
| 16 | ID: id, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | PonPortID: pon.ID, |
| 18 | PonPort: pon, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 19 | channel: make(chan Message), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 20 | } |
| 21 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID) |
| 22 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 23 | // NOTE this state machine is used to track the operational |
| 24 | // state as requested by VOLTHA |
| 25 | o.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 26 | onuLogger.WithFields(log.Fields{ |
| 27 | "ID": o.ID, |
| 28 | }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst) |
| 29 | }) |
| 30 | |
| 31 | // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 32 | o.InternalState = fsm.NewFSM( |
| 33 | "created", |
| 34 | fsm.Events{ |
| 35 | {Name: "discover", Src: []string{"created"}, Dst: "discovered"}, |
| 36 | {Name: "enable", Src: []string{"discovered"}, Dst: "enabled"}, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 37 | {Name: "start_omci", Src: []string{"enabled"}, Dst: "starting_openomci"}, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 38 | }, |
| 39 | fsm.Callbacks{ |
| 40 | "enter_state": func(e *fsm.Event) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 41 | onuLogger.WithFields(log.Fields{ |
| 42 | "ID": o.ID, |
| 43 | }).Debugf("Changing ONU InternalState from %s to %s", e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 44 | }, |
| 45 | }, |
| 46 | ) |
| 47 | return o |
| 48 | } |
| 49 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 50 | func (o Onu) processOnuMessages(stream openolt.Openolt_EnableIndicationServer) { |
| 51 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 52 | "onuID": o.ID, |
| 53 | "onuSN": o.SerialNumber, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 54 | }).Debug("Started ONU Indication Channel") |
| 55 | |
| 56 | for message := range o.channel { |
| 57 | onuLogger.WithFields(log.Fields{ |
| 58 | "onuID": o.ID, |
| 59 | "onuSN": o.SerialNumber, |
| 60 | "messageType": message.Type, |
| 61 | }).Trace("Received message") |
| 62 | |
| 63 | switch message.Type { |
| 64 | case OnuDiscIndication: |
| 65 | msg, _ := message.Data.(OnuDiscIndicationMessage) |
| 66 | o.sendOnuDiscIndication(msg, stream) |
| 67 | case OnuIndication: |
| 68 | msg, _ := message.Data.(OnuIndicationMessage) |
| 69 | o.sendOnuIndication(msg, stream) |
| 70 | case OMCI: |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame^] | 71 | msg, _ := message.Data.(OmciMessage) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 72 | o.InternalState.Event("start_omci") |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame^] | 73 | o.handleOmciMessage(msg, stream) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 74 | default: |
| 75 | onuLogger.Warnf("Received unknown message data %v for type %v in OLT channel", message.Data, message.Type) |
| 76 | } |
| 77 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
| 81 | |
| 82 | sn := new(openolt.SerialNumber) |
| 83 | |
| 84 | sn = new(openolt.SerialNumber) |
| 85 | sn.VendorId = []byte("BBSM") |
| 86 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 87 | |
| 88 | return sn |
| 89 | } |
| 90 | |
| 91 | func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 92 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
| 93 | IntfId: msg.Onu.PonPortID, |
| 94 | SerialNumber: msg.Onu.SerialNumber, |
| 95 | }} |
| 96 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
| 97 | log.Error("Failed to send Indication_OnuDiscInd: %v", err) |
| 98 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 99 | o.InternalState.Event("discover") |
| 100 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 101 | "IntfId": msg.Onu.PonPortID, |
| 102 | "SerialNumber": msg.Onu.SerialNumber, |
| 103 | }).Debug("Sent Indication_OnuDiscInd") |
| 104 | } |
| 105 | |
| 106 | func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 107 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 108 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 109 | // so we're using the internal ID that is 1 |
| 110 | // o.ID = msg.OnuID |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 111 | o.OperState.Event("enable") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 112 | |
| 113 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
| 114 | IntfId: o.PonPortID, |
| 115 | OnuId: o.ID, |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 116 | OperState: o.OperState.Current(), |
| 117 | AdminState: o.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 118 | SerialNumber: o.SerialNumber, |
| 119 | }} |
| 120 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
| 121 | log.Error("Failed to send Indication_OnuInd: %v", err) |
| 122 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 123 | o.InternalState.Event("enable") |
| 124 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 125 | "IntfId": o.PonPortID, |
| 126 | "OnuId": o.ID, |
| 127 | "OperState": msg.OperState.String(), |
| 128 | "AdminState": msg.OperState.String(), |
| 129 | "SerialNumber": o.SerialNumber, |
| 130 | }).Debug("Sent Indication_OnuInd") |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame^] | 131 | } |
| 132 | |
| 133 | func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 134 | |
| 135 | onuLogger.WithFields(log.Fields{ |
| 136 | "IntfId": o.PonPortID, |
| 137 | "SerialNumber": o.SerialNumber, |
| 138 | "omciPacket": msg.omciMsg.Pkt, |
| 139 | }).Tracef("Received OMCI message") |
| 140 | |
| 141 | var omciInd openolt.OmciIndication |
| 142 | respPkt, err := omci.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt)) |
| 143 | if err != nil { |
| 144 | onuLogger.Errorf("Error handling OMCI message %v", msg) |
| 145 | } |
| 146 | |
| 147 | omciInd.IntfId = o.PonPortID |
| 148 | omciInd.OnuId = o.ID |
| 149 | omciInd.Pkt = respPkt |
| 150 | |
| 151 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 152 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
| 153 | onuLogger.Error("send omci indication failed: %v", err) |
| 154 | } |
| 155 | onuLogger.WithFields(log.Fields{ |
| 156 | "IntfId": o.PonPortID, |
| 157 | "SerialNumber": o.SerialNumber, |
| 158 | "omciPacket": omciInd.Pkt, |
| 159 | }).Tracef("Sent OMCI message") |
| 160 | } |
| 161 | |
| 162 | // HexDecode converts the hex encoding to binary |
| 163 | func HexDecode(pkt []byte) []byte { |
| 164 | p := make([]byte, len(pkt)/2) |
| 165 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
| 166 | // Go figure this ;) |
| 167 | u := (pkt[i] & 15) + (pkt[i]>>6)*9 |
| 168 | l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9 |
| 169 | p[j] = u<<4 + l |
| 170 | } |
| 171 | onuLogger.Tracef("Omci decoded: %x.", p) |
| 172 | return p |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 173 | } |