Merge "Fix junit dependencies in poms. Reuse existing from parent, add missing ones."
[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.TlvHandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.requests.LspBuilder;
21
22 /**
23  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLspObject PCEPLspObject}
24  */
25 public class PCEPLspObjectParser extends AbstractObjectWithTlvsParser<LspBuilder> {
26
27         public static final int CLASS = 32;
28
29         public static final int TYPE = 1;
30
31         /*
32          * offset of TLVs offset of other fields are not defined as constants
33          * because of non-standard mapping of bits
34          */
35         public static final int TLVS_OFFSET = 4;
36
37         /*
38          * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
39          */
40         private static final int DELEGATE_FLAG_OFFSET = 15;
41         private static final int OPERATIONAL_FLAG_OFFSET = 13;
42         private static final int SYNC_FLAG_OFFSET = 14;
43         private static final int REMOVE_FLAG_OFFSET = 12;
44
45         public PCEPLspObjectParser(final TlvHandlerRegistry tlvReg) {
46                 super(tlvReg);
47         }
48
49         @Override
50         public LspObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
51                 if (bytes == null || bytes.length == 0) {
52                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
53                 }
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
85                 final LspObject specObj = (LspObject) object;
86
87                 // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
88
89                 final byte[] retBytes = new byte[0 + TLVS_OFFSET];
90
91                 final int lspID = specObj.getPlspId().getValue().intValue();
92                 retBytes[0] = (byte) (lspID >> 12);
93                 retBytes[1] = (byte) (lspID >> 4);
94                 retBytes[2] = (byte) (lspID << 4);
95                 if (specObj.isDelegate()) {
96                         retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
97                 }
98                 // FIXME: !!
99                 // if (specObj.isOperational())
100                 // retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
101                 if (specObj.isRemove()) {
102                         retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
103                 }
104                 if (specObj.isSync()) {
105                         retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
106                 }
107
108                 // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
109
110                 return retBytes;
111         }
112
113         @Override
114         public int getObjectType() {
115                 return TYPE;
116         }
117
118         @Override
119         public int getObjectClass() {
120                 return CLASS;
121         }
122 }