Upgrade ietf-{inet,yang}-types to 2013-07-15
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
25
26 public class RROPathKey128SubobjectParser implements RROSubobjectParser {
27
28     public static final int TYPE = 65;
29
30     private static final int PK_F_LENGTH = 2;
31
32     protected static final int PCE128_ID_F_LENGTH = 16;
33
34     private static final int PK_F_OFFSET = 0;
35     private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
36
37     private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
38
39     @Override
40     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
41         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42         if (buffer.readableBytes() != CONTENT128_LENGTH) {
43             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
44                 + CONTENT128_LENGTH + ".");
45         }
46         final int pathKey = buffer.readUnsignedShort();
47         final byte[] pceId = ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH);
48         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
49         final PathKeyBuilder pBuilder = new PathKeyBuilder();
50         pBuilder.setPceId(new PceId(pceId));
51         pBuilder.setPathKey(new PathKey(pathKey));
52         builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
53         return builder.build();
54     }
55
56     public static void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
57         final PathKeyCase pkcase = (PathKeyCase) subobject.getSubobjectType();
58         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects
59             .subobject.type.path.key._case.PathKey pk = pkcase.getPathKey();
60         final ByteBuf body = Unpooled.buffer();
61         Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
62         writeUnsignedShort(pk.getPathKey().getValue(), body);
63         Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
64         body.writeBytes(pk.getPceId().getBinary());
65         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
66     }
67 }