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