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