01f290f97b8986d660509a9473268600bd0bfe8c
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / XROUnnumberedInterfaceSubobjectParser.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
13 import com.google.common.base.Preconditions;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.UnnumberedSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
27
28 /**
29  * Parser for {@link UnnumberedCase}
30  */
31 public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
32
33     public static final int TYPE = 4;
34
35     private static final int RESERVED = 1;
36
37     private static final int CONTENT_LENGTH = 10;
38
39     @Override
40     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
41         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42         if (buffer.readableBytes() != CONTENT_LENGTH) {
43             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
44                     + CONTENT_LENGTH + ".");
45         }
46         buffer.readerIndex(buffer.readerIndex() + RESERVED);
47         final SubobjectBuilder builder = new SubobjectBuilder();
48         builder.setMandatory(mandatory);
49         builder.setAttribute(Attribute.forValue(buffer.readUnsignedByte()));
50         final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
51         ubuilder.setRouterId(buffer.readUnsignedInt());
52         ubuilder.setInterfaceId(buffer.readUnsignedInt());
53         builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
54         return builder.build();
55     }
56
57     @Override
58     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
59         Preconditions.checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
60         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
61         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
62         body.writeZero(RESERVED);
63         writeUnsignedByte(subobject.getAttribute() != null ? (short) subobject.getAttribute().getIntValue() : null, body);
64         Preconditions.checkArgument(specObj.getRouterId() != null, "RouterId is mandatory.");
65         writeUnsignedInt(specObj.getRouterId(), body);
66         Preconditions.checkArgument(specObj.getInterfaceId() != null, "InterfaceId is mandatory.");
67         writeUnsignedInt(specObj.getInterfaceId(), body);
68         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
69     }
70 }