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 | |
| 17 | package org.onosproject.pcepio.types; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.pcepio.protocol.PcepNai; |
| 23 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | |
| 29 | /** |
| 30 | * Provides SrEroSubObject. |
| 31 | */ |
| 32 | public class SrEroSubObject implements PcepValueType { |
| 33 | /* |
| 34 | SR-ERO subobject: (draft-ietf-pce-segment-routing-00) |
| 35 | 0 1 2 3 |
| 36 | 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 |
| 37 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 | |L| Type | Length | ST | Flags |F|S|C|M| |
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | | SID | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | // NAI (variable) // |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | |
| 45 | |
| 46 | |
| 47 | NAI |
| 48 | |
| 49 | 0 1 2 3 |
| 50 | 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 |
| 51 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | | Local IPv4 address | |
| 53 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 54 | | Remote IPv4 address | |
| 55 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 56 | |
| 57 | NAI for IPv4 Adjacency |
| 58 | |
| 59 | 0 1 2 3 |
| 60 | 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 |
| 61 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | // Local IPv6 address (16 bytes) // |
| 63 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | // Remote IPv6 address (16 bytes) // |
| 65 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | |
| 67 | NAI for IPv6 adjacency |
| 68 | |
| 69 | 0 1 2 3 |
| 70 | 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 |
| 71 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | | Local Node-ID | |
| 73 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 74 | | Local Interface ID | |
| 75 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 76 | | Remote Node-ID | |
| 77 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 78 | | Remote Interface ID | |
| 79 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 80 | |
| 81 | NAI for Unnumbered adjacency with IPv4 Node IDs |
| 82 | |
| 83 | */ |
| 84 | protected static final Logger log = LoggerFactory.getLogger(SrEroSubObject.class); |
| 85 | |
| 86 | public static final short TYPE = 0x60; //TODO : type to be defined |
| 87 | public static final short LENGTH = 12; |
| 88 | public static final short VALUE_LENGTH = 10; |
| 89 | public static final int SET = 1; |
| 90 | public static final byte MFLAG_SET = 0x01; |
| 91 | public static final byte CFLAG_SET = 0x02; |
| 92 | public static final byte SFLAG_SET = 0x04; |
| 93 | public static final byte FFLAG_SET = 0x08; |
| 94 | public static final byte SHIFT_ST = 12; |
| 95 | |
| 96 | private final boolean bFFlag; |
| 97 | private final boolean bSFlag; |
| 98 | private final boolean bCFlag; |
| 99 | private final boolean bMFlag; |
| 100 | private final byte st; |
| 101 | |
| 102 | private final int sID; |
| 103 | private final PcepNai nai; |
| 104 | |
| 105 | /** |
| 106 | * Constructor to initialize member variables. |
| 107 | * |
| 108 | * @param st SID type |
| 109 | * @param bFFlag F flag |
| 110 | * @param bSFlag S flag |
| 111 | * @param bCFlag C flag |
| 112 | * @param bMFlag M flag |
| 113 | * @param sID segment identifier value |
| 114 | * @param nai NAI associated with SID |
| 115 | */ |
| 116 | public SrEroSubObject(byte st, boolean bFFlag, boolean bSFlag, boolean bCFlag, boolean bMFlag, int sID, |
| 117 | PcepNai nai) { |
| 118 | this.st = st; |
| 119 | this.bFFlag = bFFlag; |
| 120 | this.bSFlag = bSFlag; |
| 121 | this.bCFlag = bCFlag; |
| 122 | this.bMFlag = bMFlag; |
| 123 | this.sID = sID; |
| 124 | this.nai = nai; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Creates object of SrEroSubObject. |
| 129 | * |
| 130 | * @param st SID type |
| 131 | * @param bFFlag F flag |
| 132 | * @param bSFlag S flag |
| 133 | * @param bCFlag C flag |
| 134 | * @param bMFlag M flag |
| 135 | * @param sID segment identifier value |
| 136 | * @param nai NAI associated with SID |
| 137 | * @return object of SrEroSubObject |
| 138 | */ |
| 139 | public static SrEroSubObject of(byte st, boolean bFFlag, boolean bSFlag, boolean bCFlag, boolean bMFlag, int sID, |
| 140 | PcepNai nai) { |
| 141 | return new SrEroSubObject(st, bFFlag, bSFlag, bCFlag, bMFlag, sID, nai); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns SID type. |
| 146 | * @return st sid type |
| 147 | */ |
| 148 | public byte getSt() { |
| 149 | return st; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns bFFlag. |
| 154 | * @return bFFlag |
| 155 | */ |
| 156 | public boolean getFFlag() { |
| 157 | return bFFlag; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns bSFlag. |
| 162 | * @return bSFlag |
| 163 | */ |
| 164 | public boolean getSFlag() { |
| 165 | return bSFlag; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns bCFlag. |
| 170 | * @return bCFlag |
| 171 | */ |
| 172 | public boolean getCFlag() { |
| 173 | return bCFlag; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns bMFlag. |
| 178 | * @return bMFlag |
| 179 | */ |
| 180 | public boolean getMFlag() { |
| 181 | return bMFlag; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns sID. |
| 186 | * @return sID |
| 187 | */ |
| 188 | public int getSID() { |
| 189 | return sID; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns nai. |
| 194 | * @return nai |
| 195 | */ |
| 196 | public PcepNai getNai() { |
| 197 | return nai; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public PcepVersion getVersion() { |
| 202 | return PcepVersion.PCEP_1; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public short getType() { |
| 207 | return TYPE; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public short getLength() { |
| 212 | return LENGTH; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public int hashCode() { |
| 217 | return Objects.hash(st, bFFlag, bSFlag, bCFlag, bMFlag, sID, nai); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public boolean equals(Object obj) { |
| 222 | if (this == obj) { |
| 223 | return true; |
| 224 | } |
| 225 | if (obj instanceof SrEroSubObject) { |
| 226 | SrEroSubObject other = (SrEroSubObject) obj; |
| 227 | return Objects.equals(this.st, other.st) && Objects.equals(this.bFFlag, other.bFFlag) |
| 228 | && Objects.equals(this.bSFlag, other.bSFlag) && Objects.equals(this.bCFlag, other.bCFlag) |
| 229 | && Objects.equals(this.bMFlag, other.bMFlag) && Objects.equals(this.sID, other.sID) |
| 230 | && Objects.equals(this.nai, other.nai); |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public int write(ChannelBuffer c) { |
| 237 | int iLenStartIndex = c.writerIndex(); |
| 238 | |
| 239 | c.writeShort(TYPE); |
| 240 | c.writeShort(LENGTH); |
| 241 | |
| 242 | short temp = 0; |
| 243 | if (bMFlag) { |
| 244 | temp = (short) (temp | MFLAG_SET); |
| 245 | } |
| 246 | if (bCFlag) { |
| 247 | temp = (short) (temp | CFLAG_SET); |
| 248 | } |
| 249 | if (bSFlag) { |
| 250 | temp = (short) (temp | SFLAG_SET); |
| 251 | } |
| 252 | if (bFFlag) { |
| 253 | temp = (short) (temp | FFLAG_SET); |
| 254 | } |
| 255 | short tempST = (short) (st << SHIFT_ST); |
| 256 | temp = (short) (temp | tempST); |
| 257 | c.writeShort(temp); |
| 258 | c.writeInt(sID); |
| 259 | nai.write(c); |
| 260 | |
| 261 | return c.writerIndex() - iLenStartIndex; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Reads the channel buffer and returns object of SrEroSubObject. |
| 266 | * @param c of type channel buffer |
| 267 | * @return object of SrEroSubObject |
| 268 | */ |
| 269 | public static PcepValueType read(ChannelBuffer c) { |
| 270 | short temp = c.readShort(); |
| 271 | boolean bMFlag; |
| 272 | boolean bCFlag; |
| 273 | boolean bSFlag; |
| 274 | boolean bFFlag; |
| 275 | byte st; |
| 276 | PcepNai nai = null; |
| 277 | |
Phanendra Manda | f346ea8 | 2015-09-04 15:21:39 -0700 | [diff] [blame] | 278 | bMFlag = (temp & MFLAG_SET) == MFLAG_SET; |
| 279 | bCFlag = (temp & CFLAG_SET) == CFLAG_SET; |
| 280 | bSFlag = (temp & SFLAG_SET) == SFLAG_SET; |
| 281 | bFFlag = (temp & FFLAG_SET) == FFLAG_SET; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 282 | |
| 283 | st = (byte) (temp >> SHIFT_ST); |
| 284 | |
| 285 | int sID = c.readInt(); |
| 286 | switch (st) { |
| 287 | case 0x01: |
| 288 | nai = PcepNaiIpv4NodeId.read(c); |
| 289 | break; |
| 290 | case 0x02: |
| 291 | nai = PcepNaiIpv6NodeId.read(c); |
| 292 | break; |
| 293 | case 0x03: |
| 294 | nai = PcepNaiIpv4Adjacency.read(c); |
| 295 | break; |
| 296 | case 0x04: |
| 297 | nai = PcepNaiIpv6Adjacency.read(c); |
| 298 | break; |
| 299 | case 0x05: |
| 300 | nai = PcepNaiUnnumberedAdjacencyIpv4.read(c); |
| 301 | break; |
| 302 | default: |
| 303 | nai = null; |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | return new SrEroSubObject(st, bFFlag, bSFlag, bCFlag, bMFlag, sID, nai); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public String toString() { |
| 312 | return MoreObjects.toStringHelper(getClass()) |
| 313 | .add("Type", TYPE) |
| 314 | .add("Length", LENGTH) |
| 315 | .add("st", st) |
| 316 | .add("bFflag", bFFlag) |
| 317 | .add("bSFlag", bSFlag) |
| 318 | .add("bCFlag", bCFlag) |
| 319 | .add("bMFlag", bMFlag) |
| 320 | .add("sID", sID) |
| 321 | .add("nAI", nai) |
| 322 | .toString(); |
| 323 | } |
| 324 | } |