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