blob: dff723d27a1467ff1d16b836fb249e0ce84c9298 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
2 * Copyright 2020-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 l2oam
18
19import (
20 "encoding/hex"
21 "fmt"
22
23 "github.com/google/gopacket"
24 "github.com/google/gopacket/layers"
25)
26
27// AutonomousEvent is a structure for Autonomous Event
28type AutonomousEvent struct {
29 ComResp TOAMGetResponse
30 EcLength uint8
31 EcValue []byte
32 EndBranch uint8
33}
34
35// String returns the string expression of AutonomousEvent
36func (d *AutonomousEvent) String() string {
37 message := d.ComResp.String()
38 message = fmt.Sprintf("%s, EcLength:%v, EcValue:%v, EndBranch:%v", message, d.EcLength, hex.EncodeToString(d.EcValue), d.EndBranch)
39 return message
40}
41
42// Len returns the length of AutonomousEvent
43func (d *AutonomousEvent) Len() int {
44 return d.ComResp.Len() + int(d.ComResp.VcLength) + 1
45}
46
47// LayerType returns the ethernet type of AutonomousEvent
48func (d *AutonomousEvent) LayerType() gopacket.LayerType { return layers.LayerTypeEthernet }
49
50// SerializeTo serializes a data structure to byte arrays
51func (d *AutonomousEvent) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
52 return nil
53}
54
55// Decode decodes byte arrays to a data structure
56func (d *AutonomousEvent) Decode(data []byte) error {
57 d.ComResp.Decode(data)
58 i := d.ComResp.Len()
59 d.EcLength = data[i]
60 i++
61 d.EcValue = data[i : i+int(d.EcLength)]
62 i = i + int(d.EcLength)
63 d.EndBranch = data[i]
64
65 return nil
66}
67
68// IsRegistrationStatusMessage returns true if the message is for ONU registration
69func (d *AutonomousEvent) IsRegistrationStatusMessage() bool {
70 return d.ComResp.VcBranch == 0x01 && d.ComResp.VcLeaf == 0x0009
71}