blob: 5a4a0c904af596468c1763432dca92fc12432ffe [file] [log] [blame]
Charles Chan64c2dfd2018-07-24 11:58:08 -07001/*
2 * Copyright 2018-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.onlab.packet.lacp;
17
18import org.onlab.packet.Deserializer;
19import org.onlab.packet.MacAddress;
20
21import java.nio.ByteBuffer;
22import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25import static org.onlab.packet.PacketUtils.checkInput;
26
27/**
28 * Represents LACP ActorInfo or PartnerInfo information.
29 */
30public class LacpBaseTlv extends LacpTlv {
31 public static final byte LENGTH = 20;
32 private static final byte[] RESERVED = new byte[3];
33
34 private short systemPriority;
35 private MacAddress systemMac;
36 private short key;
37 private short portPriority;
38 private short port;
39 private LacpState state;
40
41 /**
42 * Gets system priority.
43 *
44 * @return system priority
45 */
46 public short getSystemPriority() {
47 return systemPriority;
48 }
49
50 /**
51 * Sets system priority.
52 *
53 * @param systemPriority system priority
54 * @return this
55 */
56 public LacpBaseTlv setSystemPriority(short systemPriority) {
57 this.systemPriority = systemPriority;
58 return this;
59 }
60
61 /**
62 * Gets system MAC address.
63 *
64 * @return system MAC address
65 */
66 public MacAddress getSystemMac() {
67 return systemMac;
68 }
69
70 /**
71 * Sets system MAC address.
72 *
73 * @param systemMac system MAC
74 * @return this
75 */
76 public LacpBaseTlv setSystemMac(MacAddress systemMac) {
77 this.systemMac = systemMac;
78 return this;
79 }
80
81 /**
82 * Gets key.
83 *
84 * @return key
85 */
86 public short getKey() {
87 return key;
88 }
89
90 /**
91 * Sets key.
92 *
93 * @param key key
94 * @return this
95 */
96 public LacpBaseTlv setKey(short key) {
97 this.key = key;
98 return this;
99 }
100
101 /**
102 * Gets port priority.
103 *
104 * @return port priority
105 */
106 public short getPortPriority() {
107 return portPriority;
108 }
109
110 /**
111 * Sets port priority.
112 *
113 * @param portPriority port priority
114 * @return this
115 */
116 public LacpBaseTlv setPortPriority(short portPriority) {
117 this.portPriority = portPriority;
118 return this;
119 }
120
121 /**
122 * Gets port.
123 *
124 * @return port
125 */
126 public short getPort() {
127 return port;
128 }
129
130 /**
131 * Sets port.
132 *
133 * @param port port
134 * @return this
135 */
136 public LacpBaseTlv setPort(short port) {
137 this.port = port;
138 return this;
139 }
140
141 /**
142 * Gets state.
143 *
144 * @return state
145 */
146 public LacpState getState() {
147 return state;
148 }
149
150 /**
151 * Sets state.
152 *
153 * @param state state
154 * @return this
155 */
156 public LacpBaseTlv setState(byte state) {
157 this.state = new LacpState(state);
158 return this;
159 }
160
161 /**
162 * Deserializer function for LacpBaseTlv packets.
163 *
164 * @return deserializer function
165 */
166 public static Deserializer<LacpBaseTlv> deserializer() {
167 return (data, offset, length) -> {
168 checkInput(data, offset, length, LENGTH - HEADER_LENGTH);
169
170 LacpBaseTlv lacpBaseTlv = new LacpBaseTlv();
171 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
172
173 lacpBaseTlv.setSystemPriority(bb.getShort());
174 byte[] mac = new byte[6];
175 bb.get(mac);
176 lacpBaseTlv.setSystemMac(MacAddress.valueOf(mac));
177 lacpBaseTlv.setKey(bb.getShort());
178 lacpBaseTlv.setPortPriority(bb.getShort());
179 lacpBaseTlv.setPort(bb.getShort());
180 lacpBaseTlv.setState(bb.get());
181
182 return lacpBaseTlv;
183 };
184 }
185
186 @Override
187 public byte[] serialize() {
188 final byte[] data = new byte[LENGTH - HEADER_LENGTH];
189
190 final ByteBuffer bb = ByteBuffer.wrap(data);
191 bb.putShort(this.systemPriority);
192 bb.put(this.systemMac.toBytes());
193 bb.putShort(this.key);
194 bb.putShort(this.portPriority);
195 bb.putShort(this.port);
196 bb.put(this.state.toByte());
197 bb.put(RESERVED);
198
199 return data;
200 }
201
202 @Override
203 public boolean equals(Object obj) {
204 if (this == obj) {
205 return true;
206 }
207 if (!super.equals(obj)) {
208 return false;
209 }
210 if (!(obj instanceof LacpBaseTlv)) {
211 return false;
212 }
213 final LacpBaseTlv other = (LacpBaseTlv) obj;
214 return systemPriority == other.systemPriority &&
215 key == other.key &&
216 portPriority == other.portPriority &&
217 port == other.port &&
218 Objects.equals(state, other.state) &&
219 Objects.equals(systemMac, other.systemMac);
220 }
221
222 @Override
223 public int hashCode() {
224 return Objects.hash(super.hashCode(), systemPriority, systemMac, key,
225 portPriority, port, state);
226 }
227
228 @Override
229 public String toString() {
230 return toStringHelper(getClass())
231 .add("systemPriority", Short.toString(systemPriority))
232 .add("systemMac", systemMac.toString())
233 .add("key", Short.toString(key))
234 .add("portPriority", Short.toString(portPriority))
235 .add("port", Short.toString(port))
236 .add("state", state.toString())
237 .toString();
238 }
239}