BUG-50 : added test for XRO object.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPLspaObjectParser.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.LspaObject;
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.lsp.attributes.LspaBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AttributeFilter;
22
23 import com.google.common.primitives.UnsignedBytes;
24
25 /**
26  * Parser for {@link LspaObject}
27  */
28 public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<LspaBuilder> {
29
30         public static final int CLASS = 9;
31
32         public static final int TYPE = 1;
33
34         /*
35          * lengths of fields in bytes
36          */
37         private static final int EXC_ANY_F_LENGTH = 4;
38         private static final int INC_ANY_F_LENGTH = 4;
39         private static final int INC_ALL_F_LENGTH = 4;
40         private static final int SET_PRIO_F_LENGTH = 1;
41         private static final int HOLD_PRIO_F_LENGTH = 1;
42         private static final int FLAGS_F_LENGTH = 1;
43
44         /*
45          * offsets of flags inside flags field in bits
46          */
47         private static final int L_FLAG_OFFSET = 7;
48
49         /*
50          * offsets of fields in bytes
51          */
52         private static final int EXC_ANY_F_OFFSET = 0;
53         private static final int INC_ANY_F_OFFSET = EXC_ANY_F_OFFSET + EXC_ANY_F_LENGTH;
54         private static final int INC_ALL_F_OFFSET = INC_ANY_F_OFFSET + INC_ANY_F_LENGTH;
55         private static final int SET_PRIO_F_OFFSET = INC_ALL_F_OFFSET + INC_ALL_F_LENGTH;
56         private static final int HOLD_PRIO_F_OFFSET = SET_PRIO_F_OFFSET + SET_PRIO_F_LENGTH;
57         private static final int FLAGS_F_OFFSET = HOLD_PRIO_F_OFFSET + HOLD_PRIO_F_LENGTH;
58         private static final int TLVS_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH + 1; // added reserved field of length 1B
59
60         public PCEPLspaObjectParser(final TlvHandlerRegistry tlvReg) {
61                 super(tlvReg);
62         }
63
64         @Override
65         public LspaObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
66                 if (bytes == null) {
67                         throw new IllegalArgumentException("Bytes array is mandatory.");
68                 }
69                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
70
71                 final LspaBuilder builder = new LspaBuilder();
72                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_F_OFFSET));
73
74                 builder.setIgnore(header.isIgnore());
75                 builder.setProcessingRule(header.isProcessingRule());
76
77                 builder.setHoldPriority((short) UnsignedBytes.toInt(bytes[HOLD_PRIO_F_OFFSET]));
78                 builder.setSetupPriority((short) UnsignedBytes.toInt(bytes[SET_PRIO_F_OFFSET]));
79                 builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
80                 builder.setExcludeAny(new AttributeFilter(ByteArray.bytesToLong(ByteArray.subByte(bytes, EXC_ANY_F_OFFSET, EXC_ANY_F_LENGTH))));
81                 builder.setIncludeAll(new AttributeFilter(ByteArray.bytesToLong(ByteArray.subByte(bytes, INC_ALL_F_OFFSET, INC_ALL_F_LENGTH))));
82                 builder.setIncludeAny(new AttributeFilter(ByteArray.bytesToLong(ByteArray.subByte(bytes, INC_ANY_F_OFFSET, INC_ANY_F_LENGTH))));
83                 return builder.build();
84         }
85
86         @Override
87         public void addTlv(final LspaBuilder builder, final Tlv tlv) {
88                 // No tlvs defined
89         }
90
91         @Override
92         public byte[] serializeObject(final Object object) {
93                 if (!(object instanceof LspaObject)) {
94                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspaObject.");
95                 }
96                 final LspaObject lspaObj = (LspaObject) object;
97
98                 final byte[] retBytes = new byte[TLVS_F_OFFSET];
99
100                 System.arraycopy(ByteArray.longToBytes(lspaObj.getExcludeAny().getValue()), 4, retBytes, EXC_ANY_F_OFFSET, EXC_ANY_F_LENGTH);
101                 System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAny().getValue()), 4, retBytes, INC_ANY_F_OFFSET, INC_ANY_F_LENGTH);
102                 System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAll().getValue()), 4, retBytes, INC_ALL_F_OFFSET, INC_ALL_F_LENGTH);
103                 retBytes[SET_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getSetupPriority());
104                 retBytes[HOLD_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getHoldPriority());
105
106                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
107                 flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
108                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
109                 return retBytes;
110         }
111
112         @Override
113         public int getObjectType() {
114                 return TYPE;
115         }
116
117         @Override
118         public int getObjectClass() {
119                 return CLASS;
120         }
121 }