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