Switch common parsers to utils
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / ero / EROPathKey128SubobjectParser.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 com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.rsvp.parser.spi.subobjects.PathKeyUtils.parsePathKey;
12
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
19
20 /**
21  * Parser for { PathKey }.
22  */
23 public class EROPathKey128SubobjectParser  implements EROSubobjectParser {
24     public static final int TYPE = 65;
25
26     protected static final int PCE128_ID_F_LENGTH = 16;
27
28     private static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
29
30     @Override
31     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException {
32         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
33         if (buffer.readableBytes() != CONTENT128_LENGTH) {
34             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; "
35                 + "Expected: >" + CONTENT128_LENGTH + ".");
36         }
37         return new SubobjectContainerBuilder()
38                 .setLoose(loose)
39                 .setSubobjectType(new PathKeyCaseBuilder().setPathKey(parsePathKey(PCE128_ID_F_LENGTH, buffer)).build())
40                 .build();
41     }
42 }