Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / XROUnnumberedInterfaceSubobjectParser.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.writeUnsignedByte;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
20 import org.opendaylight.protocol.util.ByteBufUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.UnnumberedSubobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint8;
29
30 /**
31  * Parser for {@link UnnumberedCase}.
32  */
33 public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
34
35     public static final int TYPE = 4;
36
37     private static final int RESERVED = 1;
38
39     private static final int CONTENT_LENGTH = 10;
40
41     @Override
42     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) 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         buffer.readerIndex(buffer.readerIndex() + RESERVED);
49         final Attribute attr = Attribute.forValue(buffer.readUnsignedByte());
50         final UnnumberedBuilder ubuilder = new UnnumberedBuilder()
51                 .setRouterId(ByteBufUtils.readUint32(buffer))
52                 .setInterfaceId(ByteBufUtils.readUint32(buffer));
53         final SubobjectBuilder builder = new SubobjectBuilder()
54                 .setMandatory(mandatory)
55                 .setAttribute(attr)
56                 .setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
57         return builder.build();
58     }
59
60     @Override
61     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
62         checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase,
63             "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
64         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
65         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
66         body.writeZero(RESERVED);
67         writeUnsignedByte(
68                 subobject.getAttribute() != null ? Uint8.valueOf(subobject.getAttribute().getIntValue()) : (Uint8) null,
69                 body);
70         checkArgument(specObj.getRouterId() != null, "RouterId is mandatory.");
71         writeUnsignedInt(specObj.getRouterId(), body);
72         checkArgument(specObj.getInterfaceId() != null, "InterfaceId is mandatory.");
73         writeUnsignedInt(specObj.getInterfaceId(), body);
74         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
75     }
76 }