Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 22 | |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | |
| 25 | /** |
| 26 | * Provides OSPF Route Type Tlv which contains route type. |
| 27 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 28 | public class OspfRouteTypeTlv implements BgpValueType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 29 | |
| 30 | /* Reference :draft-ietf-idr-ls-distribution-11 |
| 31 | 0 1 2 3 |
| 32 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 33 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 | | Type | Length | |
| 35 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 | | Route Type | |
| 37 | +-+-+-+-+-+-+-+-+ |
| 38 | |
| 39 | Figure : OSPF Route Type TLV Format |
| 40 | */ |
| 41 | |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 42 | public static final short TYPE = 264; |
| 43 | public static final short LENGTH = 1; |
| 44 | public static final int INTRA_AREA_TYPE = 1; |
| 45 | public static final short INTER_AREA_TYPE = 2; |
| 46 | public static final short EXTERNAL_TYPE_1 = 3; |
| 47 | public static final short EXTERNAL_TYPE_2 = 4; |
| 48 | public static final short NSSA_TYPE_1 = 5; |
| 49 | public static final short NSSA_TYPE_2 = 6; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 50 | |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 51 | private final byte routeType; |
| 52 | |
| 53 | /** |
| 54 | * Enum for Route Type. |
| 55 | */ |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 56 | public enum RouteType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 57 | Intra_Area(1), Inter_Area(2), External_1(3), External_2(4), NSSA_1(5), NSSA_2(6); |
| 58 | int value; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 59 | RouteType(int val) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 60 | value = val; |
| 61 | } |
| 62 | public byte getType() { |
| 63 | return (byte) value; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Constructor to initialize routeType. |
| 69 | * |
| 70 | * @param routeType Route type |
| 71 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 72 | public OspfRouteTypeTlv(byte routeType) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 73 | this.routeType = routeType; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns object of this class with specified routeType. |
| 78 | * |
| 79 | * @param routeType Route type |
| 80 | * @return object of OSPFRouteTypeTlv |
| 81 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 82 | public static OspfRouteTypeTlv of(final byte routeType) { |
| 83 | return new OspfRouteTypeTlv(routeType); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns RouteType. |
| 88 | * |
| 89 | * @return RouteType |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 90 | * @throws BgpParseException if routeType is not matched |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 91 | */ |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 92 | public RouteType getValue() throws BgpParseException { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 93 | switch (routeType) { |
| 94 | case INTRA_AREA_TYPE: |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 95 | return RouteType.Intra_Area; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 96 | case INTER_AREA_TYPE: |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 97 | return RouteType.Inter_Area; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 98 | case EXTERNAL_TYPE_1: |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 99 | return RouteType.External_1; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 100 | case EXTERNAL_TYPE_2: |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 101 | return RouteType.External_2; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 102 | case NSSA_TYPE_1: |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 103 | return RouteType.NSSA_1; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 104 | case NSSA_TYPE_2: |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 105 | return RouteType.NSSA_2; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 106 | default: |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 107 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int hashCode() { |
| 113 | return Objects.hash(routeType); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean equals(Object obj) { |
| 118 | if (this == obj) { |
| 119 | return true; |
| 120 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 121 | if (obj instanceof OspfRouteTypeTlv) { |
| 122 | OspfRouteTypeTlv other = (OspfRouteTypeTlv) obj; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 123 | return Objects.equals(routeType, other.routeType); |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public int write(ChannelBuffer c) { |
| 130 | int iLenStartIndex = c.writerIndex(); |
| 131 | c.writeShort(TYPE); |
| 132 | c.writeShort(LENGTH); |
| 133 | c.writeByte(routeType); |
| 134 | return c.writerIndex() - iLenStartIndex; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Reads from ChannelBuffer and parses OSPFRouteTypeTlv. |
| 139 | * |
| 140 | * @param cb channelBuffer |
| 141 | * @return object of OSPFRouteTypeTlv |
| 142 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 143 | public static OspfRouteTypeTlv read(ChannelBuffer cb) { |
| 144 | return OspfRouteTypeTlv.of(cb.readByte()); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public short getType() { |
| 149 | return TYPE; |
| 150 | } |
| 151 | |
| 152 | @Override |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 153 | public int compareTo(Object o) { |
| 154 | if (this.equals(o)) { |
| 155 | return 0; |
| 156 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 157 | return ((Byte) (this.routeType)).compareTo((Byte) (((OspfRouteTypeTlv) o).routeType)); |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 161 | public String toString() { |
| 162 | return MoreObjects.toStringHelper(getClass()) |
| 163 | .add("Type", TYPE) |
| 164 | .add("Length", LENGTH) |
| 165 | .add("Value", routeType) |
| 166 | .toString(); |
| 167 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 168 | } |