Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
Matteo Scandolo | 57af5d1 | 2019-04-29 17:11:41 -0700 | [diff] [blame] | 16 | package org.opencord.dhcpl2relay.impl.packet; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 17 | |
| 18 | import java.io.ByteArrayOutputStream; |
| 19 | import java.nio.charset.StandardCharsets; |
| 20 | |
| 21 | /** |
| 22 | * Represents the DHCP Option 82 information. Currently only supports |
| 23 | * sub option 1 (agent-circuit-id) and 2 (agent=-relay-id). |
| 24 | */ |
| 25 | public class DhcpOption82 { |
| 26 | |
| 27 | private String agentCircuitId = null; |
| 28 | private String agentRemoteId = null; |
| 29 | |
| 30 | public DhcpOption82() { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | public void setAgentCircuitId(String value) { |
| 35 | this.agentCircuitId = value; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @return agentCircuitId |
| 41 | */ |
| 42 | public String getAgentCircuitId() { |
| 43 | return this.agentCircuitId; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * sets AgentRemoteId. |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 48 | * @param value Value to be set |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 49 | */ |
| 50 | public void setAgentRemoteId(String value) { |
| 51 | this.agentRemoteId = value; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * @return agentRemoteId |
| 57 | */ |
| 58 | public String getAgentRemoteId() { |
| 59 | return this.agentRemoteId; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * |
| 64 | * @return length of option 82. |
| 65 | */ |
| 66 | public byte length() { |
| 67 | int length = 0; |
| 68 | |
| 69 | // +2 below for sub option ID and length of sub option |
| 70 | if (agentCircuitId != null) { |
| 71 | length += agentCircuitId.length() + 2; |
| 72 | } |
| 73 | if (agentRemoteId != null) { |
| 74 | length += agentRemoteId.length() + 2; |
| 75 | } |
| 76 | return (byte) length; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the representation of the option 82 specification as a byte |
| 81 | * array. |
| 82 | * @return returns byte array |
| 83 | */ |
| 84 | public byte[] toByteArray() { |
| 85 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 86 | |
| 87 | // Add sub option if set |
| 88 | if (agentCircuitId != null) { |
| 89 | buf.write((byte) 1); |
| 90 | buf.write((byte) agentCircuitId.length()); |
| 91 | byte[] bytes = agentCircuitId.getBytes(StandardCharsets.UTF_8); |
| 92 | buf.write(bytes, 0, bytes.length); |
| 93 | } |
| 94 | |
| 95 | // Add sub option if set |
| 96 | if (agentRemoteId != null) { |
| 97 | buf.write((byte) 2); |
| 98 | buf.write((byte) agentRemoteId.length()); |
| 99 | byte[] bytes = agentRemoteId.getBytes(StandardCharsets.UTF_8); |
| 100 | buf.write(bytes, 0, bytes.length); |
| 101 | } |
| 102 | |
| 103 | return buf.toByteArray(); |
| 104 | } |
| 105 | |
| 106 | } |