Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.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.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
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.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
30 import org.opendaylight.yangtools.yang.common.Uint8;
31 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
32
33 /**
34  * Parser for {@link NoPath}.
35  */
36 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37     private static final int CLASS = 3;
38     private static final int TYPE = 1;
39
40     /*
41      * lengths of fields in bytes
42      */
43     private static final int FLAGS_SIZE = 16;
44     private static final int RESERVED_F_LENGTH = 1;
45
46     /*
47      * defined flags
48      */
49     private static final int C_FLAG_OFFSET = 0;
50
51     public PCEPNoPathObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
52         super(tlvReg, viTlvReg, CLASS, TYPE);
53     }
54
55     @Override
56     public NoPath parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
57         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
58
59         final Uint8 issue = ByteBufUtils.readUint8(bytes);
60         final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
61         bytes.skipBytes(RESERVED_F_LENGTH);
62         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
63         parseTlvs(tlvsBuilder, bytes.slice());
64         return new NoPathBuilder()
65                 .setIgnore(header.getIgnore())
66                 .setProcessingRule(header.getProcessingRule())
67                 .setNatureOfIssue(issue)
68                 .setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET))
69                 .setTlvs(tlvsBuilder.build())
70                 .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         checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.",
83             object.getClass());
84         final NoPath nPObj = (NoPath) object;
85         final ByteBuf body = Unpooled.buffer();
86         ByteBufUtils.writeMandatory(body, nPObj.getNatureOfIssue(), "NatureOfIssue");
87         final BitArray flags = new BitArray(FLAGS_SIZE);
88         flags.set(C_FLAG_OFFSET, nPObj.getUnsatisfiedConstraints());
89         flags.toByteBuf(body);
90         body.writeZero(RESERVED_F_LENGTH);
91         serializeTlvs(nPObj.getTlvs(), body);
92         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
93     }
94
95     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
96         if (tlvs != null) {
97             if (tlvs.getNoPathVector() != null) {
98                 serializeTlv(tlvs.getNoPathVector(), body);
99             }
100             serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
101         }
102     }
103
104     @Override
105     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
106         if (!tlvs.isEmpty()) {
107             builder.setVendorInformationTlv(tlvs);
108         }
109     }
110 }