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