-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.go
556 lines (480 loc) · 17.4 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci
import (
"encoding/binary"
"errors"
"fmt"
me "github.com/cboling/omci/generated"
"github.com/google/gopacket"
)
func decodeTestRequest(data []byte, p gopacket.PacketBuilder) error {
// Peek at Managed Entity Type
if len(data) < 8 {
p.SetTruncated()
return errors.New("frame too small")
}
classID := binary.BigEndian.Uint16(data)
// Is it a Managed Entity class we support customized decode of?
switch me.ClassID(classID) {
default:
omci := &TestRequest{}
omci.MsgLayerType = LayerTypeTestRequest
return decodingLayerDecoder(omci, data, p)
case me.AniGClassID, me.ReAniGClassID, me.PhysicalPathTerminationPointReUniClassID,
me.ReUpstreamAmplifierClassID, me.ReDownstreamAmplifierClassID:
omci := &OpticalLineSupervisionTestRequest{}
omci.MsgLayerType = LayerTypeTestRequest
return decodingLayerDecoder(omci, data, p)
}
}
// TestRequest message
type TestRequest struct {
MeBasePacket
Payload []byte
}
func (omci *TestRequest) String() string {
return fmt.Sprintf("%v, Request: %v octets", omci.MeBasePacket.String(), len(omci.Payload))
}
// LayerType returns LayerTypeTestRequest
func (omci *TestRequest) LayerType() gopacket.LayerType {
return LayerTypeTestRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *TestRequest) CanDecode() gopacket.LayerClass {
return LayerTypeTestRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *TestRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
func (omci *TestRequest) TestRequest() []byte {
return omci.Payload
}
// DecodeFromBytes decodes the given bytes of a Test Request into this layer
func (omci *TestRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 4)
if err != nil {
return err
}
omci.Payload = make([]byte, MaxTestRequestLength)
copy(omci.Payload, omci.MeBasePacket.Payload)
return nil
}
// SerializeTo provides serialization of an Test Request message
func (omci *TestRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
if omci.Payload == nil {
return errors.New("test results payload is missing")
}
if len(omci.Payload) > MaxTestRequestLength {
msg := fmt.Sprintf("Invalid Test Request payload size. Received %v bytes, expected %v",
len(omci.Payload), MaxTestRequestLength)
return errors.New(msg)
}
bytes, err := b.AppendBytes(len(omci.Payload))
if err != nil {
return err
}
copy(bytes, omci.Payload)
return nil
}
type OpticalLineSupervisionTestRequest struct {
MeBasePacket
SelectTest uint8 // Bitfield
GeneralPurposeBuffer uint16 // Pointer to General Purpose Buffer ME
VendorSpecificParameters uint16 // Pointer to Octet String ME
}
func (omci *OpticalLineSupervisionTestRequest) String() string {
return fmt.Sprintf("Optical Line Supervision Test Result: SelectTest: %#x, Buffer: %#x, Params: %#x",
omci.SelectTest, omci.GeneralPurposeBuffer, omci.VendorSpecificParameters)
}
// LayerType returns LayerTypeTestRequest
func (omci *OpticalLineSupervisionTestRequest) LayerType() gopacket.LayerType {
return LayerTypeTestRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *OpticalLineSupervisionTestRequest) CanDecode() gopacket.LayerClass {
return LayerTypeTestRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *OpticalLineSupervisionTestRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
func (omci *OpticalLineSupervisionTestRequest) TestRequest() []byte {
return omci.Payload
}
// DecodeFromBytes decodes the given bytes of a Test Result Notification into this layer
func (omci *OpticalLineSupervisionTestRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+5)
if err != nil {
return err
}
omci.SelectTest = data[4]
omci.GeneralPurposeBuffer = binary.BigEndian.Uint16(data[5:])
omci.VendorSpecificParameters = binary.BigEndian.Uint16(data[7:])
return nil
}
// SerializeTo provides serialization of an Test Result notification message
func (omci *OpticalLineSupervisionTestRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
bytes, err := b.AppendBytes(8)
if err != nil {
return err
}
bytes[0] = omci.SelectTest
binary.BigEndian.PutUint16(bytes[1:], omci.GeneralPurposeBuffer)
binary.BigEndian.PutUint16(bytes[3:], omci.VendorSpecificParameters)
return nil
}
// TestResponse message
type TestResponse struct {
MeBasePacket
Result me.Results
}
func (omci *TestResponse) String() string {
return fmt.Sprintf("%v, Results: %d (%v)", omci.MeBasePacket.String(), omci.Result, omci.Result)
}
// LayerType returns LayerTypeTestResponse
func (omci *TestResponse) LayerType() gopacket.LayerType {
return LayerTypeTestResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *TestResponse) CanDecode() gopacket.LayerClass {
return LayerTypeTestResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *TestResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Test Response into this layer
func (omci *TestResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+1)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Test requests
if !me.SupportsMsgType(meDefinition, me.Test) {
return me.NewProcessingError("managed entity does not support Test Message-Type")
}
omci.Result = me.Results(data[4])
return nil
}
func decodeTestResponse(data []byte, p gopacket.PacketBuilder) error {
omci := &TestResponse{}
omci.MsgLayerType = LayerTypeTestResponse
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Test Response message
func (omci *TestResponse) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Set
if !me.SupportsMsgType(entity, me.Test) {
return me.NewProcessingError("managed entity does not support the Test Message-Type")
}
bytes, err := b.AppendBytes(1)
if err != nil {
return err
}
bytes[0] = byte(omci.Result)
if omci.Result > me.DeviceBusy {
msg := fmt.Sprintf("invalid results code: %v, must be 0..6", omci.Result)
return errors.New(msg)
}
return nil
}
func decodeTestResult(data []byte, p gopacket.PacketBuilder) error {
// Peek at Managed Entity Type
if len(data) < 8 {
p.SetTruncated()
return errors.New("frame too small")
}
classID := binary.BigEndian.Uint16(data)
// Is it a Managed Entity class we support customized decode of?
switch me.ClassID(classID) {
default:
omci := &TestResultNotification{}
omci.MsgLayerType = LayerTypeTestResult
return decodingLayerDecoder(omci, data, p)
case me.AniGClassID, me.ReAniGClassID, me.PhysicalPathTerminationPointReUniClassID,
me.ReUpstreamAmplifierClassID, me.ReDownstreamAmplifierClassID:
omci := &OpticalLineSupervisionTestResult{}
omci.MsgLayerType = LayerTypeTestResult
return decodingLayerDecoder(omci, data, p)
}
}
func decodeTestResultExtended(data []byte, p gopacket.PacketBuilder) error {
// Peek at Managed Entity Type
if len(data) < 8 {
p.SetTruncated()
return errors.New("frame too small")
}
classID := binary.BigEndian.Uint16(data)
// Is it a Managed Entity class we support customized decode of?
switch me.ClassID(classID) {
default:
omci := &TestResultNotification{}
omci.MsgLayerType = LayerTypeTestResult
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
case me.AniGClassID, me.ReAniGClassID, me.PhysicalPathTerminationPointReUniClassID,
me.ReUpstreamAmplifierClassID, me.ReDownstreamAmplifierClassID:
omci := &OpticalLineSupervisionTestResult{}
omci.MsgLayerType = LayerTypeTestResult
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
}
type TestResultNotification struct {
MeBasePacket
Payload []byte
}
func (omci *TestResultNotification) TestResults() []byte {
return omci.Payload
}
func (omci *TestResultNotification) String() string {
return fmt.Sprintf("%v, Payload: %v octets", omci.MeBasePacket.String(), len(omci.Payload))
}
// LayerType returns LayerTypeTestResult
func (omci *TestResultNotification) LayerType() gopacket.LayerType {
return LayerTypeTestResult
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *TestResultNotification) CanDecode() gopacket.LayerClass {
return LayerTypeTestResult
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *TestResultNotification) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Test Result Notification into this layer
func (omci *TestResultNotification) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
payloadOffset := 4
if omci.Extended {
payloadOffset = 6
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, payloadOffset)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Test requests
if !me.SupportsMsgType(meDefinition, me.Test) {
return me.NewProcessingError("managed entity does not support Test Message-Type")
}
if omci.Extended {
if len(data) < 6 {
p.SetTruncated()
return errors.New("frame too small")
}
length := binary.BigEndian.Uint16(data[4:])
if len(data) < 6+int(length) {
p.SetTruncated()
return errors.New("frame too small")
}
omci.Payload = make([]byte, length)
copy(omci.Payload, data[6:])
} else {
omci.Payload = make([]byte, MaxTestResultsLength)
copy(omci.Payload, omci.MeBasePacket.Payload)
}
return nil
}
// SerializeTo provides serialization of an Test Result notification message
func (omci *TestResultNotification) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Test requests
if !me.SupportsMsgType(meDefinition, me.Test) {
return me.NewProcessingError("managed entity does not support Test Message-Type")
}
if omci.Payload == nil {
return errors.New("test results payload is missing")
}
payloadOffset := 0
maxSize := MaxTestResultsLength
if omci.Extended {
payloadOffset = 2
maxSize = MaxExtendedLength - 10 - 4
}
if len(omci.Payload) > maxSize {
msg := fmt.Sprintf("Invalid Test Results payload size. Received %v bytes, max expected %v",
len(omci.Payload), maxSize)
return errors.New(msg)
}
bytes, err := b.AppendBytes(len(omci.Payload) + payloadOffset)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(len(omci.Payload)))
}
copy(bytes[payloadOffset:], omci.Payload)
return nil
}
// OpticalLineSupervisionTestResult provides a Optical Specific test results
// message decode for the associated Managed Entities
type OpticalLineSupervisionTestResult struct {
MeBasePacket
PowerFeedVoltageType uint8 // Type = 1
PowerFeedVoltage uint16 // value
ReceivedOpticalPowerType uint8 // Type = 3
ReceivedOpticalPower uint16 // value
MeanOpticalLaunchType uint8 // Type = 5
MeanOpticalLaunch uint16 // value
LaserBiasCurrentType uint8 // Type = 9
LaserBiasCurrent uint16 // value
TemperatureType uint8 // Type = 12
Temperature uint16 // value
GeneralPurposeBuffer uint16 // Pointer to General Purpose Buffer ME
}
func (omci *OpticalLineSupervisionTestResult) String() string {
return fmt.Sprintf("Optical Line Supervision Test Result")
}
// LayerType returns LayerTypeTestResult
func (omci *OpticalLineSupervisionTestResult) LayerType() gopacket.LayerType {
return LayerTypeTestResult
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *OpticalLineSupervisionTestResult) CanDecode() gopacket.LayerClass {
return LayerTypeTestResult
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *OpticalLineSupervisionTestResult) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
func (omci *OpticalLineSupervisionTestResult) TestResults() []byte {
return omci.MeBasePacket.Payload
}
// DecodeFromBytes decodes the given bytes of a Test Result Notification into this layer
func (omci *OpticalLineSupervisionTestResult) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
payloadOffset := 4
if omci.Extended {
payloadOffset = 6
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, payloadOffset+17)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Test requests
if !me.SupportsMsgType(meDefinition, me.Test) {
return me.NewProcessingError("managed entity does not support Test Message-Type")
}
// Note: Unsupported tests will have a type = 0 and the value should be zero
// as well, but that constraint is not enforced at this time.
// Type = 1
omci.PowerFeedVoltageType = data[payloadOffset]
omci.PowerFeedVoltage = binary.BigEndian.Uint16(data[payloadOffset+1:])
// Type = 3
omci.ReceivedOpticalPowerType = data[payloadOffset+3]
omci.ReceivedOpticalPower = binary.BigEndian.Uint16(data[payloadOffset+4:])
// Type = 5
omci.MeanOpticalLaunchType = data[payloadOffset+6]
omci.MeanOpticalLaunch = binary.BigEndian.Uint16(data[payloadOffset+7:])
// Type = 9
omci.LaserBiasCurrentType = data[payloadOffset+9]
omci.LaserBiasCurrent = binary.BigEndian.Uint16(data[payloadOffset+10:])
// Type = 12
omci.TemperatureType = data[payloadOffset+12]
omci.Temperature = binary.BigEndian.Uint16(data[payloadOffset+13:])
omci.GeneralPurposeBuffer = binary.BigEndian.Uint16(data[payloadOffset+15:])
return nil
}
// SerializeTo provides serialization of an Test Result notification message
func (omci *OpticalLineSupervisionTestResult) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Test requests
if !me.SupportsMsgType(meDefinition, me.Test) {
return me.NewProcessingError("managed entity does not support Test Message-Type")
}
payloadOffset := 0
if omci.Extended {
payloadOffset = 2
}
bytes, err := b.AppendBytes(payloadOffset + 17)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, 17)
}
bytes[payloadOffset] = omci.PowerFeedVoltageType
binary.BigEndian.PutUint16(bytes[payloadOffset+1:], omci.PowerFeedVoltage)
bytes[payloadOffset+3] = omci.ReceivedOpticalPowerType
binary.BigEndian.PutUint16(bytes[payloadOffset+4:], omci.ReceivedOpticalPower)
bytes[payloadOffset+6] = omci.MeanOpticalLaunchType
binary.BigEndian.PutUint16(bytes[payloadOffset+7:], omci.MeanOpticalLaunch)
bytes[payloadOffset+9] = omci.LaserBiasCurrentType
binary.BigEndian.PutUint16(bytes[payloadOffset+10:], omci.LaserBiasCurrent)
bytes[payloadOffset+12] = omci.TemperatureType
binary.BigEndian.PutUint16(bytes[payloadOffset+13:], omci.Temperature)
binary.BigEndian.PutUint16(bytes[payloadOffset+15:], omci.GeneralPurposeBuffer)
return nil
}