Merge "BUG-1287: migrate tests to new APIs"
[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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedBytes;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.Gc;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.GcBuilder;
24
25 /**
26  * Parser for {@link Gc}
27  */
28 public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsParser<GcBuilder> {
29
30     public static final int CLASS = 24;
31
32     public static final int TYPE = 1;
33
34     public PCEPGlobalConstraintsObjectParser(final TlvRegistry tlvReg) {
35         super(tlvReg);
36     }
37
38     @Override
39     public Gc parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
40         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
41         final GcBuilder builder = new GcBuilder();
42
43         builder.setIgnore(header.isIgnore());
44         builder.setProcessingRule(header.isProcessingRule());
45
46         builder.setMaxHop((short) UnsignedBytes.toInt(bytes.readByte()));
47         builder.setMaxUtilization((short) UnsignedBytes.toInt(bytes.readByte()));
48         builder.setMinUtilization((short) UnsignedBytes.toInt(bytes.readByte()));
49         builder.setOverBookingFactor((short) UnsignedBytes.toInt(bytes.readByte()));
50         return builder.build();
51     }
52
53     @Override
54     public void serializeObject(final Object object, final ByteBuf buffer) {
55         Preconditions.checkArgument(object instanceof Gc, "Wrong instance of PCEPObject. Passed %s. Needed GcObject.", object.getClass());
56         final Gc specObj = (Gc) object;
57         final ByteBuf body = Unpooled.buffer();
58         writeUnsignedByte(specObj.getMaxHop(), body);
59         writeUnsignedByte(specObj.getMaxUtilization(), body);
60         writeUnsignedByte(specObj.getMinUtilization(), body);
61         writeUnsignedByte(specObj.getOverBookingFactor(), body);
62         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
63     }
64 }