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