Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / RROPathKey128SubobjectParser.java
1 /*
2  * Copyright (c) 2013 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.pcep.parser.subobject;
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.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
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 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26
27 public class RROPathKey128SubobjectParser implements RROSubobjectParser {
28
29     public static final int TYPE = 65;
30
31     protected static final int PCE128_ID_F_LENGTH = 16;
32
33     private static final int PK_F_OFFSET = 0;
34     private static final int PK_F_LENGTH = 2;
35     private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
36     private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
37
38     @Override
39     public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
40         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
41         if (buffer.readableBytes() != CONTENT128_LENGTH) {
42             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
43                     + "; Expected: >" + CONTENT128_LENGTH + ".");
44         }
45         return new SubobjectBuilder()
46                 .setSubobjectType(new PathKeyCaseBuilder()
47                     .setPathKey(new PathKeyBuilder()
48                         .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
49                         .setPceId(new PceId(ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH)))
50                         .build())
51                     .build())
52                 .build();
53     }
54
55     public static void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
56         final PathKeyCase pkcase = (PathKeyCase) subobject.getSubobjectType();
57         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects
58             .subobject.type.path.key._case.PathKey pk = pkcase.getPathKey();
59         final ByteBuf body = Unpooled.buffer();
60         final PathKey pathKey = pk.getPathKey();
61         checkArgument(pathKey != null, "PathKey is mandatory.");
62         ByteBufUtils.write(body, pathKey.getValue());
63         final PceId pceId = pk.getPceId();
64         checkArgument(pceId != null, "PceId is mandatory.");
65         body.writeBytes(pceId.getValue());
66         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
67     }
68 }