*/
package org.opendaylight.protocol.pcep.crabbe.initiated00;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcinitiate)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Needed PcinitiateMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcinitiate, "Wrong instance of Message. Passed instance of %s. Need Pcinitiate.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.pcinitiate.message.PcinitiateMessage init = ((Pcinitiate) message).getPcinitiateMessage();
ByteBuf buffer = Unpooled.buffer();
for (final Requests req : init.getRequests()) {
- buffer.writeBytes(serializeObject(req.getEndpointsObj()));
- buffer.writeBytes(serializeObject(req.getLspa()));
+ serializeObject(req.getEndpointsObj(), buffer);
+ serializeObject(req.getLspa(), buffer);
if (req.getEro() != null) {
- buffer.writeBytes(serializeObject(req.getEro()));
+ serializeObject(req.getEro(), buffer);
}
if (req.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(req.getBandwidth()));
+ serializeObject(req.getBandwidth(), buffer);
}
if (req.getMetrics() != null && !req.getMetrics().isEmpty()) {
for (final Metrics m : req.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
public static final int TYPE = 1;
/*
- * offset of TLVs offset of other fields are not defined as constants
- * because of non-standard mapping of bits
- */
- private static final int TLVS_OFFSET = 4;
-
- /*
- * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
+ * first 4b are restricted
*/
private static final int DELEGATE_FLAG_OFFSET = 15;
private static final int SYNC_FLAG_OFFSET = 14;
- private static final int REMOVE_FLAG_OFFSET = 12;
private static final int OPERATIONAL_FLAG_OFFSET = 13;
+ private static final int REMOVE_FLAG_OFFSET = 12;
public Stateful02LspObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
final LspBuilder builder = new LspBuilder();
builder.setIgnore(header.isIgnore());
builder.setProcessingRule(header.isProcessingRule());
- int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), };
+ int[] plspIdRaw = { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2) };
builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << 12) | (plspIdRaw[1] << 4) | (plspIdRaw[2] >> 4))));
final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, 2));
builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Lsp)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s. Needed LspObject.", object.getClass());
final Lsp specObj = (Lsp) object;
+ final ByteBuf body = Unpooled.buffer();
+ final PlspId plsp = specObj.getPlspId();
+ Preconditions.checkArgument(plsp != null, "PLSP-ID not present");
+ body.writeMedium(plsp.getValue().intValue() << 4);
- final byte[] tlvs = serializeTlvs(specObj.getTlvs());
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
- final int lspID = specObj.getPlspId().getValue().intValue();
- retBytes[0] = (byte) (lspID >> 12);
- retBytes[1] = (byte) (lspID >> 4);
- retBytes[2] = (byte) (lspID << 4);
+ BitSet flags = new BitSet(2 * Byte.SIZE);
if (specObj.isDelegate() != null && specObj.isDelegate()) {
- retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
+ flags.set(DELEGATE_FLAG_OFFSET);
}
if (specObj.isRemove() != null && specObj.isRemove()) {
- retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
+ flags.set(REMOVE_FLAG_OFFSET);
}
if (specObj.isSync() != null && specObj.isSync()) {
- retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
+ flags.set(SYNC_FLAG_OFFSET);
}
if (specObj.isOperational() != null && specObj.isOperational()) {
- retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
+ flags.set(OPERATIONAL_FLAG_OFFSET);
}
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ body.writeByte(ByteArray.bitSetToBytes(flags, 2)[1]);
+ //FIXME: switch to ByteBuf
+ final byte[] tlvs = serializeTlvs(specObj.getTlvs());
+ body.writeBytes(tlvs);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcrep)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Nedded PcrepMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
throw new IllegalArgumentException("Replies cannot be null or empty.");
if (reply.getRp() == null) {
throw new IllegalArgumentException("Reply must contain RP object.");
}
- buffer.writeBytes(serializeObject(reply.getRp()));
+ serializeObject(reply.getRp(), buffer);
if (reply.getAugmentation(Replies1.class) != null && reply.getAugmentation(Replies1.class).getLsp() != null) {
- buffer.writeBytes(serializeObject(reply.getAugmentation(Replies1.class).getLsp()));
+ serializeObject(reply.getAugmentation(Replies1.class).getLsp(), buffer);
}
if (reply.getResult() != null) {
if (reply.getResult() instanceof FailureCase) {
final FailureCase f = ((FailureCase) reply.getResult());
- buffer.writeBytes(serializeObject(f.getNoPath()));
+ serializeObject(f.getNoPath(), buffer);
if (f.getLspa() != null) {
- buffer.writeBytes(serializeObject(f.getLspa()));
+ serializeObject(f.getLspa(), buffer);
}
if (f.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(f.getBandwidth()));
+ serializeObject(f.getBandwidth(), buffer);
}
if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
for (final Metrics m : f.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (f.getIro() != null) {
- buffer.writeBytes(serializeObject(f.getIro()));
+ serializeObject(f.getIro(), buffer);
}
} else {
final SuccessCase s = (SuccessCase) reply.getResult();
for (final Paths p : s.getSuccess().getPaths()) {
- buffer.writeBytes(serializeObject(p.getEro()));
+ serializeObject(p.getEro(), buffer);
if (p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p.getLspa()));
+ serializeObject(p.getLspa(), buffer);
}
if (p.getOf() != null) {
- buffer.writeBytes(serializeObject(p.getOf()));
+ serializeObject(p.getOf(), buffer);
}
if (p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p.getBandwidth()));
+ serializeObject(p.getBandwidth(), buffer);
}
if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
for (final Metrics m : p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p.getIro() != null) {
- buffer.writeBytes(serializeObject(p.getIro()));
+ serializeObject(p.getIro(), buffer);
}
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcrpt)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Nedded PcrptMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcrpt, "Wrong instance of Message. Passed instance of %s. Need Pcrpt.", message.getClass());
final Pcrpt msg = (Pcrpt) message;
final List<Reports> reports = msg.getPcrptMessage().getReports();
ByteBuf buffer = Unpooled.buffer();
for (final Reports report : reports) {
- buffer.writeBytes(serializeObject(report.getLsp()));
+ serializeObject(report.getLsp(), buffer);
final Path p = report.getPath();
if (p != null) {
if (p.getEro() != null) {
- buffer.writeBytes(serializeObject(p.getEro()));
+ serializeObject(p.getEro(), buffer);
}
if (p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p.getLspa()));
+ serializeObject(p.getLspa(), buffer);
}
if (p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p.getBandwidth()));
+ serializeObject(p.getBandwidth(), buffer);
}
if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
for (final Metrics m : p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p.getIro() != null) {
- buffer.writeBytes(serializeObject(p.getRro()));
+ serializeObject(p.getRro(), buffer);
}
}
}
@Override
protected void serializeP2P(final ByteBuf buffer, final P2p p2p) {
if (p2p.getEndpointsObj() != null) {
- buffer.writeBytes(serializeObject(p2p.getEndpointsObj()));
+ serializeObject(p2p.getEndpointsObj(), buffer);
}
if (p2p.getAugmentation(P2p1.class) != null && p2p.getAugmentation(P2p1.class).getLsp() != null) {
- buffer.writeBytes(serializeObject(p2p.getAugmentation(P2p1.class).getLsp()));
+ serializeObject(p2p.getAugmentation(P2p1.class).getLsp(), buffer);
}
if (p2p.getReportedRoute() != null) {
final ReportedRoute rr = p2p.getReportedRoute();
if (rr.getRro() != null) {
- buffer.writeBytes(serializeObject(rr.getRro()));
+ serializeObject(rr.getRro(), buffer);
}
if (rr.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(rr.getBandwidth()));
+ serializeObject(rr.getBandwidth(), buffer);
}
}
if (p2p.getLoadBalancing() != null) {
- buffer.writeBytes(serializeObject(p2p.getLoadBalancing()));
+ serializeObject(p2p.getLoadBalancing(), buffer);
}
if (p2p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p2p.getLspa()));
+ serializeObject(p2p.getLspa(), buffer);
}
if (p2p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p2p.getBandwidth()));
+ serializeObject(p2p.getBandwidth(), buffer);
}
if (p2p.getMetrics() != null) {
for (final Metrics m : p2p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p2p.getIro() != null) {
- buffer.writeBytes(serializeObject(p2p.getIro()));
+ serializeObject(p2p.getIro(), buffer);
}
if (p2p.getRro() != null) {
- buffer.writeBytes(serializeObject(p2p.getRro()));
+ serializeObject(p2p.getRro(), buffer);
}
if (p2p.getXro() != null) {
- buffer.writeBytes(serializeObject(p2p.getXro()));
+ serializeObject(p2p.getXro(), buffer);
}
if (p2p.getOf() != null) {
- buffer.writeBytes(serializeObject(p2p.getOf()));
+ serializeObject(p2p.getOf(), buffer);
}
if (p2p.getClassType() != null) {
- buffer.writeBytes(serializeObject(p2p.getClassType()));
+ serializeObject(p2p.getClassType(), buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcupd)) {
- throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
- + ". Nedded PcupdMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
final Pcupd msg = (Pcupd) message;
final List<Updates> updates = msg.getPcupdMessage().getUpdates();
ByteBuf buffer = Unpooled.buffer();
for (final Updates update : updates) {
- buffer.writeBytes(serializeObject(update.getLsp()));
+ serializeObject(update.getLsp(), buffer);
final Path p = update.getPath();
if (p != null) {
- buffer.writeBytes(serializeObject(p.getEro()));
+ serializeObject(p.getEro(), buffer);
if (p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p.getLspa()));
+ serializeObject(p.getLspa(), buffer);
}
if (p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p.getBandwidth()));
+ serializeObject(p.getBandwidth(), buffer);
}
if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
for (final Metrics m : p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p.getIro() != null) {
- buffer.writeBytes(serializeObject(p.getIro()));
+ serializeObject(p.getIro(), buffer);
}
}
}
Tlvs2.class, statBuilder.build()).addAugmentation(Tlvs1.class, cleanupBuilder.build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2.class).getSymbolicPathName());
// assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
// 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.TlvsBuilder().setSymbolicPathName(
tlv2).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Lsp)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s. Needed LspObject.", object.getClass());
final Lsp specObj = (Lsp) object;
+ final ByteBuf body = Unpooled.buffer();
- final byte[] tlvs = serializeTlvs(specObj.getTlvs());
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
+ final byte[] retBytes = new byte[4];
+ Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
final int lspID = specObj.getPlspId().getValue().intValue();
retBytes[0] = (byte) (lspID >> 12);
retBytes[1] = (byte) (lspID >> 4);
final int op = specObj.getOperational().getIntValue();
retBytes[3] |= (op & 7) << 4;
}
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ body.writeBytes(retBytes);
+ // FIXME: switch to ByteBuf
+ final byte[] tlvs = serializeTlvs(specObj.getTlvs());
+ body.writeBytes(tlvs);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.initiated00;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof PcinitiateMessage)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Needed PcinitiateMessage.");
- }
+ Preconditions.checkArgument(message instanceof PcinitiateMessage, "Wrong instance of Message. Passed instance of %s. Need PcinitiateMessage.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.PcinitiateMessage init = ((Pcinitiate) message).getPcinitiateMessage();
ByteBuf buffer = Unpooled.buffer();
for (final Requests req : init.getRequests()) {
- buffer.writeBytes(serializeObject(req.getSrp()));
- buffer.writeBytes(serializeObject(req.getLsp()));
+ serializeObject(req.getSrp(), buffer);
+ serializeObject(req.getLsp(), buffer);
if (req.getEndpointsObj() != null) {
- buffer.writeBytes(serializeObject(req.getEndpointsObj()));
+ serializeObject(req.getEndpointsObj(), buffer);
}
if (req.getEro() != null) {
- buffer.writeBytes(serializeObject(req.getEro()));
+ serializeObject(req.getEro(), buffer);
}
if (req.getLspa() != null) {
- buffer.writeBytes(serializeObject(req.getLspa()));
+ serializeObject(req.getLspa(), buffer);
}
if (req.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(req.getBandwidth()));
+ serializeObject(req.getBandwidth(), buffer);
}
if (req.getMetrics() != null && !req.getMetrics().isEmpty()) {
for (final Metrics m : req.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (req.getIro() != null) {
- buffer.writeBytes(serializeObject(req.getIro()));
+ serializeObject(req.getIro(), buffer);
}
}
MessageUtil.formatMessage(TYPE, buffer, out);
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Srp)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SrpObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s. Needed SrpObject.", object.getClass());
final Srp srp = (Srp) object;
+ ByteBuf body = Unpooled.buffer();
+ //FIXME: switch to ByteBuf
final byte[] tlvs = serializeTlvs(srp.getTlvs());
- final Long id = srp.getOperationId().getValue();
-
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
- if (tlvs != null) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- }
final BitSet flags = new BitSet(FLAGS_SIZE * Byte.SIZE);
if (srp.getAugmentation(Srp1.class) != null && srp.getAugmentation(Srp1.class).isRemove()) {
flags.set(REMOVE_FLAG, srp.getAugmentation(Srp1.class).isRemove());
}
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_SIZE), retBytes, 0);
-
- System.arraycopy(ByteArray.intToBytes(id.intValue(), SRP_ID_SIZE), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_SIZE));
+ body.writeInt(srp.getOperationId().getValue().intValue());
+ body.writeBytes(tlvs);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
if (err.getErrorType() instanceof RequestCase) {
final List<Rps> rps = ((RequestCase) err.getErrorType()).getRequest().getRps();
for (final Rps r : rps) {
- buffer.writeBytes(serializeObject(r.getRp()));
+ serializeObject(r.getRp(), buffer);
}
}
-
if (err.getErrorType() instanceof StatefulCase) {
final List<Srps> srps = ((StatefulCase) err.getErrorType()).getStateful().getSrps();
for (final Srps s : srps) {
- buffer.writeBytes(serializeObject(s.getSrp()));
+ serializeObject(s.getSrp(), buffer);
}
}
-
for (final Errors e : err.getErrors()) {
- buffer.writeBytes(serializeObject(e.getErrorObject()));
+ serializeObject(e.getErrorObject(), buffer);
}
-
if (err.getErrorType() instanceof SessionCase) {
- buffer.writeBytes(serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen()));
+ serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen(), buffer);
}
MessageUtil.formatMessage(TYPE, buffer, out);
}
package org.opendaylight.protocol.pcep.ietf.stateful07;
import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+
import java.util.BitSet;
+
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Lsp)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s . Needed LspObject.", object.getClass());
final Lsp specObj = (Lsp) object;
+ final ByteBuf body = Unpooled.buffer();
- final byte[] tlvs = serializeTlvs(specObj.getTlvs());
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
+ final byte[] retBytes = new byte[4];
Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
final int lspID = specObj.getPlspId().getValue().intValue();
final int op = specObj.getOperational().getIntValue();
retBytes[3] |= (op & 7) << 4;
}
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ body.writeBytes(retBytes);
+ // FIXME: switch to ByteBuf
+ final byte[] tlvs = serializeTlvs(specObj.getTlvs());
+ body.writeBytes(tlvs);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcrpt)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Nedded PcrptMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcrpt, "Wrong instance of Message. Passed instance of %s. Need Pcrpt.", message.getClass());
final Pcrpt msg = (Pcrpt) message;
final List<Reports> reports = msg.getPcrptMessage().getReports();
ByteBuf buffer = Unpooled.buffer();
for (final Reports report : reports) {
if (report.getSrp() != null) {
- buffer.writeBytes(serializeObject(report.getSrp()));
+ serializeObject(report.getSrp(), buffer);
}
- buffer.writeBytes(serializeObject(report.getLsp()));
+ serializeObject(report.getLsp(), buffer);
final Path p = report.getPath();
if (p != null) {
- buffer.writeBytes(serializeObject(p.getEro()));
+ serializeObject(p.getEro(), buffer);
if (p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p.getLspa()));
+ serializeObject(p.getLspa(), buffer);
}
if (p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p.getBandwidth()));
+ serializeObject(p.getBandwidth(), buffer);
}
if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
for (final Metrics m : p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p.getIro() != null) {
- buffer.writeBytes(serializeObject(p.getIro()));
+ serializeObject(p.getIro(), buffer);
}
if (p.getRro() != null) {
- buffer.writeBytes(serializeObject(p.getRro()));
+ serializeObject(p.getRro(), buffer);
}
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcupd)) {
- throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
- + ". Nedded PcupdMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
final Pcupd msg = (Pcupd) message;
final List<Updates> updates = msg.getPcupdMessage().getUpdates();
ByteBuf buffer = Unpooled.buffer();
for (final Updates update : updates) {
- buffer.writeBytes(serializeObject(update.getSrp()));
- buffer.writeBytes(serializeObject(update.getLsp()));
+ serializeObject(update.getSrp(), buffer);
+ serializeObject(update.getLsp(), buffer);
final Path p = update.getPath();
if (p != null) {
- buffer.writeBytes(serializeObject(p.getEro()));
+ serializeObject(p.getEro(), buffer);
if (p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p.getLspa()));
+ serializeObject(p.getLspa(), buffer);
}
if (p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p.getBandwidth()));
+ serializeObject(p.getBandwidth(), buffer);
}
if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
for (final Metrics m : p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p.getIro() != null) {
- buffer.writeBytes(serializeObject(p.getIro()));
+ serializeObject(p.getIro(), buffer);
}
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Srp)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SrpObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.", object.getClass());
final Srp srp = (Srp) object;
+ ByteBuf body = Unpooled.buffer();
+ //FIXME: switch to ByteBuf
final byte[] tlvs = serializeTlvs(srp.getTlvs());
+ body.writerIndex(body.writerIndex() + FLAGS_SIZE);
final Long id = srp.getOperationId().getValue();
- final byte[] retBytes = new byte[MIN_SIZE];
- if (tlvs != null) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- }
- System.arraycopy(ByteArray.intToBytes(id.intValue(), SRP_ID_SIZE), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ body.writeInt(id.intValue());
+ body.writeBytes(tlvs);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
Tlvs1.class, statBuilder.build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder().setLspErrorCode(
tlv1).setSymbolicPathName(tlv2).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
assertEquals(tlv, ((Lspa) o).getTlvs().getAugmentation(Tlvs2.class).getSymbolicPathName());
// assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), ByteArray.cutBytes(result,
// 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.addAugmentation(Srp1.class, new Srp1Builder().setRemove(true).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof CloseMessage)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Nedded CloseMessage.");
- }
+ Preconditions.checkArgument(message instanceof CloseMessage, "Wrong instance of Message. Passed instance of %s. Need CloseMessage.", message.getClass());
final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
if (close.getCClose() == null) {
throw new IllegalArgumentException("Close Object must be present in Close Message.");
}
ByteBuf buffer = Unpooled.buffer();
- buffer.writeBytes(serializeObject(close.getCClose()));
+ serializeObject(close.getCClose(), buffer);
MessageUtil.formatMessage(TYPE, buffer, out);
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof PcerrMessage)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass()
- + ". Nedded ErrorMessage.");
- }
+ Preconditions.checkArgument(message instanceof PcerrMessage, "Wrong instance of Message. Passed instance of %s. Need PcerrMessage.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage err = ((PcerrMessage) message).getPcerrMessage();
if (err.getErrors() == null || err.getErrors().isEmpty()) {
if (err.getErrorType() instanceof RequestCase) {
final List<Rps> rps = ((RequestCase) err.getErrorType()).getRequest().getRps();
for (final Rps r : rps) {
- buffer.writeBytes(serializeObject(r.getRp()));
+ serializeObject(r.getRp(), buffer);
}
}
for (final Errors e : err.getErrors()) {
- buffer.writeBytes(serializeObject(e.getErrorObject()));
+ serializeObject(e.getErrorObject(), buffer);
}
if (err.getErrorType() instanceof SessionCase) {
- buffer.writeBytes(serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen()));
+ serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen(), buffer);
}
MessageUtil.formatMessage(TYPE, buffer, out);
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof KeepaliveMessage)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Need KeepaliveMessage.");
- }
+ Preconditions.checkArgument(message instanceof KeepaliveMessage, "Wrong instance of Message. Passed instance of %s. Need KeepaliveMessage.", message.getClass());
MessageUtil.formatMessage(TYPE, Unpooled.EMPTY_BUFFER, out);
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof PcntfMessage)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Needed PcntfMessage.");
- }
+ Preconditions.checkArgument(message instanceof PcntfMessage, "Wrong instance of Message. Passed instance of %s. Need PcntfMessage.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
ByteBuf buffer = Unpooled.buffer();
for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
if (n.getRps() != null && !n.getRps().isEmpty()) {
for (final Rps rps : n.getRps()) {
- buffer.writeBytes(serializeObject(rps.getRp()));
+ serializeObject(rps.getRp(), buffer);
}
}
if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
throw new IllegalArgumentException("Message must contain at least one notification object");
} else {
for (final Notifications not : n.getNotifications()) {
- buffer.writeBytes(serializeObject(not.getCNotification()));
+ serializeObject(not.getCNotification(), buffer);
}
}
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof OpenMessage)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Needed OpenMessage.");
- }
+ Preconditions.checkArgument(message instanceof OpenMessage, "Wrong instance of Message. Passed instance of %s. Need OpenMessage.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
if (open.getOpen() == null) {
throw new IllegalArgumentException("Open Object must be present in Open Message.");
}
ByteBuf buffer = Unpooled.buffer();
- buffer.writeBytes(serializeObject(open.getOpen()));
+ serializeObject(open.getOpen(), buffer);
MessageUtil.formatMessage(TYPE, buffer, out);
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcrep)) {
- throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
- + ". Nedded PcrepMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
throw new IllegalArgumentException("Replies cannot be null or empty.");
if (reply.getRp() == null) {
throw new IllegalArgumentException("Reply must contain RP object.");
}
- buffer.writeBytes(serializeObject(reply.getRp()));
+ serializeObject(reply.getRp(), buffer);
if (reply.getResult() != null) {
if (reply.getResult() instanceof FailureCase) {
final FailureCase f = ((FailureCase) reply.getResult());
- buffer.writeBytes(serializeObject(f.getNoPath()));
+ serializeObject(f.getNoPath(), buffer);
if (f.getLspa() != null) {
- buffer.writeBytes(serializeObject(f.getLspa()));
+ serializeObject(f.getLspa(), buffer);
}
if (f.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(f.getBandwidth()));
+ serializeObject(f.getBandwidth(), buffer);
}
if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
for (final Metrics m : f.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (f.getIro() != null) {
- buffer.writeBytes(serializeObject(f.getIro()));
+ serializeObject(f.getIro(), buffer);
}
} else {
final SuccessCase s = (SuccessCase) reply.getResult();
for (final Paths p : s.getSuccess().getPaths()) {
- buffer.writeBytes(serializeObject(p.getEro()));
+ serializeObject(p.getEro(), buffer);
if (p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p.getLspa()));
+ serializeObject(p.getLspa(), buffer);
}
if (p.getOf() != null) {
- buffer.writeBytes(serializeObject(p.getOf()));
+ serializeObject(p.getOf(), buffer);
}
if (p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p.getBandwidth()));
+ serializeObject(p.getBandwidth(), buffer);
}
if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
for (final Metrics m : p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p.getIro() != null) {
- buffer.writeBytes(serializeObject(p.getIro()));
+ serializeObject(p.getIro(), buffer);
}
}
}
*/
package org.opendaylight.protocol.pcep.impl.message;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
- if (!(message instanceof Pcreq)) {
- throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
- + ". Needed PcrepMessage.");
- }
+ Preconditions.checkArgument(message instanceof Pcreq, "Wrong instance of Message. Passed instance of %s. Need Pcreq.", message.getClass());
final PcreqMessage msg = ((Pcreq) message).getPcreqMessage();
if (msg.getRequests() == null || msg.getRequests().isEmpty()) {
throw new IllegalArgumentException("Requests cannot be null or empty.");
}
ByteBuf buffer = Unpooled.buffer();
for (final Requests req : msg.getRequests()) {
- buffer.writeBytes(serializeObject(req.getRp()));
+ serializeObject(req.getRp(), buffer);
if (req.getPathKeyExpansion() != null) {
- buffer.writeBytes(serializeObject(req.getPathKeyExpansion().getPathKey()));
+ serializeObject(req.getPathKeyExpansion().getPathKey(), buffer);
}
if (req.getSegmentComputation() != null) {
final SegmentComputation sc = req.getSegmentComputation();
}
if (msg.getSvec() != null) {
for (final Svec s : msg.getSvec()) {
- buffer.writeBytes(serializeObject(s.getSvec()));
+ serializeObject(s.getSvec(), buffer);
if (s.getOf() != null) {
- buffer.writeBytes(serializeObject(s.getOf()));
+ serializeObject(s.getOf(), buffer);
}
if (s.getGc() != null) {
- buffer.writeBytes(serializeObject(s.getGc()));
+ serializeObject(s.getGc(), buffer);
}
if (s.getXro() != null) {
- buffer.writeBytes(serializeObject(s.getXro()));
+ serializeObject(s.getXro(), buffer);
}
if (s.getMetric() != null) {
for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.Metric m : s.getMetric()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
}
protected void serializeP2P(final ByteBuf buffer, final P2p p2p) {
if (p2p.getEndpointsObj() != null) {
- buffer.writeBytes(serializeObject(p2p.getEndpointsObj()));
+ serializeObject(p2p.getEndpointsObj(), buffer);
}
if (p2p.getReportedRoute() != null) {
final ReportedRoute rr = p2p.getReportedRoute();
if (rr.getRro() != null) {
- buffer.writeBytes(serializeObject(rr.getRro()));
+ serializeObject(rr.getRro(), buffer);
}
if (rr.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(rr.getBandwidth()));
+ serializeObject(rr.getBandwidth(), buffer);
}
}
if (p2p.getLoadBalancing() != null) {
- buffer.writeBytes(serializeObject(p2p.getLoadBalancing()));
+ serializeObject(p2p.getLoadBalancing(), buffer);
}
if (p2p.getLspa() != null) {
- buffer.writeBytes(serializeObject(p2p.getLspa()));
+ serializeObject(p2p.getLspa(), buffer);
}
if (p2p.getBandwidth() != null) {
- buffer.writeBytes(serializeObject(p2p.getBandwidth()));
+ serializeObject(p2p.getBandwidth(), buffer);
}
if (p2p.getMetrics() != null) {
for (final Metrics m : p2p.getMetrics()) {
- buffer.writeBytes(serializeObject(m.getMetric()));
+ serializeObject(m.getMetric(), buffer);
}
}
if (p2p.getIro() != null) {
- buffer.writeBytes(serializeObject(p2p.getIro()));
+ serializeObject(p2p.getIro(), buffer);
}
if (p2p.getRro() != null) {
- buffer.writeBytes(serializeObject(p2p.getRro()));
+ serializeObject(p2p.getRro(), buffer);
}
if (p2p.getXro() != null) {
- buffer.writeBytes(serializeObject(p2p.getXro()));
+ serializeObject(p2p.getXro(), buffer);
}
if (p2p.getOf() != null) {
- buffer.writeBytes(serializeObject(p2p.getOf()));
+ serializeObject(p2p.getOf(), buffer);
}
if (p2p.getClassType() != null) {
- buffer.writeBytes(serializeObject(p2p.getClassType()));
+ serializeObject(p2p.getClassType(), buffer);
}
}
&& builder.getBandwidth() != null
&& builder.getReportedRoute().getBandwidth().getBandwidth() != new BandwidthBuilder().setBandwidth(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth(new byte[] { 0 })).build()
- && builder.getReportedRoute().getRro() == null) {
+ && builder.getReportedRoute().getRro() == null) {
errors.add(createErrorMsg(PCEPErrors.RRO_MISSING, rp));
return null;
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
import org.opendaylight.protocol.util.ByteArray;
+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.bandwidth.object.Bandwidth;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.BandwidthBuilder;
builder.setBandwidth(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth(ByteArray.getAllBytes(bytes)));
return builder.build();
}
+
+ @Override
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Bandwidth, "Wrong instance of PCEPObject. Passed %s. Needed BandwidthObject.", object.getClass());
+ final ByteBuf body = Unpooled.buffer();
+ body.writeBytes(((Bandwidth) object).getBandwidth().getValue());
+ formatBandwidth(object.isProcessingRule(), object.isIgnore(), body, buffer);
+ }
+
+ protected abstract void formatBandwidth(final boolean processed, final boolean ignored, final ByteBuf body, final ByteBuf buffer);
}
*/
package org.opendaylight.protocol.pcep.impl.object;
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-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.bandwidth.object.Bandwidth;
/**
- * Parser for {@link Bandwidth}
+ * Parser for Bandwidth
*/
public class PCEPBandwidthObjectParser extends AbstractBandwidthParser {
public static final int TYPE = 1;
- private static final int BANDWIDTH_LENGTH = 4;
-
public PCEPBandwidthObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Bandwidth)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed BandwidthObject.");
- }
- final byte[] retBytes = Arrays.copyOf(((Bandwidth) object).getBandwidth().getValue(), BANDWIDTH_LENGTH);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ protected void formatBandwidth(boolean processed, boolean ignored, ByteBuf body, ByteBuf buffer) {
+ ObjectUtil.formatSubobject(TYPE, CLASS, processed, ignored, body, buffer);
}
}
package org.opendaylight.protocol.pcep.impl.object;
import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
/**
* Size of the object in bytes.
*/
- private static final int SIZE = (RESERVED + CT_F_LENGTH) / 8;
+ private static final int SIZE = (RESERVED + CT_F_LENGTH) / Byte.SIZE;
public PCEPClassTypeObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof ClassType)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ClasstypeObject.");
- }
- final byte[] retBytes = new byte[SIZE];
- retBytes[SIZE - 1] = UnsignedBytes.checkedCast(((ClassType) object).getClassType().getValue());
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof ClassType, "Wrong instance of PCEPObject. Passed %s. Needed ClassTypeObject.", object.getClass());
+ final ByteBuf body = Unpooled.buffer(SIZE);
+ body.writeZero(SIZE -1);
+ body.writeByte(((ClassType) object).getClassType().getValue());
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
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.close.object.CClose;
/*
* lengths of fields in bytes
*/
+ private static final int RESERVED = 2;
private static final int FLAGS_F_LENGTH = 1;
- private static final int REASON_F_LENGTH = 1;
-
- /*
- * offsets of fields in bytes
- */
- private static final int FLAGS_F_OFFSET = 2;
- private static final int REASON_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-
- /*
- * total size of object in bytes
- */
- private static final int TLVS_OFFSET = REASON_F_OFFSET + REASON_F_LENGTH;
public PCEPCloseObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
final CCloseBuilder builder = new CCloseBuilder();
builder.setIgnore(header.isIgnore());
builder.setProcessingRule(header.isProcessingRule());
- bytes.readerIndex(bytes.readerIndex() + REASON_F_OFFSET);
+ bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
builder.setReason((short) UnsignedBytes.toInt(bytes.readByte()));
parseTlvs(builder, bytes.slice());
return builder.build();
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof CClose)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed CloseObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof CClose, "Wrong instance of PCEPObject. Passed %s. Needed CCloseObject.", object.getClass());
final CClose obj = (CClose) object;
-
+ final ByteBuf body = Unpooled.buffer();
+ body.writeZero(RESERVED + FLAGS_F_LENGTH);
+ body.writeByte(obj.getReason());
+ // FIXME: switch to ByteBuf
final byte[] tlvs = serializeTlvs(obj.getTlvs());
- int tlvsLength = 0;
- if (tlvs != null) {
- tlvsLength = tlvs.length;
- }
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
if (tlvs != null) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+ body.writeBytes(tlvs);
}
- retBytes[REASON_F_OFFSET] = UnsignedBytes.checkedCast(obj.getReason());
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
public static final int CLASS = 4;
- public static final int TYPE = 1;
-
- /*
- * fields lengths and offsets for IPv4 in bytes
- */
- private static final int SRC4_F_OFFSET = 0;
- private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + Ipv4Util.IP4_LENGTH;
+ public static final int TYPE = 1;
public PCEPEndPointsIpv4ObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof EndpointsObj)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
- }
+ 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();
-
- if (!(afi instanceof Ipv4Case)) {
- throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
- }
- final byte[] retBytes = new byte[Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH];
- ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
- ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ Preconditions.checkArgument(afi instanceof Ipv4Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv4", afi.getClass());
+ final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
+ body.writeBytes(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()));
+ body.writeBytes(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()));
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.concepts.Ipv6Util;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv6ObjectParser.class);
public static final int CLASS = 4;
- public static final int TYPE = 2;
- /*
- * fields lengths and offsets for IPv6 in bytes
- */
- private static final int SRC6_F_OFFSET = 0;
- private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + Ipv6Util.IPV6_LENGTH;
+ public static final int TYPE = 2;
public PCEPEndPointsIpv6ObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof EndpointsObj)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
- }
+ 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();
-
- if (!(afi instanceof Ipv6Case)) {
- throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
- }
- final byte[] retBytes = new byte[Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_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);
+ 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);
+ body.writeBytes(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()));
+ body.writeBytes(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()));
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
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;
public static final int TYPE = 1;
private static final int FLAGS_F_LENGTH = 1;
- private static final int ET_F_LENGTH = 1;
- private static final int EV_F_LENGTH = 1;
- private static final int FLAGS_F_OFFSET = 1;
- private static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
- private static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
- private static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
+ private static final int RESERVED = 1;
public PCEPErrorObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
final ErrorObjectBuilder builder = new ErrorObjectBuilder();
builder.setIgnore(header.isIgnore());
builder.setProcessingRule(header.isProcessingRule());
- bytes.readerIndex(bytes.readerIndex() + ET_F_OFFSET);
+ bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
parseTlvs(builder, bytes.slice());
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof ErrorObject)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
final ErrorObject errObj = (ErrorObject) object;
-
+ final ByteBuf body = Unpooled.buffer();
+ body.writeZero(FLAGS_F_LENGTH + RESERVED);
+ body.writeByte(errObj.getType());
+ body.writeByte(errObj.getValue());
+ // FIXME: switch to ByteBuf
final byte[] tlvs = serializeTlvs(errObj.getTlvs());
-
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
if (tlvs.length != 0) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+ body.writeBytes(tlvs);
}
- retBytes[ET_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getType());
- retBytes[EV_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getValue());
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
/*
* Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
*
-* This program and the accompanying materials are made available under the
+ * This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
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.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.exclude.route.object.Xro;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Xro)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Xro, "Wrong instance of PCEPObject. Passed %s. Needed XroObject.", object.getClass());
final Xro obj = (Xro) object;
- assert !(obj.getSubobject().isEmpty()) : "Empty Excluded Route Object.";
- final byte[] bytes = serializeSubobject(obj.getSubobject());
- final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
- if (obj.getFlags().isFail()) {
- result[FLAGS_OFFSET] = 1;
+ final ByteBuf body = Unpooled.buffer();
+ body.writeZero(FLAGS_OFFSET);
+ if (obj.getFlags().isFail() != null) {
+ body.writeBoolean(obj.getFlags().isFail());
}
- ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), result);
+ // FIXME: switch to ByteBuf
+ final byte[] bytes = serializeSubobject(obj.getSubobject());
+ body.writeBytes(bytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.impl.object;
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-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.bandwidth.object.Bandwidth;
/**
- * Parser for {@link Bandwidth}
+ * Parser for Bandwidth
*/
public class PCEPExistingBandwidthObjectParser extends AbstractBandwidthParser {
public static final int TYPE = 2;
- private static final int BANDWIDTH_LENGTH = 4;
-
public PCEPExistingBandwidthObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Bandwidth)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed BandwidthObject.");
- }
- final byte[] retBytes = Arrays.copyOf(((Bandwidth) object).getBandwidth().getValue(), BANDWIDTH_LENGTH);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ protected void formatBandwidth(boolean processed, boolean ignored, ByteBuf body, ByteBuf buffer) {
+ ObjectUtil.formatSubobject(TYPE, CLASS, processed, ignored, body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Ero)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
- + ". Needed ExplicitRouteObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Ero, "Wrong instance of PCEPObject. Passed %s. Needed EroObject.", object.getClass());
final Ero ero = ((Ero) object);
-
- assert !(ero.getSubobject().isEmpty()) : "Empty Explicit Route Object.";
-
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(ero.getSubobject()));
+ final ByteBuf body = Unpooled.buffer();
+ // FIXME: switch to ByteBuf
+ final byte[] bytes = serializeSubobject(ero.getSubobject());
+ body.writeBytes(bytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
public static final int TYPE = 1;
- private static final int MAX_HOP_F_LENGTH = 1;
- private static final int MAX_UTIL_F_LENGTH = 1;
- private static final int MIN_UTIL_F_LENGTH = 1;
- private static final int OVER_BOOKING_FACTOR_F_LENGTH = 1;
-
- private static final int MAX_HOP_F_OFFSET = 0;
- private static final int MAX_UTIL_F_OFFSET = MAX_HOP_F_OFFSET + MAX_HOP_F_LENGTH;
- private static final int MIN_UTIL_F_OFFSET = MAX_UTIL_F_OFFSET + MAX_UTIL_F_LENGTH;
- private static final int OVER_BOOKING_FACTOR_F_OFFSET = MIN_UTIL_F_OFFSET + MIN_UTIL_F_LENGTH;
-
- private static final int TLVS_OFFSET = OVER_BOOKING_FACTOR_F_OFFSET + OVER_BOOKING_FACTOR_F_LENGTH;
-
public PCEPGlobalConstraintsObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Gc)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed GcObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Gc, "Wrong instance of PCEPObject. Passed %s. Needed GcObject.", object.getClass());
final Gc specObj = (Gc) object;
- final byte[] retBytes = new byte[TLVS_OFFSET + 0];
- retBytes[MAX_HOP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxHop());
- retBytes[MAX_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxUtilization());
- retBytes[MIN_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMinUtilization());
- retBytes[OVER_BOOKING_FACTOR_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getOverBookingFactor());
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ final ByteBuf body = Unpooled.buffer();
+ body.writeByte(specObj.getMaxHop());
+ body.writeByte(specObj.getMaxUtilization());
+ body.writeByte(specObj.getMinUtilization());
+ body.writeByte(specObj.getOverBookingFactor());
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
package org.opendaylight.protocol.pcep.impl.object;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Iro)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed IncludeRouteObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Iro, "Wrong instance of PCEPObject. Passed %s. Needed IroObject.", object.getClass());
final Iro iro = ((Iro) object);
-
- assert !(iro.getSubobject().isEmpty()) : "Empty Include Route Object.";
-
- final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = Lists.newArrayList();
-
+ final ByteBuf body = Unpooled.buffer();
+ final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = new ArrayList<>();
for (final Subobject s : iro.getSubobject()) {
subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
false).setSubobjectType(s.getSubobjectType()).build());
}
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
+ //FIXME: switch to Bytebuf
+ byte[] bytes = serializeSubobject(subs);
+ body.writeBytes(bytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
public static final int TYPE = 1;
+ private static final int RESERVED = 2;
private static final int FLAGS_F_LENGTH = 1;
- private static final int MAX_LSP_F_LENGTH = 1;
- private static final int MIN_BAND_F_LENGTH = 4;
- private static final int FLAGS_F_OFFSET = 2;
- private static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
- private static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
-
- private static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
+ private static final int SIZE = 8;
public PCEPLoadBalancingObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
final LoadBalancingBuilder builder = new LoadBalancingBuilder();
builder.setIgnore(header.isIgnore());
builder.setProcessingRule(header.isProcessingRule());
- bytes.readerIndex(bytes.readerIndex() + MAX_LSP_F_OFFSET);
+ bytes.readerIndex(bytes.readerIndex() + RESERVED + FLAGS_F_LENGTH);
builder.setMaxLsp((short) UnsignedBytes.toInt(bytes.readByte()));
- builder.setMinBandwidth(new Bandwidth(ByteArray.readBytes(bytes, MIN_BAND_F_LENGTH)));
+ builder.setMinBandwidth(new Bandwidth(ByteArray.readAllBytes(bytes)));
return builder.build();
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof LoadBalancing)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
- + ". Needed LoadBalancingObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof LoadBalancing, "Wrong instance of PCEPObject. Passed %s. Needed LoadBalancingObject.", object.getClass());
final LoadBalancing specObj = (LoadBalancing) object;
- final byte[] retBytes = new byte[SIZE];
- retBytes[MAX_LSP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxLsp());
- ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ final ByteBuf body = Unpooled.buffer(SIZE);
+ body.writeZero(RESERVED + FLAGS_F_LENGTH);
+ body.writeByte(specObj.getMaxLsp());
+ body.writeBytes(specObj.getMinBandwidth().getValue());
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
/*
* lengths of fields in bytes
*/
- private static final int EXC_ANY_F_LENGTH = 4;
- private static final int INC_ANY_F_LENGTH = 4;
- private static final int INC_ALL_F_LENGTH = 4;
- private static final int SET_PRIO_F_LENGTH = 1;
- private static final int HOLD_PRIO_F_LENGTH = 1;
private static final int FLAGS_F_LENGTH = 1;
/*
*/
private static final int L_FLAG_OFFSET = 7;
- /*
- * offsets of fields in bytes
- */
- private static final int EXC_ANY_F_OFFSET = 0;
- private static final int INC_ANY_F_OFFSET = EXC_ANY_F_OFFSET + EXC_ANY_F_LENGTH;
- private static final int INC_ALL_F_OFFSET = INC_ANY_F_OFFSET + INC_ANY_F_LENGTH;
- private static final int SET_PRIO_F_OFFSET = INC_ALL_F_OFFSET + INC_ALL_F_LENGTH;
- private static final int HOLD_PRIO_F_OFFSET = SET_PRIO_F_OFFSET + SET_PRIO_F_LENGTH;
- private static final int FLAGS_F_OFFSET = HOLD_PRIO_F_OFFSET + HOLD_PRIO_F_LENGTH;
- private static final int TLVS_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH + 1;
+ private static final int RESERVED = 1;
public PCEPLspaObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
final BitSet flags = ByteArray.bytesToBitSet(new byte[] { bytes.readByte() });
builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
final TlvsBuilder tbuilder = new TlvsBuilder();
- bytes.readerIndex(TLVS_F_OFFSET);
+ bytes.readerIndex(bytes.readerIndex() + RESERVED);
parseTlvs(tbuilder, bytes.slice());
builder.setTlvs(tbuilder.build());
return builder.build();
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Lspa)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspaObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Lspa, "Wrong instance of PCEPObject. Passed %s. Needed LspaObject.", object.getClass());
final Lspa lspaObj = (Lspa) object;
-
- final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
- final byte[] retBytes = new byte[TLVS_F_OFFSET + tlvs.length + getPadding(TLVS_F_OFFSET + tlvs.length, PADDED_TO)];
-
- if (lspaObj.getExcludeAny() != null) {
- System.arraycopy(ByteArray.longToBytes(lspaObj.getExcludeAny().getValue(), EXC_ANY_F_LENGTH), 0, retBytes, EXC_ANY_F_OFFSET,
- EXC_ANY_F_LENGTH);
- }
- if (lspaObj.getIncludeAny() != null) {
- System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAny().getValue(), INC_ANY_F_LENGTH), 0, retBytes, INC_ANY_F_OFFSET,
- INC_ANY_F_LENGTH);
- }
- if (lspaObj.getIncludeAll() != null) {
- System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAll().getValue(), INC_ALL_F_LENGTH), 0, retBytes, INC_ALL_F_OFFSET,
- INC_ALL_F_LENGTH);
- }
- if (lspaObj.getSetupPriority() != null) {
- retBytes[SET_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getSetupPriority());
- }
- if (lspaObj.getHoldPriority() != null) {
- retBytes[HOLD_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getHoldPriority());
- }
-
+ final ByteBuf body = Unpooled.buffer();
+ body.writeInt(lspaObj.getExcludeAny().getValue().intValue());
+ body.writeInt(lspaObj.getIncludeAny().getValue().intValue());
+ body.writeInt(lspaObj.getIncludeAll().getValue().intValue());
+ body.writeByte(lspaObj.getSetupPriority());
+ body.writeByte(lspaObj.getHoldPriority());
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- if (lspaObj.isLocalProtectionDesired() != null && lspaObj.isLocalProtectionDesired()) {
+ if (lspaObj.isLocalProtectionDesired() != null) {
flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
}
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
- ByteArray.copyWhole(tlvs, retBytes, TLVS_F_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ body.writeZero(RESERVED);
+ // FIXME: switch to ByteBuf
+ final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
+ if (tlvs.length != 0) {
+ body.writeBytes(tlvs);
+ }
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
* lengths of fields in bytes
*/
private static final int FLAGS_F_LENGTH = 1;
- private static final int TYPE_F_LENGTH = 1;
private static final int METRIC_VALUE_F_LENGTH = 4;
/*
* offsets of fields in bytes
*/
- private static final int FLAGS_F_OFFSET = 2;
- private static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
- private static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
+ private static final int RESERVED = 2;
/*
* flags offsets inside flags field in bits
private static final int C_FLAG_OFFSET = 6;
private static final int B_FLAG_OFFSET = 7;
- private static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
+ private static final int SIZE = 4 + METRIC_VALUE_F_LENGTH;
public PCEPMetricObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
+ ".");
}
- bytes.readerIndex(bytes.readerIndex() + FLAGS_F_OFFSET);
+ bytes.readerIndex(bytes.readerIndex() + RESERVED);
final byte[] flagBytes = { bytes.readByte() };
final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
final MetricBuilder builder = new MetricBuilder();
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Metric)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Metric, "Wrong instance of PCEPObject. Passed %s. Needed MetricObject.", object.getClass());
final Metric mObj = (Metric) object;
- final byte[] retBytes = new byte[SIZE];
+ final ByteBuf body = Unpooled.buffer(SIZE);
+ body.writeZero(RESERVED);
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(C_FLAG_OFFSET, mObj.isComputed());
- flags.set(B_FLAG_OFFSET, mObj.isBound());
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
- retBytes[TYPE_F_OFFSET] = UnsignedBytes.checkedCast(mObj.getMetricType());
- System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ if (mObj.isComputed() != null) {
+ flags.set(C_FLAG_OFFSET, mObj.isComputed());
+ }
+ if (mObj.isBound() != null) {
+ flags.set(B_FLAG_OFFSET, mObj.isBound());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ body.writeByte(mObj.getMetricType());
+ body.writeBytes(mObj.getValue().getValue());
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
/*
* lengths of fields in bytes
*/
- private static final int NI_F_LENGTH = 1;
private static final int FLAGS_F_LENGTH = 2;
private static final int RESERVED_F_LENGTH = 1;
- /*
- * offsets of field in bytes
- */
- private static final int NI_F_OFFSET = 0;
- private static final int FLAGS_F_OFFSET = NI_F_OFFSET + NI_F_LENGTH;
- private static final int RESERVED_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
- private static final int TLVS_OFFSET = RESERVED_F_OFFSET + RESERVED_F_LENGTH;
-
/*
* defined flags
*/
final byte[] flagsByte = ByteArray.readBytes(bytes, FLAGS_F_LENGTH);
final BitSet flags = ByteArray.bytesToBitSet(flagsByte);
builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
- bytes.readerIndex(TLVS_OFFSET);
+ bytes.readerIndex(bytes.readerIndex() + RESERVED_F_LENGTH);
parseTlvs(builder, bytes.slice());
return builder.build();
}
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof NoPath)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NoPathObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.", object.getClass());
final NoPath nPObj = (NoPath) object;
-
+ final ByteBuf body = Unpooled.buffer();
+ body.writeByte(nPObj.getNatureOfIssue());
+ final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
+ if (nPObj.isUnsatisfiedConstraints() != null) {
+ flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ body.writeZero(RESERVED_F_LENGTH);
+ // FIXME: switch to ByteBuf
final byte[] tlvs = serializeTlvs(nPObj.getTlvs());
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
if (tlvs != null) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+ body.writeBytes(tlvs);
}
- final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
- retBytes[NI_F_OFFSET] = UnsignedBytes.checkedCast(nPObj.getNatureOfIssue());
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
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;
public static final int TYPE = 1;
- /*
- * lengths of fields
- */
- private static final int FLAGS_F_LENGTH = 1;
- private static final int NT_F_LENGTH = 1;
- private static final int NV_F_LENGTH = 1;
-
/*
* offsets of fields
*/
- private static final int FLAGS_F_OFFSET = 1;
- private static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
- private static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
- private static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
+ private static final int NT_F_OFFSET = 2;
public PCEPNotificationObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof CNotification)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof CNotification, "Wrong instance of PCEPObject. Passed %s. Needed CNotificationObject.", object.getClass());
final CNotification notObj = (CNotification) object;
-
+ final ByteBuf body = Unpooled.buffer();
+ body.writeZero(NT_F_OFFSET);
+ body.writeByte(notObj.getType());
+ body.writeByte(notObj.getValue());
+ // FIXME: switch to ByteBuf
final byte[] tlvs = serializeTlvs(notObj.getTlvs());
-
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
if (tlvs.length != 0) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+ body.writeBytes(tlvs);
}
- retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
- retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
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.OfId;
public static final int CLASS = 21;
public static final int TYPE = 1;
- /*
- * lengths of fields
- */
- private static final int OF_CODE_F_LENGTH = 2;
- /*
- * offsets of fields
- */
- private static final int OF_CODE_F_OFFSET = 0;
- private static final int TLVS_OFFSET = OF_CODE_F_OFFSET + OF_CODE_F_LENGTH + 2;
+ private static final int RESERVED = 2;
public PCEPObjectiveFunctionObjectParser(final TlvRegistry tlvReg) {
super(tlvReg);
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Of)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
- + ". Needed PCEPObjectiveFunction.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Of, "Wrong instance of PCEPObject. Passed %s. Needed OfObject.", object.getClass());
final Of specObj = (Of) object;
- final byte[] retBytes = new byte[TLVS_OFFSET + 0];
- ByteArray.copyWhole(ByteArray.shortToBytes(specObj.getCode().getValue().shortValue()), retBytes, OF_CODE_F_OFFSET);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ final ByteBuf body = Unpooled.buffer();
+ body.writeShort(specObj.getCode().getValue().shortValue());
+ body.writeZero(RESERVED);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
public static final int TYPE = 1;
- /*
- * lengths of fields in bytes
- */
- private static final int VER_FLAGS_MF_LENGTH = 1;
- private static final int KEEPALIVE_F_LENGTH = 1;
- private static final int DEAD_TIMER_LENGTH = 1;
- private static final int SID_F_LENGTH = 1;
-
/*
* lengths of subfields inside multi-field in bits
*/
private static final int VERSION_SF_LENGTH = 3;
- /*
- * offsets of field in bytes
- */
- private static final int VER_FLAGS_MF_OFFSET = 0;
- private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
- private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
- private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
- private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
-
/*
* offsets of subfields inside multi-field in bits
*/
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Open)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
final Open open = (Open) object;
-
- final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
-
- final byte[] tlvs = serializeTlvs(open.getTlvs());
-
- final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
- bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
- bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
- bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
- bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
- if (tlvs.length != 0) {
- ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
- }
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
+ final ByteBuf body = Unpooled.buffer();
+ body.writeByte(PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
+ body.writeByte(open.getKeepalive());
+ body.writeByte(open.getDeadTimer());
+ body.writeByte(open.getSessionId());
+ //FIXME: switch to ByteBuf
+ body.writeBytes(serializeTlvs(open.getTlvs()));
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
package org.opendaylight.protocol.pcep.impl.object;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof PathKey)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PathKeyObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof PathKey, "Wrong instance of PCEPObject. Passed %s. Needed PathKeyObject.", object.getClass());
final PathKey pkey = (PathKey) object;
+ final ByteBuf body = Unpooled.buffer();
final List<PathKeys> pk = pkey.getPathKeys();
- final List<Subobject> subs = Lists.newArrayList();
+ final List<Subobject> subs = new ArrayList<>();
for (final PathKeys p : pk) {
subs.add(new SubobjectBuilder().setLoose(p.isLoose()).setSubobjectType(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder().setPathKey(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKeyBuilder().setPathKey(
p.getPathKey()).setPceId(p.getPceId()).build()).build()).build());
}
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(subs));
+ // FIXME: switch to ByteBuf
+ body.writeBytes(serializeSubobject(subs));
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.ObjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Rro)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
- + ". Needed ReportedRouteObject.");
- }
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Rro, "Wrong instance of PCEPObject. Passed %s. Needed RroObject.", object.getClass());
final Rro obj = (Rro) object;
- assert !(obj.getSubobject().isEmpty()) : "Empty Reported Route Object.";
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), serializeSubobject(obj.getSubobject()));
+ final ByteBuf body = Unpooled.buffer();
+ // FIXME: switch to ByteBuf
+ final byte[] subs = serializeSubobject(obj.getSubobject());
+ body.writeBytes(subs);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
package org.opendaylight.protocol.pcep.impl.object;
import com.google.common.base.Preconditions;
+import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
* lengths of fields in bytes
*/
private static final int FLAGS_PRI_MF_LENGTH = 4;
- private static final int RID_F_LENGTH = 4;
/*
* lengths of subfields inside multi-field in bits
*/
private static final int FLAGS_SF_LENGTH = 29;
- /*
- * offsets of field in bytes
- */
-
- private static final int FLAGS_PRI_MF_OFFSET = 0;
- private static final int RID_F_OFFSET = FLAGS_PRI_MF_OFFSET + FLAGS_PRI_MF_LENGTH;
- private static final int TLVS_OFFSET = RID_F_OFFSET + RID_F_LENGTH;
-
/*
* offsets of subfields inside multi-field in bits
*/
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 BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, FLAGS_PRI_MF_LENGTH));
- short priority = 0;
- priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
- priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
- priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
final RpBuilder builder = new RpBuilder();
builder.setIgnore(header.isIgnore());
builder.setProcessingRule(header.isProcessingRule());
+ short priority = 0;
+ priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
+ priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
+ priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
builder.setPriority(priority);
builder.setFragmentation(flags.get(F_FLAG_OFFSET));
builder.setP2mp(flags.get(N_FLAG_OFFSET));
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Rp)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed RpObject.");
- }
- final Rp rPObj = (Rp) object;
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Rp, "Wrong instance of PCEPObject. Passed %s. Needed RPObject.", object.getClass());
+ final ByteBuf body = Unpooled.buffer();
+ final Rp rpObj = (Rp) object;
final BitSet flags = new BitSet(FLAGS_PRI_MF_LENGTH * Byte.SIZE);
-
- flags.set(R_FLAG_OFFSET, rPObj.isReoptimization());
- flags.set(B_FLAG_OFFSET, rPObj.isBiDirectional());
- flags.set(O_FLAG_OFFSET, rPObj.isLoose());
- flags.set(M_FLAG_OFFSET, rPObj.isMakeBeforeBreak());
- flags.set(D_FLAG_OFFSET, rPObj.isOrder());
- flags.set(P_FLAG_OFFSET, rPObj.isPathKey());
- flags.set(S_FLAG_OFFSET, rPObj.isSupplyOf());
- flags.set(F_FLAG_OFFSET, rPObj.isFragmentation());
- flags.set(N_FLAG_OFFSET, rPObj.isP2mp());
- flags.set(E_FLAG_OFFSET, rPObj.isEroCompression());
-
- flags.set(PRI_SF_OFFSET, (rPObj.getPriority() & 1 << 2) != 0);
- flags.set(PRI_SF_OFFSET + 1, (rPObj.getPriority() & 1 << 1) != 0);
- flags.set(PRI_SF_OFFSET + 2, (rPObj.getPriority() & 1) != 0);
-
- final byte[] tlvs = serializeTlvs(rPObj.getTlvs());
- final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_PRI_MF_LENGTH), retBytes, FLAGS_PRI_MF_OFFSET);
- ByteArray.copyWhole(ByteArray.longToBytes(rPObj.getRequestId().getValue(), RID_F_LENGTH), retBytes, RID_F_OFFSET);
+ if (rpObj.isReoptimization() != null) {
+ flags.set(R_FLAG_OFFSET, rpObj.isReoptimization());
+ }
+ if (rpObj.isBiDirectional() != null) {
+ flags.set(B_FLAG_OFFSET, rpObj.isBiDirectional());
+ }
+ if (rpObj.isLoose() != null) {
+ flags.set(O_FLAG_OFFSET, rpObj.isLoose());
+ }
+ if (rpObj.isMakeBeforeBreak() != null) {
+ flags.set(M_FLAG_OFFSET, rpObj.isMakeBeforeBreak());
+ }
+ if (rpObj.isOrder() != null) {
+ flags.set(D_FLAG_OFFSET, rpObj.isOrder());
+ }
+ if (rpObj.isPathKey() != null) {
+ flags.set(P_FLAG_OFFSET, rpObj.isPathKey());
+ }
+ if (rpObj.isSupplyOf() != null) {
+ flags.set(S_FLAG_OFFSET, rpObj.isSupplyOf());
+ }
+ if (rpObj.isFragmentation() != null) {
+ flags.set(F_FLAG_OFFSET, rpObj.isFragmentation());
+ }
+ if (rpObj.isP2mp() != null) {
+ flags.set(N_FLAG_OFFSET, rpObj.isP2mp());
+ }
+ if (rpObj.isEroCompression() != null) {
+ flags.set(E_FLAG_OFFSET, rpObj.isEroCompression());
+ }
+ if (rpObj.getPriority() != null) {
+ final byte[] p = { 0, 0, 0, UnsignedBytes.checkedCast(rpObj.getPriority().shortValue())};
+ flags.or(ByteArray.bytesToBitSet(p));
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_PRI_MF_LENGTH));
+ body.writeInt(rpObj.getRequestId().getValue().intValue());
+ //FIXME: switch to ByteBuf
+ final byte[] tlvs = serializeTlvs(rpObj.getTlvs());
if (tlvs.length != 0) {
- ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
+ body.writeBytes(tlvs);
}
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
public byte[] serializeTlvs(final Tlvs tlvs) {
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
import java.util.List;
* field lengths in bytes
*/
private static final int FLAGS_F_LENGTH = 3;
- private static final int REQ_LIST_ITEM_LENGTH = 4;
/*
* fields offsets in bytes
*/
private static final int FLAGS_F_OFFSET = 1;
- private static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
/*
* flags offsets inside flags field in bits
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Svec)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
- }
-
+ public void serializeObject(final Object object, final ByteBuf buffer) {
+ Preconditions.checkArgument(object instanceof Svec, "Wrong instance of PCEPObject. Passed %s. Needed SvecObject.", object.getClass());
final Svec svecObj = (Svec) object;
- final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
- final List<RequestId> requestIDs = svecObj.getRequestsIds();
+ final ByteBuf body = Unpooled.buffer();
+ body.writeZero(FLAGS_F_OFFSET);
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
- flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
- flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
-
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
+ if (svecObj.isLinkDiverse() != null) {
+ flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
+ }
+ if (svecObj.isNodeDiverse() != null) {
+ flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
+ }
+ if (svecObj.isSrlgDiverse() != null) {
+ flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ final List<RequestId> requestIDs = svecObj.getRequestsIds();
+ assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
for (int i = 0; i < requestIDs.size(); i++) {
- System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue(), REQ_LIST_ITEM_LENGTH), 0, retBytes, REQ_LIST_ITEM_LENGTH
- * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
+ body.writeInt(requestIDs.get(i).getValue().intValue());
}
- assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder().build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setReason((short) 5);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setMinBandwidth(new Bandwidth(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = Lists.newArrayList();
subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
true).setSubobjectType(
- new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0xffffL)).build()).build()).build());
+ new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0xffffL)).build()).build()).build());
subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
true).setSubobjectType(
- new IpPrefixCaseBuilder().setIpPrefix(
- new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).build());
+ new IpPrefixCaseBuilder().setIpPrefix(
+ new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).build());
subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
true).setSubobjectType(
- new UnnumberedCaseBuilder().setUnnumbered(
- new UnnumberedBuilder().setRouterId(0xffffffffL).setInterfaceId(0xffffffffL).build()).build()).build());
+ new UnnumberedCaseBuilder().setUnnumbered(
+ new UnnumberedBuilder().setRouterId(0xffffffffL).setInterfaceId(0xffffffffL).build()).build()).build());
builder.setSubobject(subs);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setSubobject(subs);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder().setIpPrefix(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder().setIpPrefix(
new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).setProtectionAvailable(false).setProtectionInUse(
- false).build());
+ false).build());
subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder().setIpPrefix(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder().setIpPrefix(
new IpPrefix(Ipv6Util.prefixForBytes(ip6PrefixBytes, 22))).build()).build()).setProtectionAvailable(false).setProtectionInUse(
- false).build());
+ false).build());
subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder().setUnnumbered(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder().setRouterId(
0x1245678L).setInterfaceId(0x9abcdef0L).build()).build()).setProtectionAvailable(false).setProtectionInUse(
- false).build());
+ false).build());
builder.setSubobject(subs);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setBandwidth(new Bandwidth(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setBandwidth(new Bandwidth(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
Ipv4Util.addressForBytes(destIPBytes)).build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
Ipv6Util.addressForBytes(destIPBytes)).build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setValue((short) 1);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPErrorObject3.bin"));
builder.setTlvs(new TlvsBuilder().setReqMissing(new ReqMissingBuilder().setRequestId(new RequestId(0x00001155L)).build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder().build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspaObject2UpperBounds.bin"));
builder.setLocalProtectionDesired(true);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setValue(new Float32(new byte[] { 0, 0, 0, 0 }));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPMetricObject2UpperBounds.bin"));
builder.setValue(new Float32(new byte[] { (byte) 0x4f, (byte) 0x70, (byte) 0x00, (byte) 0x00 }));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setUnsatisfiedConstraints(true);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNoPathObject2WithTLV.bin"));
b.build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setValue((short) 0xff);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNotificationObject1WithTlv.bin"));
new OverloadDurationBuilder().setDuration(0xff0000a2L).build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setRequestId(new RequestId(0xdeadbeefL));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPRPObject2.bin"));
b.build()).build());
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setRequestsIds(Lists.newArrayList(new RequestId(0xFFL)));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPSvecObject1_10ReqIDs.bin"));
builder.setRequestsIds(requestIDs);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setClassType(new ClassType((short) 4));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
subs.add(new SubobjectBuilder().setMandatory(true).setSubobjectType(
new IpPrefixCaseBuilder().setIpPrefix(
new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("192.168.0.0/16"))).build()).build()).setAttribute(
- Attribute.Node).build());
+ Attribute.Node).build());
subs.add(new SubobjectBuilder().setMandatory(false).setSubobjectType(
new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x1234L)).build()).build()).build());
builder.setSubobject(subs);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setPathKeys(list);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setCode(new OfId(4));
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
builder.setOverBookingFactor((short) 99);
assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(result.array(), parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
}
@Test
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.protocol.pcep.segment.routing02.SegmentRoutingActivator;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
+import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
final ByteBuf result = Unpooled.wrappedBuffer(rpObjectWithPstTlvBytes);
assertEquals(builder.build(),
parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(rpObjectWithPstTlvBytes, parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(rpObjectWithPstTlvBytes,ByteArray.getAllBytes(buf));
}
@Test
builder.setIgnore(false);
builder.setOperationId(new SrpIdNumber(1L));
builder.addAugmentation(Srp1.class, new Srp1Builder().setRemove(true).build());
- builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder()
- .addAugmentation(Tlvs8.class,
- new Tlvs8Builder().setPathSetupType(new PathSetupTypeBuilder().setPst(true).build()).build())
+ builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder().addAugmentation(Tlvs8.class,
+ new Tlvs8Builder().setPathSetupType(new PathSetupTypeBuilder().setPst(true).build()).build())
.build());
final ByteBuf result = Unpooled.wrappedBuffer(srpObjectWithPstTlvBytes);
assertEquals(builder.build(),
parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(srpObjectWithPstTlvBytes, parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(srpObjectWithPstTlvBytes,ByteArray.getAllBytes(buf));
}
}
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
+import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapabilityBuilder;
@Test
public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
- final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(tlvRegistry);
+ final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(this.tlvRegistry);
final OpenBuilder builder = new OpenBuilder();
builder.setProcessingRule(false);
final ByteBuf result = Unpooled.wrappedBuffer(openObjectBytes);
assertEquals(builder.build(),
parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
- assertArrayEquals(openObjectBytes, parser.serializeObject(builder.build()));
+ ByteBuf buf = Unpooled.buffer();
+ parser.serializeObject(builder.build(), buf);
+ assertArrayEquals(openObjectBytes,ByteArray.getAllBytes(buf));
}
}
this.registry = Preconditions.checkNotNull(registry);
}
- protected byte[] serializeObject(final Object object) {
+ protected void serializeObject(final Object object, final ByteBuf buffer) {
if (object == null) {
- return new byte[] {};
+ return;
}
- return this.registry.serializeObject(object);
+ this.registry.serializeObject(object, buffer);
}
private List<Object> parseObjects(final ByteBuf bytes) throws PCEPDeserializerException {
new PcerrMessageBuilder().setErrorType(
new RequestCaseBuilder().setRequest(
new RequestBuilder().setRps(Lists.newArrayList(new RpsBuilder().setRp(rp).build())).build()).build()).setErrors(
- Arrays.asList(new ErrorsBuilder().setErrorObject(
- new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).getType()).setValue(
- maping.getFromErrorsEnum(e).getValue()).build()).build())).build()).build();
+ Arrays.asList(new ErrorsBuilder().setErrorObject(
+ new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).getType()).setValue(
+ maping.getFromErrorsEnum(e).getValue()).build()).build())).build()).build();
}
protected abstract Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException;
public interface MessageSerializer {
+ /**
+ * Serializes given message to bytes wrapped in given ByteBuf.
+ * @param message PCEP message to be serialized
+ * @param buffer ByteBuf wrapper around serialized message
+ */
void serializeMessage(Message message, ByteBuf buffer);
}
package org.opendaylight.protocol.pcep.spi;
import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
}
public static void formatMessage(final int messageType, final ByteBuf body, final ByteBuf out) {
- final int msgLength = body.readableBytes();
- final byte[] header = new byte[] { UnsignedBytes.checkedCast(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)),
- UnsignedBytes.checkedCast(messageType),
- UnsignedBytes.checkedCast((msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH) / 256),
- UnsignedBytes.checkedCast((msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH) % 256) };
- Preconditions.checkState(header.length == PCEPMessageConstants.COMMON_HEADER_LENGTH);
- out.writeBytes(header);
+ final int msgLength = body.writerIndex();
+ out.writeByte(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
+ out.writeByte(messageType);
+ out.writeShort(msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH);
+ Preconditions.checkState(out.writerIndex() == PCEPMessageConstants.COMMON_HEADER_LENGTH);
out.writeBytes(body);
}
}
* Find serializer for given object. Delegates parsing to found serializer.
*
* @param object to be parsed
- * @return null if the serializer for this object could not be found
+ * @param buffer ByteBuf wrapped aroung bytes representing given object
*/
- byte[] serializeObject(final Object object);
+ void serializeObject(final Object object, final ByteBuf buffer);
}
*/
package org.opendaylight.protocol.pcep.spi;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
public interface ObjectSerializer {
- byte[] serializeObject(Object object);
+ /**
+ * Serializes given object to bytes wrapped in given ByteBuf.
+ * @param message PCEP object to be serialized
+ * @param buffer ByteBuf wrapper around serialized object
+ */
+ void serializeObject(final Object object, final ByteBuf buffer);
}
*/
package org.opendaylight.protocol.pcep.spi;
-import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
+
+import java.util.BitSet;
import org.opendaylight.protocol.util.ByteArray;
private static final int HEADER_SIZE = 4;
- private static final int OC_F_LENGTH = 1;
- private static final int OT_FLAGS_MF_LENGTH = 1;
- private static final int OBJ_LENGTH_F_LENGTH = 2;
-
- private static final int OC_F_OFFSET = 0;
- private static final int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH;
- private static final int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH;
-
private static final int OT_SF_LENGTH = 4;
/*
- * flags offsets inside multi-filed
+ * flags offsets inside multi-field
*/
private static final int P_FLAG_OFFSET = 6;
private static final int I_FLAG_OFFSET = 7;
private ObjectUtil() {
}
- public static byte[] formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, final Boolean ignore,
- final byte[] valueBytes) {
-
- final byte[] bytes = new byte[HEADER_SIZE + valueBytes.length];
-
- // objClass
- bytes[0] = UnsignedBytes.checkedCast(objectClass);
-
- // objType_flags multi-field
- bytes[OT_FLAGS_MF_OFFSET] = UnsignedBytes.checkedCast(objectType << (Byte.SIZE - OT_SF_LENGTH));
- if (processingRule != null && processingRule) {
- bytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1;
- }
+ public static void formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, final Boolean ignore,
+ final ByteBuf body, final ByteBuf out) {
+ out.writeByte(objectClass);
+ BitSet flags = new BitSet(Byte.SIZE);
if (ignore != null && ignore) {
- bytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1;
+ flags.set(I_FLAG_OFFSET);
}
-
- // objLength
- System.arraycopy(ByteArray.intToBytes(valueBytes.length + HEADER_SIZE, OBJ_LENGTH_F_LENGTH), 0, bytes, OBJ_LENGTH_F_OFFSET,
- OBJ_LENGTH_F_LENGTH);
- System.arraycopy(valueBytes, 0, bytes, HEADER_SIZE, valueBytes.length);
- return bytes;
+ if (processingRule != null && processingRule) {
+ flags.set(P_FLAG_OFFSET);
+ }
+ byte[] flagB = ByteArray.bitSetToBytes(flags, 1);
+ int typeByte = objectType << OT_SF_LENGTH | flagB[0];
+ out.writeByte(typeByte);
+ out.writeShort(body.writerIndex() + HEADER_SIZE);
+ out.writeBytes(body);
}
}
}
@Override
- public byte[] serializeObject(Object object) {
+ public void serializeObject(final Object object, final ByteBuf buffer) {
final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
if (serializer == null) {
- return null;
+ return;
}
- return serializer.serializeObject(object);
+ serializer.serializeObject(object, buffer);
}
}
@Test
public void testLabelUtil() {
- byte[] expected = new byte[] { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
+ byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
assertArrayEquals(expected, LabelUtil.formatLabel(4, true, true, new byte[] { 1, 2, 3, 4 }));
}
@Test
public void testMessageUtil() {
- byte[] expected = new byte[] { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
+ byte[] expected = { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
ByteBuf out = Unpooled.buffer();
ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
MessageUtil.formatMessage(8, body, out);
@Test
public void testObjectUtil() {
- byte[] expected = new byte[] { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
- assertArrayEquals(expected, ObjectUtil.formatSubobject(1, 8, true, true, new byte[] { 1, 2 }));
+ byte[] expected = { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
+ ByteBuf out = Unpooled.buffer();
+ ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
+ ObjectUtil.formatSubobject(1, 8, true, true, body, out);
+ assertArrayEquals(expected, ByteArray.getAllBytes(out));
+ }
+
+ @Test
+ public void testObjectUtilFalse() {
+ byte[] expected = { 0x08, 0x10, 0, 0x06, 0x01, 0x02 };
+ ByteBuf out = Unpooled.buffer();
+ ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
+ ObjectUtil.formatSubobject(1, 8, false, false, body, out);
+ assertArrayEquals(expected, ByteArray.getAllBytes(out));
}
}