Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPLspObjectParser.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.PCEPObject;
14 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
15 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
16 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
17 import org.opendaylight.protocol.util.ByteArray;
18
19 /**
20  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLspObject PCEPLspObject}
21  */
22 public class PCEPLspObjectParser implements PCEPObjectParser {
23
24         /*
25          * offset of TLVs offset of other fields are not defined as constants
26          * because of non-standard mapping of bits
27          */
28         public static final int TLVS_OFFSET = 4;
29
30         /*
31          * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
32          */
33         private static final int DELEGATE_FLAG_OFFSET = 15;
34         private static final int OPERATIONAL_FLAG_OFFSET = 13;
35         private static final int SYNC_FLAG_OFFSET = 14;
36         private static final int REMOVE_FLAG_OFFSET = 12;
37
38         @Override
39         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
40                 if (bytes == null || bytes.length == 0)
41                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
42
43                 if (bytes.length < TLVS_OFFSET)
44                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
45
46                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
47
48                 return new PCEPLspObject((ByteArray.bytesToShort(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4,
49                                 flags.get(DELEGATE_FLAG_OFFSET), flags.get(SYNC_FLAG_OFFSET), flags.get(OPERATIONAL_FLAG_OFFSET), flags.get(REMOVE_FLAG_OFFSET),
50                                 PCEPTlvParser.parse(ByteArray.cutBytes(bytes, TLVS_OFFSET)));
51         }
52
53         @Override
54         public byte[] put(PCEPObject obj) {
55                 if (!(obj instanceof PCEPLspObject))
56                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPLspObject.");
57
58                 final PCEPLspObject specObj = (PCEPLspObject) obj;
59
60                 final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
61
62                 final byte[] retBytes = new byte[tlvs.length + TLVS_OFFSET];
63
64                 final int lspID = specObj.getLspID();
65                 retBytes[0] = (byte) (lspID >> 12);
66                 retBytes[1] = (byte) (lspID >> 4);
67                 retBytes[2] = (byte) (lspID << 4);
68                 if (specObj.isDelegate())
69                         retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
70                 if (specObj.isOperational())
71                         retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
72                 if (specObj.isRemove())
73                         retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
74                 if (specObj.isSync())
75                         retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
76
77                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
78
79                 return retBytes;
80         }
81
82 }