Reduce PathKey serialization duplication
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / rro / RROPathKey128SubobjectParser.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.rro;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
14 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
15 import org.opendaylight.protocol.rsvp.parser.spi.subobjects.PathKeyUtils;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
24 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
25
26 public class RROPathKey128SubobjectParser implements RROSubobjectParser {
27     public static final int TYPE = 65;
28
29     protected static final int PCE128_ID_F_LENGTH = 16;
30
31     private static final int PK_F_LENGTH = 2;
32     private static final int PK_F_OFFSET = 0;
33     private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
34     private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
35
36     public static void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
37         final PathKeyCase pkcase = (PathKeyCase) subobject.getSubobjectType();
38         RROSubobjectUtil.formatSubobject(TYPE, PathKeyUtils.serializePathKey(pkcase.getPathKey()), buffer);
39     }
40
41     @Override
42     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
43         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44         if (buffer.readableBytes() != CONTENT128_LENGTH) {
45             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; "
46                 + "Expected: >" + CONTENT128_LENGTH + ".");
47         }
48         return new SubobjectContainerBuilder()
49                 .setSubobjectType(new PathKeyCaseBuilder()
50                     .setPathKey(new PathKeyBuilder()
51                         .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
52                         .setPceId(new PceId(ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH)))
53                         .build())
54                     .build())
55                 .build();
56     }
57 }