2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.subobject;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.ByteBufUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint16;
31 * Parser for {@link PathKey}.
33 public class XROPathKey32SubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
35 public static final int TYPE = 64;
37 private static final int PCE_ID_F_LENGTH = 4;
39 private static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
42 public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
43 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
44 if (buffer.readableBytes() != CONTENT_LENGTH) {
45 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
46 + "; Expected: >" + CONTENT_LENGTH + ".");
48 final Uint16 pathKey = ByteBufUtils.readUint16(buffer);
49 final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH);
50 final PathKeyBuilder pBuilder = new PathKeyBuilder()
51 .setPceId(new PceId(pceId))
52 .setPathKey(new PathKey(pathKey));
53 final SubobjectBuilder builder = new SubobjectBuilder()
54 .setMandatory(mandatory)
55 .setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
56 return builder.build();
60 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
61 checkArgument(subobject.getSubobjectType() instanceof PathKeyCase,
62 "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
63 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
64 .subobjects.subobject.type.path.key._case.PathKey pk =
65 ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
66 final ByteBuf body = Unpooled.buffer();
67 checkArgument(pk.getPceId() != null, "PceId is mandatory.");
69 final byte[] pceId = pk.getPceId().getValue();
70 if (pceId.length == XROPathKey128SubobjectParser.PCE128_ID_F_LENGTH) {
71 XROPathKey128SubobjectParser.serializeSubobject(subobject, buffer);
73 checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
74 writeUnsignedShort(pk.getPathKey().getValue(), body);
75 checkArgument(pceId.length == PCE_ID_F_LENGTH, "PceId 32 Bit required.");
76 body.writeBytes(pceId);
77 XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);