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