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