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