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 | b2988fa | 2015-10-09 12:45:36 +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; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +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 | * BGP Multi-Topology ID of the LS attribute. |
| 34 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 35 | public class BgpAttrNodeMultiTopologyId implements BgpValueType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 36 | |
| 37 | private static final Logger log = LoggerFactory |
| 38 | .getLogger(BgpAttrNodeMultiTopologyId.class); |
| 39 | |
| 40 | public static final int ATTRNODE_MULTITOPOLOGY = 263; |
| 41 | |
| 42 | /* Opaque Node Attribute */ |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 43 | private List<Short> multiTopologyId = new ArrayList<Short>(); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Constructor to initialize the Node attribute multi-topology ID. |
| 47 | * |
| 48 | * @param multiTopologyId multi-topology ID |
| 49 | */ |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 50 | public BgpAttrNodeMultiTopologyId(List<Short> multiTopologyId) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 51 | this.multiTopologyId = multiTopologyId; |
| 52 | } |
| 53 | |
| 54 | /** |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 55 | * Returns object of this class with specified values. |
| 56 | * |
| 57 | * @param multiTopologyId Prefix Metric |
| 58 | * @return object of BgpAttrNodeMultiTopologyId |
| 59 | */ |
| 60 | public static BgpAttrNodeMultiTopologyId of(ArrayList<Short> multiTopologyId) { |
| 61 | return new BgpAttrNodeMultiTopologyId(multiTopologyId); |
| 62 | } |
| 63 | |
| 64 | /** |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 65 | * Reads the Multi-topology ID of Node attribute. |
| 66 | * |
| 67 | * @param cb ChannelBuffer |
| 68 | * @return Constructor of BgpAttrNodeMultiTopologyId |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 69 | * @throws BgpParseException while parsing BgpAttrNodeMultiTopologyId |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 70 | */ |
| 71 | public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 72 | throws BgpParseException { |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 73 | ArrayList<Short> multiTopologyId = new ArrayList<Short>(); |
| 74 | short tempMultiTopologyId; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 75 | short lsAttrLength = cb.readShort(); |
| 76 | int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs |
| 77 | |
| 78 | if (cb.readableBytes() < lsAttrLength) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 79 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 80 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 81 | lsAttrLength); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 82 | } |
| 83 | |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 84 | for (int i = 0; i < len; i++) { |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 85 | tempMultiTopologyId = cb.readShort(); |
| 86 | multiTopologyId.add(new Short(tempMultiTopologyId)); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return new BgpAttrNodeMultiTopologyId(multiTopologyId); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * to get the multi-topology ID. |
| 94 | * |
| 95 | * @return multitopology ID |
| 96 | */ |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 97 | public List<Short> attrMultiTopologyId() { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 98 | return multiTopologyId; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public short getType() { |
| 103 | return ATTRNODE_MULTITOPOLOGY; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(multiTopologyId); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object obj) { |
| 113 | if (this == obj) { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | if (obj instanceof BgpAttrNodeMultiTopologyId) { |
| 118 | BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj; |
| 119 | return Objects.equals(multiTopologyId, other.multiTopologyId); |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int write(ChannelBuffer cb) { |
Thejaswi NK | 380fa1b | 2015-11-18 17:50:51 +0530 | [diff] [blame] | 126 | // TODO This will be implemented in the next version |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(getClass()) |
| 133 | .omitNullValues() |
| 134 | .add("multiTopologyId", multiTopologyId) |
| 135 | .toString(); |
| 136 | } |
| 137 | } |