Enable restart of CSS modules on blueprint container restart
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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
9 package org.opendaylight.protocol.pcep.impl.tlv;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
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;
17 import java.util.Set;
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;
25
26 public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
27
28     public static final int TYPE = 28;
29
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.";
35
36     protected static final Set<Short> PSTS = Sets.newHashSet();
37
38     public PathSetupTypeTlvParser() {
39         PSTS.add(RSVP_TE_PST);
40     }
41
42     @Override
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);
51     }
52
53     @Override
54     public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
55         if (buffer == null) {
56             return null;
57         }
58         final short pst = buffer.readerIndex(OFFSET).readUnsignedByte();
59         if (!checkPST(pst)) {
60             throw new PCEPDeserializerException(UNSUPPORTED_PST);
61         }
62         return new PathSetupTypeBuilder().setPst(pst).build();
63     }
64
65     private boolean checkPST(final Short pst) {
66         if (pst != null) {
67             return PSTS.contains(pst);
68         }
69         return false;
70     }
71
72 }