blob: 172ead48aecc23a1af798fb2d2f1b88071d50013 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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 Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
Matteo Scandolod54283a2019-08-13 16:22:31 -070020 "github.com/opencord/voltha-protos/go/openolt"
Matteo Scandolo4747d292019-08-05 11:50:18 -070021 "github.com/looplab/fsm"
Matteo Scandoloc559ef12019-08-20 13:24:21 -070022 omci "github.com/opencord/omci-sim"
Matteo Scandolo4747d292019-08-05 11:50:18 -070023 log "github.com/sirupsen/logrus"
24)
25
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070026var onuLogger = log.WithFields(log.Fields{
27 "module": "ONU",
28})
29
Matteo Scandolo4747d292019-08-05 11:50:18 -070030func CreateONU(olt OltDevice, pon PonPort, id uint32) Onu {
31 o := Onu{
32 ID: id,
Matteo Scandolo4747d292019-08-05 11:50:18 -070033 PonPortID: pon.ID,
34 PonPort: pon,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070035 channel: make(chan Message),
Matteo Scandolo4747d292019-08-05 11:50:18 -070036 }
37 o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
38
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070039 // 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 Scandolo4747d292019-08-05 11:50:18 -070048 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 Scandolo9a3518c2019-08-13 14:36:01 -070053 {Name: "start_omci", Src: []string{"enabled"}, Dst: "starting_openomci"},
Matteo Scandolo4747d292019-08-05 11:50:18 -070054 },
55 fsm.Callbacks{
56 "enter_state": func(e *fsm.Event) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070057 onuLogger.WithFields(log.Fields{
58 "ID": o.ID,
59 }).Debugf("Changing ONU InternalState from %s to %s", e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -070060 },
61 },
62 )
63 return o
64}
65
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070066func (o Onu) processOnuMessages(stream openolt.Openolt_EnableIndicationServer) {
67 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -070068 "onuID": o.ID,
69 "onuSN": o.SerialNumber,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070070 }).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 Scandoloc559ef12019-08-20 13:24:21 -070087 msg, _ := message.Data.(OmciMessage)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070088 o.InternalState.Event("start_omci")
Matteo Scandoloc559ef12019-08-20 13:24:21 -070089 o.handleOmciMessage(msg, stream)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070090 default:
91 onuLogger.Warnf("Received unknown message data %v for type %v in OLT channel", message.Data, message.Type)
92 }
93 }
Matteo Scandolo4747d292019-08-05 11:50:18 -070094}
95
96func (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
107func (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 Scandolo11006992019-08-28 11:29:46 -0700113 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700114 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700115 o.InternalState.Event("discover")
116 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700117 "IntfId": msg.Onu.PonPortID,
118 "SerialNumber": msg.Onu.SerialNumber,
119 }).Debug("Sent Indication_OnuDiscInd")
120}
121
122func (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 Scandolo9a3518c2019-08-13 14:36:01 -0700127 o.OperState.Event("enable")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700128
129 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
130 IntfId: o.PonPortID,
131 OnuId: o.ID,
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700132 OperState: o.OperState.Current(),
133 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700134 SerialNumber: o.SerialNumber,
135 }}
136 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700137 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700138 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700139 o.InternalState.Event("enable")
140 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700141 "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 Scandoloc559ef12019-08-20 13:24:21 -0700147}
148
149func (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 Scandolo11006992019-08-28 11:29:46 -0700169 onuLogger.Errorf("send omci indication failed: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700170 }
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
179func 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 Scandolo4747d292019-08-05 11:50:18 -0700189}