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/hex" |
| 21 | "fmt" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/google/gopacket" |
| 25 | ) |
| 26 | |
| 27 | // GenerateGetMacAddress generates "PON Port/MAC Address" message |
| 28 | func GenerateGetMacAddress() gopacket.SerializableLayer { |
| 29 | tibitData := &TOAMGetRequest{ |
| 30 | // IEEE 1904.2 |
| 31 | Opcode: 0x03, |
| 32 | // OAM Protocol |
| 33 | Flags: 0x0050, |
| 34 | OAMPDUCode: 0xfe, |
| 35 | OUId: []byte{0x2a, 0xea, 0x15}, |
| 36 | // TiBit OLT Management Interface |
| 37 | TOMIOpcode: 0x01, |
| 38 | // Correlation Tag |
| 39 | CTBranch: 0x0c, |
| 40 | CTType: 0x0c7a, |
| 41 | CTLength: 4, |
| 42 | CTInstance: getOltInstance(), |
| 43 | // Object Context |
| 44 | OCBranch: 0x0c, |
| 45 | OCType: 0x0007, |
| 46 | OCLength: 4, |
| 47 | OCInstance: 0x00000000, |
| 48 | // Vd |
| 49 | VdBranch: 0x07, |
| 50 | VdLeaf: 0x0004, |
| 51 | // End |
| 52 | EndBranch: 0x00, |
| 53 | } |
| 54 | return tibitData |
| 55 | } |
| 56 | |
| 57 | // GetMacAddressRes is a structure for a toam response |
| 58 | type GetMacAddressRes struct { |
| 59 | ComResp TOAMGetResponse |
| 60 | EcLength uint8 |
| 61 | EcValue []byte |
| 62 | EndBranch uint8 |
| 63 | } |
| 64 | |
| 65 | // String returns the string expression of GetMacAddressRes |
| 66 | func (d *GetMacAddressRes) String() string { |
| 67 | message := d.ComResp.String() |
| 68 | message = fmt.Sprintf("%s, EcLength:%x, EcValue:%v, EndBranch:%x", message, d.EcLength, hex.EncodeToString(d.EcValue), d.EndBranch) |
| 69 | return message |
| 70 | } |
| 71 | |
| 72 | // Len returns the length of GetMacAddressRes |
| 73 | func (d *GetMacAddressRes) Len() int { |
| 74 | return d.ComResp.Len() + int(d.ComResp.VcLength) + 1 |
| 75 | } |
| 76 | |
| 77 | // Decode decodes byte arrays to a data structure |
| 78 | func (d *GetMacAddressRes) Decode(data []byte) error { |
| 79 | d.ComResp.Decode(data) |
| 80 | i := d.ComResp.Len() |
| 81 | d.EcLength = data[i] |
| 82 | i++ |
| 83 | d.EcValue = data[i : i+int(d.EcLength)] |
| 84 | i = i + int(d.EcLength) |
| 85 | d.EndBranch = data[i] |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | // GetMacAddress returns a MAC address |
| 91 | func (d *GetMacAddressRes) GetMacAddress() string { |
| 92 | var buf []string |
| 93 | for _, b := range d.EcValue { |
| 94 | buf = append(buf, fmt.Sprintf("%02x", b)) |
| 95 | } |
| 96 | return strings.Join(buf, ":") |
| 97 | } |