Merge "BUG-47 : switched subobjects to generated source code."
[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.impl.Util;
15 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.pcrep.pcrep.message.replies.result.failure.NoPath;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.pcrep.pcrep.message.replies.result.failure.no.path.Tlvs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathObject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.NoPathBuilder;
25
26 /**
27  * Parser for {@link NoPathObject}
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         public static final int NI_F_LENGTH = 1; // multi-field
39         public static final int FLAGS_F_LENGTH = 2;
40         public static final int RESERVED_F_LENGTH = 1;
41
42         /*
43          * offsets of field in bytes
44          */
45
46         public static final int NI_F_OFFSET = 0;
47         public static final int FLAGS_F_OFFSET = NI_F_OFFSET + NI_F_LENGTH;
48         public static final int RESERVED_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
49         public static final int TLVS_OFFSET = RESERVED_F_OFFSET + RESERVED_F_LENGTH;
50
51         /*
52          * defined flags
53          */
54
55         public 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
68                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
69
70                 final NoPathBuilder builder = new NoPathBuilder();
71
72                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
73
74                 builder.setIgnore(header.isIgnore());
75                 builder.setProcessingRule(header.isProcessingRule());
76
77                 builder.setNatureOfIssue((short) (bytes[NI_F_OFFSET] & 0xFF));
78                 builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
79
80                 return builder.build();
81         }
82
83         @Override
84         public void addTlv(final NoPathBuilder builder, final Tlv tlv) {
85                 // FIXME : add no-path-vector-tlv
86         }
87
88         @Override
89         public byte[] serializeObject(final Object object) {
90                 if (!(object instanceof NoPathObject)) {
91                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NoPathObject.");
92                 }
93
94                 final NoPathObject nPObj = (NoPathObject) object;
95
96                 final byte[] tlvs = serializeTlvs(((NoPath) nPObj).getTlvs());
97                 int tlvsLength = 0;
98                 if (tlvs != null) {
99                         tlvsLength = tlvs.length;
100                 }
101                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
102
103                 if (tlvs != null) {
104                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
105                 }
106                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
107                 flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
108                 retBytes[NI_F_OFFSET] = ByteArray.shortToBytes(nPObj.getNatureOfIssue())[1];
109                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
110                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
111
112                 return retBytes;
113         }
114
115         public byte[] serializeTlvs(final Tlvs tlvs) {
116                 if (tlvs.getNoPathVector() != null) {
117                         // FIXME : add NoPath
118                         // return serializeTlv(new NoPathVectorBuilder().setFlags(tlvs.getNoPathVector()).build());
119                 }
120                 return null;
121         }
122
123         @Override
124         public int getObjectType() {
125                 return TYPE;
126         }
127
128         @Override
129         public int getObjectClass() {
130                 return CLASS;
131         }
132 }