Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 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 | |
| 18 | package org.onlab.packet.dhcp; |
| 19 | |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 21 | import org.onlab.packet.DHCP6; |
| 22 | import org.onlab.packet.Deserializer; |
| 23 | import org.onlab.packet.IPacket; |
| 24 | |
| 25 | /** |
| 26 | * Relay option for DHCPv6. |
| 27 | * Based on RFC-3315. |
| 28 | */ |
| 29 | public final class Dhcp6RelayOption extends Dhcp6Option { |
| 30 | @Override |
| 31 | public short getCode() { |
| 32 | return DHCP6.OptionCode.RELAY_MSG.value(); |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public short getLength() { |
| 37 | return (short) payload.serialize().length; |
| 38 | } |
| 39 | |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 40 | @Override |
| 41 | public byte[] getData() { |
| 42 | return this.payload.serialize(); |
| 43 | } |
| 44 | |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Default constructor. |
| 47 | */ |
| 48 | public Dhcp6RelayOption() { |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Constructs a DHCPv6 relay option with DHCPv6 option. |
| 53 | * |
| 54 | * @param dhcp6Option the DHCPv6 option |
| 55 | */ |
| 56 | public Dhcp6RelayOption(Dhcp6Option dhcp6Option) { |
| 57 | super(dhcp6Option); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets deserializer for DHCPv6 relay option. |
| 62 | * |
| 63 | * @return the deserializer |
| 64 | */ |
| 65 | public static Deserializer<Dhcp6Option> deserializer() { |
| 66 | return (data, offset, len) -> { |
| 67 | Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, len); |
| 68 | IPacket payload = DHCP6.deserializer() |
| 69 | .deserialize(dhcp6Option.getData(), 0, dhcp6Option.getLength()); |
| 70 | Dhcp6RelayOption relayOption = new Dhcp6RelayOption(dhcp6Option); |
| 71 | relayOption.setPayload(payload); |
| 72 | payload.setParent(relayOption); |
| 73 | return relayOption; |
| 74 | }; |
| 75 | } |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public String toString() { |
| 79 | return MoreObjects.toStringHelper(getClass()) |
| 80 | .add("code", getCode()) |
| 81 | .add("length", getLength()) |
| 82 | .add("data", payload.toString()) |
| 83 | .toString(); |
| 84 | } |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 85 | } |