BUG-612 : switched PCEP message serializers to ByteBuf
[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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15
16 import java.util.BitSet;
17
18 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
22 import org.opendaylight.protocol.util.ByteArray;
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.rsvp.rev130820.AttributeFilter;
30
31 /**
32  * Parser for {@link Lspa}
33  */
34 public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
35
36     public static final int CLASS = 9;
37
38     public static final int TYPE = 1;
39
40     /*
41      * lengths of fields in bytes
42      */
43     private static final int FLAGS_F_LENGTH = 1;
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) {
53         super(tlvReg);
54     }
55
56     @Override
57     public Lspa parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
58         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
59         final LspaBuilder builder = new LspaBuilder();
60         builder.setIgnore(header.isIgnore());
61         builder.setProcessingRule(header.isProcessingRule());
62
63         builder.setExcludeAny(new AttributeFilter(bytes.readUnsignedInt()));
64         builder.setIncludeAll(new AttributeFilter(bytes.readUnsignedInt()));
65         builder.setIncludeAny(new AttributeFilter(bytes.readUnsignedInt()));
66         builder.setSetupPriority((short) UnsignedBytes.toInt(bytes.readByte()));
67         builder.setHoldPriority((short) UnsignedBytes.toInt(bytes.readByte()));
68
69         final BitSet flags = ByteArray.bytesToBitSet(new byte[] { bytes.readByte() });
70         builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
71         final TlvsBuilder tbuilder = new TlvsBuilder();
72         bytes.readerIndex(bytes.readerIndex() + RESERVED);
73         parseTlvs(tbuilder, bytes.slice());
74         builder.setTlvs(tbuilder.build());
75         return builder.build();
76     }
77
78     @Override
79     public void serializeObject(final Object object, final ByteBuf buffer) {
80         Preconditions.checkArgument(object instanceof Lspa, "Wrong instance of PCEPObject. Passed %s. Needed LspaObject.", object.getClass());
81         final Lspa lspaObj = (Lspa) object;
82         final ByteBuf body = Unpooled.buffer();
83         body.writeInt(lspaObj.getExcludeAny().getValue().intValue());
84         body.writeInt(lspaObj.getIncludeAny().getValue().intValue());
85         body.writeInt(lspaObj.getIncludeAll().getValue().intValue());
86         body.writeByte(lspaObj.getSetupPriority());
87         body.writeByte(lspaObj.getHoldPriority());
88         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
89         if (lspaObj.isLocalProtectionDesired() != null) {
90             flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
91         }
92         body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
93         body.writeZero(RESERVED);
94         // FIXME: switch to ByteBuf
95         final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
96         if (tlvs.length != 0) {
97             body.writeBytes(tlvs);
98         }
99         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
100     }
101
102     public byte[] serializeTlvs(final Tlvs tlvs) {
103         return new byte[0];
104     }
105 }