blob: 5576a60256242934756bf1ac0ed19a533539e105 [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;
33
34 private int localPref;
35
36 /**
37 * Constructor to initialize LocalPref.
38 *
39 * @param localPref local preference
40 */
41 public LocalPref(int localPref) {
42 this.localPref = localPref;
43 }
44
45 /**
46 * Returns local preference value.
47 *
48 * @return local preference value
49 */
50 public int localPref() {
51 return this.localPref;
52 }
53
54 /**
55 * Reads the channel buffer and returns object of LocalPref.
56 *
57 * @param cb channelBuffer
58 * @return object of LocalPref
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053059 * @throws BgpParseException while parsing localPref attribute
Priyanka B85843952015-10-14 11:56:30 +053060 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053061 public static LocalPref read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053062 int localPref;
63 ChannelBuffer tempCb = cb.copy();
64 Validation parseFlags = Validation.parseAttributeHeader(cb);
65 if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053066 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053067 parseFlags.getLength());
68 }
69
Priyanka Be9ed7292015-11-18 22:12:29 +053070 int len = parseFlags.isShort() ? parseFlags.getLength() +
71 Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053072 ChannelBuffer data = tempCb.readBytes(len);
73 if (parseFlags.getFirstBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +053075 }
76
77 localPref = cb.readInt();
78 return new LocalPref(localPref);
79 }
80
81 @Override
82 public short getType() {
83 return LOCAL_PREF_TYPE;
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(localPref);
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) {
94 return true;
95 }
96 if (obj instanceof LocalPref) {
97 LocalPref other = (LocalPref) obj;
98 return Objects.equals(localPref, other.localPref);
99 }
100 return false;
101 }
102
103 @Override
104 public String toString() {
105 return MoreObjects.toStringHelper(getClass())
106 .add("localPref", localPref)
107 .toString();
108 }
109
110 @Override
111 public int write(ChannelBuffer cb) {
112 //Not to implement as of now
113 return 0;
114 }
115}