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.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
16 import java.util.BitSet;
18 import org.opendaylight.protocol.concepts.Ipv4Util;
19 import org.opendaylight.protocol.concepts.Ipv6Util;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.TlvParser;
22 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
23 import org.opendaylight.protocol.pcep.spi.TlvUtil;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
28 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;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags;
34 * Parser for {@link RsvpErrorSpec}
36 public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
38 public static final int TYPE = 21;
40 private static final int FLAGS_F_LENGTH = 1;
42 private static final int IN_PLACE_FLAG_OFFSET = 7;
43 private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
45 private static final int V4_RSVP_LENGTH = 8;
46 private static final int V6_RSVP_LENGTH = 20;
49 public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
53 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
54 if (buffer.readableBytes() == V4_RSVP_LENGTH) {
55 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
56 } else if (buffer.readableBytes() == V6_RSVP_LENGTH) {
57 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
59 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
60 builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
61 final short errorCode = (short) UnsignedBytes.toInt(buffer.readByte());
62 builder.setCode(errorCode);
63 final int errorValue = buffer.readUnsignedShort();
64 builder.setValue(errorValue);
65 return new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
69 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
70 Preconditions.checkArgument(tlv != null, "RSVPErrorSpecTlv is mandatory.");
71 final RsvpErrorSpec rsvpTlv = (RsvpErrorSpec) tlv;
72 final RsvpError rsvp = rsvpTlv.getRsvpError();
73 final ByteBuf body = Unpooled.buffer();
74 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
75 Flags f = rsvp.getFlags();
76 if (f.isInPlace() != null) {
77 flags.set(IN_PLACE_FLAG_OFFSET, f.isInPlace());
79 if (f.isNotGuilty() != null) {
80 flags.set(NOT_GUILTY_FLAGS_OFFSET, f.isNotGuilty());
82 final IpAddress node = rsvp.getNode();
83 if (node.getIpv4Address() != null) {
84 body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
86 body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
88 body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
89 body.writeByte(rsvp.getCode());
90 body.writeShort(rsvp.getValue().shortValue());
91 TlvUtil.formatTlv(TYPE, body, buffer);