blob: 3b2070de4c5427c89d34f1973836acaa0905861b [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;
21import org.onosproject.bgpio.exceptions.BGPParseException;
22import org.onosproject.bgpio.util.Validation;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides Implementation of mandatory BGP Origin path attribute.
30 */
31public class Origin implements BGPValueType {
32 private static final Logger log = LoggerFactory.getLogger(Origin.class);
33
34 /**
35 * Enum to provide ORIGIN types.
36 */
37 public enum ORIGINTYPE {
38 IGP(0), EGP(1), INCOMPLETE(2);
39 int value;
40 /**
41 * Assign val with the value as the ORIGIN type.
42 *
43 * @param val ORIGIN type
44 */
45 ORIGINTYPE(int val) {
46 value = val;
47 }
48
49 /**
50 * Returns value of ORIGIN type.
51 *
52 * @return ORIGIN type
53 */
54 public byte getType() {
55 return (byte) value;
56 }
57 }
58
59 public static final byte ORIGIN_TYPE = 1;
60 public static final byte ORIGIN_VALUE_LEN = 1;
61 public static final int TYPE_AND_LEN_AS_SHORT = 4;
62 public static final int TYPE_AND_LEN_AS_BYTE = 3;
63
64 private boolean isOrigin = false;
65 private byte origin;
66
67 /**
68 * Constructor to initialize parameters.
69 *
70 * @param origin origin value
71 */
72 public Origin(byte origin) {
73 this.origin = origin;
74 this.isOrigin = true;
75 }
76
77 /**
78 * Returns true if origin attribute is present otherwise false.
79 *
80 * @return whether origin is present or not
81 */
82 public boolean isOriginSet() {
83 return this.isOrigin;
84 }
85
86 /**
87 * Returns type of Origin in Enum values.
88 *
89 * @return type of Origin in Enum values
90 */
91 public ORIGINTYPE origin() {
92 if (this.origin == 0) {
93 return ORIGINTYPE.IGP;
94 } else if (this.origin == 1) {
95 return ORIGINTYPE.EGP;
96 } else {
97 return ORIGINTYPE.INCOMPLETE;
98 }
99 }
100
101 /**
102 * Reads from ChannelBuffer and parses Origin.
103 *
104 * @param cb ChannelBuffer
105 * @return object of Origin
106 * @throws BGPParseException while parsing Origin path attribute
107 */
108 public static Origin read(ChannelBuffer cb) throws BGPParseException {
109 ChannelBuffer tempCb = cb.copy();
110 Validation parseFlags = Validation.parseAttributeHeader(cb);
111
112 int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags
113 .getLength() + TYPE_AND_LEN_AS_BYTE;
114 ChannelBuffer data = tempCb.readBytes(len);
115 if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (cb.readableBytes() < parseFlags.getLength())) {
116 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
117 parseFlags.getLength());
118 }
119 if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
120 throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
121 }
122
123 byte originValue;
124 originValue = cb.readByte();
125 if ((originValue != ORIGINTYPE.INCOMPLETE.value) || (originValue != ORIGINTYPE.IGP.value) ||
126 (originValue != ORIGINTYPE.EGP.value)) {
127 throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.INVALID_ORIGIN_ATTRIBUTE, data);
128 }
129 return new Origin(originValue);
130 }
131
132 @Override
133 public short getType() {
134 return ORIGIN_TYPE;
135 }
136
137 @Override
138 public int write(ChannelBuffer cb) {
139 //Not required to Implement as of now
140 return 0;
141 }
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 }
166}