blob: 7999f82f47f0da9bfbb0e1d1b2738b792ee68168 [file] [log] [blame]
Priyanka B85843952015-10-14 11:56:30 +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
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
Priyanka B85843952015-10-14 11:56:30 +053019import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053020import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka B69dca712015-11-20 16:08:53 +053021import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053022import org.onosproject.bgpio.util.Validation;
Priyanka B85843952015-10-14 11:56:30 +053023
Jonathan Hart51539b82015-10-29 09:53:04 -070024import java.util.Objects;
Priyanka B85843952015-10-14 11:56:30 +053025
26/**
27 * Provides Implementation of mandatory BGP Origin path attribute.
28 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029public class Origin implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053030
31 /**
32 * Enum to provide ORIGIN types.
33 */
Jonathan Hart51539b82015-10-29 09:53:04 -070034 public enum OriginType {
Priyanka B85843952015-10-14 11:56:30 +053035 IGP(0), EGP(1), INCOMPLETE(2);
36 int value;
37 /**
38 * Assign val with the value as the ORIGIN type.
39 *
40 * @param val ORIGIN type
41 */
Jonathan Hart51539b82015-10-29 09:53:04 -070042 OriginType(int val) {
Priyanka B85843952015-10-14 11:56:30 +053043 value = val;
44 }
45
46 /**
47 * Returns value of ORIGIN type.
48 *
49 * @return ORIGIN type
50 */
51 public byte getType() {
52 return (byte) value;
53 }
54 }
55
56 public static final byte ORIGIN_TYPE = 1;
57 public static final byte ORIGIN_VALUE_LEN = 1;
Shashikanth VH15de0d92016-02-08 15:11:50 +053058 public static final byte FLAGS = (byte) 0x40;
Priyanka B85843952015-10-14 11:56:30 +053059
60 private boolean isOrigin = false;
61 private byte origin;
62
63 /**
64 * Constructor to initialize parameters.
65 *
66 * @param origin origin value
67 */
68 public Origin(byte origin) {
69 this.origin = origin;
70 this.isOrigin = true;
71 }
72
73 /**
74 * Returns true if origin attribute is present otherwise false.
75 *
76 * @return whether origin is present or not
77 */
78 public boolean isOriginSet() {
79 return this.isOrigin;
80 }
81
82 /**
83 * Returns type of Origin in Enum values.
84 *
85 * @return type of Origin in Enum values
86 */
Jonathan Hart51539b82015-10-29 09:53:04 -070087 public OriginType origin() {
Priyanka B85843952015-10-14 11:56:30 +053088 if (this.origin == 0) {
Jonathan Hart51539b82015-10-29 09:53:04 -070089 return OriginType.IGP;
Priyanka B85843952015-10-14 11:56:30 +053090 } else if (this.origin == 1) {
Jonathan Hart51539b82015-10-29 09:53:04 -070091 return OriginType.EGP;
Priyanka B85843952015-10-14 11:56:30 +053092 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -070093 return OriginType.INCOMPLETE;
Priyanka B85843952015-10-14 11:56:30 +053094 }
95 }
96
97 /**
98 * Reads from ChannelBuffer and parses Origin.
99 *
100 * @param cb ChannelBuffer
101 * @return object of Origin
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530102 * @throws BgpParseException while parsing Origin path attribute
Priyanka B85843952015-10-14 11:56:30 +0530103 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530104 public static Origin read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +0530105 ChannelBuffer tempCb = cb.copy();
106 Validation parseFlags = Validation.parseAttributeHeader(cb);
107
Priyanka B69dca712015-11-20 16:08:53 +0530108 int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
109 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +0530110 ChannelBuffer data = tempCb.readBytes(len);
111 if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (cb.readableBytes() < parseFlags.getLength())) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530112 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +0530113 parseFlags.getLength());
114 }
115 if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530116 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +0530117 }
118
119 byte originValue;
120 originValue = cb.readByte();
Jonathan Hart51539b82015-10-29 09:53:04 -0700121 if ((originValue != OriginType.INCOMPLETE.value) && (originValue != OriginType.IGP.value) &&
122 (originValue != OriginType.EGP.value)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530123 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.INVALID_ORIGIN_ATTRIBUTE, data);
Priyanka B85843952015-10-14 11:56:30 +0530124 }
125 return new Origin(originValue);
126 }
127
128 @Override
129 public short getType() {
130 return ORIGIN_TYPE;
131 }
132
133 @Override
134 public int write(ChannelBuffer cb) {
Shashikanth VH15de0d92016-02-08 15:11:50 +0530135 int iLenStartIndex = cb.writerIndex();
136 cb.writeByte(FLAGS);
137 cb.writeByte(ORIGIN_TYPE);
138 cb.writeByte(1);
139 cb.writeByte(origin().value);
140 return cb.writerIndex() - iLenStartIndex;
Priyanka B85843952015-10-14 11:56:30 +0530141 }
142
143 @Override
144 public int hashCode() {
145 return Objects.hash(origin);
146 }
147
148 @Override
149 public boolean equals(Object obj) {
150 if (this == obj) {
151 return true;
152 }
153 if (obj instanceof Origin) {
154 Origin other = (Origin) obj;
155 return Objects.equals(origin, other.origin);
156 }
157 return false;
158 }
159
160 @Override
161 public String toString() {
162 return MoreObjects.toStringHelper(getClass())
163 .add("origin", origin)
164 .toString();
165 }
Priyanka B02040732015-11-29 11:30:29 +0530166
167 @Override
168 public int compareTo(Object o) {
169 // TODO Auto-generated method stub
170 return 0;
171 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700172}