blob: ce8cf58d3206aa753bfea83cfd2b6112405bef43 [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart3930f632015-10-19 12:12:51 -07003 *
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.onosproject.routing.fpm.protocol;
18
19import com.google.common.base.MoreObjects;
20import org.onlab.packet.DeserializationException;
21
22import java.nio.ByteBuffer;
23
24import static org.onlab.packet.PacketUtils.checkInput;
25
26/**
27 * FPM header.
28 */
29public final class FpmHeader {
30 public static final int FPM_HEADER_LENGTH = 4;
31
32 public static final short FPM_VERSION_1 = 1;
33 public static final short FPM_TYPE_NETLINK = 1;
34
35 private static final String VERSION_NOT_SUPPORTED = "FPM version not supported: ";
36 private static final String TYPE_NOT_SUPPORTED = "FPM type not supported: ";
37
38 private final short version;
39 private final short type;
40 private final int length;
41
42 private final Netlink netlink;
43
44 /**
45 * Class constructor.
46 *
47 * @param version version
48 * @param type type
49 * @param length length
50 * @param netlink netlink header
51 */
52 private FpmHeader(short version, short type, int length, Netlink netlink) {
53 this.version = version;
54 this.type = type;
55 this.length = length;
56 this.netlink = netlink;
57 }
58
59 /**
60 * Returns the protocol version.
61 *
62 * @return protocol version
63 */
64 public short version() {
65 return version;
66 }
67
68 /**
69 * Returns the type.
70 *
71 * @return type
72 */
73 public short type() {
74 return type;
75 }
76
77 /**
78 * Returns the message length.
79 *
80 * @return message length
81 */
82 public int length() {
83 return length;
84 }
85
86 /**
87 * Returns the netlink header.
88 *
89 * @return netlink header
90 */
91 public Netlink netlink() {
92 return netlink;
93 }
94
95 @Override
96 public String toString() {
97 return MoreObjects.toStringHelper(getClass())
98 .add("version", version)
99 .add("type", type)
100 .add("length", length)
101 .add("netlink", netlink)
102 .toString();
103 }
104
105 /**
106 * Decodes an FPM header from an input buffer.
107 *
108 * @param buffer input buffer
109 * @param start starting position the FPM header
110 * @param length length of the message
111 * @return FPM header
112 * @throws DeserializationException if an FPM header could not be decoded
113 * from the input buffer
114 */
115 public static FpmHeader decode(byte[] buffer, int start, int length) throws
116 DeserializationException {
117 checkInput(buffer, start, length, FPM_HEADER_LENGTH);
118
119 ByteBuffer bb = ByteBuffer.wrap(buffer, start, length);
120
121 short version = bb.get();
122 if (version != FPM_VERSION_1) {
123 throw new DeserializationException(VERSION_NOT_SUPPORTED + version);
124 }
125
126 short type = bb.get();
127 if (type != FPM_TYPE_NETLINK) {
128 throw new DeserializationException(TYPE_NOT_SUPPORTED + type);
129 }
130
131 int messageLength = bb.getShort();
132
133 Netlink netlink = Netlink.decode(buffer, bb.position(), bb.limit() - bb.position());
134
135 return new FpmHeader(version, type, messageLength, netlink);
136 }
137}