blob: be321c95959d7bff2f960467ad2b6bbe211dc399 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
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 */
16package org.onosproject.bgpio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Bb2988fa2015-10-09 12:45:36 +053022
23import com.google.common.base.MoreObjects;
24
25/**
26 * Provides OSPF Route Type Tlv which contains route type.
27 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053028public class OSPFRouteTypeTlv implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053029
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 Bb2988fa2015-10-09 12:45:36 +053042 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 B02040732015-11-29 11:30:29 +053050
Priyanka Bb2988fa2015-10-09 12:45:36 +053051 private final byte routeType;
52
53 /**
54 * Enum for Route Type.
55 */
Priyanka B02040732015-11-29 11:30:29 +053056 public enum RouteType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053057 Intra_Area(1), Inter_Area(2), External_1(3), External_2(4), NSSA_1(5), NSSA_2(6);
58 int value;
Priyanka B02040732015-11-29 11:30:29 +053059 RouteType(int val) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053060 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 */
72 public OSPFRouteTypeTlv(byte routeType) {
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 */
82 public static OSPFRouteTypeTlv of(final byte routeType) {
83 return new OSPFRouteTypeTlv(routeType);
84 }
85
86 /**
87 * Returns RouteType.
88 *
89 * @return RouteType
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053090 * @throws BgpParseException if routeType is not matched
Priyanka Bb2988fa2015-10-09 12:45:36 +053091 */
Priyanka B02040732015-11-29 11:30:29 +053092 public RouteType getValue() throws BgpParseException {
Priyanka Bb2988fa2015-10-09 12:45:36 +053093 switch (routeType) {
94 case INTRA_AREA_TYPE:
Priyanka B02040732015-11-29 11:30:29 +053095 return RouteType.Intra_Area;
Priyanka Bb2988fa2015-10-09 12:45:36 +053096 case INTER_AREA_TYPE:
Priyanka B02040732015-11-29 11:30:29 +053097 return RouteType.Inter_Area;
Priyanka Bb2988fa2015-10-09 12:45:36 +053098 case EXTERNAL_TYPE_1:
Priyanka B02040732015-11-29 11:30:29 +053099 return RouteType.External_1;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530100 case EXTERNAL_TYPE_2:
Priyanka B02040732015-11-29 11:30:29 +0530101 return RouteType.External_2;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530102 case NSSA_TYPE_1:
Priyanka B02040732015-11-29 11:30:29 +0530103 return RouteType.NSSA_1;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530104 case NSSA_TYPE_2:
Priyanka B02040732015-11-29 11:30:29 +0530105 return RouteType.NSSA_2;
Priyanka Bb2988fa2015-10-09 12:45:36 +0530106 default:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530107 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530108 }
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 }
121 if (obj instanceof OSPFRouteTypeTlv) {
122 OSPFRouteTypeTlv other = (OSPFRouteTypeTlv) obj;
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 */
143 public static OSPFRouteTypeTlv read(ChannelBuffer cb) {
144 return OSPFRouteTypeTlv.of(cb.readByte());
145 }
146
147 @Override
148 public short getType() {
149 return TYPE;
150 }
151
152 @Override
Priyanka B02040732015-11-29 11:30:29 +0530153 public int compareTo(Object o) {
154 if (this.equals(o)) {
155 return 0;
156 }
157 return ((Byte) (this.routeType)).compareTo((Byte) (((OSPFRouteTypeTlv) o).routeType));
158 }
159
160 @Override
Priyanka Bb2988fa2015-10-09 12:45:36 +0530161 public String toString() {
162 return MoreObjects.toStringHelper(getClass())
163 .add("Type", TYPE)
164 .add("Length", LENGTH)
165 .add("Value", routeType)
166 .toString();
167 }
168}