Merge "BUG-113: split HandlerRegistry into per-class registries"
[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.BitSet;
11 import java.util.List;
12
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
16 import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SvecObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.SvecBuilder;
25
26 import com.google.common.collect.Lists;
27
28 /**
29  * Parser for {@link SvecObject}
30  */
31 public class PCEPSvecObjectParser extends AbstractObjectParser<SvecBuilder> {
32
33         public static final int CLASS = 11;
34
35         public static final int TYPE = 1;
36
37         /*
38          * field lengths in bytes
39          */
40         public static final int FLAGS_F_LENGTH = 3;
41         public static final int REQ_LIST_ITEM_LENGTH = 4;
42
43         /*
44          * fields offsets in bytes
45          */
46         public static final int FLAGS_F_OFFSET = 1; // aded reserved field of size 1
47         public static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
48
49         /*
50          * flags offsets inside flags field in bits
51          */
52         public static final int S_FLAG_OFFSET = 21;
53         public static final int N_FLAG_OFFSET = 22;
54         public static final int L_FLAG_OFFSET = 23;
55
56         /*
57          * min size in bytes
58          */
59         public static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
60
61         public PCEPSvecObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
62                 super(subobjReg, tlvReg);
63         }
64
65         @Override
66         public SvecObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
67                 if (bytes == null || bytes.length == 0) {
68                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
69                 }
70
71                 if (bytes.length < MIN_SIZE) {
72                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
73                                         + ".");
74                 }
75
76                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
77                 final List<RequestId> requestIDs = Lists.newArrayList();
78
79                 for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
80                         requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
81                 }
82
83                 if (requestIDs.isEmpty()) {
84                         throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
85                 }
86
87                 final SvecBuilder builder = new SvecBuilder();
88
89                 builder.setIgnore(header.isIgnore());
90                 builder.setProcessingRule(header.isProcessingRule());
91
92                 builder.setLinkDiverse(flags.get(L_FLAG_OFFSET));
93                 builder.setNodeDiverse(flags.get(N_FLAG_OFFSET));
94                 builder.setSrlgDiverse(flags.get(S_FLAG_OFFSET));
95                 builder.setRequestsIds(requestIDs);
96                 return builder.build();
97         }
98
99         @Override
100         public void addTlv(final SvecBuilder builder, final Tlv tlv) {
101                 // No tlvs defined
102         }
103
104         @Override
105         public byte[] serializeObject(final Object object) {
106                 if (!(object instanceof SvecObject)) {
107                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
108                 }
109
110                 final SvecObject svecObj = (SvecObject) object;
111                 final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
112                 final List<RequestId> requestIDs = svecObj.getRequestsIds();
113                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
114                 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
115                 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
116                 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
117
118                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
119
120                 for (int i = 0; i < requestIDs.size(); i++) {
121                         System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
122                                         + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
123                 }
124
125                 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
126
127                 return retBytes;
128         }
129
130         @Override
131         public int getObjectType() {
132                 return TYPE;
133         }
134
135         @Override
136         public int getObjectClass() {
137                 return CLASS;
138         }
139 }