BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROUnnumberedInterfaceSubobjectParser.java
1 /*
2  * Copyright (c) 2013 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.impl.subobject;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
26
27 /**
28  * Parser for {@link UnnumberedCase}
29  */
30 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
31
32     public static final int TYPE = 4;
33
34     private static final int FLAGS_SIZE = 8;
35     private static final int RESERVED = 1;
36
37     private static final int CONTENT_LENGTH = 10;
38
39     private static final int LPA_F_OFFSET = 7;
40     private static final int LPIU_F_OFFSET = 6;
41
42     @Override
43     public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
44         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
45         if (buffer.readableBytes() != CONTENT_LENGTH) {
46             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
47                     + CONTENT_LENGTH + ".");
48         }
49         final SubobjectBuilder builder = new SubobjectBuilder();
50         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
51         builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
52         builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
53         final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
54         buffer.skipBytes(RESERVED);
55         ubuilder.setRouterId(buffer.readUnsignedInt());
56         ubuilder.setInterfaceId(buffer.readUnsignedInt());
57         builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
58         return builder.build();
59     }
60
61     @Override
62     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
63         Preconditions.checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
64         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
65         final BitArray flags = new BitArray(FLAGS_SIZE);
66         flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
67         flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
68         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
69         flags.toByteBuf(body);
70         body.writeZero(RESERVED);
71         Preconditions.checkArgument(specObj.getRouterId() != null, "RouterId is mandatory.");
72         writeUnsignedInt(specObj.getRouterId(), body);
73         Preconditions.checkArgument(specObj.getInterfaceId() != null, "InterfaceId is mandatory.");
74         writeUnsignedInt(specObj.getInterfaceId(), body);
75         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
76     }
77 }