Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [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 | |
| 17 | package org.onosproject.pcepio.types; |
| 18 | |
Saritha | 8539a2e | 2017-01-05 11:58:54 +0530 | [diff] [blame] | 19 | import java.util.Arrays; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import com.google.common.base.MoreObjects; |
| 27 | |
| 28 | /** |
| 29 | * Provides SymbolicPathNameTlv. |
| 30 | */ |
| 31 | public class SymbolicPathNameTlv implements PcepValueType { |
| 32 | |
| 33 | /* |
| 34 | * SYMBOLIC-PATH-NAME TLV format |
| 35 | * Reference :PCEP Extensions for Stateful PCE draft-ietf-pce-stateful-pce-10 |
| 36 | * |
| 37 | 0 1 2 3 |
| 38 | 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 |
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | | Type=17 | Length (variable) | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | | |
| 43 | // Symbolic Path Name // |
| 44 | | | |
| 45 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | */ |
| 47 | protected static final Logger log = LoggerFactory.getLogger(SymbolicPathNameTlv.class); |
| 48 | |
| 49 | public static final short TYPE = 17; |
| 50 | private short hLength; |
| 51 | |
| 52 | private final byte[] rawValue; |
| 53 | |
| 54 | /** |
| 55 | * Constructor to initialize raw Value. |
| 56 | * |
| 57 | * @param rawValue Symbolic path name |
| 58 | */ |
| 59 | public SymbolicPathNameTlv(byte[] rawValue) { |
| 60 | this.rawValue = rawValue; |
| 61 | this.hLength = (short) rawValue.length; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Constructor to initialize raw Value. |
| 66 | * |
| 67 | * @param rawValue Symbolic path name |
| 68 | * @param hLength length of Symbolic path name |
| 69 | */ |
| 70 | public SymbolicPathNameTlv(byte[] rawValue, short hLength) { |
| 71 | this.rawValue = rawValue; |
| 72 | if (0 == hLength) { |
| 73 | this.hLength = (short) rawValue.length; |
| 74 | } else { |
| 75 | this.hLength = hLength; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Creates an object of SymbolicPathNameTlv. |
| 81 | * |
| 82 | * @param raw Symbolic path name |
| 83 | * @param hLength length of Symbolic path name |
| 84 | * @return object of SymbolicPathNameTlv |
| 85 | */ |
| 86 | public static SymbolicPathNameTlv of(final byte[] raw, short hLength) { |
| 87 | return new SymbolicPathNameTlv(raw, hLength); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns Symbolic path name. |
| 92 | * |
| 93 | * @return Symbolic path name byte array |
| 94 | */ |
| 95 | public byte[] getValue() { |
| 96 | return rawValue; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public PcepVersion getVersion() { |
| 101 | return PcepVersion.PCEP_1; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public short getType() { |
| 106 | return TYPE; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public short getLength() { |
| 111 | return hLength; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
Saritha | 8539a2e | 2017-01-05 11:58:54 +0530 | [diff] [blame] | 116 | return Arrays.hashCode(rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
| 121 | if (this == obj) { |
| 122 | return true; |
| 123 | } |
| 124 | if (obj instanceof SymbolicPathNameTlv) { |
| 125 | SymbolicPathNameTlv other = (SymbolicPathNameTlv) obj; |
Saritha | 8539a2e | 2017-01-05 11:58:54 +0530 | [diff] [blame] | 126 | return Arrays.equals(this.rawValue, other.rawValue); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int write(ChannelBuffer c) { |
| 133 | int iLenStartIndex = c.writerIndex(); |
| 134 | c.writeShort(TYPE); |
| 135 | c.writeShort(hLength); |
| 136 | c.writeBytes(rawValue); |
| 137 | return c.writerIndex() - iLenStartIndex; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Reads channel buffer and returns object of SymbolicPathNameTlv. |
| 142 | * |
| 143 | * @param c of type channel buffer |
| 144 | * @param hLength length of bytes to read |
| 145 | * @return object of SymbolicPathNameTlv |
| 146 | */ |
| 147 | public static SymbolicPathNameTlv read(ChannelBuffer c, short hLength) { |
| 148 | byte[] symbolicPathName = new byte[hLength]; |
| 149 | c.readBytes(symbolicPathName, 0, hLength); |
| 150 | return new SymbolicPathNameTlv(symbolicPathName, hLength); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 155 | return MoreObjects.toStringHelper(getClass()) |
| 156 | .add("SymbolicPathName ", rawValue) |
| 157 | .toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 158 | } |
| 159 | } |