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