2af20a1e7186833866fdc5c1bd39bcc867befc1e
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / RROUnnumberedInterfaceSubobjectParser.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 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12
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;
27
28 /**
29  * Parser for {@link UnnumberedCase}.
30  */
31 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
32     public static final int TYPE = 4;
33
34     private static final int FLAGS_SIZE = 8;
35     private static final int RESERVED = 1;
36
37     private static final int CONTENT_LENGTH = 10;
38
39     private static final int LPA_F_OFFSET = 7;
40     private static final int LPIU_F_OFFSET = 6;
41
42     @Override
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 + ".");
48         }
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();
59     }
60
61     @Override
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);
77     }
78 }