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.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
15 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
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 AbstractObjectParser<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 HandlerRegistry registry) {
47                 super(registry);
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                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
56
57                 final LspBuilder builder = new LspBuilder();
58
59                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
60
61                 builder.setIgnore(header.isIgnore());
62                 builder.setProcessingRule(header.isProcessingRule());
63
64                 // builder.setPlspId(new PlspId(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2]
65                 // & 0xFF) >> 4));
66                 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
67                 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
68                 // builder.setOperational(Operational.flags.get(OPERATIONAL_FLAG_OFFSET));
69                 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
70
71                 return builder.build();
72         }
73
74         @Override
75         public void addTlv(final LspBuilder builder, final Tlv tlv) {
76                 // FIXME : finish
77         }
78
79         @Override
80         public byte[] serializeObject(final Object object) {
81                 if (!(object instanceof LspObject))
82                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
83
84                 final LspObject specObj = (LspObject) object;
85
86                 // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
87
88                 final byte[] retBytes = new byte[0 + TLVS_OFFSET];
89
90                 final int lspID = specObj.getPlspId().getValue().intValue();
91                 retBytes[0] = (byte) (lspID >> 12);
92                 retBytes[1] = (byte) (lspID >> 4);
93                 retBytes[2] = (byte) (lspID << 4);
94                 if (specObj.isDelegate())
95                         retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
96                 // FIXME: !!
97                 // if (specObj.isOperational())
98                 // retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
99                 if (specObj.isRemove())
100                         retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
101                 if (specObj.isSync())
102                         retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
103
104                 // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
105
106                 return retBytes;
107         }
108
109         @Override
110         public int getObjectType() {
111                 return TYPE;
112         }
113
114         @Override
115         public int getObjectClass() {
116                 return CLASS;
117         }
118 }