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