Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
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 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | |
| 24 | /** |
| 25 | * Output interface route attribute. |
| 26 | */ |
| 27 | public 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 | } |