Removed unused PCEPDocumentedException from object parsers.
[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 java.util.BitSet;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.NoPath;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.NoPathBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.no.path.Tlvs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.no.path.TlvsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.no.path.tlvs.NoPathVector;
23
24 import com.google.common.primitives.UnsignedBytes;
25
26 /**
27  * Parser for {@link NoPath}
28  */
29 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<NoPathBuilder> {
30
31         public static final int CLASS = 3;
32
33         public static final int TYPE = 1;
34
35         /*
36          * lengths of fields in bytes
37          */
38         private static final int NI_F_LENGTH = 1;
39         private static final int FLAGS_F_LENGTH = 2;
40         private static final int RESERVED_F_LENGTH = 1;
41
42         /*
43          * offsets of field in bytes
44          */
45         private static final int NI_F_OFFSET = 0;
46         private static final int FLAGS_F_OFFSET = NI_F_OFFSET + NI_F_LENGTH;
47         private static final int RESERVED_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
48         private static final int TLVS_OFFSET = RESERVED_F_OFFSET + RESERVED_F_LENGTH;
49
50         /*
51          * defined flags
52          */
53         private static final int C_FLAG_OFFSET = 0;
54
55         public PCEPNoPathObjectParser(final TlvHandlerRegistry tlvReg) {
56                 super(tlvReg);
57         }
58
59         @Override
60         public NoPath parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
61                 if (bytes == null || bytes.length == 0) {
62                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
63                 }
64                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
65
66                 final NoPathBuilder builder = new NoPathBuilder();
67                 builder.setIgnore(header.isIgnore());
68                 builder.setProcessingRule(header.isProcessingRule());
69
70                 builder.setNatureOfIssue((short) UnsignedBytes.toInt(bytes[NI_F_OFFSET]));
71                 builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
72                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
73                 return builder.build();
74         }
75
76         @Override
77         public void addTlv(final NoPathBuilder builder, final Tlv tlv) {
78                 if (tlv instanceof NoPathVector) {
79                         builder.setTlvs(new TlvsBuilder().setNoPathVector((NoPathVector) tlv).build());
80                 }
81         }
82
83         @Override
84         public byte[] serializeObject(final Object object) {
85                 if (!(object instanceof NoPath)) {
86                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NoPathObject.");
87                 }
88                 final NoPath nPObj = (NoPath) object;
89
90                 final byte[] tlvs = serializeTlvs(nPObj.getTlvs());
91                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
92                 if (tlvs != null) {
93                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
94                 }
95                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
96                 flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
97                 retBytes[NI_F_OFFSET] = UnsignedBytes.checkedCast(nPObj.getNatureOfIssue());
98                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
99                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
100                 return retBytes;
101         }
102
103         public byte[] serializeTlvs(final Tlvs tlvs) {
104                 if (tlvs == null) {
105                         return new byte[0];
106                 } else if (tlvs.getNoPathVector() != null) {
107                         return serializeTlv(tlvs.getNoPathVector());
108                 }
109                 return new byte[0];
110         }
111
112         @Override
113         public int getObjectType() {
114                 return TYPE;
115         }
116
117         @Override
118         public int getObjectClass() {
119                 return CLASS;
120         }
121 }