Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / XROPathKey32SubobjectParser.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.XROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
26 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
27
28 /**
29  * Parser for {@link PathKey}.
30  */
31 public class XROPathKey32SubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
32     public static final int TYPE = 64;
33
34     private static final int PCE_ID_F_LENGTH = 4;
35     private static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
36
37     @Override
38     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
39         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
40         if (buffer.readableBytes() != CONTENT_LENGTH) {
41             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
42             + "; Expected: >" + CONTENT_LENGTH + ".");
43         }
44         return new SubobjectBuilder()
45                 .setMandatory(mandatory)
46                 .setSubobjectType(new PathKeyCaseBuilder()
47                     .setPathKey(new PathKeyBuilder()
48                         .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
49                         .setPceId(new PceId(ByteArray.readBytes(buffer, PCE_ID_F_LENGTH)))
50                         .build())
51                     .build())
52                 .build();
53     }
54
55     @Override
56     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
57         checkArgument(subobject.getSubobjectType() instanceof PathKeyCase,
58             "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
59         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
60             .subobjects.subobject.type.path.key._case.PathKey pk =
61                 ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
62         final ByteBuf body = Unpooled.buffer();
63         checkArgument(pk.getPceId() != null, "PceId is mandatory.");
64
65         final byte[] pceId = pk.getPceId().getValue();
66         if (pceId.length == XROPathKey128SubobjectParser.PCE128_ID_F_LENGTH) {
67             XROPathKey128SubobjectParser.serializeSubobject(subobject, buffer);
68         }
69
70         final PathKey pathKey = pk.getPathKey();
71         checkArgument(pathKey != null, "PathKey is mandatory.");
72         ByteBufUtils.write(body, pathKey.getValue());
73         checkArgument(pceId.length == PCE_ID_F_LENGTH, "PceId 32 Bit required.");
74         body.writeBytes(pceId);
75         XROSubobjectUtil.formatSubobject(TYPE, subobject.getMandatory(), body, buffer);
76     }
77 }