BUG-50 : added test for XRO object.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPGlobalConstraintsObjectParser.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 org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
12 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.GcObject;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.GcBuilder;
18
19 import com.google.common.primitives.UnsignedBytes;
20
21 /**
22  * Parser for {@link GcObject}
23  */
24 public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsParser<GcBuilder> {
25
26         public static final int CLASS = 24;
27
28         public static final int TYPE = 1;
29
30         private static final int MAX_HOP_F_LENGTH = 1;
31         private static final int MAX_UTIL_F_LENGTH = 1;
32         private static final int MIN_UTIL_F_LENGTH = 1;
33         private static final int OVER_BOOKING_FACTOR_F_LENGTH = 1;
34
35         private static final int MAX_HOP_F_OFFSET = 0;
36         private static final int MAX_UTIL_F_OFFSET = MAX_HOP_F_OFFSET + MAX_HOP_F_LENGTH;
37         private static final int MIN_UTIL_F_OFFSET = MAX_UTIL_F_OFFSET + MAX_UTIL_F_LENGTH;
38         private static final int OVER_BOOKING_FACTOR_F_OFFSET = MIN_UTIL_F_OFFSET + MIN_UTIL_F_LENGTH;
39
40         private static final int TLVS_OFFSET = OVER_BOOKING_FACTOR_F_OFFSET + OVER_BOOKING_FACTOR_F_LENGTH;
41
42         public PCEPGlobalConstraintsObjectParser(final TlvHandlerRegistry tlvReg) {
43                 super(tlvReg);
44         }
45
46         @Override
47         public GcObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
48                 if (bytes == null || bytes.length == 0) {
49                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
50                 }
51                 final GcBuilder builder = new GcBuilder();
52
53                 builder.setIgnore(header.isIgnore());
54                 builder.setProcessingRule(header.isProcessingRule());
55
56                 builder.setMaxHop((short) UnsignedBytes.toInt(bytes[MAX_HOP_F_OFFSET]));
57                 builder.setMinUtilization((short) UnsignedBytes.toInt(bytes[MIN_UTIL_F_OFFSET]));
58                 builder.setMaxUtilization((short) UnsignedBytes.toInt(bytes[MAX_UTIL_F_OFFSET]));
59                 builder.setOverBookingFactor((short) UnsignedBytes.toInt(bytes[OVER_BOOKING_FACTOR_F_OFFSET]));
60                 return builder.build();
61         }
62
63         @Override
64         public void addTlv(final GcBuilder builder, final Tlv tlv) {
65                 // No tlvs defined
66         }
67
68         @Override
69         public byte[] serializeObject(final Object object) {
70                 if (!(object instanceof GcObject)) {
71                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed GcObject.");
72                 }
73                 final GcObject specObj = (GcObject) object;
74                 final byte[] retBytes = new byte[TLVS_OFFSET + 0];
75                 retBytes[MAX_HOP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxHop());
76                 retBytes[MAX_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxUtilization());
77                 retBytes[MIN_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMinUtilization());
78                 retBytes[OVER_BOOKING_FACTOR_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getOverBookingFactor());
79                 return retBytes;
80         }
81
82         @Override
83         public int getObjectType() {
84                 return TYPE;
85         }
86
87         @Override
88         public int getObjectClass() {
89                 return CLASS;
90         }
91 }