57f8820d90a02c1e5a10b484404d075cf07e4406
[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
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.UnnumberedSubobject;
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
36     private static final int CONTENT_LENGTH = 10;
37
38     private static final int LPA_F_OFFSET = 7;
39     private static final int LPIU_F_OFFSET = 6;
40
41     @Override
42     public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
43         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
44         if (buffer.readableBytes() != CONTENT_LENGTH) {
45             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
46                 + "; Expected: " + CONTENT_LENGTH + ".");
47         }
48         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
49         buffer.skipBytes(RESERVED);
50         return new SubobjectBuilder()
51                 .setProtectionAvailable(flags.get(LPA_F_OFFSET))
52                 .setProtectionInUse(flags.get(LPIU_F_OFFSET))
53                 .setSubobjectType(new UnnumberedCaseBuilder()
54                     .setUnnumbered(new UnnumberedBuilder()
55                         .setRouterId(ByteBufUtils.readUint32(buffer))
56                         .setInterfaceId(ByteBufUtils.readUint32(buffer))
57                         .build())
58                     .build())
59                 .build();
60     }
61
62     @Override
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         ByteBufUtils.writeMandatory(body, specObj.getRouterId(), "RouterId");
74         ByteBufUtils.writeMandatory(body, specObj.getInterfaceId(), "InterfaceId");
75         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
76     }
77 }