Add new revision for pcep types model
[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 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.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
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.Tlvs;
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.TlvsBuilder;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
31
32 /**
33  * Parser for {@link NoPath}
34  */
35 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36
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         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
58         final NoPathBuilder builder = new NoPathBuilder();
59         builder.setIgnore(header.isIgnore());
60         builder.setProcessingRule(header.isProcessingRule());
61
62         builder.setNatureOfIssue(bytes.readUnsignedByte());
63         final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
64         builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
65         bytes.skipBytes(RESERVED_F_LENGTH);
66         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
67         parseTlvs(tlvsBuilder, bytes.slice());
68         builder.setTlvs(tlvsBuilder.build());
69         return builder.build();
70     }
71
72     @Override
73     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
74         if (tlv instanceof NoPathVector) {
75             builder.setNoPathVector((NoPathVector) tlv);
76         }
77     }
78
79     @Override
80     public void serializeObject(final Object object, final ByteBuf buffer) {
81         Preconditions.checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.", object.getClass());
82         final NoPath nPObj = (NoPath) object;
83         final ByteBuf body = Unpooled.buffer();
84         Preconditions.checkArgument(nPObj.getNatureOfIssue() != null, "NatureOfIssue is mandatory.");
85         writeUnsignedByte(nPObj.getNatureOfIssue(), body);
86         final BitArray flags = new BitArray(FLAGS_SIZE);
87         flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
88         flags.toByteBuf(body);
89         body.writeZero(RESERVED_F_LENGTH);
90         serializeTlvs(nPObj.getTlvs(), body);
91         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
92     }
93
94     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
95         if (tlvs == null) {
96             return;
97         }
98         if (tlvs.getNoPathVector() != null) {
99             serializeTlv(tlvs.getNoPathVector(), body);
100         }
101         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
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 }