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