Remove superfluous constants
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPOverloadObjectParser.java
1 /*
2  * Copyright (c) 2014 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.parser.object;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.util.ByteBufUtils;
18 import org.opendaylight.protocol.util.ByteBufWriteUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.object.Overload;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.object.OverloadBuilder;
23
24 public class PCEPOverloadObjectParser extends CommonObjectParser implements ObjectSerializer {
25
26     private static final int CLASS = 27;
27     private static final int TYPE = 1;
28     private static final int RESERVED = 1;
29     private static final int FLAGS = RESERVED;
30     private static final int BODY_SIZE = RESERVED + FLAGS + Short.BYTES;
31
32     public PCEPOverloadObjectParser() {
33         super(CLASS, TYPE);
34     }
35
36     @Override
37     public void serializeObject(final Object object, final ByteBuf buffer) {
38         Preconditions.checkArgument(object instanceof Overload,
39             "Wrong instance of PCEPObject. Passed %s. Needed OverloadObject.", object.getClass());
40         final Overload overload = (Overload) object;
41         final ByteBuf body = Unpooled.buffer(BODY_SIZE);
42         body.writeZero(RESERVED + FLAGS);
43         ByteBufWriteUtil.writeUnsignedShort(overload.getDuration(), body);
44         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
45     }
46
47     @Override
48     public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
49         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
50             "Array of bytes is mandatory. Can't be null or empty.");
51         final OverloadBuilder builder = new OverloadBuilder();
52         buffer.readBytes(RESERVED + FLAGS);
53         builder.setDuration(ByteBufUtils.readUint16(buffer));
54         return builder.build();
55     }
56 }