2  * Copyright 2015 Open Networking Laboratory
 
   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
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  16 package org.onosproject.bgpio.types.attr;
 
  18 import java.util.Objects;
 
  20 import org.jboss.netty.buffer.ChannelBuffer;
 
  21 import org.onosproject.bgpio.exceptions.BgpParseException;
 
  22 import org.onosproject.bgpio.types.BgpErrorType;
 
  23 import org.onosproject.bgpio.types.BgpValueType;
 
  24 import org.onosproject.bgpio.util.Validation;
 
  25 import org.slf4j.Logger;
 
  26 import org.slf4j.LoggerFactory;
 
  28 import com.google.common.base.MoreObjects;
 
  31  * Implements BGP link protection type attribute.
 
  33 public final class BgpLinkAttrProtectionType implements BgpValueType {
 
  35     protected static final Logger log = LoggerFactory
 
  36             .getLogger(BgpLinkAttrProtectionType.class);
 
  38     public static final int ATTRLINK_PROTECTIONTYPE = 1093;
 
  39     public static final int LINK_PROTECTION_LEN = 2;
 
  41     public static final int EXTRA_TRAFFIC = 0x01;
 
  42     public static final int UNPROTECTED = 0x02;
 
  43     public static final int SHARED = 0x04;
 
  44     public static final int DEDICATED_ONE_ISTO_ONE = 0x08;
 
  45     public static final int DEDICATED_ONE_PLUS_ONE = 0x10;
 
  46     public static final int ENHANCED = 0x20;
 
  48     /* Link Protection type flags */
 
  49     private final boolean bExtraTraffic;
 
  50     private final boolean bUnprotected;
 
  51     private final boolean bShared;
 
  52     private final boolean bDedOneIstoOne;
 
  53     private final boolean bDedOnePlusOne;
 
  54     private final boolean bEnhanced;
 
  57      * Constructor to initialize the value.
 
  59      * @param bExtraTraffic Extra Traffic
 
  60      * @param bUnprotected Unprotected
 
  61      * @param bShared Shared
 
  62      * @param bDedOneIstoOne Dedicated 1:1
 
  63      * @param bDedOnePlusOne Dedicated 1+1
 
  64      * @param bEnhanced Enhanced
 
  66     private BgpLinkAttrProtectionType(boolean bExtraTraffic,
 
  68                                       boolean bShared, boolean bDedOneIstoOne,
 
  69                                       boolean bDedOnePlusOne, boolean bEnhanced) {
 
  70         this.bExtraTraffic = bExtraTraffic;
 
  71         this.bUnprotected = bUnprotected;
 
  72         this.bShared = bShared;
 
  73         this.bDedOneIstoOne = bDedOneIstoOne;
 
  74         this.bDedOnePlusOne = bDedOnePlusOne;
 
  75         this.bEnhanced = bEnhanced;
 
  79      * Returns object of this class with specified values.
 
  81      * @param bExtraTraffic Extra Traffic
 
  82      * @param bUnprotected Unprotected
 
  83      * @param bShared Shared
 
  84      * @param bDedOneIstoOne Dedicated 1:1
 
  85      * @param bDedOnePlusOne Dedicated 1+1
 
  86      * @param bEnhanced Enhanced
 
  87      * @return object of BgpLinkAttrProtectionType
 
  89     public static BgpLinkAttrProtectionType of(boolean bExtraTraffic,
 
  92                                                boolean bDedOneIstoOne,
 
  93                                                boolean bDedOnePlusOne,
 
  95         return new BgpLinkAttrProtectionType(bExtraTraffic, bUnprotected,
 
  96                                              bShared, bDedOneIstoOne,
 
  97                                              bDedOnePlusOne, bEnhanced);
 
 101      * Reads the BGP link attributes protection type.
 
 103      * @param cb Channel buffer
 
 104      * @return object of type BgpLinkAttrProtectionType
 
 105      * @throws BgpParseException while parsing BgpLinkAttrProtectionType
 
 107     public static BgpLinkAttrProtectionType read(ChannelBuffer cb)
 
 108             throws BgpParseException {
 
 109         short linkProtectionType;
 
 111         short lsAttrLength = cb.readShort();
 
 113         boolean bExtraTraffic;
 
 114         boolean bUnprotected;
 
 116         boolean bDedOneIstoOne;
 
 117         boolean bDedOnePlusOne;
 
 120         if ((lsAttrLength != LINK_PROTECTION_LEN)
 
 121                 || (cb.readableBytes() < lsAttrLength)) {
 
 122             Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
 
 123                                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
 
 127         linkProtectionType = cb.readShort();
 
 128         higherByte = (byte) (linkProtectionType >> 8);
 
 130         bExtraTraffic = ((higherByte & (byte) EXTRA_TRAFFIC) == EXTRA_TRAFFIC);
 
 131         bUnprotected = ((higherByte & (byte) UNPROTECTED) == UNPROTECTED);
 
 132         bShared = ((higherByte & (byte) SHARED) == SHARED);
 
 133         bDedOneIstoOne = ((higherByte & (byte) DEDICATED_ONE_ISTO_ONE) == DEDICATED_ONE_ISTO_ONE);
 
 134         bDedOnePlusOne = ((higherByte & (byte) DEDICATED_ONE_PLUS_ONE) == DEDICATED_ONE_PLUS_ONE);
 
 135         bEnhanced = ((higherByte & (byte) ENHANCED) == ENHANCED);
 
 137         return BgpLinkAttrProtectionType.of(bExtraTraffic, bUnprotected,
 
 138                                             bShared, bDedOneIstoOne,
 
 139                                             bDedOnePlusOne, bEnhanced);
 
 143      * Returns ExtraTraffic Bit.
 
 145      * @return ExtraTraffic Bit
 
 147     public boolean extraTraffic() {
 
 148         return bExtraTraffic;
 
 152      * Returns Unprotected Bit.
 
 154      * @return Unprotected Bit
 
 156     public boolean unprotected() {
 
 161      * Returns Shared Bit.
 
 165     public boolean shared() {
 
 170      * Returns DedOneIstoOne Bit.
 
 172      * @return DedOneIstoOne Bit
 
 174     public boolean dedOneIstoOne() {
 
 175         return bDedOneIstoOne;
 
 179      * Returns DedOnePlusOne Bit.
 
 181      * @return DedOnePlusOne Bit
 
 183     public boolean dedOnePlusOne() {
 
 184         return bDedOnePlusOne;
 
 188      * Returns Enhanced Bit.
 
 190      * @return Enhanced Bit
 
 192     public boolean enhanced() {
 
 197     public short getType() {
 
 198         return ATTRLINK_PROTECTIONTYPE;
 
 202     public int hashCode() {
 
 203         return Objects.hash(bExtraTraffic, bUnprotected, bShared,
 
 204                             bDedOneIstoOne, bDedOnePlusOne, bEnhanced);
 
 208     public boolean equals(Object obj) {
 
 213         if (obj instanceof BgpLinkAttrProtectionType) {
 
 214             BgpLinkAttrProtectionType other = (BgpLinkAttrProtectionType) obj;
 
 215             return Objects.equals(bExtraTraffic, other.bExtraTraffic)
 
 216                     && Objects.equals(bUnprotected, other.bUnprotected)
 
 217                     && Objects.equals(bShared, other.bShared)
 
 218                     && Objects.equals(bDedOneIstoOne, other.bDedOneIstoOne)
 
 219                     && Objects.equals(bDedOnePlusOne, other.bDedOnePlusOne)
 
 220                     && Objects.equals(bEnhanced, other.bEnhanced);
 
 226     public int write(ChannelBuffer cb) {
 
 227         // TODO This will be implemented in the next version
 
 232     public String toString() {
 
 233         return MoreObjects.toStringHelper(getClass())
 
 234                 .add("bExtraTraffic", bExtraTraffic)
 
 235                 .add("bUnprotected", bUnprotected).add("bShared", bShared)
 
 236                 .add("bDedOneIstoOne", bDedOneIstoOne)
 
 237                 .add("bDedOnePlusOne", bDedOnePlusOne)
 
 238                 .add("bEnhanced", bEnhanced).toString();
 
 242     public int compareTo(Object o) {
 
 243         // TODO Auto-generated method stub