e1781ede930ad915b9b741b35e9d3762b3d8357c
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.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.EnterpriseSpecificInformationParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectParser;
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.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObjectBuilder;
28
29 public abstract class AbstractVendorInformationObjectParser implements ObjectSerializer, ObjectParser, EnterpriseSpecificInformationParser {
30
31     @Override
32     public final Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
33         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
34         final VendorInformationObjectBuilder builder = new VendorInformationObjectBuilder();
35         builder.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
36         builder.setEnterpriseSpecificInformation(parseEnterpriseSpecificInformation(buffer));
37         return builder.build();
38     }
39
40     @Override
41     public final void serializeObject(final Object object, final ByteBuf buffer) {
42         Preconditions.checkArgument(object instanceof VendorInformationObject, "Wrong instance of PCEPObject. Passed %s. Needed VendorInformationObject.", object.getClass());
43         final ByteBuf body = Unpooled.buffer();
44         writeUnsignedInt(getEnterpriseNumber().getValue(), body);
45         serializeEnterpriseSpecificInformation(((VendorInformationObject) object).getEnterpriseSpecificInformation(), body);
46         ObjectUtil.formatSubobject(VENDOR_INFORMATION_OBJECT_TYPE, VENDOR_INFORMATION_OBJECT_CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
47     }
48
49 }