blob: a1abe974de935ff31c23a7c9f7512ea6d6d1ff4d [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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
24/**
25 * Priority route attribute.
26 */
27public final class RouteAttributePriority extends RouteAttribute {
28
29 private static final int VALUE_LENGTH = 4;
30
31 private final long priority;
32
33 /**
34 * Class constructor.
35 *
36 * @param length length
37 * @param type type
38 * @param priority priority
39 */
40 private RouteAttributePriority(int length, int type, long priority) {
41 super(length, type);
42
43 this.priority = priority;
44 }
45
46 /**
47 * Returns the priority.
48 *
49 * @return priority
50 */
51 public long priority() {
52 return priority;
53 }
54
55 @Override
56 public String toString() {
57 return MoreObjects.toStringHelper(getClass())
58 .add("type", type())
59 .add("length", length())
60 .add("priority", priority)
61 .toString();
62 }
63
64 /**
65 * Returns a decoder for a priority route attribute.
66 *
67 * @return priority route attribute decoder
68 */
69 public static RouteAttributeDecoder<RouteAttributePriority> decoder() {
70 return (int length, int type, byte[] value) -> {
71 if (value.length != VALUE_LENGTH) {
72 throw new DeserializationException("Wrong value length");
73 }
74
75 long priority = Integer.reverseBytes(ByteBuffer.wrap(value).getInt());
76
77 return new RouteAttributePriority(length, type, priority);
78 };
79 }
80
81}