blob: d642d83cedca6a7fbdef976303e04d628e78ab8d [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
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 B69dca712015-11-20 16:08:53 +053022import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053023import org.onosproject.bgpio.util.Validation;
Priyanka B85843952015-10-14 11:56:30 +053024
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides Implementation of mandatory BGP Origin path attribute.
29 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030public class Origin implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053031
32 /**
33 * Enum to provide ORIGIN types.
34 */
35 public enum ORIGINTYPE {
36 IGP(0), EGP(1), INCOMPLETE(2);
37 int value;
38 /**
39 * Assign val with the value as the ORIGIN type.
40 *
41 * @param val ORIGIN type
42 */
43 ORIGINTYPE(int val) {
44 value = val;
45 }
46
47 /**
48 * Returns value of ORIGIN type.
49 *
50 * @return ORIGIN type
51 */
52 public byte getType() {
53 return (byte) value;
54 }
55 }
56
57 public static final byte ORIGIN_TYPE = 1;
58 public static final byte ORIGIN_VALUE_LEN = 1;
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 */
87 public ORIGINTYPE origin() {
88 if (this.origin == 0) {
89 return ORIGINTYPE.IGP;
90 } else if (this.origin == 1) {
91 return ORIGINTYPE.EGP;
92 } else {
93 return ORIGINTYPE.INCOMPLETE;
94 }
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();
Priyanka B69dca712015-11-20 16:08:53 +0530121 if ((originValue != ORIGINTYPE.INCOMPLETE.value) && (originValue != ORIGINTYPE.IGP.value) &&
Priyanka B85843952015-10-14 11:56:30 +0530122 (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) {
135 //Not required to Implement as of now
136 return 0;
137 }
138
139 @Override
140 public int hashCode() {
141 return Objects.hash(origin);
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 if (this == obj) {
147 return true;
148 }
149 if (obj instanceof Origin) {
150 Origin other = (Origin) obj;
151 return Objects.equals(origin, other.origin);
152 }
153 return false;
154 }
155
156 @Override
157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
159 .add("origin", origin)
160 .toString();
161 }
Priyanka B02040732015-11-29 11:30:29 +0530162
163 @Override
164 public int compareTo(Object o) {
165 // TODO Auto-generated method stub
166 return 0;
167 }
Priyanka B85843952015-10-14 11:56:30 +0530168}