9cf96b08e79f33ddaccb8824c06bdf29c3f3f14a
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / CommonPathKeyParser.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
9 package org.opendaylight.protocol.rsvp.parser.spi.subobjects;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
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.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.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
20
21 public class CommonPathKeyParser {
22     public static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
23         .subobjects.subobject.type.path.key._case.PathKey parsePathKey(final int pceIdFLength, final ByteBuf buffer) {
24         final int pathKey = buffer.readUnsignedShort();
25         final byte[] pceId = ByteArray.readBytes(buffer, pceIdFLength);
26         final PathKeyBuilder pBuilder = new PathKeyBuilder();
27         pBuilder.setPceId(new PceId(pceId));
28         pBuilder.setPathKey(new PathKey(pathKey));
29         return pBuilder.build();
30     }
31
32     public static ByteBuf serializePathKey(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
33         .rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKey pk) {
34         final ByteBuf body = Unpooled.buffer();
35         Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
36         writeUnsignedShort(pk.getPathKey().getValue(), body);
37         Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
38         body.writeBytes(pk.getPceId().getBinary());
39         return body;
40     }
41 }