-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalarms.go
821 lines (756 loc) · 26.8 KB
/
alarms.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*
* 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/v2/generated"
"github.com/google/gopacket"
)
type GetAllAlarmsRequest struct {
MeBasePacket
AlarmRetrievalMode byte
}
func (omci *GetAllAlarmsRequest) String() string {
return fmt.Sprintf("%v, Retrieval Mode: %v",
omci.MeBasePacket.String(), omci.AlarmRetrievalMode)
}
// LayerType returns LayerTypeGetAllAlarmsRequest
func (omci *GetAllAlarmsRequest) LayerType() gopacket.LayerType {
return LayerTypeGetAllAlarmsRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetAllAlarmsRequest) CanDecode() gopacket.LayerClass {
return LayerTypeGetAllAlarmsRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetAllAlarmsRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get All Alarms Request into this layer
func (omci *GetAllAlarmsRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize int
if omci.Extended {
//start here
hdrSize = 6 + 1
} else {
hdrSize = 4 + 1
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
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 Get All Alarms
if !me.SupportsMsgType(meDefinition, me.GetAllAlarms) {
return me.NewProcessingError("managed entity does not support Get All Alarms Message-Type")
}
// Entity Class are always ONU DATA (2) and Entity Instance of 0
if omci.EntityClass != me.OnuDataClassID {
msg := fmt.Sprintf("invalid Entity Class for Get All Alarms request: %v",
omci.EntityClass)
return me.NewProcessingError(msg)
}
if omci.EntityInstance != 0 {
msg := fmt.Sprintf("invalid Entity Instance for Get All Alarms request: %v",
omci.EntityInstance)
return me.NewUnknownInstanceError(msg)
}
var offset int
if omci.Extended {
offset = 2
}
omci.AlarmRetrievalMode = data[4+offset]
if omci.AlarmRetrievalMode > 1 {
msg := fmt.Sprintf("invalid Alarm Retrieval Mode for Get All Alarms request: %v, must be 0..1",
omci.AlarmRetrievalMode)
return errors.New(msg)
}
return nil
}
func decodeGetAllAlarmsRequest(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsRequest{}
omci.MsgLayerType = LayerTypeGetAllAlarmsRequest
return decodingLayerDecoder(omci, data, p)
}
func decodeGetAllAlarmsRequestExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsRequest{}
omci.MsgLayerType = LayerTypeGetAllAlarmsRequest
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get All Alarms Request message
func (omci *GetAllAlarmsRequest) 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 Get All Alarms
if !me.SupportsMsgType(entity, me.GetAllAlarms) {
return me.NewProcessingError("managed entity does not support the Get All Alarms Message-Type")
}
var offset int
if omci.Extended {
offset = 2
}
bytes, err := b.AppendBytes(offset + 1)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(1))
}
bytes[offset] = omci.AlarmRetrievalMode
return nil
}
type GetAllAlarmsResponse struct {
MeBasePacket
NumberOfCommands uint16
}
func (omci *GetAllAlarmsResponse) String() string {
return fmt.Sprintf("%v, NumberOfCommands: %d",
omci.MeBasePacket.String(), omci.NumberOfCommands)
}
// LayerType returns LayerTypeGetAllAlarmsResponse
func (omci *GetAllAlarmsResponse) LayerType() gopacket.LayerType {
return LayerTypeGetAllAlarmsResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetAllAlarmsResponse) CanDecode() gopacket.LayerClass {
return LayerTypeGetAllAlarmsResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetAllAlarmsResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get All Alarms Response into this layer
func (omci *GetAllAlarmsResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize int
if omci.Extended {
//start here
hdrSize = 6 + 2
} else {
hdrSize = 4 + 2
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
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 Get All Alarms
if !me.SupportsMsgType(meDefinition, me.GetAllAlarms) {
return me.NewProcessingError("managed entity does not support Get All Alarms Message-Type")
}
// Entity Class are always ONU DATA (2) and Entity Instance of 0
if omci.EntityClass != me.OnuDataClassID {
msg := fmt.Sprintf("invalid Entity Class for Get All Alarms response: %v",
omci.EntityClass)
return me.NewProcessingError(msg)
}
if omci.EntityInstance != 0 {
msg := fmt.Sprintf("invalid Entity Instance for Get All Alarms response: %v",
omci.EntityInstance)
return me.NewUnknownInstanceError(msg)
}
var offset int
if omci.Extended {
offset = 2
}
omci.NumberOfCommands = binary.BigEndian.Uint16(data[4+offset:])
return nil
}
func decodeGetAllAlarmsResponse(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsResponse{}
omci.MsgLayerType = LayerTypeGetAllAlarmsResponse
return decodingLayerDecoder(omci, data, p)
}
func decodeGetAllAlarmsResponseExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsResponse{}
omci.MsgLayerType = LayerTypeGetAllAlarmsResponse
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get All Alarms Response message
func (omci *GetAllAlarmsResponse) 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 Get All Alarms
if !me.SupportsMsgType(entity, me.GetAllAlarms) {
return me.NewProcessingError("managed entity does not support the Get All Alarms Message-Type")
}
var offset int
if omci.Extended {
offset = 2
}
bytes, err := b.AppendBytes(offset + 2)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(2))
}
binary.BigEndian.PutUint16(bytes[offset:], omci.NumberOfCommands)
return nil
}
type GetAllAlarmsNextRequest struct {
MeBasePacket
CommandSequenceNumber uint16
}
func (omci *GetAllAlarmsNextRequest) String() string {
return fmt.Sprintf("%v, Sequence Number: %d",
omci.MeBasePacket.String(), omci.CommandSequenceNumber)
}
// LayerType returns LayerTypeGetAllAlarmsNextRequest
func (omci *GetAllAlarmsNextRequest) LayerType() gopacket.LayerType {
return LayerTypeGetAllAlarmsNextRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetAllAlarmsNextRequest) CanDecode() gopacket.LayerClass {
return LayerTypeGetAllAlarmsNextRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetAllAlarmsNextRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get All Alarms Next Request into this layer
func (omci *GetAllAlarmsNextRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize int
if omci.Extended {
//start here
hdrSize = 6 + 2
} else {
hdrSize = 4 + 2
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
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 Get All Alarms
if !me.SupportsMsgType(meDefinition, me.GetAllAlarmsNext) {
return me.NewProcessingError("managed entity does not support Get All Alarms Next Message-Type")
}
// Entity Class are always ONU DATA (2) and Entity Instance of 0
if omci.EntityClass != me.OnuDataClassID {
msg := fmt.Sprintf("invalid Entity Class for Get All Alarms Next request: %v",
omci.EntityClass)
return me.NewProcessingError(msg)
}
if omci.EntityInstance != 0 {
msg := fmt.Sprintf("invalid Entity Instance for Get All Alarms Next request: %v",
omci.EntityInstance)
return me.NewUnknownInstanceError(msg)
}
var offset int
if omci.Extended {
offset = 2
}
omci.CommandSequenceNumber = binary.BigEndian.Uint16(data[4+offset:])
return nil
}
func decodeGetAllAlarmsNextRequest(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsNextRequest{}
omci.MsgLayerType = LayerTypeGetAllAlarmsNextRequest
return decodingLayerDecoder(omci, data, p)
}
func decodeGetAllAlarmsNextRequestExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsNextRequest{}
omci.MsgLayerType = LayerTypeGetAllAlarmsNextRequest
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get All Alarms Next Request message
func (omci *GetAllAlarmsNextRequest) 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 Get All Alarms Next
if !me.SupportsMsgType(entity, me.GetAllAlarmsNext) {
return me.NewProcessingError("managed entity does not support the Get All Alarms Next Message-Type")
}
var offset int
if omci.Extended {
offset = 2
}
bytes, err := b.AppendBytes(offset + 2)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(2))
}
binary.BigEndian.PutUint16(bytes[offset:], omci.CommandSequenceNumber)
return nil
}
type AdditionalAlarmsData struct {
AlarmEntityClass me.ClassID
AlarmEntityInstance uint16
AlarmBitMap [28]byte // 224 bits
}
type GetAllAlarmsNextResponse struct {
MeBasePacket
AlarmEntityClass me.ClassID
AlarmEntityInstance uint16
AlarmBitMap [28]byte // 224 bits
AdditionalAlarms []AdditionalAlarmsData // Valid only for extended message set version
}
func (omci *GetAllAlarmsNextResponse) String() string {
return fmt.Sprintf("%v, CID: %v, EID: (%d/%#x), Bitmap: %v",
omci.MeBasePacket.String(), omci.AlarmEntityClass, omci.AlarmEntityInstance,
omci.AlarmEntityInstance, omci.AlarmBitMap)
}
// LayerType returns LayerTypeGetAllAlarmsNextResponse
func (omci *GetAllAlarmsNextResponse) LayerType() gopacket.LayerType {
return LayerTypeGetAllAlarmsNextResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetAllAlarmsNextResponse) CanDecode() gopacket.LayerClass {
return LayerTypeGetAllAlarmsNextResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetAllAlarmsNextResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get All Alarms Next Response into this layer
func (omci *GetAllAlarmsNextResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize int
if omci.Extended {
hdrSize = 6
} else {
hdrSize = 4
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
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 Get All Alarms Next
if !me.SupportsMsgType(meDefinition, me.GetAllAlarmsNext) {
return me.NewProcessingError("managed entity does not support Get All Alarms Next Message-Type")
}
// Entity Class are always ONU DATA (2) and Entity Instance of 0
if omci.EntityClass != me.OnuDataClassID {
msg := fmt.Sprintf("invalid Entity Class for Get All Alarms Next response: %v",
omci.EntityClass)
return me.NewProcessingError(msg)
}
if omci.EntityInstance != 0 {
msg := fmt.Sprintf("invalid Entity Instance for Get All Alarms Next response: %v",
omci.EntityInstance)
return me.NewUnknownInstanceError(msg)
}
//err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+4+28) // Decode reported ME. If an out-of-range sequence number was sent, this will
// // contain an ME with class ID and entity ID of zero and you should get an
// // error of "managed entity definition not found" returned.
var offset int
msgContentsLen := 28
if omci.Extended {
offset = 2 // Message Contents length (2)
msgContentsLen = int(binary.BigEndian.Uint16(data[6:]))
}
if len(data[4+offset:]) < 4+msgContentsLen {
p.SetTruncated()
return errors.New("frame too small: Get All Alarms Next Response Managed Entity attribute truncated")
}
omci.AlarmEntityClass = me.ClassID(binary.BigEndian.Uint16(data[4+offset:]))
omci.AlarmEntityInstance = binary.BigEndian.Uint16(data[6+offset:])
copy(omci.AlarmBitMap[:], data[8+offset:36])
remaining := len(data) - (6 + 4 + 28)
if !omci.Extended || remaining <= 0 {
return nil
}
offset = 6 + 4 + 28
omci.AdditionalAlarms = make([]AdditionalAlarmsData, 0)
for remaining > 0 {
if remaining < 4+28 {
p.SetTruncated()
return errors.New("frame too small: Get All Alarms Next Response Managed Entity attribute truncated")
}
alarm := AdditionalAlarmsData{
AlarmEntityClass: me.ClassID(binary.BigEndian.Uint16(data[offset:])),
AlarmEntityInstance: binary.BigEndian.Uint16(data[offset+2:]),
}
copy(alarm.AlarmBitMap[:], data[offset+4:])
omci.AdditionalAlarms = append(omci.AdditionalAlarms, alarm)
offset += 4 + 28
remaining -= 4 + 28
}
return nil
}
func decodeGetAllAlarmsNextResponse(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsNextResponse{}
omci.MsgLayerType = LayerTypeGetAllAlarmsNextResponse
return decodingLayerDecoder(omci, data, p)
}
func decodeGetAllAlarmsNextResponseExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetAllAlarmsNextResponse{}
omci.MsgLayerType = LayerTypeGetAllAlarmsNextResponse
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get All Alarms Next Response message
func (omci *GetAllAlarmsNextResponse) 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 Get All Alarms Next
if !me.SupportsMsgType(entity, me.GetAllAlarmsNext) {
return me.NewProcessingError("managed entity does not support the Get All Alarms Next Message-Type")
}
contentLength := 2 + 2 + 28
maxLength := MaxBaselineLength - 8 - 8
var extraMEs int
var offset int
if omci.Extended {
maxLength = MaxExtendedLength - 10 - 4
offset = 2
contentLength += 2 // Length field
if omci.AdditionalAlarms != nil {
extraMEs = len(omci.AdditionalAlarms)
contentLength += extraMEs*4 + 28
}
}
if contentLength > maxLength {
msg := fmt.Sprintf("not enough space to fit all requested Managed Entities, have %v, requested: %v",
maxLength, contentLength)
return me.NewMessageTruncatedError(msg)
}
// Allocate space for all
bytes, err := b.AppendBytes(contentLength)
if err != nil {
return err
}
// Always encode the first ME alarm data
binary.BigEndian.PutUint16(bytes[offset:], uint16(omci.AlarmEntityClass))
binary.BigEndian.PutUint16(bytes[offset+2:], omci.AlarmEntityInstance)
copy(bytes[offset+4:], omci.AlarmBitMap[:])
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(contentLength-2))
if omci.AdditionalAlarms != nil {
for index, value := range omci.AdditionalAlarms {
offset = (32 * (index + 1)) + 2
binary.BigEndian.PutUint16(bytes[offset:], uint16(value.AlarmEntityClass))
binary.BigEndian.PutUint16(bytes[offset+2:], value.AlarmEntityInstance)
copy(bytes[offset+4:], value.AlarmBitMap[:])
}
}
}
return nil
}
const AlarmBitmapSize = 224
type AlarmNotificationMsg struct {
MeBasePacket
AlarmBitmap [AlarmBitmapSize / 8]byte
zeroPadding [3]byte // Note: This zero padding is not present in the Extended Message Set
AlarmSequenceNumber byte
RelaxedError me.IRelaxedDecodeError
}
func (omci *AlarmNotificationMsg) String() string {
return fmt.Sprintf("%v, Sequence Number: %d, Alarm Bitmap: %v",
omci.MeBasePacket.String(), omci.AlarmSequenceNumber, omci.AlarmBitmap)
}
// LayerType returns LayerTypeAlarmNotification
func (omci *AlarmNotificationMsg) LayerType() gopacket.LayerType {
return LayerTypeAlarmNotification
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *AlarmNotificationMsg) CanDecode() gopacket.LayerClass {
return LayerTypeAlarmNotification
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *AlarmNotificationMsg) NextLayerType() gopacket.LayerType {
if omci.RelaxedError != nil {
return LayerTypeUnknownAlarm
}
return gopacket.LayerTypePayload
}
// addRelaxedError appends relaxed decode errors to this message
func (omci *AlarmNotificationMsg) addRelaxedError(err me.IRelaxedDecodeError) {
if omci.RelaxedError == nil {
omci.RelaxedError = err
}
}
func (omci *AlarmNotificationMsg) IsAlarmActive(alarmNumber uint8) (bool, error) {
if alarmNumber >= AlarmBitmapSize {
msg := fmt.Sprintf("invalid alarm number: %v, must be 0..224", alarmNumber)
return false, errors.New(msg)
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return false, omciErr.GetError()
}
alarmMap := entity.GetAlarmMap()
if alarmMap == nil {
msg := "managed entity does not support Alarm notifications"
return false, errors.New(msg)
}
if _, ok := alarmMap[alarmNumber]; !ok {
msg := fmt.Sprintf("unsupported invalid alarm number: %v", alarmNumber)
return false, errors.New(msg)
}
octet := alarmNumber / 8
bit := 7 - (alarmNumber % 8)
return omci.AlarmBitmap[octet]>>bit == 1, nil
}
func (omci *AlarmNotificationMsg) IsAlarmClear(alarmNumber uint8) (bool, error) {
if alarmNumber >= AlarmBitmapSize {
msg := fmt.Sprintf("invalid alarm number: %v, must be 0..224", alarmNumber)
return false, errors.New(msg)
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return false, omciErr.GetError()
}
alarmMap := entity.GetAlarmMap()
if alarmMap == nil {
return false, errors.New("managed entity does not support Alarm notifications")
}
if _, ok := alarmMap[alarmNumber]; !ok {
msg := fmt.Sprintf("unsupported invalid alarm number: %v", alarmNumber)
return false, errors.New(msg)
}
octet := alarmNumber / 8
bit := 7 - (alarmNumber % 8)
return omci.AlarmBitmap[octet]>>bit == 0, nil
}
func (omci *AlarmNotificationMsg) ActivateAlarm(alarmNumber uint8) error {
if alarmNumber >= AlarmBitmapSize {
msg := fmt.Sprintf("invalid alarm number: %v, must be 0..224", alarmNumber)
return errors.New(msg)
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
alarmMap := entity.GetAlarmMap()
if alarmMap == nil {
return errors.New("managed entity does not support Alarm notifications")
}
if _, ok := alarmMap[alarmNumber]; !ok {
msg := fmt.Sprintf("unsupported invalid alarm number: %v", alarmNumber)
return errors.New(msg)
}
octet := alarmNumber / 8
bit := 7 - (alarmNumber % 8)
omci.AlarmBitmap[octet] |= 1 << bit
return nil
}
func (omci *AlarmNotificationMsg) ClearAlarm(alarmNumber uint8) error {
if alarmNumber >= AlarmBitmapSize {
msg := fmt.Sprintf("invalid alarm number: %v, must be 0..224", alarmNumber)
return errors.New(msg)
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
alarmMap := entity.GetAlarmMap()
if alarmMap == nil {
return errors.New("managed entity does not support Alarm notifications")
}
if _, ok := alarmMap[alarmNumber]; !ok {
msg := fmt.Sprintf("unsupported invalid alarm number: %v", alarmNumber)
return errors.New(msg)
}
octet := alarmNumber / 8
bit := 7 - (alarmNumber % 8)
omci.AlarmBitmap[octet] &= ^(1 << bit)
return nil
}
// DecodeFromBytes decodes the given bytes of an Alarm Notification into this layer
func (omci *AlarmNotificationMsg) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+28)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
mapOffset := 4
if omci.Extended {
mapOffset = 6
if len(data) < 6+28+1 {
p.SetTruncated()
return errors.New("frame too small")
}
}
if omci.Extended {
omci.AlarmSequenceNumber = data[mapOffset+(AlarmBitmapSize/8)]
} else {
padOffset := mapOffset + (AlarmBitmapSize / 8)
omci.zeroPadding[0] = data[padOffset]
omci.zeroPadding[1] = data[padOffset+1]
omci.zeroPadding[2] = data[padOffset+2]
omci.AlarmSequenceNumber = data[padOffset+3]
}
// Is this an unsupported or vendor specific ME. If so, it is not an error to decode
// the alarms. We just cannot provide any alarm names. Handle decode here.
classSupport := meDefinition.GetClassSupport()
isUnsupported := classSupport == me.UnsupportedManagedEntity ||
classSupport == me.UnsupportedVendorSpecificManagedEntity
copy(omci.AlarmBitmap[:], data[mapOffset:(AlarmBitmapSize/8)-mapOffset])
if !isUnsupported {
// Look for a non-nil/not empty Alarm Map to determine if this ME supports alarms
alarmMap := meDefinition.GetAlarmMap()
relaxedDecode := me.GetRelaxedDecode(me.AlarmNotification, false)
invalidAlarmBits := make([]byte, AlarmBitmapSize/8)
invalidAlarmBitsFound := false
if alarmMap != nil {
// Look for invalid bits
for octet := 0; octet < AlarmBitmapSize/8; octet++ {
if omci.AlarmBitmap[octet] != 0 {
for bit := 0; bit < 8; bit++ {
val := byte(1 << (7 - bit))
if omci.AlarmBitmap[octet]&val == val {
if _, validBit := alarmMap[uint8(bit)]; !validBit {
// If valid ALARM bit found
if !relaxedDecode {
return errors.New(fmt.Sprintf("alarm notification decode: invalid alarm bit %v set", bit))
}
invalidAlarmBitsFound = true
invalidAlarmBits[octet] |= val
omci.AlarmBitmap[octet] &= ^val
}
}
}
}
}
} else if relaxedDecode {
invalidAlarmBitsFound = true
copy(invalidAlarmBits, data[mapOffset:(AlarmBitmapSize/8)-mapOffset])
copy(omci.AlarmBitmap[:], make([]byte, AlarmBitmapSize/8))
} else {
return errors.New("alarm notification decode: managed entity does not support alarms")
}
if invalidAlarmBitsFound {
decodeErr := me.NewUnknownAlarmDecodeError("alarm map not supported", invalidAlarmBits)
omci.addRelaxedError(decodeErr)
// Create our error layer now
err = newUnknownAlarmsLayer(omci, decodeErr, p)
}
}
return err
}
func decodeAlarmNotification(data []byte, p gopacket.PacketBuilder) error {
omci := &AlarmNotificationMsg{}
omci.MsgLayerType = LayerTypeAlarmNotification
return decodingLayerDecoder(omci, data, p)
}
func decodeAlarmNotificationExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &AlarmNotificationMsg{}
omci.MsgLayerType = LayerTypeAlarmNotification
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Alarm Notification message
func (omci *AlarmNotificationMsg) 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
}
// TODO: Support of encoding AlarmNotification into supported types not yet supported
//meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
// me.ParamData{EntityID: omci.EntityInstance})
//if omciErr.StatusCode() != me.Success {
// return omciErr.GetError()
//}
//if !me.SupportsMsgType(meDefinition, me.AlarmNotification) {
// return me.NewProcessingError("managed entity does not support Alarm Notification Message-Type")
//}
if omci.Extended {
bytes, err := b.AppendBytes(2 + (AlarmBitmapSize / 8) + 1)
if err != nil {
return err
}
binary.BigEndian.PutUint16(bytes, uint16((AlarmBitmapSize/8)+1))
for index, octet := range omci.AlarmBitmap {
bytes[2+index] = octet
}
bytes[2+(AlarmBitmapSize/8)] = omci.AlarmSequenceNumber
} else {
bytes, err := b.AppendBytes((AlarmBitmapSize / 8) + 3 + 1)
if err != nil {
return err
}
for index, octet := range omci.AlarmBitmap {
bytes[index] = octet
}
padOffset := AlarmBitmapSize / 8
bytes[padOffset] = 0
bytes[padOffset+1] = 0
bytes[padOffset+2] = 0
bytes[padOffset+3] = omci.AlarmSequenceNumber
}
return nil
}