Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.bgpio.types.attr; |
| 17 | |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 24 | import org.onosproject.bgpio.types.BgpErrorType; |
| 25 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.util.Validation; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.google.common.base.MoreObjects; |
| 31 | |
| 32 | /** |
| 33 | * Implements BGP prefix route tag attribute. |
| 34 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 35 | public class BgpPrefixAttrRouteTag implements BgpValueType { |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 36 | |
| 37 | protected static final Logger log = LoggerFactory |
| 38 | .getLogger(BgpPrefixAttrRouteTag.class); |
| 39 | |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 40 | public static final short ATTR_PREFIX_ROUTETAG = 1153; |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 41 | public static final short SIZE = 4; |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 42 | |
| 43 | /* Prefix Route Tag */ |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 44 | private List<Integer> pfxRouteTag = new ArrayList<Integer>(); |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Constructor to initialize the values. |
| 48 | * |
| 49 | * @param pfxRouteTag prefix route tag |
| 50 | */ |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 51 | public BgpPrefixAttrRouteTag(List<Integer> pfxRouteTag) { |
| 52 | this.pfxRouteTag = pfxRouteTag; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns object of this class with specified values. |
| 57 | * |
| 58 | * @param pfxRouteTag Prefix Metric |
| 59 | * @return object of BgpPrefixAttrRouteTag |
| 60 | */ |
| 61 | public static BgpPrefixAttrRouteTag of(ArrayList<Integer> pfxRouteTag) { |
| 62 | return new BgpPrefixAttrRouteTag(pfxRouteTag); |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Reads the Route Tag. |
| 67 | * |
| 68 | * @param cb ChannelBuffer |
| 69 | * @return object of BgpPrefixAttrRouteTag |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | * @throws BgpParseException while parsing BgpPrefixAttrRouteTag |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 71 | */ |
| 72 | public static BgpPrefixAttrRouteTag read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 73 | throws BgpParseException { |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 74 | int tmp; |
| 75 | ArrayList<Integer> pfxRouteTag = new ArrayList<Integer>(); |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 76 | |
| 77 | short lsAttrLength = cb.readShort(); |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 78 | int len = lsAttrLength / SIZE; |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 79 | |
| 80 | if (cb.readableBytes() < lsAttrLength) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 81 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 82 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 83 | lsAttrLength); |
| 84 | } |
| 85 | |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 86 | for (int i = 0; i < len; i++) { |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 87 | tmp = cb.readInt(); |
| 88 | pfxRouteTag.add(new Integer(tmp)); |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 91 | return BgpPrefixAttrRouteTag.of(pfxRouteTag); |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the prefix route tag. |
| 96 | * |
| 97 | * @return route tag |
| 98 | */ |
Thejaswi N K | e79b0ef | 2015-11-21 20:59:04 +0530 | [diff] [blame] | 99 | public List<Integer> getPfxRouteTag() { |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 100 | return pfxRouteTag; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public short getType() { |
| 105 | return ATTR_PREFIX_ROUTETAG; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int hashCode() { |
| 110 | return Objects.hash(pfxRouteTag); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean equals(Object obj) { |
| 115 | if (this == obj) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (obj instanceof BgpPrefixAttrRouteTag) { |
| 120 | BgpPrefixAttrRouteTag other = (BgpPrefixAttrRouteTag) obj; |
| 121 | return Objects.equals(pfxRouteTag, other.pfxRouteTag); |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int write(ChannelBuffer cb) { |
| 128 | // TODO This will be implemented in the next version |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 135 | .add("pfxRouteTag", pfxRouteTag).toString(); |
| 136 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 137 | |
| 138 | @Override |
| 139 | public int compareTo(Object o) { |
| 140 | // TODO Auto-generated method stub |
| 141 | return 0; |
| 142 | } |
Thejaswi N K | 80252d2 | 2015-10-14 23:34:23 +0530 | [diff] [blame] | 143 | } |