blob: ded518558494b9e108970534f3813003230a3beb [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 */
16
17package org.onlab.packet;
18
19import com.google.common.collect.ImmutableMap;
20import org.onlab.packet.lacp.Lacp;
21
22import java.nio.ByteBuffer;
23import java.util.Map;
24import java.util.Objects;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27import static org.onlab.packet.PacketUtils.checkInput;
28
29/**
30 * Implements ethernet slow protocols.
31 */
32public class Slow extends BasePacket {
33 public static final int HEADER_LENGTH = 1;
34
35 public static final byte SUBTYPE_LACP = 0x1;
36 // Subtypes below has not been implemented yet
37 // public static final byte SUBTYPE_LAMP = 0x2;
38 // public static final byte SUBTYPE_OAM = 0x3;
39 // public static final byte SUBTYPE_OSSP = 0xa;
40
41 public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP =
42 ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
43 .put(Slow.SUBTYPE_LACP, Lacp.deserializer())
44 .build();
45
46 private byte subtype;
47
48 /**
49 * Gets subtype.
50 *
51 * @return subtype
52 */
53 public byte getSubtype() {
54 return subtype;
55 }
56
57 /**
58 * Sets subtype.
59 *
60 * @param subtype the subtype to set
61 * @return this
62 */
63 public Slow setSubtype(byte subtype) {
64 this.subtype = subtype;
65 return this;
66 }
67
68 /**
69 * Deserializer function for Slow packets.
70 *
71 * @return deserializer function
72 */
73 public static Deserializer<Slow> deserializer() {
74 return (data, offset, length) -> {
75 checkInput(data, offset, length, HEADER_LENGTH);
76
77 Slow slow = new Slow();
78 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
79 slow.setSubtype(bb.get());
80
81 Deserializer<? extends IPacket> deserializer;
82 if (Slow.PROTOCOL_DESERIALIZER_MAP.containsKey(slow.subtype)) {
83 deserializer = Slow.PROTOCOL_DESERIALIZER_MAP.get(slow.subtype);
84 } else {
85 throw new DeserializationException("Unsupported slow protocol subtype " + Byte.toString(slow.subtype));
86 }
87
88 int remainingLength = bb.limit() - bb.position();
89 slow.payload = deserializer.deserialize(data, bb.position(), remainingLength);
90 slow.payload.setParent(slow);
91
92 return slow;
93 };
94 }
95
96 @Override
97 public byte[] serialize() {
98 int length = HEADER_LENGTH;
99 byte[] payloadData = null;
100 if (this.payload != null) {
101 this.payload.setParent(this);
102 payloadData = this.payload.serialize();
103 length += payloadData.length;
104 }
105
106 final byte[] data = new byte[length];
107
108 final ByteBuffer bb = ByteBuffer.wrap(data);
109 bb.put(this.subtype);
110 if (payloadData != null) {
111 bb.put(payloadData);
112 }
113
114 return data;
115 }
116
117 @Override
118 public int hashCode() {
119 return Objects.hash(super.hashCode(), subtype);
120 }
121
122 @Override
123 public boolean equals(final Object obj) {
124 if (this == obj) {
125 return true;
126 }
127 if (!super.equals(obj)) {
128 return false;
129 }
130 if (!(obj instanceof Slow)) {
131 return false;
132 }
133 final Slow other = (Slow) obj;
134
135 return this.subtype == other.subtype;
136 }
137
138 @Override
139 public String toString() {
140 return toStringHelper(getClass())
141 .add("subtype", Byte.toString(subtype))
142 .toString();
143 }
144}