BUG-612 : switched RRO subobjects to ByteBuf.
[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 io.netty.buffer.ByteBuf;
11
12 import java.util.BitSet;
13
14 import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
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.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
25
26 import com.google.common.base.Preconditions;
27
28 /**
29  * Parser for {@link UnnumberedCase}
30  */
31 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
32
33         public static final int TYPE = 4;
34
35         private static final int FLAGS_F_LENGTH = 1;
36         private static final int ROUTER_ID_NUMBER_LENGTH = 4;
37         private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
38
39         private static final int ROUTER_ID_NUMBER_OFFSET = 2;
40         private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
41
42         private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
43
44         private static final int LPA_F_OFFSET = 7;
45         private static final int LPIU_F_OFFSET = 6;
46
47         @Override
48         public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
49                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
50                 if (buffer.readableBytes() != CONTENT_LENGTH) {
51                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
52                                         + CONTENT_LENGTH + ".");
53                 }
54                 final SubobjectBuilder builder = new SubobjectBuilder();
55                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
56                 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
57                 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
58                 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
59                 buffer.readerIndex(buffer.readerIndex() + 1);
60                 ubuilder.setRouterId(buffer.readUnsignedInt());
61                 ubuilder.setInterfaceId(buffer.readUnsignedInt());
62                 builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
63                 return builder.build();
64         }
65
66         @Override
67         public byte[] serializeSubobject(final Subobject subobject) {
68                 if (!(subobject.getSubobjectType() instanceof UnnumberedCase)) {
69                         throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
70                                         + ". Needed UnnumberedCase.");
71                 }
72                 final byte[] retBytes = new byte[CONTENT_LENGTH];
73                 final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
74                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
75                 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
76                 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
77                 retBytes[0] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
78                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
79                 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
80                                 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
81                 return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
82         }
83 }