Merge "BUG-1287: migrate tests to new APIs"
[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 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;
15
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;
35
36 /**
37  * Parser for {@link RsvpErrorSpec}
38  */
39 public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
40
41     public static final int TYPE = 21;
42
43     private static final int FLAGS_F_LENGTH = 1;
44
45     private static final int IN_PLACE_FLAG_OFFSET = 7;
46     private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
47
48     private static final int V4_RSVP_LENGTH = 8;
49     private static final int V6_RSVP_LENGTH = 20;
50
51     @Override
52     public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
53         if (buffer == null) {
54             return null;
55         }
56         final RsvpErrorBuilder builder = new RsvpErrorBuilder();
57         if (buffer.readableBytes() == V4_RSVP_LENGTH) {
58             builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
59         } else if (buffer.readableBytes() == V6_RSVP_LENGTH) {
60             builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
61         }
62         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
63         builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
64         final short errorCode = (short) UnsignedBytes.toInt(buffer.readByte());
65         builder.setCode(errorCode);
66         final int errorValue = buffer.readUnsignedShort();
67         builder.setValue(errorValue);
68         return new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
69     }
70
71     @Override
72     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
73         Preconditions.checkArgument(tlv instanceof RsvpErrorSpec, "RSVPErrorSpecTlv is mandatory.");
74         final RsvpErrorSpec rsvpTlv = (RsvpErrorSpec) tlv;
75         final RsvpError rsvp = rsvpTlv.getRsvpError();
76         final ByteBuf body = Unpooled.buffer();
77         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
78         Flags f = rsvp.getFlags();
79         if (f.isInPlace() != null) {
80             flags.set(IN_PLACE_FLAG_OFFSET, f.isInPlace());
81         }
82         if (f.isNotGuilty() != null) {
83             flags.set(NOT_GUILTY_FLAGS_OFFSET, f.isNotGuilty());
84         }
85         final IpAddress node = rsvp.getNode();
86         Preconditions.checkArgument(node != null, "Node is mandatory.");
87         if (node.getIpv4Address() != null) {
88             writeIpv4Address(node.getIpv4Address(), body);
89         } else {
90             writeIpv6Address(node.getIpv6Address(), body);
91         }
92         writeBitSet(flags, FLAGS_F_LENGTH, body);
93         Preconditions.checkArgument(rsvp.getCode() != null, "Code is mandatory.");
94         writeUnsignedByte(rsvp.getCode(), body);
95         Preconditions.checkArgument(rsvp.getValue() != null, "Value is mandatory.");
96         writeUnsignedShort(rsvp.getValue(), body);
97         TlvUtil.formatTlv(TYPE, body, buffer);
98     }
99 }