2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.pcep.parser.subobject;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
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.rev181109.exclude.route.object.xro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgSubobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
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 Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
40 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
41 if (buffer.readableBytes() != CONTENT_LENGTH) {
42 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
43 + CONTENT_LENGTH + ".");
45 final SubobjectBuilder builder = new SubobjectBuilder();
46 builder.setMandatory(mandatory);
48 builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(buffer.readUnsignedInt())).build()).build());
50 builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
51 return builder.build();
55 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
56 Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase, "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
57 final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
58 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
59 Preconditions.checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
60 writeUnsignedInt(specObj.getSrlgId().getValue(), body);
61 Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
62 writeUnsignedByte(null, body);
63 writeUnsignedByte((short) subobject.getAttribute().getIntValue(), body);
64 XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);