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