b5ad0e30d2a8bf5e79c55aadc2bf6a4caddc1578
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02RSVPErrorSpecTlvParser.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.ietf.stateful02;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15
16 import java.util.BitSet;
17
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;
32
33 /**
34  * Parser for {@link RsvpErrorSpec}
35  */
36 public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
37
38     public static final int TYPE = 21;
39
40     private static final int FLAGS_F_LENGTH = 1;
41
42     private static final int IN_PLACE_FLAG_OFFSET = 7;
43     private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
44
45     private static final int V4_RSVP_LENGTH = 8;
46     private static final int V6_RSVP_LENGTH = 20;
47
48     @Override
49     public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
50         if (buffer == null) {
51             return null;
52         }
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))));
58         }
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();
66     }
67
68     @Override
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());
78         }
79         if (f.isNotGuilty() != null) {
80             flags.set(NOT_GUILTY_FLAGS_OFFSET, f.isNotGuilty());
81         }
82         final IpAddress node = rsvp.getNode();
83         if (node.getIpv4Address() != null) {
84             body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
85         } else {
86             body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
87         }
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);
92     }
93 }