Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +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 | cd4a90e | 2015-10-14 19:08:46 +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 java.util.ArrayList; |
| 19 | import java.util.List; |
| 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 NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.util.Validation; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Implements BGP link Shared Risk Link Group attribute. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class BgpLinkAttrSrlg implements BgpValueType { |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 34 | |
| 35 | public static final short ATTRNODE_SRLG = 1097; |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 36 | public static final short SIZE = 4; |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 37 | |
| 38 | /* Shared Risk Link Group */ |
| 39 | private List<Integer> sRlg = new ArrayList<Integer>(); |
| 40 | |
| 41 | /** |
| 42 | * Constructor to initialize the date. |
| 43 | * |
| 44 | * @param sRlg Shared Risk link group data |
| 45 | */ |
| 46 | public BgpLinkAttrSrlg(List<Integer> sRlg) { |
| 47 | this.sRlg = sRlg; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns object of this class with specified values. |
| 52 | * |
| 53 | * @param sRlg Shared Risk link group data |
| 54 | * @return object of BgpLinkAttrSrlg |
| 55 | */ |
| 56 | public static BgpLinkAttrSrlg of(ArrayList<Integer> sRlg) { |
| 57 | return new BgpLinkAttrSrlg(sRlg); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Reads the BGP link attributes Shared Risk link group data. |
| 62 | * |
| 63 | * @param cb Channel buffer |
| 64 | * @return object of type BgpLinkAttrSrlg |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 65 | * @throws BgpParseException while parsing BgpLinkAttrSrlg |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 66 | */ |
| 67 | public static BgpLinkAttrSrlg read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | throws BgpParseException { |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 69 | int tempSrlg; |
| 70 | ArrayList<Integer> sRlg = new ArrayList<Integer>(); |
| 71 | |
| 72 | short lsAttrLength = cb.readShort(); |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 73 | int len = lsAttrLength / SIZE; // each element is of 4 octets |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 74 | |
| 75 | if (cb.readableBytes() < lsAttrLength) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 76 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 77 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 78 | lsAttrLength); |
| 79 | } |
| 80 | |
| 81 | for (int i = 0; i < len; i++) { |
| 82 | tempSrlg = cb.readInt(); |
| 83 | sRlg.add(new Integer(tempSrlg)); |
| 84 | } |
| 85 | |
| 86 | return BgpLinkAttrSrlg.of(sRlg); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the Shared Risk link group data. |
| 91 | * |
| 92 | * @return array of Shared Risk link group data |
| 93 | */ |
| 94 | public List<Integer> attrSrlg() { |
| 95 | return sRlg; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public short getType() { |
| 100 | return ATTRNODE_SRLG; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hash(sRlg); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean equals(Object obj) { |
| 110 | if (this == obj) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | if (obj instanceof BgpLinkAttrSrlg) { |
| 115 | BgpLinkAttrSrlg other = (BgpLinkAttrSrlg) obj; |
| 116 | return Objects.equals(sRlg, other.sRlg); |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int write(ChannelBuffer cb) { |
| 123 | // TODO This will be implemented in the next version |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return MoreObjects.toStringHelper(getClass()).omitNullValues().add("sRlg", sRlg).toString(); |
| 130 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 131 | |
| 132 | @Override |
| 133 | public int compareTo(Object o) { |
| 134 | // TODO Auto-generated method stub |
| 135 | return 0; |
| 136 | } |
Thejaswi NK | cd4a90e | 2015-10-14 19:08:46 +0530 | [diff] [blame] | 137 | } |