Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPLspaObjectParser.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.parser.object;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
21 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
22 import org.opendaylight.protocol.util.BitArray;
23 import org.opendaylight.protocol.util.ByteBufUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.Lspa;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.LspaBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.lspa.Tlvs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.lspa.TlvsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AttributeFilter;
32
33 /**
34  * Parser for {@link Lspa}.
35  */
36 public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37
38     private static final int CLASS = 9;
39
40     private static final int TYPE = 1;
41
42     /*
43      * lengths of fields in bytes
44      */
45     private static final int FLAGS_SIZE = 8;
46
47     /*
48      * offsets of flags inside flags field in bits
49      */
50     private static final int L_FLAG_OFFSET = 7;
51
52     private static final int RESERVED = 1;
53
54     public PCEPLspaObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
55         super(tlvReg, viTlvReg, CLASS, TYPE);
56     }
57
58     @Override
59     public Lspa parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
60         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
61         final LspaBuilder builder = new LspaBuilder()
62                 .setIgnore(header.isIgnore())
63                 .setProcessingRule(header.isProcessingRule())
64                 .setExcludeAny(new AttributeFilter(ByteBufUtils.readUint32(bytes)))
65                 .setIncludeAll(new AttributeFilter(ByteBufUtils.readUint32(bytes)))
66                 .setIncludeAny(new AttributeFilter(ByteBufUtils.readUint32(bytes)))
67                 .setSetupPriority(ByteBufUtils.readUint8(bytes))
68                 .setHoldPriority(ByteBufUtils.readUint8(bytes));
69
70         final BitArray flags = BitArray.valueOf(bytes.readByte());
71         builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
72         final TlvsBuilder tbuilder = new TlvsBuilder();
73         bytes.skipBytes(RESERVED);
74         parseTlvs(tbuilder, bytes.slice());
75         builder.setTlvs(tbuilder.build());
76         return builder.build();
77     }
78
79     @Override
80     public void serializeObject(final Object object, final ByteBuf buffer) {
81         checkArgument(object instanceof Lspa,
82             "Wrong instance of PCEPObject. Passed %s. Needed LspaObject.", object.getClass());
83         final Lspa lspaObj = (Lspa) object;
84         final ByteBuf body = Unpooled.buffer();
85         writeAttributeFilter(lspaObj.getExcludeAny(), body);
86         writeAttributeFilter(lspaObj.getIncludeAny(), body);
87         writeAttributeFilter(lspaObj.getIncludeAll(), body);
88         writeUnsignedByte(lspaObj.getSetupPriority(), body);
89         writeUnsignedByte(lspaObj.getHoldPriority(), body);
90         final BitArray flags = new BitArray(FLAGS_SIZE);
91         flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
92         flags.toByteBuf(body);
93         body.writeZero(RESERVED);
94         serializeTlvs(lspaObj.getTlvs(), body);
95         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
96     }
97
98     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
99         if (tlvs == null) {
100             return;
101         }
102         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
103     }
104
105     private static void writeAttributeFilter(final AttributeFilter attributeFilter, final ByteBuf body) {
106         writeUnsignedInt(attributeFilter != null ? attributeFilter.getValue() : null, body);
107     }
108
109     @Override
110     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
111         if (!tlvs.isEmpty()) {
112             builder.setVendorInformationTlv(tlvs);
113         }
114     }
115 }