Further removal of unused imports
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / XROSrlgSubobjectParser.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;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgSubobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30
31 /**
32  * Parser for {@link SrlgCase}.
33  */
34 public class XROSrlgSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
35
36     public static final int TYPE = 34;
37
38     private static final int CONTENT_LENGTH = 6;
39
40     @Override
41     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
42         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
43         if (buffer.readableBytes() != CONTENT_LENGTH) {
44             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
45                 + "; Expected: " + CONTENT_LENGTH + ".");
46         }
47         final SubobjectBuilder builder = new SubobjectBuilder()
48                 .setMandatory(mandatory)
49                 .setSubobjectType(new SrlgCaseBuilder()
50                     .setSrlg(new SrlgBuilder()
51                         .setSrlgId(new SrlgId(ByteBufUtils.readUint32(buffer)))
52                         .build())
53                     .build());
54         buffer.readByte();
55         return builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()))
56                 .build();
57     }
58
59     @Override
60     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
61         checkArgument(subobject.getSubobjectType() instanceof SrlgCase,
62             "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
63         final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
64         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
65         checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
66         writeUnsignedInt(specObj.getSrlgId().getValue(), body);
67         checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
68         writeUnsignedByte((Uint8) null, body);
69         writeUnsignedByte(Uint8.valueOf(subobject.getAttribute().getIntValue()), body);
70         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
71     }
72 }