Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +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 | */ |
| 16 | package org.onosproject.bgpio.types.attr; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import org.onosproject.bgpio.types.BgpErrorType; |
| 23 | import org.onosproject.bgpio.types.BgpValueType; |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.util.Constants; |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.util.Validation; |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | |
| 29 | /** |
| 30 | * Implements BGP link protection type attribute. |
| 31 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 32 | public final class BgpLinkAttrProtectionType implements BgpValueType { |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 33 | public static final int ATTRLINK_PROTECTIONTYPE = 1093; |
| 34 | public static final int LINK_PROTECTION_LEN = 2; |
| 35 | |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 36 | private byte linkProtectionType; |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 37 | |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 38 | /** |
| 39 | * Enum to provide Link protection types. |
| 40 | */ |
| 41 | public enum ProtectionType { |
| 42 | EXTRA_TRAFFIC(1), UNPROTECTED(2), SHARED(4), DEDICATED_ONE_ISTO_ONE(8), |
| 43 | DEDICATED_ONE_PLUS_ONE(0x10), ENHANCED(0x20), RESERVED(0x40); |
| 44 | int value; |
| 45 | |
| 46 | /** |
| 47 | * Assign val with the value as the link protection type. |
| 48 | * |
| 49 | * @param val link protection |
| 50 | */ |
| 51 | ProtectionType(int val) { |
| 52 | value = val; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns value of link protection type. |
| 57 | * |
| 58 | * @return link protection type |
| 59 | */ |
| 60 | public byte type() { |
| 61 | return (byte) value; |
| 62 | } |
| 63 | } |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Constructor to initialize the value. |
| 67 | * |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 68 | * @param linkProtectionType link protection type |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 69 | */ |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 70 | public BgpLinkAttrProtectionType(byte linkProtectionType) { |
| 71 | this.linkProtectionType = linkProtectionType; |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns object of this class with specified values. |
| 76 | * |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 77 | * @param linkProtectionType link protection type |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 78 | * @return object of BgpLinkAttrProtectionType |
| 79 | */ |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 80 | public static BgpLinkAttrProtectionType of(byte linkProtectionType) { |
| 81 | return new BgpLinkAttrProtectionType(linkProtectionType); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Reads the BGP link attributes protection type. |
| 86 | * |
| 87 | * @param cb Channel buffer |
| 88 | * @return object of type BgpLinkAttrProtectionType |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 89 | * @throws BgpParseException while parsing BgpLinkAttrProtectionType |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 90 | */ |
| 91 | public static BgpLinkAttrProtectionType read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 92 | throws BgpParseException { |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 93 | short lsAttrLength = cb.readShort(); |
| 94 | |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 95 | if ((lsAttrLength != LINK_PROTECTION_LEN) || (cb.readableBytes() < lsAttrLength)) { |
| 96 | Validation |
| 97 | .validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, lsAttrLength); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 98 | } |
| 99 | |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 100 | byte linkProtectionType = cb.readByte(); |
| 101 | byte reserved = cb.readByte(); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 102 | |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 103 | return BgpLinkAttrProtectionType.of(linkProtectionType); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 107 | * Returns Link Protection Type. |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 108 | * |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 109 | * @return Link Protection Type |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 110 | */ |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 111 | public ProtectionType protectionType() throws BgpParseException { |
| 112 | switch (linkProtectionType) { |
| 113 | case Constants.EXTRA_TRAFFIC: |
| 114 | return ProtectionType.EXTRA_TRAFFIC; |
| 115 | case Constants.UNPROTECTED: |
| 116 | return ProtectionType.UNPROTECTED; |
| 117 | case Constants.SHARED: |
| 118 | return ProtectionType.SHARED; |
| 119 | case Constants.DEDICATED_ONE_ISTO_ONE: |
| 120 | return ProtectionType.DEDICATED_ONE_ISTO_ONE; |
| 121 | case Constants.DEDICATED_ONE_PLUS_ONE: |
| 122 | return ProtectionType.DEDICATED_ONE_PLUS_ONE; |
| 123 | case Constants.ENHANCED: |
| 124 | return ProtectionType.ENHANCED; |
| 125 | case Constants.RESERVED: |
| 126 | return ProtectionType.RESERVED; |
| 127 | default: |
| 128 | throw new BgpParseException("Got another type " + linkProtectionType); |
| 129 | } |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public short getType() { |
| 134 | return ATTRLINK_PROTECTIONTYPE; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int hashCode() { |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 139 | return Objects.hash(linkProtectionType); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean equals(Object obj) { |
| 144 | if (this == obj) { |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | if (obj instanceof BgpLinkAttrProtectionType) { |
| 149 | BgpLinkAttrProtectionType other = (BgpLinkAttrProtectionType) obj; |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 150 | return Objects.equals(linkProtectionType, other.linkProtectionType); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public int write(ChannelBuffer cb) { |
| 157 | // TODO This will be implemented in the next version |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public String toString() { |
| 163 | return MoreObjects.toStringHelper(getClass()) |
Priyanka B | 0ab34b9 | 2015-12-03 21:13:52 +0530 | [diff] [blame] | 164 | .add("linkProtectionType", linkProtectionType) |
| 165 | .toString(); |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 166 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 167 | |
| 168 | @Override |
| 169 | public int compareTo(Object o) { |
| 170 | // TODO Auto-generated method stub |
| 171 | return 0; |
| 172 | } |
Thejaswi N K | 65800e1 | 2015-10-15 17:31:30 +0530 | [diff] [blame] | 173 | } |