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; |
| 20 | import org.onlab.packet.DeserializationException; |
| 21 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 22 | import org.jboss.netty.buffer.ChannelBuffer; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 23 | import java.nio.ByteBuffer; |
| 24 | |
| 25 | /** |
| 26 | * Priority route attribute. |
| 27 | */ |
| 28 | public final class RouteAttributePriority extends RouteAttribute { |
| 29 | |
| 30 | private static final int VALUE_LENGTH = 4; |
| 31 | |
| 32 | private final long priority; |
| 33 | |
| 34 | /** |
| 35 | * Class constructor. |
| 36 | * |
| 37 | * @param length length |
| 38 | * @param type type |
| 39 | * @param priority priority |
| 40 | */ |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 41 | public RouteAttributePriority(int length, int type, long priority) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 42 | super(length, type); |
| 43 | |
| 44 | this.priority = priority; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the priority. |
| 49 | * |
| 50 | * @return priority |
| 51 | */ |
| 52 | public long priority() { |
| 53 | return priority; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public String toString() { |
| 58 | return MoreObjects.toStringHelper(getClass()) |
| 59 | .add("type", type()) |
| 60 | .add("length", length()) |
| 61 | .add("priority", priority) |
| 62 | .toString(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a decoder for a priority route attribute. |
| 67 | * |
| 68 | * @return priority route attribute decoder |
| 69 | */ |
| 70 | public static RouteAttributeDecoder<RouteAttributePriority> decoder() { |
| 71 | return (int length, int type, byte[] value) -> { |
| 72 | if (value.length != VALUE_LENGTH) { |
| 73 | throw new DeserializationException("Wrong value length"); |
| 74 | } |
| 75 | |
| 76 | long priority = Integer.reverseBytes(ByteBuffer.wrap(value).getInt()); |
| 77 | |
| 78 | return new RouteAttributePriority(length, type, priority); |
| 79 | }; |
| 80 | } |
| 81 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 82 | /** |
| 83 | * Encode the RouteAttributePriority contents into the ChannelBuffer. |
| 84 | * |
| 85 | * @param cb channelbuffer to be filled in |
| 86 | */ |
| 87 | @Override |
| 88 | public void encode(ChannelBuffer cb) { |
| 89 | |
| 90 | cb.writeShort(Short.reverseBytes((short) length())); |
| 91 | cb.writeShort(Short.reverseBytes((short) type())); |
| 92 | cb.writeInt(Integer.reverseBytes((int) priority)); |
| 93 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 94 | } |