blob: 048d81ee81a92e47add77cf7ee621fb467273adc [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 LocalPref BGP Path Attribute.
30 */
31public class LocalPref implements BGPValueType {
32
33 private static final Logger log = LoggerFactory.getLogger(LocalPref.class);
34 public static final byte LOCAL_PREF_TYPE = 5;
35 public static final int TYPE_AND_LEN_AS_SHORT = 4;
36 public static final int TYPE_AND_LEN_AS_BYTE = 3;
37 public static final byte LOCAL_PREF_MAX_LEN = 4;
38
39 private int localPref;
40
41 /**
42 * Constructor to initialize LocalPref.
43 *
44 * @param localPref local preference
45 */
46 public LocalPref(int localPref) {
47 this.localPref = localPref;
48 }
49
50 /**
51 * Returns local preference value.
52 *
53 * @return local preference value
54 */
55 public int localPref() {
56 return this.localPref;
57 }
58
59 /**
60 * Reads the channel buffer and returns object of LocalPref.
61 *
62 * @param cb channelBuffer
63 * @return object of LocalPref
64 * @throws BGPParseException while parsing localPref attribute
65 */
66 public static LocalPref read(ChannelBuffer cb) throws BGPParseException {
67 int localPref;
68 ChannelBuffer tempCb = cb.copy();
69 Validation parseFlags = Validation.parseAttributeHeader(cb);
70 if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
71 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
72 parseFlags.getLength());
73 }
74
75 int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags.getLength()
76 + TYPE_AND_LEN_AS_BYTE;
77 ChannelBuffer data = tempCb.readBytes(len);
78 if (parseFlags.getFirstBit()) {
79 throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
80 }
81
82 localPref = cb.readInt();
83 return new LocalPref(localPref);
84 }
85
86 @Override
87 public short getType() {
88 return LOCAL_PREF_TYPE;
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(localPref);
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
101 if (obj instanceof LocalPref) {
102 LocalPref other = (LocalPref) obj;
103 return Objects.equals(localPref, other.localPref);
104 }
105 return false;
106 }
107
108 @Override
109 public String toString() {
110 return MoreObjects.toStringHelper(getClass())
111 .add("localPref", localPref)
112 .toString();
113 }
114
115 @Override
116 public int write(ChannelBuffer cb) {
117 //Not to implement as of now
118 return 0;
119 }
120}