Merge "Bug-2250: pcc-mock support TCP MD5"
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractObjectWithTlvsParser.java
1 /*
2  * Copyright (c) 2013 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.spi;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import java.util.List;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, ObjectSerializer {
23
24     private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
25
26     public static final int PADDED_TO = 4;
27
28     private final TlvRegistry tlvReg;
29
30     private final VendorInformationTlvRegistry viTlvReg;
31
32     protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
33         this.tlvReg = Preconditions.checkNotNull(tlvReg);
34         this.viTlvReg = Preconditions.checkNotNull(viTlvReg);
35     }
36
37     protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
38         Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
39         if (!bytes.isReadable()) {
40             return;
41         }
42         final List<VendorInformationTlv> viTlvs = Lists.newArrayList();
43         while (bytes.isReadable()) {
44             final int type = bytes.readUnsignedShort();
45             final int length = bytes.readUnsignedShort();
46             if (length > bytes.readableBytes()) {
47                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + bytes.readableBytes()
48                     + ".");
49             }
50             final ByteBuf tlvBytes = bytes.readSlice(length);
51             LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
52
53             if (VendorInformationUtil.isVendorInformationTlv(type)) {
54                 final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(tlvBytes.readUnsignedInt());
55                 final Optional<VendorInformationTlv> viTlv = this.viTlvReg.parseVendorInformationTlv(enterpriseNumber, tlvBytes);
56                 if(viTlv.isPresent()) {
57                     LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv.get());
58                     viTlvs.add(viTlv.get());
59                 }
60             } else {
61                 final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
62                 if(tlv != null) {
63                     LOG.trace("Parsed PCEP TLV {}.", tlv);
64                     addTlv(builder, tlv);
65                 }
66             }
67             bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
68         }
69         addVendorInformationTlvs(builder, viTlvs);
70     }
71
72     protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
73         Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
74         LOG.trace("Serializing PCEP TLV {}", tlv);
75         this.tlvReg.serializeTlv(tlv, buffer);
76         LOG.trace("Serialized PCEP TLV : {}.", ByteBufUtil.hexDump(buffer));
77     }
78
79     protected void addTlv(final T builder, final Tlv tlv) {
80         // FIXME: No TLVs by default, fallback to augments
81     }
82
83     protected abstract void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
84
85     protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
86         if (tlvs != null && !tlvs.isEmpty()) {
87             for (final VendorInformationTlv tlv : tlvs) {
88                 LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
89                 this.viTlvReg.serializeVendorInformationTlv(tlv, buffer);
90                 LOG.trace("Serialized VENDOR-INFORMATION TLV : {}.", ByteBufUtil.hexDump(buffer));
91             }
92         }
93     }
94 }