Takahiro Suzuki | d7bf820 | 2020-12-17 20:21:59 +0900 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package l2oam |
| 18 | |
| 19 | import ( |
| 20 | "encoding/binary" |
| 21 | "encoding/hex" |
| 22 | "fmt" |
| 23 | |
| 24 | "github.com/google/gopacket" |
| 25 | "github.com/google/gopacket/layers" |
| 26 | ) |
| 27 | |
| 28 | // GetMpcpLLIdReq is a structure for "MPCP/LLID" message |
| 29 | type GetMpcpLLIdReq struct { |
| 30 | layers.BaseLayer |
| 31 | Opcode uint8 |
| 32 | Flags uint16 |
| 33 | OAMPDUCode uint8 |
| 34 | OUId []byte // Organizationally Unique Identifier: 2a:ea:15 (Tibit Communications) |
| 35 | TOMIOpcode uint8 |
| 36 | OCBranch uint8 |
| 37 | OCType uint16 |
| 38 | OCLength uint8 |
| 39 | OCInstance uint32 |
| 40 | VdBranch uint8 |
| 41 | VdLeaf uint16 |
| 42 | EndBranch uint8 |
| 43 | } |
| 44 | |
| 45 | // GenerateGetMpcpLLId generates "MPCP/LLID" message |
| 46 | func GenerateGetMpcpLLId(oc *TomiObjectContext) gopacket.SerializableLayer { |
| 47 | tibitData := &GetMpcpLLIdReq{ |
| 48 | // IEEE 1904.2 |
| 49 | Opcode: 0x03, |
| 50 | // OMI Protocol |
| 51 | Flags: 0x0050, |
| 52 | OAMPDUCode: 0xfe, |
| 53 | OUId: []byte{0x2a, 0xea, 0x15}, |
| 54 | // TiBiT OLT Management Interface |
| 55 | TOMIOpcode: 0x01, |
| 56 | // Object Context |
| 57 | OCBranch: oc.Branch, |
| 58 | OCType: oc.Type, |
| 59 | OCLength: oc.Length, |
| 60 | OCInstance: oc.Instance, |
| 61 | // Vd |
| 62 | VdBranch: 0xcc, |
| 63 | VdLeaf: 0x0007, |
| 64 | // End |
| 65 | EndBranch: 0, |
| 66 | } |
| 67 | return tibitData |
| 68 | } |
| 69 | |
| 70 | // String returns the string expression of GetMpcpLLIdReq |
| 71 | func (d *GetMpcpLLIdReq) String() string { |
| 72 | message := fmt.Sprintf("Opcode:%02x, Flags:%04x, OAMPDUCode:%02x, OUId:%v", d.Opcode, d.Flags, d.OAMPDUCode, hex.EncodeToString(d.OUId)) |
| 73 | message = fmt.Sprintf("%s, TOMIOpcode:%02x", message, d.TOMIOpcode) |
| 74 | message = fmt.Sprintf("%s, OCBranch:%02x, OCType:%04x, OCLength:%02x, OCInstance:%08x", message, d.OCBranch, d.OCType, d.OCLength, d.OCInstance) |
| 75 | message = fmt.Sprintf("%s, VdBranch:%02x, VdLeaf:%04x, EndBranch:%02x", message, d.VdBranch, d.VdLeaf, d.EndBranch) |
| 76 | return message |
| 77 | } |
| 78 | |
| 79 | // Len returns the length of GetMpcpLLIdReq |
| 80 | func (d *GetMpcpLLIdReq) Len() int { |
| 81 | return 21 |
| 82 | } |
| 83 | |
| 84 | // LayerType returns the ethernet type of GetMpcpLLIdReq |
| 85 | func (d *GetMpcpLLIdReq) LayerType() gopacket.LayerType { return layers.LayerTypeEthernet } |
| 86 | |
| 87 | // SerializeTo serializes a data structure to byte arrays |
| 88 | func (d *GetMpcpLLIdReq) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error { |
| 89 | plen := int(d.Len()) |
| 90 | data, err := b.PrependBytes(plen) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | |
| 95 | i := 0 |
| 96 | data[i] = byte(d.Opcode) |
| 97 | i++ |
| 98 | binary.BigEndian.PutUint16(data[i:i+2], d.Flags) |
| 99 | i += 2 |
| 100 | data[i] = byte(d.OAMPDUCode) |
| 101 | i++ |
| 102 | copy(data[i:i+len(d.OUId)], d.OUId) |
| 103 | i += len(d.OUId) |
| 104 | data[i] = byte(d.TOMIOpcode) |
| 105 | i++ |
| 106 | data[i] = byte(d.OCBranch) |
| 107 | i++ |
| 108 | binary.BigEndian.PutUint16(data[i:i+2], d.OCType) |
| 109 | i += 2 |
| 110 | data[i] = byte(d.OCLength) |
| 111 | i++ |
| 112 | binary.BigEndian.PutUint32(data[i:i+4], d.OCInstance) |
| 113 | i += 4 |
| 114 | data[i] = byte(d.VdBranch) |
| 115 | i++ |
| 116 | binary.BigEndian.PutUint16(data[i:i+2], d.VdLeaf) |
| 117 | i += 2 |
| 118 | data[i] = byte(d.EndBranch) |
| 119 | |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // GetMpcpLLIdRes is a structure for a response of "MPCP/LLID" message |
| 124 | type GetMpcpLLIdRes struct { |
| 125 | layers.BaseLayer |
| 126 | Opcode uint8 |
| 127 | Flags uint16 |
| 128 | OAMPDUCode uint8 |
| 129 | OUId []byte // Organizationally Unique Identifier: 2a:ea:15 (Tibit Communications) |
| 130 | TOMIOpcode uint8 |
| 131 | OCBranch uint8 |
| 132 | OCType uint16 |
| 133 | OCLength uint8 |
| 134 | OCInstance uint32 |
| 135 | VcBranch uint8 |
| 136 | VcLeaf uint16 |
| 137 | VcLength uint8 |
| 138 | EcLength uint8 |
| 139 | EcValue []byte |
| 140 | EndBranch uint8 |
| 141 | } |
| 142 | |
| 143 | // String returns the string expression of GetMpcpLLIdRes |
| 144 | func (d *GetMpcpLLIdRes) String() string { |
| 145 | message := fmt.Sprintf("Opcode:%02x, Flags:%04x, OAMPDUCode:%02x, OUId:%v", d.Opcode, d.Flags, d.OAMPDUCode, hex.EncodeToString(d.OUId)) |
| 146 | message = fmt.Sprintf("%s, TOMIOpcode:%02x", message, d.TOMIOpcode) |
| 147 | message = fmt.Sprintf("%s, OCBranch:%02x, OCType:%04x, OCLength:%02x, OCInstance:%08x", message, d.OCBranch, d.OCType, d.OCLength, d.OCInstance) |
| 148 | message = fmt.Sprintf("%s, VcBranch:%02x, VcLeaf:%04x, VcLength:%02x", message, d.VcBranch, d.VcLeaf, d.VcLength) |
| 149 | message = fmt.Sprintf("%s, EcLength:%02x, EcValue:%v, EndBranch:%02x", message, d.EcLength, hex.EncodeToString(d.EcValue), d.EndBranch) |
| 150 | return message |
| 151 | } |
| 152 | |
| 153 | // Len returns the length of GetMpcpLLIdRes |
| 154 | func (d *GetMpcpLLIdRes) Len() int { |
| 155 | return 20 + int(d.VcLength) + 1 |
| 156 | } |
| 157 | |
| 158 | // LayerType returns the ethernet type of GetMpcpLLIdRes |
| 159 | func (d *GetMpcpLLIdRes) LayerType() gopacket.LayerType { return layers.LayerTypeEthernet } |
| 160 | |
| 161 | // Decode decodes byte arrays to a data structure |
| 162 | func (d *GetMpcpLLIdRes) Decode(data []byte) error { |
| 163 | i := 0 |
| 164 | d.Opcode = data[i] |
| 165 | i++ |
| 166 | d.Flags = binary.BigEndian.Uint16(data[i : i+2]) |
| 167 | i += 2 |
| 168 | d.OAMPDUCode = data[i] |
| 169 | i++ |
| 170 | d.OUId = data[i : i+3] |
| 171 | i += len(d.OUId) |
| 172 | d.TOMIOpcode = data[i] |
| 173 | i++ |
| 174 | d.OCBranch = data[i] |
| 175 | i++ |
| 176 | d.OCType = binary.BigEndian.Uint16(data[i : i+2]) |
| 177 | i += 2 |
| 178 | d.OCLength = data[i] |
| 179 | i++ |
| 180 | d.OCInstance = binary.BigEndian.Uint32(data[i : i+4]) |
| 181 | i += 4 |
| 182 | d.VcBranch = data[i] |
| 183 | i++ |
| 184 | d.VcLeaf = binary.BigEndian.Uint16(data[i : i+2]) |
| 185 | i += 2 |
| 186 | d.VcLength = data[i] |
| 187 | i++ |
| 188 | d.EcLength = data[i] |
| 189 | i++ |
| 190 | d.EcValue = data[i : i+int(d.EcLength)] |
| 191 | i += int(d.EcLength) |
| 192 | d.EndBranch = data[i] |
| 193 | |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | // GetLLID returns a LLID |
| 198 | func (d *GetMpcpLLIdRes) GetLLID() uint16 { |
| 199 | return binary.BigEndian.Uint16(d.EcValue) |
| 200 | } |