Migrate pcep-base-parser to use ByteBufUtils
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / AbstractVendorInformationObjectParser.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 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS;
12 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
17 import org.opendaylight.protocol.pcep.spi.EnterpriseSpecificInformationParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObjectBuilder;
26 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
27
28 public abstract class AbstractVendorInformationObjectParser extends CommonObjectParser
29         implements ObjectSerializer, EnterpriseSpecificInformationParser {
30     public AbstractVendorInformationObjectParser(final int objectClass, final int objectType) {
31         super(objectClass, objectType);
32     }
33
34     @Override
35     public final Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
36         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
37         return new VendorInformationObjectBuilder()
38                 .setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()))
39                 .setEnterpriseSpecificInformation(parseEnterpriseSpecificInformation(buffer))
40                 .build();
41     }
42
43     @Override
44     public final void serializeObject(final Object object, final ByteBuf buffer) {
45         checkArgument(object instanceof VendorInformationObject,
46                 "Wrong instance of PCEPObject. Passed %s. Needed VendorInformationObject.", object.getClass());
47         final ByteBuf body = Unpooled.buffer();
48         ByteBufUtils.write(body, getEnterpriseNumber().getValue());
49         serializeEnterpriseSpecificInformation(((VendorInformationObject) object).getEnterpriseSpecificInformation(),
50                 body);
51         ObjectUtil.formatSubobject(VENDOR_INFORMATION_OBJECT_TYPE, VENDOR_INFORMATION_OBJECT_CLASS,
52                 object.isProcessingRule(), object.isIgnore(), body, buffer);
53     }
54
55 }