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
8 package org.opendaylight.protocol.pcep.parser.tlv;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.HashSet;
16 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.TlvParser;
18 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
19 import org.opendaylight.protocol.pcep.spi.TlvUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.setup.type.tlv.PathSetupType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.setup.type.tlv.PathSetupTypeBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint8;
24 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26 public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
27 public static final int TYPE = 28;
29 private static final int CONTENT_LENGTH = 4;
30 private static final int PST_LENGTH = 1;
31 private static final int OFFSET = CONTENT_LENGTH - PST_LENGTH;
32 private static final short RSVP_TE_PST = 0;
33 private static final String UNSUPPORTED_PST = "Unsupported path setup type.";
35 private static final Set<Uint8> PSTS = new HashSet<>();
37 public PathSetupTypeTlvParser() {
38 PSTS.add(Uint8.valueOf(RSVP_TE_PST));
41 public PathSetupTypeTlvParser(final short srTePst) {
43 PSTS.add(Uint8.valueOf(srTePst));
47 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
48 checkArgument(tlv instanceof PathSetupType, "PathSetupType is mandatory.");
49 final PathSetupType pstTlv = (PathSetupType) tlv;
50 checkArgument(checkPST(pstTlv.getPst()), UNSUPPORTED_PST);
51 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
52 body.writeZero(OFFSET);
53 ByteBufUtils.writeOrZero(body, pstTlv.getPst());
54 TlvUtil.formatTlv(TYPE, body, buffer);
58 public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
62 final Uint8 pst = ByteBufUtils.readUint8(buffer.readerIndex(OFFSET));
64 throw new PCEPDeserializerException(UNSUPPORTED_PST);
66 return new PathSetupTypeBuilder().setPst(pst).build();
69 private static boolean checkPST(final Uint8 pst) {
70 return pst != null && PSTS.contains(pst);