Reduce noise from BGPSessionImpl
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / tlv / PathSetupTypeTlvParser.java
1 /*
2  * Copyright (c) 2014 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.tlv;
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 java.util.HashSet;
15 import java.util.Set;
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;
25
26 public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
27     public static final int TYPE = 28;
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.";
34
35     private static final Set<Uint8> PSTS = new HashSet<>();
36
37     public PathSetupTypeTlvParser() {
38         PSTS.add(Uint8.valueOf(RSVP_TE_PST));
39     }
40
41     public PathSetupTypeTlvParser(final short srTePst) {
42         this();
43         PSTS.add(Uint8.valueOf(srTePst));
44     }
45
46     @Override
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);
55     }
56
57     @Override
58     public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
59         if (buffer == null) {
60             return null;
61         }
62         final Uint8 pst = ByteBufUtils.readUint8(buffer.readerIndex(OFFSET));
63         if (!checkPST(pst)) {
64             throw new PCEPDeserializerException(UNSUPPORTED_PST);
65         }
66         return new PathSetupTypeBuilder().setPst(pst).build();
67     }
68
69     private static boolean checkPST(final Uint8 pst) {
70         return pst != null && PSTS.contains(pst);
71     }
72 }