blob: b673f942f4bc111272d8895b9408d7d9553a1a60 [file] [log] [blame]
Deepa vaddireddy0060f532017-08-04 06:46:05 +00001/*
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 */
16package org.opencord.dhcpl2relay.packet;
17
18import java.io.ByteArrayOutputStream;
19import 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 */
25public 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.
48 * @param value
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}