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