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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
13 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
14 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
16 import com.google.common.base.Preconditions;
17 import com.google.common.primitives.UnsignedBytes;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.BitSet;
21 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
22 import org.opendaylight.protocol.pcep.spi.TlvParser;
23 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
24 import org.opendaylight.protocol.pcep.spi.TlvUtil;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.protocol.util.Ipv4Util;
27 import org.opendaylight.protocol.util.Ipv6Util;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
31 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;
32 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;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags;
37 * Parser for {@link RsvpErrorSpec}
40 public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
42 public static final int TYPE = 21;
44 private static final int FLAGS_F_LENGTH = 1;
46 private static final int IN_PLACE_FLAG_OFFSET = 7;
47 private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
49 private static final int V4_RSVP_LENGTH = 8;
50 private static final int V6_RSVP_LENGTH = 20;
53 public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
57 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
58 if (buffer.readableBytes() == V4_RSVP_LENGTH) {
59 builder.setNode(new IpAddress(Ipv4Util.addressForByteBuf(buffer)));
60 } else if (buffer.readableBytes() == V6_RSVP_LENGTH) {
61 builder.setNode(new IpAddress(Ipv6Util.addressForByteBuf(buffer)));
63 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
64 builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
65 final short errorCode = (short) UnsignedBytes.toInt(buffer.readByte());
66 builder.setCode(errorCode);
67 final int errorValue = buffer.readUnsignedShort();
68 builder.setValue(errorValue);
69 return new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
73 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
74 Preconditions.checkArgument(tlv instanceof RsvpErrorSpec, "RSVPErrorSpecTlv is mandatory.");
75 final RsvpErrorSpec rsvpTlv = (RsvpErrorSpec) tlv;
76 final RsvpError rsvp = rsvpTlv.getRsvpError();
77 final ByteBuf body = Unpooled.buffer();
78 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
79 final Flags f = rsvp.getFlags();
80 if (f.isInPlace() != null) {
81 flags.set(IN_PLACE_FLAG_OFFSET, f.isInPlace());
83 if (f.isNotGuilty() != null) {
84 flags.set(NOT_GUILTY_FLAGS_OFFSET, f.isNotGuilty());
86 final IpAddress node = rsvp.getNode();
87 Preconditions.checkArgument(node != null, "Node is mandatory.");
88 if (node.getIpv4Address() != null) {
89 writeIpv4Address(node.getIpv4Address(), body);
91 writeIpv6Address(node.getIpv6Address(), body);
93 writeBitSet(flags, FLAGS_F_LENGTH, body);
94 Preconditions.checkArgument(rsvp.getCode() != null, "Code is mandatory.");
95 writeUnsignedByte(rsvp.getCode(), body);
96 Preconditions.checkArgument(rsvp.getValue() != null, "Value is mandatory.");
97 writeUnsignedShort(rsvp.getValue(), body);
98 TlvUtil.formatTlv(TYPE, body, buffer);