2 * Copyright (c) 2014 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.pcep.parser.tlv;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Sets;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvParser;
20 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
21 import org.opendaylight.protocol.pcep.spi.TlvUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupTypeBuilder;
26 public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
28 public static final int TYPE = 28;
30 private static final int CONTENT_LENGTH = 4;
31 private static final int PST_LENGTH = 1;
32 private static final int OFFSET = CONTENT_LENGTH - PST_LENGTH;
33 private static final short RSVP_TE_PST = 0;
34 private static final String UNSUPPORTED_PST = "Unsupported path setup type.";
36 protected static final Set<Short> PSTS = Sets.newHashSet();
38 public PathSetupTypeTlvParser() {
39 PSTS.add(RSVP_TE_PST);
43 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
44 Preconditions.checkArgument(tlv instanceof PathSetupType, "PathSetupType is mandatory.");
45 final PathSetupType pstTlv = (PathSetupType) tlv;
46 Preconditions.checkArgument(checkPST(pstTlv.getPst()), UNSUPPORTED_PST);
47 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
48 body.writeZero(OFFSET);
49 writeUnsignedByte(pstTlv.getPst(), body);
50 TlvUtil.formatTlv(TYPE, body, buffer);
54 public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
58 final short pst = buffer.readerIndex(OFFSET).readUnsignedByte();
60 throw new PCEPDeserializerException(UNSUPPORTED_PST);
62 return new PathSetupTypeBuilder().setPst(pst).build();
65 private boolean checkPST(final Short pst) {
67 return PSTS.contains(pst);