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