Switch common parsers to utils
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / xro / XROUnnumberedInterfaceSubobjectParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.subobject.xro;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.rsvp.parser.spi.subobjects.UnnumberedInterfaceSubobjectUtils.parseUnnumeredInterface;
12 import static org.opendaylight.protocol.rsvp.parser.spi.subobjects.UnnumberedInterfaceSubobjectUtils.serializeUnnumeredInterface;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
17 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
18 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
24
25 /**
26  * Parser for {@link UnnumberedCase}.
27  */
28 public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
29     public static final int TYPE = 4;
30
31     private static final int RESERVED = 1;
32     private static final int CONTENT_LENGTH = 10;
33
34     @Override
35     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory) throws
36         RSVPParsingException {
37         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
38         if (buffer.readableBytes() != CONTENT_LENGTH) {
39             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
40                 + "; " + "Expected: " + CONTENT_LENGTH + ".");
41         }
42         buffer.readerIndex(buffer.readerIndex() + RESERVED);
43         return new SubobjectContainerBuilder()
44                 .setMandatory(mandatory)
45                 .setAttribute(Attribute.forValue(buffer.readUnsignedByte()))
46                 .setSubobjectType(parseUnnumeredInterface(buffer))
47                 .build();
48     }
49
50     @Override
51     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
52         final SubobjectType type = subobject.getSubobjectType();
53         checkArgument(type instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.",
54             type.getClass());
55         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
56         body.writeZero(RESERVED);
57
58         final Attribute attribute = subobject.getAttribute();
59         body.writeByte(attribute != null ? attribute.getIntValue() : 0);
60         serializeUnnumeredInterface(((UnnumberedCase) type).getUnnumbered(), body);
61         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
62     }
63 }