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