2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.rsvp.parser.impl.subobject.xro;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
14 import com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
18 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
19 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgSubobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
30 * Parser for {@link SrlgCase}.
32 public class XROSrlgSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
34 public static final int TYPE = 34;
36 private static final int CONTENT_LENGTH = 6;
39 public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory) throws
40 RSVPParsingException {
41 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
42 "Array of bytes is mandatory. Can't be null or empty.");
43 if (buffer.readableBytes() != CONTENT_LENGTH) {
44 throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
45 + "; " + "Expected: " + CONTENT_LENGTH + ".");
47 final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
48 builder.setMandatory(mandatory);
49 builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer
50 .readUnsignedInt())).build()).build());
52 builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
53 return builder.build();
57 public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
58 Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase,
59 "Unknown subobject instance. Passed %s. Needed SrlgCase.",
60 subobject.getSubobjectType().getClass());
61 final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
62 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
63 Preconditions.checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
64 writeUnsignedInt(specObj.getSrlgId().getValue(), body);
65 Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
66 writeUnsignedByte(null, body);
67 writeUnsignedByte((short) subobject.getAttribute().getIntValue(), body);
68 XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);