Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[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.ArrayList;
11 import java.util.BitSet;
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPObject;
16 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
17 import org.opendaylight.protocol.pcep.object.PCEPSvecObject;
18 import org.opendaylight.protocol.util.ByteArray;
19 import com.google.common.primitives.UnsignedInts;
20
21 /**
22  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPSvecObject PCEPSvecObject}
23  */
24 public class PCEPSvecObjectParser implements PCEPObjectParser {
25
26         /*
27          * field lengths in bytes
28          */
29         public static final int FLAGS_F_LENGTH = 3;
30         public static final int REQ_LIST_ITEM_LENGTH = 4;
31
32         /*
33          * fields offsets in bytes
34          */
35         public static final int FLAGS_F_OFFSET = 1; // aded reserved field of size 1
36         public static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
37
38         /*
39          * flags offsets inside flags field in bits
40          */
41         public static final int S_FLAG_OFFSET = 21;
42         public static final int N_FLAG_OFFSET = 22;
43         public static final int L_FLAG_OFFSET = 23;
44
45         public static final int P_FLAG_OFFSET = 19;
46         public static final int D_FLAG_OFFSET = 20;
47
48         /*
49          * min size in bytes
50          */
51
52         public static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
53
54         @Override
55         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
56                 if (bytes == null || bytes.length == 0)
57                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
58
59                 if (bytes.length < MIN_SIZE)
60                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE + ".");
61
62                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
63                 final int numOfRIDs = (bytes.length - FLAGS_F_LENGTH - FLAGS_F_OFFSET) / REQ_LIST_ITEM_LENGTH;
64                 final List<Long> requestIDs = new ArrayList<Long>(numOfRIDs);
65
66                 for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
67                         requestIDs.add(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
68                 }
69
70                 if (requestIDs.isEmpty())
71                         throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
72
73                 return new PCEPSvecObject(flags.get(L_FLAG_OFFSET), flags.get(N_FLAG_OFFSET), flags.get(S_FLAG_OFFSET), flags.get(P_FLAG_OFFSET),
74                                 flags.get(D_FLAG_OFFSET), requestIDs, processed);
75         }
76
77         @Override
78         public byte[] put(PCEPObject obj) {
79                 if (!(obj instanceof PCEPSvecObject))
80                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPSvecObject.");
81
82                 final PCEPSvecObject svecObj = (PCEPSvecObject) obj;
83                 final byte[] retBytes = new byte[svecObj.getRequestIDs().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
84                 final List<Long> requestIDs = svecObj.getRequestIDs();
85                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
86                 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiversed());
87                 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiversed());
88                 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiversed());
89                 flags.set(P_FLAG_OFFSET, svecObj.isParitialPathDiversed());
90                 flags.set(D_FLAG_OFFSET, svecObj.isLinkDirectionDiversed());
91
92                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
93
94                 for (int i = 0; i < requestIDs.size(); i++) {
95                         System.arraycopy(ByteArray.longToBytes(requestIDs.get(i)), 4, retBytes, REQ_LIST_ITEM_LENGTH * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
96                 }
97
98                 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
99
100                 return retBytes;
101         }
102
103 }