BUG 2230: RSVP RRO Subobjects
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / rro / RROPathKey32SubobjectParser.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 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
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.pcep.types.rev131005.PathKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.path.key._case.PathKeyBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
25
26 public class RROPathKey32SubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
27
28     public static final int TYPE = 64;
29
30     private static final int PK_F_LENGTH = 2;
31     private static final int PCE_ID_F_LENGTH = 4;
32
33     private static final int PK_F_OFFSET = 0;
34     private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
35
36     private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
37
38     @Override
39     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
40         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
41         if (buffer.readableBytes() != CONTENT_LENGTH) {
42             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
43                 + CONTENT_LENGTH + ".");
44         }
45         final int pathKey = buffer.readUnsignedShort();
46         final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH);
47         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
48         final PathKeyBuilder pBuilder = new PathKeyBuilder();
49         pBuilder.setPceId(new PceId(pceId));
50         pBuilder.setPathKey(new PathKey(pathKey));
51         builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
52         return builder.build();
53     }
54
55     @Override
56     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
57         Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
58         final PathKeyCase pkcase = (PathKeyCase) subobject.getSubobjectType();
59         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.path.key._case.PathKey pk = pkcase.getPathKey();
60         final ByteBuf body = Unpooled.buffer();
61         Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
62         if(pk.getPceId().getBinary().length == 16) {
63             RROPathKey128SubobjectParser.serializeSubobject(subobject,buffer);
64         }
65         Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
66         Preconditions.checkArgument(pk.getPceId().getBinary().length == 4, "PathKey 32Bit is mandatory.");
67         writeUnsignedShort(pk.getPathKey().getValue(), body);
68         body.writeBytes(pk.getPceId().getBinary());
69         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
70     }
71 }