Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 16 | package org.onosproject.bgpio.types.attr; |
| 17 | |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 20 | import java.util.ListIterator; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
| 23 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 25 | import org.onosproject.bgpio.types.BgpErrorType; |
| 26 | import org.onosproject.bgpio.types.BgpValueType; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 27 | import org.onosproject.bgpio.util.Validation; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import com.google.common.base.MoreObjects; |
| 32 | |
| 33 | /** |
| 34 | * BGP Multi-Topology ID of the LS attribute. |
| 35 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 36 | public class BgpAttrNodeMultiTopologyId implements BgpValueType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 37 | |
| 38 | private static final Logger log = LoggerFactory |
| 39 | .getLogger(BgpAttrNodeMultiTopologyId.class); |
| 40 | |
| 41 | public static final int ATTRNODE_MULTITOPOLOGY = 263; |
| 42 | |
| 43 | /* Opaque Node Attribute */ |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 44 | private List<Short> multiTopologyId = new ArrayList<Short>(); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Constructor to initialize the Node attribute multi-topology ID. |
| 48 | * |
| 49 | * @param multiTopologyId multi-topology ID |
| 50 | */ |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 51 | public BgpAttrNodeMultiTopologyId(List<Short> multiTopologyId) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 52 | this.multiTopologyId = multiTopologyId; |
| 53 | } |
| 54 | |
| 55 | /** |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 56 | * Returns object of this class with specified values. |
| 57 | * |
| 58 | * @param multiTopologyId Prefix Metric |
| 59 | * @return object of BgpAttrNodeMultiTopologyId |
| 60 | */ |
| 61 | public static BgpAttrNodeMultiTopologyId of(ArrayList<Short> multiTopologyId) { |
| 62 | return new BgpAttrNodeMultiTopologyId(multiTopologyId); |
| 63 | } |
| 64 | |
| 65 | /** |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 66 | * Reads the Multi-topology ID of Node attribute. |
| 67 | * |
| 68 | * @param cb ChannelBuffer |
| 69 | * @return Constructor of BgpAttrNodeMultiTopologyId |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | * @throws BgpParseException while parsing BgpAttrNodeMultiTopologyId |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 71 | */ |
| 72 | public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 73 | throws BgpParseException { |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 74 | ArrayList<Short> multiTopologyId = new ArrayList<Short>(); |
| 75 | short tempMultiTopologyId; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 76 | short lsAttrLength = cb.readShort(); |
| 77 | int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs |
| 78 | |
| 79 | if (cb.readableBytes() < lsAttrLength) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 81 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 82 | lsAttrLength); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 83 | } |
| 84 | |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 85 | for (int i = 0; i < len; i++) { |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 86 | tempMultiTopologyId = cb.readShort(); |
| 87 | multiTopologyId.add(new Short(tempMultiTopologyId)); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return new BgpAttrNodeMultiTopologyId(multiTopologyId); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * to get the multi-topology ID. |
| 95 | * |
| 96 | * @return multitopology ID |
| 97 | */ |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 98 | public List<Short> attrMultiTopologyId() { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 99 | return multiTopologyId; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public short getType() { |
| 104 | return ATTRNODE_MULTITOPOLOGY; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
| 109 | return Objects.hash(multiTopologyId); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean equals(Object obj) { |
| 114 | if (this == obj) { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | if (obj instanceof BgpAttrNodeMultiTopologyId) { |
| 119 | BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj; |
| 120 | return Objects.equals(multiTopologyId, other.multiTopologyId); |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public int write(ChannelBuffer cb) { |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 127 | // TODO This will be implemented in the next version |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String toString() { |
| 133 | return MoreObjects.toStringHelper(getClass()) |
| 134 | .omitNullValues() |
| 135 | .add("multiTopologyId", multiTopologyId) |
| 136 | .toString(); |
| 137 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | public int compareTo(Object o) { |
| 141 | if (this.equals(o)) { |
| 142 | return 0; |
| 143 | } |
| 144 | int countOtherSubTlv = ((BgpAttrNodeMultiTopologyId) o).multiTopologyId.size(); |
| 145 | int countObjSubTlv = multiTopologyId.size(); |
| 146 | if (countOtherSubTlv != countObjSubTlv) { |
| 147 | if (countOtherSubTlv > countObjSubTlv) { |
| 148 | return 1; |
| 149 | } else { |
| 150 | return -1; |
| 151 | } |
| 152 | } |
| 153 | ListIterator<Short> listIterator = multiTopologyId.listIterator(); |
| 154 | ListIterator<Short> listIteratorOther = ((BgpAttrNodeMultiTopologyId) o).multiTopologyId.listIterator(); |
| 155 | while (listIterator.hasNext()) { |
| 156 | short id = listIterator.next(); |
| 157 | short id1 = listIteratorOther.next(); |
| 158 | if (((Short) id).compareTo((Short) id1) != 0) { |
| 159 | return ((Short) id).compareTo((Short) id1); |
| 160 | } |
| 161 | } |
| 162 | return 0; |
| 163 | } |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 164 | } |