blob: f6a84d8ca0f71b7c8e681b44b47857d5819d8f51 [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 * Output interface route attribute.
26 */
27public final class RouteAttributeOif extends RouteAttribute {
28
29 private static final int VALUE_LENGTH = 4;
30
31 private final long outputInterface;
32
33 /**
34 * Class constructor.
35 *
36 * @param length length
37 * @param type type
38 * @param outputInterface output interface
39 */
40 private RouteAttributeOif(int length, int type, long outputInterface) {
41 super(length, type);
42
43 this.outputInterface = outputInterface;
44 }
45
46 /**
47 * Returns the output interface.
48 *
49 * @return output interface
50 */
51 public long outputInterface() {
52 return outputInterface;
53 }
54
55 @Override
56 public String toString() {
57 return MoreObjects.toStringHelper(getClass())
58 .add("type", type())
59 .add("length", length())
60 .add("outputInterface", outputInterface)
61 .toString();
62 }
63
64 /**
65 * Returns a decoder for a output interface route attribute.
66 *
67 * @return output interface route attribute decoder
68 */
69 public static RouteAttributeDecoder<RouteAttributeOif> 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 outputInterface = Integer.reverseBytes(ByteBuffer.wrap(value).getInt());
76
77 return new RouteAttributeOif(length, type, outputInterface);
78 };
79 }
80}