2 * Copyright (c) 2016 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.ietf.stateful07;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvParser;
19 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
20 import org.opendaylight.protocol.pcep.spi.TlvUtil;
21 import org.opendaylight.protocol.util.ByteBufUtils;
22 import org.opendaylight.protocol.util.ByteBufWriteUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.PathBinding;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.PathBindingBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.path.binding.BindingTypeValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.path.binding.binding.type.value.MplsLabel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.path.binding.binding.type.value.MplsLabelBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.path.binding.binding.type.value.MplsLabelEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.path.binding.tlv.path.binding.binding.type.value.MplsLabelEntryBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
31 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
32 import org.opendaylight.yangtools.yang.common.Uint16;
33 import org.opendaylight.yangtools.yang.common.Uint32;
34 import org.opendaylight.yangtools.yang.common.Uint8;
37 * Parser for {@link PathBinding}.
39 public final class PathBindingTlvParser implements TlvParser, TlvSerializer {
41 // TODO: to be confirmed by IANA
42 public static final int TYPE = 31;
44 private static final Uint16 MPLS_LABEL = Uint16.ZERO;
45 private static final Uint16 MPLS_STACK_ENTRY = Uint16.ONE;
47 private static final int LABEL_MASK = 0xfffff;
48 private static final int TC_MASK = 0x7;
49 private static final int S_MASK = 0x1;
50 private static final int TTL_MASK = 0xff;
51 private static final int LABEL_SHIFT = 12;
52 private static final int TC_SHIFT = LABEL_SHIFT - 3;
53 private static final int S_SHIFT = TC_SHIFT - 1;
54 private static final int MPLS_ENTRY_LENGTH = 4;
55 private static final int MPLS_BINDING_LENGTH = MPLS_ENTRY_LENGTH + 2;
57 private static final Map<Uint16, PathBindingTlvCodec> BT_PARSERS;
58 private static final Map<Class<? extends BindingTypeValue>, PathBindingTlvCodec> BT_SERIALIZERS;
61 final MplsLabelCodec mplsLabelCodec = new MplsLabelCodec();
62 final MplsLabelEntryCodec mplsLabelEntryCodec = new MplsLabelEntryCodec();
63 final Builder<Uint16, PathBindingTlvCodec> parsers = ImmutableMap.builder();
64 final Builder<Class<? extends BindingTypeValue>, PathBindingTlvCodec> serializers =
65 ImmutableMap.builder();
67 parsers.put(mplsLabelCodec.getBindingType(), mplsLabelCodec);
68 serializers.put(MplsLabel.class, mplsLabelCodec);
70 parsers.put(mplsLabelEntryCodec.getBindingType(), mplsLabelEntryCodec);
71 serializers.put(MplsLabelEntry.class, mplsLabelEntryCodec);
73 BT_PARSERS = parsers.build();
74 BT_SERIALIZERS = serializers.build();
78 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
79 Preconditions.checkArgument(tlv instanceof PathBinding,
80 "The TLV must be PathBinding type, but was %s", tlv.getClass());
81 final PathBinding pTlv = (PathBinding) tlv;
82 final BindingTypeValue bindingTypeValue = pTlv.getBindingTypeValue();
83 Preconditions.checkArgument(bindingTypeValue != null,
84 "Missing Binding Value in Path Bidning TLV: %s", pTlv);
85 final ByteBuf body = Unpooled.buffer(MPLS_BINDING_LENGTH);
86 final PathBindingTlvCodec codec = BT_SERIALIZERS.get(bindingTypeValue.implementedInterface());
87 Preconditions.checkArgument(codec != null,
88 "Unsupported Path Binding Type: %s", bindingTypeValue.implementedInterface());
89 ByteBufUtils.writeMandatory(body, codec.getBindingType(), "bindingType");
90 body.writeBytes(codec.serialize(bindingTypeValue));
92 TlvUtil.formatTlv(TYPE, body, buffer);
96 public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
100 final Uint16 type = ByteBufUtils.readUint16(buffer);
101 final PathBindingTlvCodec codec = BT_PARSERS.get(type);
103 throw new PCEPDeserializerException("Unsupported Path Binding Type: " + type);
105 return new PathBindingBuilder().setBindingTypeValue(codec.deserialize(buffer)).build();
108 private static final class MplsLabelCodec implements PathBindingTlvCodec {
111 public ByteBuf serialize(final BindingTypeValue bindingValue) {
112 Preconditions.checkArgument(bindingValue instanceof MplsLabel, "bindingValue is not MplsLabel");
113 final MplsLabel mplsLabel = (MplsLabel) bindingValue;
114 final ByteBuf value = Unpooled.buffer(MPLS_ENTRY_LENGTH);
115 ByteBufWriteUtil.writeUnsignedInt(Uint32.valueOf(getMplsStackEntry(mplsLabel.getMplsLabel())), value);
120 public BindingTypeValue deserialize(final ByteBuf buffer) {
121 final MplsLabelBuilder builder = new MplsLabelBuilder();
122 builder.setMplsLabel(getMplsLabel(buffer.readUnsignedInt()));
123 return builder.build();
127 public Uint16 getBindingType() {
132 private static final class MplsLabelEntryCodec implements PathBindingTlvCodec {
135 public ByteBuf serialize(final BindingTypeValue bindingValue) {
136 Preconditions.checkArgument(bindingValue instanceof MplsLabelEntry,
137 "bindingValue is not MplsLabelEntry");
138 final MplsLabelEntry mplsEntry = (MplsLabelEntry) bindingValue;
139 final ByteBuf value = Unpooled.buffer(MPLS_ENTRY_LENGTH);
140 final long entry = getMplsStackEntry(mplsEntry.getLabel())
141 | mplsEntry.getTrafficClass().toJava() << TC_SHIFT
142 | (mplsEntry.isBottomOfStack() ? 1 : 0) << S_SHIFT
143 | mplsEntry.getTimeToLive().toJava();
144 ByteBufWriteUtil.writeUnsignedInt(Uint32.valueOf(entry), value);
149 public BindingTypeValue deserialize(final ByteBuf buffer) {
150 final long entry = buffer.readUnsignedInt();
151 final MplsLabelEntryBuilder builder = new MplsLabelEntryBuilder()
152 .setLabel(getMplsLabel(entry))
153 .setTrafficClass(Uint8.valueOf(entry >> TC_SHIFT & TC_MASK))
154 .setBottomOfStack((entry >> S_SHIFT & S_MASK) == 1)
155 .setTimeToLive(Uint8.valueOf(entry & TTL_MASK));
156 return builder.build();
160 public Uint16 getBindingType() {
161 return MPLS_STACK_ENTRY;
165 private interface PathBindingTlvCodec extends IllegalArgumentCodec<ByteBuf, BindingTypeValue> {
166 Uint16 getBindingType();
169 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
170 justification = "https://github.com/spotbugs/spotbugs/issues/811")
171 private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel
172 getMplsLabel(final long mplsStackEntry) {
173 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125
174 .MplsLabel(Uint32.valueOf(mplsStackEntry >> LABEL_SHIFT & LABEL_MASK));
177 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
178 justification = "https://github.com/spotbugs/spotbugs/issues/811")
179 private static long getMplsStackEntry(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network
180 .concepts.rev131125.MplsLabel mplsLabel) {
181 return mplsLabel.getValue().toJava() << LABEL_SHIFT;