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