Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [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.pcepio.types; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import com.google.common.base.MoreObjects; |
| 26 | |
| 27 | /** |
| 28 | * Provide Link Protection Type. |
| 29 | */ |
| 30 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 31 | public class LinkProtectionTypeSubTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 32 | |
| 33 | /* Reference :[RFC5307]/1.2 |
| 34 | * 0 1 2 3 |
| 35 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 36 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | | Type=[TDB38] | Length=2 | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | |Protection Cap | Reserved | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 42 | protected static final Logger log = LoggerFactory.getLogger(LinkProtectionTypeSubTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 43 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 44 | public static final short TYPE = 27; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 45 | public static final short LENGTH = 2; |
| 46 | private final byte protectionCap; |
| 47 | private final byte reserved; |
| 48 | |
| 49 | /** |
| 50 | * Constructor to initialize protectionCap. |
| 51 | * |
| 52 | * @param protectionCap Protection Cap |
| 53 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 54 | public LinkProtectionTypeSubTlv(byte protectionCap) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 55 | this.protectionCap = protectionCap; |
| 56 | this.reserved = 0; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Constructor to initialize protectionCap, reserved. |
| 61 | * |
| 62 | * @param protectionCap Protection Cap |
| 63 | * @param reserved Reserved value |
| 64 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 65 | public LinkProtectionTypeSubTlv(byte protectionCap, byte reserved) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 66 | this.protectionCap = protectionCap; |
| 67 | this.reserved = reserved; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns Protection Cap. |
| 72 | * |
| 73 | * @return protectionCap Protection Cap |
| 74 | */ |
| 75 | public byte getProtectionCap() { |
| 76 | return protectionCap; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public PcepVersion getVersion() { |
| 81 | return PcepVersion.PCEP_1; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public short getType() { |
| 86 | return TYPE; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public short getLength() { |
| 91 | return LENGTH; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return Objects.hash(protectionCap, reserved); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 104 | if (obj instanceof LinkProtectionTypeSubTlv) { |
| 105 | LinkProtectionTypeSubTlv other = (LinkProtectionTypeSubTlv) obj; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 106 | return Objects.equals(protectionCap, other.protectionCap) && Objects.equals(reserved, other.reserved); |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int write(ChannelBuffer c) { |
| 114 | int iLenStartIndex = c.writerIndex(); |
| 115 | c.writeShort(TYPE); |
| 116 | c.writeShort(LENGTH); |
| 117 | c.writeByte(protectionCap); |
| 118 | c.writeByte(reserved); |
| 119 | return c.writerIndex() - iLenStartIndex; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reads the channel buffer and returns object of LinkProtectionTypeTlv. |
| 124 | * |
| 125 | * @param c input channel buffer |
| 126 | * @return object of LinkProtectionTypeTlv |
| 127 | */ |
| 128 | public static PcepValueType read(ChannelBuffer c) { |
| 129 | byte protectionCap = c.readByte(); |
| 130 | byte reserved = c.readByte(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 131 | return new LinkProtectionTypeSubTlv(protectionCap, reserved); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 136 | return MoreObjects.toStringHelper(getClass()) |
| 137 | .add("Type", TYPE) |
| 138 | .add("Length", LENGTH) |
| 139 | .add("ProtectionCap", protectionCap) |
| 140 | .toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 141 | } |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 142 | } |