2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.ietf.stateful02;
10 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
14 import java.util.BitSet;
16 import org.opendaylight.protocol.concepts.Ipv4Util;
17 import org.opendaylight.protocol.concepts.Ipv6Util;
18 import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.TlvParser;
21 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.RsvpError;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.RsvpErrorBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags;
32 * Parser for {@link RsvpErrorSpec}
34 public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
36 public static final int TYPE = 21;
38 private static final int IP4_F_LENGTH = 4;
39 private static final int IP6_F_LENGTH = 16;
40 private static final int FLAGS_F_LENGTH = 1;
41 private static final int ERROR_CODE_F_LENGTH = 1;
42 private static final int ERROR_VALUE_F_LENGTH = 2;
44 private static final int IN_PLACE_FLAG_OFFSET = 7;
45 private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
47 private static final int V4_RSVP_LENGTH = 8;
48 private static final int V6_RSVP_LENGTH = 20;
51 public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
55 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
56 if (buffer.readableBytes() == V4_RSVP_LENGTH) {
57 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH))));
58 } else if (buffer.readableBytes() == V6_RSVP_LENGTH) {
59 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH))));
61 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
62 builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
63 final short errorCode = (short) UnsignedBytes.toInt(buffer.readByte());
64 builder.setCode(errorCode);
65 final int errorValue = buffer.readUnsignedShort();
66 builder.setValue(errorValue);
67 return new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
71 public byte[] serializeTlv(final Tlv tlv) {
73 throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
75 final RsvpErrorSpec rsvpTlv = (RsvpErrorSpec) tlv;
76 final RsvpError rsvp = rsvpTlv.getRsvpError();
77 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
78 flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
79 flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
81 final IpAddress node = rsvp.getNode();
83 if (node.getIpv4Address() != null) {
84 bytes = new byte[V4_RSVP_LENGTH];
85 ByteArray.copyWhole(Ipv4Util.bytesForAddress(node.getIpv4Address()), bytes, offset);
86 offset += IP4_F_LENGTH;
88 bytes = new byte[V6_RSVP_LENGTH];
89 ByteArray.copyWhole(Ipv6Util.bytesForAddress(node.getIpv6Address()), bytes, offset);
90 offset += IP6_F_LENGTH;
92 bytes[offset] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
93 offset += FLAGS_F_LENGTH;
94 bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
95 offset += ERROR_CODE_F_LENGTH;
96 final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
97 ByteArray.copyWhole(value, bytes, offset);
98 return TlvUtil.formatTlv(TYPE, bytes);