blob: 3e0e06c75e529f3b616d1e5d19fc00f97f4c095a [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;
Priyanka B85843952015-10-14 11:56:30 +053058
59 private boolean isOrigin = false;
60 private byte origin;
61
62 /**
63 * Constructor to initialize parameters.
64 *
65 * @param origin origin value
66 */
67 public Origin(byte origin) {
68 this.origin = origin;
69 this.isOrigin = true;
70 }
71
72 /**
73 * Returns true if origin attribute is present otherwise false.
74 *
75 * @return whether origin is present or not
76 */
77 public boolean isOriginSet() {
78 return this.isOrigin;
79 }
80
81 /**
82 * Returns type of Origin in Enum values.
83 *
84 * @return type of Origin in Enum values
85 */
Jonathan Hart51539b82015-10-29 09:53:04 -070086 public OriginType origin() {
Priyanka B85843952015-10-14 11:56:30 +053087 if (this.origin == 0) {
Jonathan Hart51539b82015-10-29 09:53:04 -070088 return OriginType.IGP;
Priyanka B85843952015-10-14 11:56:30 +053089 } else if (this.origin == 1) {
Jonathan Hart51539b82015-10-29 09:53:04 -070090 return OriginType.EGP;
Priyanka B85843952015-10-14 11:56:30 +053091 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -070092 return OriginType.INCOMPLETE;
Priyanka B85843952015-10-14 11:56:30 +053093 }
94 }
95
96 /**
97 * Reads from ChannelBuffer and parses Origin.
98 *
99 * @param cb ChannelBuffer
100 * @return object of Origin
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530101 * @throws BgpParseException while parsing Origin path attribute
Priyanka B85843952015-10-14 11:56:30 +0530102 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530103 public static Origin read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +0530104 ChannelBuffer tempCb = cb.copy();
105 Validation parseFlags = Validation.parseAttributeHeader(cb);
106
Priyanka B69dca712015-11-20 16:08:53 +0530107 int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
108 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +0530109 ChannelBuffer data = tempCb.readBytes(len);
110 if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (cb.readableBytes() < parseFlags.getLength())) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530111 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +0530112 parseFlags.getLength());
113 }
114 if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530115 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +0530116 }
117
118 byte originValue;
119 originValue = cb.readByte();
Jonathan Hart51539b82015-10-29 09:53:04 -0700120 if ((originValue != OriginType.INCOMPLETE.value) && (originValue != OriginType.IGP.value) &&
121 (originValue != OriginType.EGP.value)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530122 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.INVALID_ORIGIN_ATTRIBUTE, data);
Priyanka B85843952015-10-14 11:56:30 +0530123 }
124 return new Origin(originValue);
125 }
126
127 @Override
128 public short getType() {
129 return ORIGIN_TYPE;
130 }
131
132 @Override
133 public int write(ChannelBuffer cb) {
134 //Not required to Implement as of now
135 return 0;
136 }
137
138 @Override
139 public int hashCode() {
140 return Objects.hash(origin);
141 }
142
143 @Override
144 public boolean equals(Object obj) {
145 if (this == obj) {
146 return true;
147 }
148 if (obj instanceof Origin) {
149 Origin other = (Origin) obj;
150 return Objects.equals(origin, other.origin);
151 }
152 return false;
153 }
154
155 @Override
156 public String toString() {
157 return MoreObjects.toStringHelper(getClass())
158 .add("origin", origin)
159 .toString();
160 }
Priyanka B02040732015-11-29 11:30:29 +0530161
162 @Override
163 public int compareTo(Object o) {
164 // TODO Auto-generated method stub
165 return 0;
166 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700167}