blob: 2fe48672b9befc780e86febd3cb72a30d364d441 [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 Med BGP Path Attribute.
29 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030public class Med implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053031 public static final byte MED_TYPE = 4;
Priyanka B85843952015-10-14 11:56:30 +053032 public static final byte MED_MAX_LEN = 4;
Shashikanth VH15de0d92016-02-08 15:11:50 +053033 public static final byte FLAGS = (byte) 0x80;
Priyanka B85843952015-10-14 11:56:30 +053034
35 private int med;
36
37 /**
38 * Constructor to initialize med.
39 *
40 * @param med MULTI_EXIT_DISC value
41 */
42 public Med(int med) {
43 this.med = med;
44 }
45
46 /**
47 * Returns Med value.
48 *
49 * @return Med value
50 */
51 public int med() {
52 return this.med;
53 }
54
55 /**
56 * Reads the channel buffer and returns object of Med.
57 *
58 * @param cb ChannelBuffer
59 * @return object of Med
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060 * @throws BgpParseException while parsing Med path attribute
Priyanka B85843952015-10-14 11:56:30 +053061 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053062 public static Med read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053063 int med;
64 ChannelBuffer tempCb = cb.copy();
65 Validation parseFlags = Validation.parseAttributeHeader(cb);
66
67 if ((parseFlags.getLength() > MED_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053069 parseFlags.getLength());
70 }
Priyanka Be9ed7292015-11-18 22:12:29 +053071 int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
72 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053073 ChannelBuffer data = tempCb.readBytes(len);
74 if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
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 med = cb.readInt();
79 return new Med(med);
80 }
81
82 @Override
83 public short getType() {
84 return MED_TYPE;
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hash(med);
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj) {
95 return true;
96 }
97 if (obj instanceof Med) {
98 Med other = (Med) obj;
99 return Objects.equals(med, other.med);
100 }
101 return false;
102 }
103
104 @Override
105 public String toString() {
106 return MoreObjects.toStringHelper(getClass())
107 .add("med", med)
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(med());
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}