blob: 43a21c080475ebd0f067b078a4b393a67a5cec59 [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.collect.ImmutableMap;
20import org.onlab.packet.DeserializationException;
21
Kalhee Kimba366062017-11-07 16:32:09 +000022import org.jboss.netty.buffer.ChannelBuffer;
Jonathan Hart3930f632015-10-19 12:12:51 -070023import java.nio.ByteBuffer;
24import java.util.Map;
25
26import static org.onlab.packet.PacketUtils.checkInput;
27
28/**
29 * Route attribute header.
30 */
31public abstract class RouteAttribute {
32
33 public static final int ROUTE_ATTRIBUTE_HEADER_LENGTH = 4;
34
35 public static final int RTA_DST = 1;
36 public static final int RTA_OIF = 4;
37 public static final int RTA_GATEWAY = 5;
38 public static final int RTA_PRIORITY = 6;
39
40 private final int length;
41 private final int type;
42
43 private static final Map<Integer, RouteAttributeDecoder<?>> TYPE_DECODER_MAP
44 = ImmutableMap.<Integer, RouteAttributeDecoder<?>>builder()
45 .put(RTA_DST, RouteAttributeDst.decoder())
46 .put(RTA_OIF, RouteAttributeOif.decoder())
47 .put(RTA_GATEWAY, RouteAttributeGateway.decoder())
48 .put(RTA_PRIORITY, RouteAttributePriority.decoder())
49 .build();
50
51 /**
52 * Class constructor.
53 *
54 * @param length attribute length
55 * @param type attribute type
56 */
57 protected RouteAttribute(int length, int type) {
58 this.length = length;
59 this.type = type;
60 }
61
62 /**
63 * Returns the attribute length.
64 *
65 * @return length
66 */
67 public int length() {
68 return length;
69 }
70
71 /**
72 * Returns the attribute type.
73 *
74 * @return type
75 */
76 public int type() {
77 return type;
78 }
79
80 @Override
81 public abstract String toString();
82
83 /**
84 * Decodes a route attribute from an input buffer.
85 *
86 * @param buffer input buffer
87 * @param start starting position the route attribute message
88 * @param length length of the message
89 * @return route attribute message
90 * @throws DeserializationException if a route attribute could not be
91 * decoded from the input buffer
92 */
93 public static RouteAttribute decode(byte[] buffer, int start, int length)
94 throws DeserializationException {
95 checkInput(buffer, start, length, ROUTE_ATTRIBUTE_HEADER_LENGTH);
96
97 ByteBuffer bb = ByteBuffer.wrap(buffer, start, length);
98
99 int tlvLength = Short.reverseBytes(bb.getShort());
100 int type = Short.reverseBytes(bb.getShort());
101
102 if (bb.remaining() < tlvLength - ROUTE_ATTRIBUTE_HEADER_LENGTH) {
103 throw new DeserializationException(
104 "Incorrect buffer size when decoding route attribute");
105 }
106
107 byte[] value = new byte[tlvLength - ROUTE_ATTRIBUTE_HEADER_LENGTH];
108 bb.get(value);
109
110 RouteAttributeDecoder<?> decoder = TYPE_DECODER_MAP.get(type);
111 if (decoder == null) {
112 throw new DeserializationException(
113 "No decoder found for route attribute type " + type);
114 }
115
116 return decoder.decodeAttribute(tlvLength, type, value);
117 }
Kalhee Kimba366062017-11-07 16:32:09 +0000118
119 /**
120 * Encode the RouteAttribute into the ChannelBuffer.
121 *
122 * @param cb channelbuffer to be filled in
123 */
124 public abstract void encode(ChannelBuffer cb);
125
Jonathan Hart3930f632015-10-19 12:12:51 -0700126}