*/
package org.opendaylight.protocol.pcep.impl.object;
-import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.pcep.spi.ObjectParser;
+import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.PCEPErrors;
-import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
import org.opendaylight.protocol.pcep.spi.UnknownObject;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
/**
* Parser for IPv6 {@link EndpointsObj}
*/
-public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
-
- private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
-
- public static final int CLASS = 4;
- public static final int TYPE = 2;
-
- /*
- * fields lengths and offsets for IPv4 in bytes
- */
- private static final int SRC6_F_LENGTH = 16;
- private static final int DEST6_F_LENGTH = 16;
-
- private static final int SRC6_F_OFFSET = 0;
- private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
-
- public PCEPEndPointsIpv6ObjectParser(final TlvHandlerRegistry tlvReg) {
- super(tlvReg);
- }
-
- @Override
- public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
- if (bytes == null) {
- throw new IllegalArgumentException("Array of bytes is mandatory");
- }
- final EndpointsObjBuilder builder = new EndpointsObjBuilder();
- builder.setIgnore(header.isIgnore());
- builder.setProcessingRule(header.isProcessingRule());
- if (!builder.isProcessingRule()) {
- LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
- return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
- }
- if (bytes.length != SRC6_F_LENGTH + DEST6_F_LENGTH) {
- throw new PCEPDeserializerException("Wrong length of array of bytes.");
- }
- final Ipv6Builder b = new Ipv6Builder();
- b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
- b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
- builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
- return builder.build();
- }
-
- @Override
- public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
- // No tlvs defined
- }
+public class PCEPEndPointsIpv6ObjectParser implements ObjectParser, ObjectSerializer {
- @Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof EndpointsObj)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
- }
- final EndpointsObj ePObj = (EndpointsObj) object;
+ private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv6ObjectParser.class);
- final AddressFamily afi = ePObj.getAddressFamily();
+ public static final int CLASS = 4;
- if (!(afi instanceof Ipv6Case)) {
- throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
- }
- final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
- ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
- ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
- }
+ public static final int TYPE = 2;
- @Override
- public int getObjectType() {
- return TYPE;
- }
+ @Override
+ public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
+ Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ final EndpointsObjBuilder builder = new EndpointsObjBuilder();
+ if (!header.isProcessingRule()) {
+ LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
+ return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
+ }
+ if (bytes.readableBytes() != Ipv6Util.IPV6_LENGTH * 2) {
+ throw new PCEPDeserializerException("Wrong length of array of bytes.");
+ }
+ builder.setIgnore(header.isIgnore());
+ builder.setProcessingRule(header.isProcessingRule());
+ final Ipv6Builder b = new Ipv6Builder();
+ b.setSourceIpv6Address(Ipv6Util.addressForByteBuf(bytes));
+ b.setDestinationIpv6Address(Ipv6Util.addressForByteBuf(bytes));
+ builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
+ return builder.build();
+ }
- @Override
- public int getObjectClass() {
- return CLASS;
- }
+ @Override
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
+ final EndpointsObj ePObj = (EndpointsObj) object;
+ final AddressFamily afi = ePObj.getAddressFamily();
+ Preconditions.checkArgument(afi instanceof Ipv6Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv6", afi.getClass());
+ final ByteBuf body = Unpooled.buffer(Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_LENGTH);
+ final Ipv6 ipv6 = ((Ipv6Case) afi).getIpv6();
+ Preconditions.checkArgument(ipv6.getSourceIpv6Address() != null, "SourceIpv6Address is mandatory.");
+ writeIpv6Address(ipv6.getSourceIpv6Address(), body);
+ Preconditions.checkArgument(ipv6.getDestinationIpv6Address() != null, "DestinationIpv6Address is mandatory.");
+ writeIpv6Address(ipv6.getDestinationIpv6Address(), body);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+ }
}