Switch common parsers to utils
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / PathKeyUtils.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.spi.subobjects;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
18 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
19
20 public final class PathKeyUtils {
21     private PathKeyUtils() {
22         // Hidden on purpose
23     }
24
25     public static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
26         .subobjects.subobject.type.path.key._case.PathKey parsePathKey(final int pceIdFLength, final ByteBuf buffer) {
27         return new PathKeyBuilder()
28                 .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
29                 .setPceId(new PceId(ByteArray.readBytes(buffer, pceIdFLength)))
30                 .build();
31     }
32
33     public static ByteBuf serializePathKey(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
34         .rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKey pk) {
35         final ByteBuf body = Unpooled.buffer();
36         final PathKey pathKey = pk.getPathKey();
37         checkArgument(pathKey != null, "PathKey is mandatory.");
38         ByteBufUtils.write(body, pathKey.getValue());
39
40         final PceId pceId = pk.getPceId();
41         checkArgument(pceId != null, "PceId is mandatory.");
42         body.writeBytes(pceId.getValue());
43         return body;
44     }
45 }