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