BUG-612 : switched PCEP XRO subobject serializers to ByteBuf
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.subobject;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
27
28 /**
29  * Parser for {@link SrlgCase}
30  */
31 public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
32
33     public static final int TYPE = 34;
34
35     private static final int CONTENT_LENGTH = 5;
36
37     @Override
38     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
39         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
40         if (buffer.readableBytes() != CONTENT_LENGTH) {
41             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
42                     + CONTENT_LENGTH + ".");
43         }
44         final SubobjectBuilder builder = new SubobjectBuilder();
45         builder.setMandatory(mandatory);
46         builder.setAttribute(Attribute.Srlg);
47         builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer.readUnsignedInt())).build()).build());
48         return builder.build();
49     }
50
51     @Override
52     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
53         Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase, "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
54         final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
55         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
56         body.writeInt(specObj.getSrlgId().getValue().intValue());
57         body.writeByte(subobject.getAttribute().getIntValue());
58         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
59     }
60 }