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 com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
29 import org.opendaylight.yangtools.yang.common.Uint16;
31 public class RROPathKey32SubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
33 public static final int TYPE = 64;
35 private static final int PK_F_LENGTH = 2;
36 private static final int PCE_ID_F_LENGTH = 4;
38 private static final int PK_F_OFFSET = 0;
39 private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
41 private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
44 public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
45 checkArgument(buffer != null && buffer.isReadable(),
46 "Array of bytes is mandatory. Cannot be null or empty.");
47 if (buffer.readableBytes() != CONTENT_LENGTH) {
48 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
49 + "; Expected: >" + CONTENT_LENGTH + ".");
51 final Uint16 pathKey = ByteBufUtils.readUint16(buffer);
52 final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH);
53 final SubobjectBuilder builder = new SubobjectBuilder();
54 final PathKeyBuilder pBuilder = new PathKeyBuilder();
55 pBuilder.setPceId(new PceId(pceId));
56 pBuilder.setPathKey(new PathKey(pathKey));
57 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
58 return builder.build();
62 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
63 checkArgument(subobject.getSubobjectType() instanceof PathKeyCase,
64 "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
65 final PathKeyCase pkcase = (PathKeyCase) subobject.getSubobjectType();
66 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects
67 .subobject.type.path.key._case.PathKey pk = pkcase.getPathKey();
68 final ByteBuf body = Unpooled.buffer();
69 checkArgument(pk.getPceId() != null, "PceId is mandatory.");
71 final byte[] pceId = pk.getPceId().getValue();
72 if (pceId.length == RROPathKey128SubobjectParser.PCE128_ID_F_LENGTH) {
73 RROPathKey128SubobjectParser.serializeSubobject(subobject,buffer);
75 checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
76 writeUnsignedShort(pk.getPathKey().getValue(), body);
77 Preconditions.checkArgument(pceId.length == PCE_ID_F_LENGTH, "PceId 32 Bit required.");
78 body.writeBytes(pceId);
79 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);