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