BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPSvecObjectParser.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 import java.util.List;
12
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
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.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SvecObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.SvecBuilder;
24
25 import com.google.common.collect.Lists;
26
27 /**
28  * Parser for {@link SvecObject}
29  */
30 public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuilder> {
31
32         public static final int CLASS = 11;
33
34         public static final int TYPE = 1;
35
36         /*
37          * field lengths in bytes
38          */
39         public static final int FLAGS_F_LENGTH = 3;
40         public static final int REQ_LIST_ITEM_LENGTH = 4;
41
42         /*
43          * fields offsets in bytes
44          */
45         public static final int FLAGS_F_OFFSET = 1; // aded reserved field of size 1
46         public static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
47
48         /*
49          * flags offsets inside flags field in bits
50          */
51         public static final int S_FLAG_OFFSET = 21;
52         public static final int N_FLAG_OFFSET = 22;
53         public static final int L_FLAG_OFFSET = 23;
54
55         /*
56          * min size in bytes
57          */
58         public static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
59
60         public PCEPSvecObjectParser(final TlvHandlerRegistry tlvReg) {
61                 super(tlvReg);
62         }
63
64         @Override
65         public SvecObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
66                 if (bytes == null || bytes.length == 0) {
67                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
68                 }
69
70                 if (bytes.length < MIN_SIZE) {
71                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
72                                         + ".");
73                 }
74
75                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
76                 final List<RequestId> requestIDs = Lists.newArrayList();
77
78                 for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
79                         requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
80                 }
81
82                 if (requestIDs.isEmpty()) {
83                         throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
84                 }
85
86                 final SvecBuilder builder = new SvecBuilder();
87
88                 builder.setIgnore(header.isIgnore());
89                 builder.setProcessingRule(header.isProcessingRule());
90
91                 builder.setLinkDiverse(flags.get(L_FLAG_OFFSET));
92                 builder.setNodeDiverse(flags.get(N_FLAG_OFFSET));
93                 builder.setSrlgDiverse(flags.get(S_FLAG_OFFSET));
94                 builder.setRequestsIds(requestIDs);
95                 return builder.build();
96         }
97
98         @Override
99         public void addTlv(final SvecBuilder builder, final Tlv tlv) {
100                 // No tlvs defined
101         }
102
103         @Override
104         public byte[] serializeObject(final Object object) {
105                 if (!(object instanceof SvecObject)) {
106                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
107                 }
108
109                 final SvecObject svecObj = (SvecObject) object;
110                 final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
111                 final List<RequestId> requestIDs = svecObj.getRequestsIds();
112                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
113                 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
114                 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
115                 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
116
117                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
118
119                 for (int i = 0; i < requestIDs.size(); i++) {
120                         System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
121                                         + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
122                 }
123
124                 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
125
126                 return retBytes;
127         }
128
129         @Override
130         public int getObjectType() {
131                 return TYPE;
132         }
133
134         @Override
135         public int getObjectClass() {
136                 return CLASS;
137         }
138 }