Removing deprecated getType() method from serializers.
[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.spi.AbstractObjectWithTlvsParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
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.svec.object.Svec;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.SvecBuilder;
23
24 import com.google.common.collect.Lists;
25
26 /**
27  * Parser for {@link Svec}
28  */
29 public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuilder> {
30
31         public static final int CLASS = 11;
32
33         public static final int TYPE = 1;
34
35         /*
36          * field lengths in bytes
37          */
38         private static final int FLAGS_F_LENGTH = 3;
39         private static final int REQ_LIST_ITEM_LENGTH = 4;
40
41         /*
42          * fields offsets in bytes
43          */
44         private static final int FLAGS_F_OFFSET = 1;
45         private static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
46
47         /*
48          * flags offsets inside flags field in bits
49          */
50         private static final int S_FLAG_OFFSET = 21;
51         private static final int N_FLAG_OFFSET = 22;
52         private static final int L_FLAG_OFFSET = 23;
53
54         /*
55          * min size in bytes
56          */
57         private static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
58
59         public PCEPSvecObjectParser(final TlvRegistry tlvReg) {
60                 super(tlvReg);
61         }
62
63         @Override
64         public Svec parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
65                 if (bytes == null || bytes.length == 0) {
66                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
67                 }
68                 if (bytes.length < MIN_SIZE) {
69                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
70                                         + ".");
71                 }
72                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
73                 final List<RequestId> requestIDs = Lists.newArrayList();
74
75                 for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
76                         requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
77                 }
78                 if (requestIDs.isEmpty()) {
79                         throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
80                 }
81                 final SvecBuilder builder = new SvecBuilder();
82
83                 builder.setIgnore(header.isIgnore());
84                 builder.setProcessingRule(header.isProcessingRule());
85
86                 builder.setLinkDiverse(flags.get(L_FLAG_OFFSET));
87                 builder.setNodeDiverse(flags.get(N_FLAG_OFFSET));
88                 builder.setSrlgDiverse(flags.get(S_FLAG_OFFSET));
89                 builder.setRequestsIds(requestIDs);
90                 return builder.build();
91         }
92
93         @Override
94         public byte[] serializeObject(final Object object) {
95                 if (!(object instanceof Svec)) {
96                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
97                 }
98
99                 final Svec svecObj = (Svec) object;
100                 final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
101                 final List<RequestId> requestIDs = svecObj.getRequestsIds();
102                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
103                 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
104                 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
105                 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
106
107                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
108
109                 for (int i = 0; i < requestIDs.size(); i++) {
110                         System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue(), REQ_LIST_ITEM_LENGTH), 0, retBytes, REQ_LIST_ITEM_LENGTH
111                                         * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
112                 }
113                 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
114                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
115         }
116 }