Move pcep base parser Activator to its own bundle
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
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.rev150820.ExcludeRouteSubobjects;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.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 = 6;
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
47         builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer.readUnsignedInt())).build()).build());
48         buffer.readByte();
49         builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
50         return builder.build();
51     }
52
53     @Override
54     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
55         Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase, "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
56         final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
57         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
58         Preconditions.checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
59         writeUnsignedInt(specObj.getSrlgId().getValue(), body);
60         Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
61         writeUnsignedByte(null, body);
62         writeUnsignedByte((short) subobject.getAttribute().getIntValue(), body);
63         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
64     }
65 }