Bump MRI upstreams
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / rro / RROUnnumberedInterfaceSubobjectParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.subobject.rro;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
16 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
17 import org.opendaylight.protocol.util.BitArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.UnnumberedSubobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.SubobjectType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.list.SubobjectContainer;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.list.SubobjectContainerBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.UnnumberedCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
25 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26
27 /**
28  * Parser for {@link UnnumberedCase}.
29  */
30 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
31     public static final int TYPE = 4;
32
33     private static final int FLAGS_SIZE = 8;
34     private static final int RESERVED = 1;
35     private static final int CONTENT_LENGTH = 10;
36     private static final int LPA_F_OFFSET = 7;
37     private static final int LPIU_F_OFFSET = 6;
38
39     @Override
40     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
41         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
42         if (buffer.readableBytes() != CONTENT_LENGTH) {
43             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; "
44                 + "Expected: " + CONTENT_LENGTH + ".");
45         }
46         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
47         buffer.skipBytes(RESERVED);
48         return new SubobjectContainerBuilder()
49                 .setProtectionAvailable(flags.get(LPA_F_OFFSET))
50                 .setProtectionInUse(flags.get(LPIU_F_OFFSET))
51                 .setSubobjectType(new UnnumberedCaseBuilder()
52                     .setUnnumbered(new UnnumberedBuilder()
53                         .setRouterId(ByteBufUtils.readUint32(buffer))
54                         .setInterfaceId(ByteBufUtils.readUint32(buffer))
55                         .build())
56                     .build())
57                 .build();
58     }
59
60     @Override
61     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
62         final SubobjectType type = subobject.getSubobjectType();
63         checkArgument(type instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.",
64             type.getClass());
65         final BitArray flags = new BitArray(FLAGS_SIZE);
66         flags.set(LPA_F_OFFSET, subobject.getProtectionAvailable());
67         flags.set(LPIU_F_OFFSET, subobject.getProtectionInUse());
68         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
69         flags.toByteBuf(body);
70         body.writeZero(RESERVED);
71
72         final UnnumberedSubobject specObj = ((UnnumberedCase) type).getUnnumbered();
73         ByteBufUtils.writeMandatory(body, specObj.getRouterId(), "RouterId");
74         ByteBufUtils.writeMandatory(body, specObj.getInterfaceId(), "InterfaceId");
75         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
76     }
77 }