BUG-47 : switched subobjects to generated source code.
[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.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
15 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspObject;
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.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.requests.LspBuilder;
22
23 /**
24  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLspObject PCEPLspObject}
25  */
26 public class PCEPLspObjectParser extends AbstractObjectWithTlvsParser<LspBuilder> {
27
28         public static final int CLASS = 32;
29
30         public static final int TYPE = 1;
31
32         /*
33          * offset of TLVs offset of other fields are not defined as constants
34          * because of non-standard mapping of bits
35          */
36         public static final int TLVS_OFFSET = 4;
37
38         /*
39          * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
40          */
41         private static final int DELEGATE_FLAG_OFFSET = 15;
42         private static final int OPERATIONAL_FLAG_OFFSET = 13;
43         private static final int SYNC_FLAG_OFFSET = 14;
44         private static final int REMOVE_FLAG_OFFSET = 12;
45
46         public PCEPLspObjectParser(final TlvHandlerRegistry tlvReg) {
47                 super(tlvReg);
48         }
49
50         @Override
51         public LspObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
52                 if (bytes == null || bytes.length == 0) {
53                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
54                 }
55
56                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
57
58                 final LspBuilder builder = new LspBuilder();
59
60                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
61
62                 builder.setIgnore(header.isIgnore());
63                 builder.setProcessingRule(header.isProcessingRule());
64
65                 // builder.setPlspId(new PlspId(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2]
66                 // & 0xFF) >> 4));
67                 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
68                 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
69                 // builder.setOperational(Operational.flags.get(OPERATIONAL_FLAG_OFFSET));
70                 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
71
72                 return builder.build();
73         }
74
75         @Override
76         public void addTlv(final LspBuilder builder, final Tlv tlv) {
77                 // FIXME : finish
78         }
79
80         @Override
81         public byte[] serializeObject(final Object object) {
82                 if (!(object instanceof LspObject)) {
83                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
84                 }
85
86                 final LspObject specObj = (LspObject) object;
87
88                 // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
89
90                 final byte[] retBytes = new byte[0 + TLVS_OFFSET];
91
92                 final int lspID = specObj.getPlspId().getValue().intValue();
93                 retBytes[0] = (byte) (lspID >> 12);
94                 retBytes[1] = (byte) (lspID >> 4);
95                 retBytes[2] = (byte) (lspID << 4);
96                 if (specObj.isDelegate()) {
97                         retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
98                 }
99                 // FIXME: !!
100                 // if (specObj.isOperational())
101                 // retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
102                 if (specObj.isRemove()) {
103                         retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
104                 }
105                 if (specObj.isSync()) {
106                         retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
107                 }
108
109                 // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
110
111                 return retBytes;
112         }
113
114         @Override
115         public int getObjectType() {
116                 return TYPE;
117         }
118
119         @Override
120         public int getObjectClass() {
121                 return CLASS;
122         }
123 }