enforce check-style for rsvp impl
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / xro / XROSrlgSubobjectParser.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
9 package org.opendaylight.protocol.rsvp.parser.impl.subobject.xro;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
13
14 import com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
18 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
19 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgSubobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
28
29 /**
30  * Parser for {@link SrlgCase}.
31  */
32 public class XROSrlgSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
33
34     public static final int TYPE = 34;
35
36     private static final int CONTENT_LENGTH = 6;
37
38     @Override
39     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory) throws
40         RSVPParsingException {
41         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
42             "Array of bytes is mandatory. Can't be null or empty.");
43         if (buffer.readableBytes() != CONTENT_LENGTH) {
44             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
45                 + "; " + "Expected: " + CONTENT_LENGTH + ".");
46         }
47         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
48         builder.setMandatory(mandatory);
49         builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer
50             .readUnsignedInt())).build()).build());
51         buffer.readByte();
52         builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
53         return builder.build();
54     }
55
56     @Override
57     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
58         Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase,
59             "Unknown subobject instance. Passed %s. Needed SrlgCase.",
60             subobject.getSubobjectType().getClass());
61         final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
62         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
63         Preconditions.checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
64         writeUnsignedInt(specObj.getSrlgId().getValue(), body);
65         Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
66         writeUnsignedByte(null, body);
67         writeUnsignedByte((short) subobject.getAttribute().getIntValue(), body);
68         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
69     }
70 }