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.writeUnsignedInt;
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.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.protocol.util.ByteBufUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.UnnumberedSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.UnnumberedCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
29 * Parser for {@link UnnumberedCase}.
31 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
33 public static final int TYPE = 4;
35 private static final int FLAGS_SIZE = 8;
36 private static final int RESERVED = 1;
38 private static final int CONTENT_LENGTH = 10;
40 private static final int LPA_F_OFFSET = 7;
41 private static final int LPIU_F_OFFSET = 6;
44 public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
45 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
46 if (buffer.readableBytes() != CONTENT_LENGTH) {
47 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
48 + "; Expected: " + CONTENT_LENGTH + ".");
50 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
51 buffer.skipBytes(RESERVED);
52 final UnnumberedBuilder ubuilder = new UnnumberedBuilder()
53 .setRouterId(ByteBufUtils.readUint32(buffer))
54 .setInterfaceId(ByteBufUtils.readUint32(buffer));
55 final SubobjectBuilder builder = new SubobjectBuilder()
56 .setProtectionAvailable(flags.get(LPA_F_OFFSET))
57 .setProtectionInUse(flags.get(LPIU_F_OFFSET))
58 .setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
59 return builder.build();
63 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
64 checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase,
65 "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
66 final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
67 final BitArray flags = new BitArray(FLAGS_SIZE);
68 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
69 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
70 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
71 flags.toByteBuf(body);
72 body.writeZero(RESERVED);
73 checkArgument(specObj.getRouterId() != null, "RouterId is mandatory.");
74 writeUnsignedInt(specObj.getRouterId(), body);
75 checkArgument(specObj.getInterfaceId() != null, "InterfaceId is mandatory.");
76 writeUnsignedInt(specObj.getInterfaceId(), body);
77 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);