BUG-612 : switched PCEP XRO subobject serializers to ByteBuf
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROUnnumberedInterfaceSubobjectParser.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 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 org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
27
28 /**
29  * Parser for {@link UnnumberedCase}
30  */
31 public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
32
33     public static final int TYPE = 4;
34
35     private static final int RESERVED = 1;
36
37     private static final int CONTENT_LENGTH = 10;
38
39     @Override
40     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
41         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42         if (buffer.readableBytes() != CONTENT_LENGTH) {
43             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
44                     + CONTENT_LENGTH + ".");
45         }
46         buffer.readerIndex(buffer.readerIndex() + RESERVED);
47         final SubobjectBuilder builder = new SubobjectBuilder();
48         builder.setMandatory(mandatory);
49         builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
50         final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
51         ubuilder.setRouterId(buffer.readUnsignedInt());
52         ubuilder.setInterfaceId(buffer.readUnsignedInt());
53         builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
54         return builder.build();
55     }
56
57     @Override
58     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
59         Preconditions.checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
60         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
61         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
62         body.writeZero(RESERVED);
63         body.writeByte(subobject.getAttribute().getIntValue());
64         body.writeInt(specObj.getRouterId().intValue());
65         body.writeInt(specObj.getInterfaceId().intValue());
66         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
67     }
68 }