Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / EROUnnumberedInterfaceSubobjectParser.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.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.ByteBufUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.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.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
26
27 /**
28  * Parser for {@link UnnumberedCase}.
29  */
30 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
31
32     public static final int TYPE = 4;
33
34     private static final int RESERVED = 2;
35
36     private static final int CONTENT_LENGTH = 10;
37
38     @Override
39     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
40         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
41         if (buffer.readableBytes() != CONTENT_LENGTH) {
42             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
43                 + "; Expected: " + CONTENT_LENGTH + ".");
44         }
45         buffer.skipBytes(RESERVED);
46         final UnnumberedBuilder ubuilder = new UnnumberedBuilder()
47                 .setRouterId(ByteBufUtils.readUint32(buffer))
48                 .setInterfaceId(ByteBufUtils.readUint32(buffer));
49         final SubobjectBuilder builder = new SubobjectBuilder()
50                 .setLoose(loose)
51                 .setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
52         return builder.build();
53     }
54
55     @Override
56     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
57         checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase,
58             "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
59         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
60         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
61         body.writeZero(RESERVED);
62         checkArgument(specObj.getRouterId() != null, "RouterId is mandatory.");
63         writeUnsignedInt(specObj.getRouterId(), body);
64         checkArgument(specObj.getInterfaceId() != null, "InterfaceId is mandatory");
65         writeUnsignedInt(specObj.getInterfaceId(), body);
66         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
67     }
68 }