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