X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=pcep%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fpcep%2Fimpl%2Fsubobject%2FEROExplicitExclusionRouteSubobjectParser.java;h=682a5f87e630da8db6e92abbdb832af4b6f096b4;hb=bb1a5b0139b814666b85de460a89604bea5db0ec;hp=5143844d91faf4d98a1114bb806fc9dd577b67f7;hpb=24ff14a7c06ae51089a2a38e587933330a6782b1;p=bgpcep.git diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java index 5143844d91..682a5f87e6 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java @@ -7,20 +7,25 @@ */ package org.opendaylight.protocol.pcep.impl.subobject; +import io.netty.buffer.ByteBuf; + +import java.util.ArrayList; import java.util.List; -import org.opendaylight.protocol.pcep.PCEPDeserializerException; +import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil; import org.opendaylight.protocol.pcep.spi.EROSubobjectParser; import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer; -import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry; -import org.opendaylight.protocol.pcep.spi.XROSubobjectParser; -import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer; +import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; +import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry; import org.opendaylight.protocol.util.ByteArray; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.Exrs; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs.ExrsBuilder; - +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.Exrs; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder; + +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.primitives.UnsignedBytes; @@ -28,119 +33,78 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar public static final int TYPE = 33; - private static final int SUB_TYPE_FLAG_F_LENGTH = 1; - private static final int SUB_LENGTH_F_LENGTH = 1; - private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH; - - private static final int TYPE_FLAG_F_OFFSET = 0; - private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH; - private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH; + private static final int HEADER_LENGTH = 2; - private final XROSubobjectHandlerRegistry registry; + private final XROSubobjectRegistry registry; - public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectHandlerRegistry registry) { + public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) { this.registry = registry; } @Override - public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException { - if (buffer == null || buffer.length == 0) { - throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty."); - } - final SubobjectsBuilder builder = new SubobjectsBuilder(); + public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + final SubobjectBuilder builder = new SubobjectBuilder(); builder.setLoose(loose); - final List list = parseSubobjects(buffer); - final List exrss = Lists.newArrayList(); - for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects s : list) { - final ExrsBuilder b = new ExrsBuilder(); + final List list = parseSubobject(buffer); + final List exrss = Lists.newArrayList(); + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject s : list) { + final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder(); b.setAttribute(s.getAttribute()); b.setMandatory(s.isMandatory()); b.setSubobjectType(s.getSubobjectType()); exrss.add(b.build()); } - builder.setSubobjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsBuilder().setExrs( - exrss).build()); + builder.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(exrss).build()).build()); return builder.build(); } @Override - public byte[] serializeSubobject(final Subobjects subobject) { - if (!(subobject.getSubobjectType() instanceof Exrs)) { + public byte[] serializeSubobject(final Subobject subobject) { + if (!(subobject.getSubobjectType() instanceof ExrsCase)) { throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass() + ". Needed Exrs."); } - final Exrs e = (Exrs) subobject.getSubobjectType(); - final List list = Lists.newArrayList(); - for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs.Exrs ex : e.getExrs()) { - final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder(); + final Exrs e = ((ExrsCase) subobject.getSubobjectType()).getExrs(); + final List list = new ArrayList<>(); + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs ex : e.getExrs()) { + final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder(); b.setAttribute(ex.getAttribute()); b.setMandatory(ex.isMandatory()); b.setSubobjectType(ex.getSubobjectType()); list.add(b.build()); } - return serializeSubobject(list); + return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), serializeSubobject(list)); } - private List parseSubobjects( - final byte[] bytes) throws PCEPDeserializerException { - if (bytes == null) { - throw new IllegalArgumentException("Byte array is mandatory."); - } - - int type; - - byte[] soContentsBytes; - int length; - int offset = 0; - - final List subs = Lists.newArrayList(); - - while (offset < bytes.length) { - - length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH)); - - final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false; - type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7); - if (length > bytes.length - offset) { + private List parseSubobject( + final ByteBuf buffer) throws PCEPDeserializerException { + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); + final List subs = new ArrayList<>(); + while (buffer.isReadable()) { + final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false; + int type = (buffer.readByte() & 0xff) & ~(1 << 7); + int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH; + if (length > buffer.readableBytes()) { throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " - + (bytes.length - offset)); + + buffer.readableBytes()); } - - soContentsBytes = new byte[length - SO_CONTENTS_OFFSET]; - System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET); - - final XROSubobjectParser parser = this.registry.getSubobjectParser(type); - - subs.add(parser.parseSubobject(soContentsBytes, mandatory)); - - offset += length; + //FIXME: switch to ByteBuf + subs.add(this.registry.parseSubobject(type, ByteArray.readBytes(buffer, length), mandatory)); } return subs; } private byte[] serializeSubobject( - final List subobjects) { + final List subobjects) { final List result = Lists.newArrayList(); int finalLength = 0; - for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects subobject : subobjects) { - - final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType()); - - final byte[] valueBytes = serializer.serializeSubobject(subobject); - - final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length]; - - final byte typeBytes = (byte) (UnsignedBytes.checkedCast(serializer.getType()) | (subobject.isMandatory() ? 1 << 7 : 0)); - - final byte lengthBytes = UnsignedBytes.checkedCast(valueBytes.length); - - bytes[0] = typeBytes; - bytes[1] = lengthBytes; - System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length); + for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) { + final byte[] bytes = this.registry.serializeSubobject(subobject); finalLength += bytes.length; result.add(bytes); } @@ -153,9 +117,4 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar } return resultBytes; } - - @Override - public int getType() { - return TYPE; - } }