BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPNoPathObjectParser.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
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
21 import org.opendaylight.protocol.util.BitArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVector;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
31
32 /**
33  * Parser for {@link NoPath}
34  */
35 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36
37     public static final int CLASS = 3;
38
39     public static final int TYPE = 1;
40
41     /*
42      * lengths of fields in bytes
43      */
44     private static final int FLAGS_SIZE = 16;
45     private static final int RESERVED_F_LENGTH = 1;
46
47     /*
48      * defined flags
49      */
50     private static final int C_FLAG_OFFSET = 0;
51
52     public PCEPNoPathObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
53         super(tlvReg, viTlvReg);
54     }
55
56     @Override
57     public NoPath 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 NoPathBuilder builder = new NoPathBuilder();
60         builder.setIgnore(header.isIgnore());
61         builder.setProcessingRule(header.isProcessingRule());
62
63         builder.setNatureOfIssue(bytes.readUnsignedByte());
64         final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
65         builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
66         bytes.skipBytes(RESERVED_F_LENGTH);
67         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
68         parseTlvs(tlvsBuilder, bytes.slice());
69         builder.setTlvs(tlvsBuilder.build());
70         return builder.build();
71     }
72
73     @Override
74     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
75         if (tlv instanceof NoPathVector) {
76             builder.setNoPathVector((NoPathVector) tlv);
77         }
78     }
79
80     @Override
81     public void serializeObject(final Object object, final ByteBuf buffer) {
82         Preconditions.checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.", object.getClass());
83         final NoPath nPObj = (NoPath) object;
84         final ByteBuf body = Unpooled.buffer();
85         Preconditions.checkArgument(nPObj.getNatureOfIssue() != null, "NatureOfIssue is mandatory.");
86         writeUnsignedByte(nPObj.getNatureOfIssue(), body);
87         final BitArray flags = new BitArray(FLAGS_SIZE);
88         flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
89         flags.toByteBuf(body);
90         body.writeZero(RESERVED_F_LENGTH);
91         serializeTlvs(nPObj.getTlvs(), body);
92         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
93     }
94
95     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
96         if (tlvs == null) {
97             return;
98         }
99         if (tlvs.getNoPathVector() != null) {
100             serializeTlv(tlvs.getNoPathVector(), body);
101         }
102         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
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 }