Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPExcludeRouteObjectParser.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.Arrays;
11 import java.util.BitSet;
12 import java.util.List;
13
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
17 import org.opendaylight.protocol.pcep.PCEPObject;
18 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
19 import org.opendaylight.protocol.pcep.impl.PCEPXROSubobjectParser;
20 import org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject;
21 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
22
23 /**
24  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject
25  * PCEPExcludeRouteObject}
26  */
27 public class PCEPExcludeRouteObjectParser implements PCEPObjectParser {
28
29         /*
30          * lengths of fields in bytes
31          */
32         public final int FLAGS_F_LENGTH = 2;
33
34         /*
35          * offsets of fields in bytes
36          */
37         public final int FLAGS_F_OFFSET = 2; // added reserved 2 bytes
38         public final int SO_F_OFFSET = this.FLAGS_F_OFFSET + this.FLAGS_F_LENGTH;
39
40         /*
41          * Flag offsets inside flags field in bits
42          */
43         public final int F_FLAG_OFFSET = 15;
44
45         @Override
46         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
47                 if (bytes == null || bytes.length == 0)
48                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
49
50                 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(bytes, this.FLAGS_F_OFFSET, this.FLAGS_F_LENGTH));
51
52                 final List<ExcludeRouteSubobject> subobjects = PCEPXROSubobjectParser.parse(ByteArray.cutBytes(bytes, this.SO_F_OFFSET));
53                 if (subobjects.isEmpty())
54                         throw new PCEPDeserializerException("Empty Exclude Route Object.");
55
56                 return new PCEPExcludeRouteObject(subobjects, flags.get(this.F_FLAG_OFFSET), processed, ignored);
57         }
58
59         @Override
60         public byte[] put(PCEPObject obj) {
61                 if (!(obj instanceof PCEPExcludeRouteObject))
62                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPExcludeRouteObject.");
63
64                 assert !(((PCEPExcludeRouteObject) obj).getSubobjects().isEmpty()) : "Empty Exclude Route Object.";
65
66                 final byte[] subObjsBytes = PCEPXROSubobjectParser.put(((PCEPExcludeRouteObject) obj).getSubobjects());
67                 final byte[] retBytes = new byte[this.SO_F_OFFSET + subObjsBytes.length];
68                 final BitSet flags = new BitSet(this.FLAGS_F_LENGTH * Byte.SIZE);
69                 flags.set(this.F_FLAG_OFFSET, ((PCEPExcludeRouteObject) obj).isFail());
70                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, this.FLAGS_F_LENGTH), retBytes, this.FLAGS_F_OFFSET);
71                 ByteArray.copyWhole(subObjsBytes, retBytes, this.SO_F_OFFSET);
72
73                 return retBytes;
74         }
75
76 }