Support proper route redistribution
[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.writeBitSet;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.BitSet;
17 import java.util.List;
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.pcep.spi.VendorInformationTlvRegistry;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
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;
30 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;
31 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;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
33
34 /**
35  * Parser for {@link NoPath}
36  */
37 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38
39     public static final int CLASS = 3;
40
41     public static final int TYPE = 1;
42
43     /*
44      * lengths of fields in bytes
45      */
46     private static final int FLAGS_F_LENGTH = 2;
47     private static final int RESERVED_F_LENGTH = 1;
48
49     /*
50      * defined flags
51      */
52     private static final int C_FLAG_OFFSET = 0;
53
54     public PCEPNoPathObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
55         super(tlvReg, viTlvReg);
56     }
57
58     @Override
59     public NoPath parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
60         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
61         final NoPathBuilder builder = new NoPathBuilder();
62         builder.setIgnore(header.isIgnore());
63         builder.setProcessingRule(header.isProcessingRule());
64
65         builder.setNatureOfIssue(bytes.readUnsignedByte());
66         final byte[] flagsByte = ByteArray.readBytes(bytes, FLAGS_F_LENGTH);
67         final BitSet flags = ByteArray.bytesToBitSet(flagsByte);
68         builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
69         bytes.readerIndex(bytes.readerIndex() + RESERVED_F_LENGTH);
70         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
71         parseTlvs(tlvsBuilder, bytes.slice());
72         builder.setTlvs(tlvsBuilder.build());
73         return builder.build();
74     }
75
76     @Override
77     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
78         if (tlv instanceof NoPathVector) {
79             builder.setNoPathVector((NoPathVector) tlv);
80         }
81     }
82
83     @Override
84     public void serializeObject(final Object object, final ByteBuf buffer) {
85         Preconditions.checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.", object.getClass());
86         final NoPath nPObj = (NoPath) object;
87         final ByteBuf body = Unpooled.buffer();
88         Preconditions.checkArgument(nPObj.getNatureOfIssue() != null, "NatureOfIssue is mandatory.");
89         writeUnsignedByte(nPObj.getNatureOfIssue(), body);
90         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
91         if (nPObj.isUnsatisfiedConstraints() != null) {
92             flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
93         }
94         writeBitSet(flags, FLAGS_F_LENGTH, body);
95         body.writeZero(RESERVED_F_LENGTH);
96         serializeTlvs(nPObj.getTlvs(), body);
97         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
98     }
99
100     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
101         if (tlvs == null) {
102             return;
103         }
104         if (tlvs.getNoPathVector() != null) {
105             serializeTlv(tlvs.getNoPathVector(), body);
106         }
107         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
108     }
109
110     @Override
111     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
112         if(!tlvs.isEmpty()) {
113             builder.setVendorInformationTlv(tlvs);
114         }
115     }
116 }