blob: f4147dd516eadf6a0c6815251515c9cd8cf6021c [file] [log] [blame]
Shad Ansari2eac6a42018-11-14 22:35:39 -08001/*
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
17package core
18
Shad Ansari45806172018-11-19 17:13:08 -080019import (
Shad Ansaria5c79892018-11-29 16:32:59 -080020 "bytes"
21 "encoding/binary"
22
Shad Ansari45806172018-11-19 17:13:08 -080023 "gerrit.opencord.org/voltha-bbsim/common/logger"
Shad Ansaria5c79892018-11-29 16:32:59 -080024 "gerrit.opencord.org/voltha-bbsim/protos"
Shad Ansari45806172018-11-19 17:13:08 -080025)
26
Shad Ansaria5c79892018-11-29 16:32:59 -080027//
28// OMCI definitions
29//
30
31// OmciMsgType represents a OMCI message-type
32type OmciMsgType byte
33
34const (
35 // Message Types
36 _ = iota
37 Create OmciMsgType = 4
38 Delete OmciMsgType = 6
39 Set OmciMsgType = 8
40 Get OmciMsgType = 9
41 GetAllAlarms OmciMsgType = 11
42 GetAllAlarmsNext OmciMsgType = 12
43 MibUpload OmciMsgType = 13
44 MibUploadNext OmciMsgType = 14
45 MibReset OmciMsgType = 15
46 AlarmNotification OmciMsgType = 16
47 AttributeValueChange OmciMsgType = 17
48 Test OmciMsgType = 18
49 StartSoftwareDownload OmciMsgType = 19
50 DownloadSection OmciMsgType = 20
51 EndSoftwareDownload OmciMsgType = 21
52 ActivateSoftware OmciMsgType = 22
53 CommitSoftware OmciMsgType = 23
54 SynchronizeTime OmciMsgType = 24
55 Reboot OmciMsgType = 25
56 GetNext OmciMsgType = 26
57 TestResult OmciMsgType = 27
58 GetCurrentData OmciMsgType = 28
59 SetTable OmciMsgType = 29 // Defined in Extended Message Set Only
60)
61
62const (
63 // Managed Entity Class values
64 GEMPortNetworkCTP OmciClass = 268
65)
66
67// OMCI Managed Entity Class
68type OmciClass uint16
69
70// OMCI Message Identifier
71type OmciMessageIdentifier struct {
72 Class OmciClass
73 Instance uint16
74}
75
76type OmciContent [32]byte
77
78type OmciMessage struct {
79 TransactionId uint16
80 MessageType OmciMsgType
81 DeviceId uint8
82 MessageId OmciMessageIdentifier
83 Content OmciContent
Shad Ansari2eac6a42018-11-14 22:35:39 -080084}
85
Shad Ansari45806172018-11-19 17:13:08 -080086const NumMibUploads byte = 9
87
88type OnuKey struct {
89 IntfId, OnuId uint32
90}
91
92type OnuState struct {
Shad Ansaria5c79892018-11-29 16:32:59 -080093 gemPortId uint16
Shad Ansari45806172018-11-19 17:13:08 -080094 mibUploadCtr uint16
95 uniGInstance uint8
96 pptpInstance uint8
97}
98
Shad Ansaria5c79892018-11-29 16:32:59 -080099var Onus = map[OnuKey]*OnuState{}
Shad Ansari45806172018-11-19 17:13:08 -0800100
Shad Ansaria5c79892018-11-29 16:32:59 -0800101func OmciRun(omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication) {
Shad Ansari45806172018-11-19 17:13:08 -0800102
Shad Ansari2eac6a42018-11-14 22:35:39 -0800103 for {
Shad Ansaria5c79892018-11-29 16:32:59 -0800104 var resp openolt.OmciIndication
Shad Ansari5bb48db2018-11-17 22:03:41 -0800105
Shad Ansari45806172018-11-19 17:13:08 -0800106 m := <-omciOut
Shad Ansari5bb48db2018-11-17 22:03:41 -0800107
Shad Ansaria5c79892018-11-29 16:32:59 -0800108 transactionId, deviceId, msgType, class, instance, content := ParsePkt(m.Pkt)
Shad Ansari5bb48db2018-11-17 22:03:41 -0800109
Shad Ansari45806172018-11-19 17:13:08 -0800110 logger.Debug("OmciRun - transactionId: %d msgType: %d, ME Class: %d, ME Instance: %d",
Shad Ansaria5c79892018-11-29 16:32:59 -0800111 transactionId, msgType, class, instance)
Shad Ansari45806172018-11-19 17:13:08 -0800112
113 key := OnuKey{m.IntfId, m.OnuId}
Shad Ansaria5c79892018-11-29 16:32:59 -0800114 if _, ok := Onus[key]; !ok {
115 Onus[key] = NewOnuState()
Shad Ansari45806172018-11-19 17:13:08 -0800116 }
117 switch msgType {
Shad Ansaria5c79892018-11-29 16:32:59 -0800118 case MibReset:
119 resp.Pkt = mibReset()
Shad Ansari45806172018-11-19 17:13:08 -0800120 case 13:
Shad Ansaria5c79892018-11-29 16:32:59 -0800121 resp.Pkt = mibUpload()
Shad Ansari45806172018-11-19 17:13:08 -0800122 case 14:
Shad Ansaria5c79892018-11-29 16:32:59 -0800123 resp.Pkt = mibUploadNext(Onus[key])
Shad Ansari45806172018-11-19 17:13:08 -0800124 case 8:
Shad Ansaria5c79892018-11-29 16:32:59 -0800125 resp.Pkt = set()
Shad Ansari45806172018-11-19 17:13:08 -0800126 case 4:
Shad Ansaria5c79892018-11-29 16:32:59 -0800127 resp.Pkt = create(class, content, key)
Shad Ansari45806172018-11-19 17:13:08 -0800128 case 9:
Shad Ansaria5c79892018-11-29 16:32:59 -0800129 resp.Pkt = get()
130 case 11:
131 resp.Pkt = getAllAlarms()
Shad Ansari45806172018-11-19 17:13:08 -0800132 default:
Shad Ansariab44c212018-11-26 11:54:26 -0800133 logger.Warn("Omci msg type not handled: %d", msgType)
134 continue
Shad Ansari45806172018-11-19 17:13:08 -0800135 }
136
137 resp.Pkt[0] = byte(transactionId >> 8)
138 resp.Pkt[1] = byte(transactionId & 0xFF)
Shad Ansaria5c79892018-11-29 16:32:59 -0800139 resp.Pkt[2] = 0x2<<4 | byte(msgType)
140 resp.Pkt[3] = deviceId
Shad Ansari45806172018-11-19 17:13:08 -0800141 resp.IntfId = m.IntfId
142 resp.OnuId = m.OnuId
Shad Ansari5bb48db2018-11-17 22:03:41 -0800143 omciIn <- resp
Shad Ansari2eac6a42018-11-14 22:35:39 -0800144 }
145}
Shad Ansari45806172018-11-19 17:13:08 -0800146
Shad Ansaria5c79892018-11-29 16:32:59 -0800147func ParsePkt(pkt []byte) (uint16, uint8, OmciMsgType, OmciClass, uint16, OmciContent) {
148 var m OmciMessage
149
150 r := bytes.NewReader(HexDecode(pkt))
151
152 if err := binary.Read(r, binary.BigEndian, &m); err != nil {
153 logger.Error("binary.Read failed: %s", err)
154 }
155 logger.Debug("OmciRun - TransactionId: %d MessageType: %d, ME Class: %d, ME Instance: %d, Content: %x",
156 m.TransactionId, m.MessageType&0x0F, m.MessageId.Class, m.MessageId.Instance, m.Content)
157
158 return m.TransactionId, m.DeviceId, m.MessageType & 0x0F, m.MessageId.Class, m.MessageId.Instance, m.Content
159
160}
161
162func HexDecode(pkt []byte) []byte {
163 // Convert the hex encoding to binary
164 // TODO - Change openolt adapter to send raw binary instead of hex encoded
Shad Ansari45806172018-11-19 17:13:08 -0800165 p := make([]byte, len(pkt)/2)
166 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
Shad Ansaria5c79892018-11-29 16:32:59 -0800167 // Go figure this ;)
Shad Ansari45806172018-11-19 17:13:08 -0800168 u := (pkt[i] & 15) + (pkt[i]>>6)*9
169 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
170 p[j] = u<<4 + l
171 }
172 logger.Debug("Omci decoded: %x.", p)
Shad Ansaria5c79892018-11-29 16:32:59 -0800173 return p
Shad Ansari45806172018-11-19 17:13:08 -0800174}
175
176func NewOnuState() *OnuState {
Shad Ansaria5c79892018-11-29 16:32:59 -0800177 return &OnuState{gemPortId: 0, mibUploadCtr: 0, uniGInstance: 1, pptpInstance: 1}
Shad Ansari45806172018-11-19 17:13:08 -0800178}
179
Shad Ansaria5c79892018-11-29 16:32:59 -0800180func mibReset() []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800181 var pkt []byte
182
183 logger.Debug("Omci MibReset")
184
185 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800186 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800187 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800192 return pkt
193}
194
Shad Ansaria5c79892018-11-29 16:32:59 -0800195func mibUpload() []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800196 var pkt []byte
197
198 logger.Debug("Omci MibUpload")
199
200 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800201 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800207
208 pkt[9] = NumMibUploads // Number of subsequent MibUploadNext cmds
209
210 return pkt
211}
212
Shad Ansaria5c79892018-11-29 16:32:59 -0800213func mibUploadNext(state *OnuState) []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800214 var pkt []byte
215
216 logger.Debug("Omci MibUploadNext")
217
218 switch state.mibUploadCtr {
219 case 0:
220 // ANI-G
221 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800222 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800223 0x01, 0x07, 0x80, 0x01, 0xff, 0xff, 0x01, 0x00,
224 0x08, 0x00, 0x30, 0x00, 0x00, 0x05, 0x09, 0x00,
225 0x00, 0xe0, 0x54, 0xff, 0xff, 0x00, 0x00, 0x0c,
226 0x63, 0x81, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800228 case 1, 2, 3, 4:
229 // UNI-G
230 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800231 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800232 0x01, 0x08, 0x01, 0x01, 0xf8, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800237 pkt[11] = state.uniGInstance // ME Instance
238 state.uniGInstance++
239 case 5, 6, 7, 8:
240 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800241 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800242 0x00, 0x0b, 0x01, 0x01, 0xff, 0xfe, 0x00, 0x2f,
243 0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0xee, 0x00,
244 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800247 pkt[11] = state.pptpInstance // ME Instance
248 state.pptpInstance++
249 default:
250 logger.Error("Invalid MibUpload request %d", state.mibUploadCtr)
251 }
252
253 state.mibUploadCtr++
254
255 return pkt
256}
257
Shad Ansaria5c79892018-11-29 16:32:59 -0800258func set() []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800259 var pkt []byte
260
261 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800262 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
265 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
266 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800267 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800268
269 logger.Debug("Omci Set")
270
271 return pkt
272}
273
Shad Ansaria5c79892018-11-29 16:32:59 -0800274func create(class OmciClass, content OmciContent, key OnuKey) []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800275 var pkt []byte
276
Shad Ansaria5c79892018-11-29 16:32:59 -0800277 if class == GEMPortNetworkCTP {
278 if onuState, ok := Onus[key]; !ok {
279 logger.Error("ONU Key Error - IntfId: %d, OnuId:", key.IntfId, key.OnuId)
280 } else {
281 onuState.gemPortId = binary.BigEndian.Uint16(content[:2])
282 logger.Debug("Gem Port Id %d", onuState.gemPortId)
283 }
284 }
285
Shad Ansari45806172018-11-19 17:13:08 -0800286 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800287 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x01,
Shad Ansari45806172018-11-19 17:13:08 -0800288 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800293
294 logger.Debug("Omci Create")
295
296 return pkt
297}
298
Shad Ansaria5c79892018-11-29 16:32:59 -0800299func get() []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800300 var pkt []byte
301
302 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800303 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x02, 0x01,
Shad Ansari45806172018-11-19 17:13:08 -0800304 0x00, 0x20, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800309
310 logger.Debug("Omci Get")
311
312 return pkt
313}
Shad Ansaria5c79892018-11-29 16:32:59 -0800314
315func getAllAlarms() []byte {
316 var pkt []byte
317
318 pkt = []byte{
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
320 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
322 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
325
326 logger.Debug("Omci GetAllAlarms")
327
328 return pkt
329}