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