blob: b15c74c78882c660a1a5509b32efd0731af74f11 [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 Be9ed7292015-11-18 22:12:29 +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 LocalPref BGP Path Attribute.
29 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030public class LocalPref implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053031 public static final byte LOCAL_PREF_TYPE = 5;
Priyanka B85843952015-10-14 11:56:30 +053032 public static final byte LOCAL_PREF_MAX_LEN = 4;
Shashikanth VH15de0d92016-02-08 15:11:50 +053033 public static final byte FLAGS = (byte) 0x40;
Priyanka B85843952015-10-14 11:56:30 +053034
35 private int localPref;
36
37 /**
38 * Constructor to initialize LocalPref.
39 *
40 * @param localPref local preference
41 */
42 public LocalPref(int localPref) {
43 this.localPref = localPref;
44 }
45
46 /**
47 * Returns local preference value.
48 *
49 * @return local preference value
50 */
51 public int localPref() {
52 return this.localPref;
53 }
54
55 /**
56 * Reads the channel buffer and returns object of LocalPref.
57 *
58 * @param cb channelBuffer
59 * @return object of LocalPref
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060 * @throws BgpParseException while parsing localPref attribute
Priyanka B85843952015-10-14 11:56:30 +053061 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053062 public static LocalPref read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053063 int localPref;
64 ChannelBuffer tempCb = cb.copy();
65 Validation parseFlags = Validation.parseAttributeHeader(cb);
66 if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053068 parseFlags.getLength());
69 }
70
Priyanka Be9ed7292015-11-18 22:12:29 +053071 int len = parseFlags.isShort() ? parseFlags.getLength() +
72 Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053073 ChannelBuffer data = tempCb.readBytes(len);
74 if (parseFlags.getFirstBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +053076 }
77
78 localPref = cb.readInt();
79 return new LocalPref(localPref);
80 }
81
82 @Override
83 public short getType() {
84 return LOCAL_PREF_TYPE;
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hash(localPref);
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj) {
95 return true;
96 }
97 if (obj instanceof LocalPref) {
98 LocalPref other = (LocalPref) obj;
99 return Objects.equals(localPref, other.localPref);
100 }
101 return false;
102 }
103
104 @Override
105 public String toString() {
106 return MoreObjects.toStringHelper(getClass())
107 .add("localPref", localPref)
108 .toString();
109 }
110
111 @Override
112 public int write(ChannelBuffer cb) {
Shashikanth VH15de0d92016-02-08 15:11:50 +0530113 int iLenStartIndex = cb.writerIndex();
114 cb.writeByte(FLAGS);
115 cb.writeByte(getType());
116 cb.writeByte(4);
117 cb.writeInt(localPref());
118 return cb.writerIndex() - iLenStartIndex;
Priyanka B85843952015-10-14 11:56:30 +0530119 }
Priyanka B02040732015-11-29 11:30:29 +0530120
121 @Override
122 public int compareTo(Object o) {
123 // TODO Auto-generated method stub
124 return 0;
125 }
Priyanka B85843952015-10-14 11:56:30 +0530126}