blob: 25701b0776abd7700532bea240c236f424d85f0d [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 Ansari8213c532018-12-11 13:53:34 -080099type OmciMsgHandler func(class OmciClass, content OmciContent, key OnuKey) []byte
100
101var Handlers = map[OmciMsgType]OmciMsgHandler{
102 MibReset: mibReset,
103 MibUpload: mibUpload,
104 MibUploadNext: mibUploadNext,
105 Set: set,
106 Create: create,
107 Get: get,
108 GetAllAlarms: getAllAlarms,
109}
110
Shad Ansaria5c79892018-11-29 16:32:59 -0800111var Onus = map[OnuKey]*OnuState{}
Shad Ansari45806172018-11-19 17:13:08 -0800112
Shad Ansaria5c79892018-11-29 16:32:59 -0800113func OmciRun(omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication) {
Shad Ansari45806172018-11-19 17:13:08 -0800114
Shad Ansari2eac6a42018-11-14 22:35:39 -0800115 for {
Shad Ansaria5c79892018-11-29 16:32:59 -0800116 var resp openolt.OmciIndication
Shad Ansari5bb48db2018-11-17 22:03:41 -0800117
Shad Ansari45806172018-11-19 17:13:08 -0800118 m := <-omciOut
Shad Ansari5bb48db2018-11-17 22:03:41 -0800119
Shad Ansaria5c79892018-11-29 16:32:59 -0800120 transactionId, deviceId, msgType, class, instance, content := ParsePkt(m.Pkt)
Shad Ansari5bb48db2018-11-17 22:03:41 -0800121
Shad Ansari45806172018-11-19 17:13:08 -0800122 logger.Debug("OmciRun - transactionId: %d msgType: %d, ME Class: %d, ME Instance: %d",
Shad Ansaria5c79892018-11-29 16:32:59 -0800123 transactionId, msgType, class, instance)
Shad Ansari45806172018-11-19 17:13:08 -0800124
125 key := OnuKey{m.IntfId, m.OnuId}
Shad Ansaria5c79892018-11-29 16:32:59 -0800126 if _, ok := Onus[key]; !ok {
127 Onus[key] = NewOnuState()
Shad Ansari45806172018-11-19 17:13:08 -0800128 }
Shad Ansari8213c532018-12-11 13:53:34 -0800129
130 if _, ok := Handlers[msgType]; !ok {
131 logger.Warn("Ignore omci msg (msgType %d not handled)", msgType)
Shad Ansariab44c212018-11-26 11:54:26 -0800132 continue
Shad Ansari45806172018-11-19 17:13:08 -0800133 }
134
Shad Ansari8213c532018-12-11 13:53:34 -0800135 resp.Pkt = Handlers[msgType](class, content, key)
136
Shad Ansari45806172018-11-19 17:13:08 -0800137 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 Ansari8213c532018-12-11 13:53:34 -0800180func mibReset(class OmciClass, content OmciContent, key OnuKey) []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 Ansari8213c532018-12-11 13:53:34 -0800195func mibUpload(class OmciClass, content OmciContent, key OnuKey) []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 Ansari8213c532018-12-11 13:53:34 -0800213func mibUploadNext(class OmciClass, content OmciContent, key OnuKey) []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800214 var pkt []byte
215
216 logger.Debug("Omci MibUploadNext")
217
Shad Ansari8213c532018-12-11 13:53:34 -0800218 state := Onus[key]
219
Shad Ansari45806172018-11-19 17:13:08 -0800220 switch state.mibUploadCtr {
221 case 0:
222 // ANI-G
223 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800224 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800225 0x01, 0x07, 0x80, 0x01, 0xff, 0xff, 0x01, 0x00,
226 0x08, 0x00, 0x30, 0x00, 0x00, 0x05, 0x09, 0x00,
227 0x00, 0xe0, 0x54, 0xff, 0xff, 0x00, 0x00, 0x0c,
228 0x63, 0x81, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800230 case 1, 2, 3, 4:
231 // UNI-G
232 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800233 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800234 0x01, 0x08, 0x01, 0x01, 0xf8, 0x00, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800239 pkt[11] = state.uniGInstance // ME Instance
240 state.uniGInstance++
241 case 5, 6, 7, 8:
242 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800243 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800244 0x00, 0x0b, 0x01, 0x01, 0xff, 0xfe, 0x00, 0x2f,
245 0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0xee, 0x00,
246 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800249 pkt[11] = state.pptpInstance // ME Instance
250 state.pptpInstance++
251 default:
252 logger.Error("Invalid MibUpload request %d", state.mibUploadCtr)
253 }
254
255 state.mibUploadCtr++
256
257 return pkt
258}
259
Shad Ansari8213c532018-12-11 13:53:34 -0800260func set(class OmciClass, content OmciContent, key OnuKey) []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800261 var pkt []byte
262
263 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800264 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
Shad Ansari45806172018-11-19 17:13:08 -0800265 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
266 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800269 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800270
271 logger.Debug("Omci Set")
272
273 return pkt
274}
275
Shad Ansaria5c79892018-11-29 16:32:59 -0800276func create(class OmciClass, content OmciContent, key OnuKey) []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800277 var pkt []byte
278
Shad Ansaria5c79892018-11-29 16:32:59 -0800279 if class == GEMPortNetworkCTP {
280 if onuState, ok := Onus[key]; !ok {
281 logger.Error("ONU Key Error - IntfId: %d, OnuId:", key.IntfId, key.OnuId)
282 } else {
283 onuState.gemPortId = binary.BigEndian.Uint16(content[:2])
284 logger.Debug("Gem Port Id %d", onuState.gemPortId)
285 }
286 }
287
Shad Ansari45806172018-11-19 17:13:08 -0800288 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800289 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x01,
Shad Ansari45806172018-11-19 17:13:08 -0800290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800295
296 logger.Debug("Omci Create")
297
298 return pkt
299}
300
Shad Ansari8213c532018-12-11 13:53:34 -0800301func get(class OmciClass, content OmciContent, key OnuKey) []byte {
Shad Ansari45806172018-11-19 17:13:08 -0800302 var pkt []byte
303
304 pkt = []byte{
Shad Ansaria5c79892018-11-29 16:32:59 -0800305 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x02, 0x01,
Shad Ansari45806172018-11-19 17:13:08 -0800306 0x00, 0x20, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Shad Ansaria5c79892018-11-29 16:32:59 -0800310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Shad Ansari45806172018-11-19 17:13:08 -0800311
312 logger.Debug("Omci Get")
313
314 return pkt
315}
Shad Ansaria5c79892018-11-29 16:32:59 -0800316
Shad Ansari8213c532018-12-11 13:53:34 -0800317func getAllAlarms(class OmciClass, content OmciContent, key OnuKey) []byte {
Shad Ansaria5c79892018-11-29 16:32:59 -0800318 var pkt []byte
319
320 pkt = []byte{
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
322 0x00, 0x03, 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 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
327
328 logger.Debug("Omci GetAllAlarms")
329
330 return pkt
331}