List of usage examples for io.netty.buffer ByteBuf readUnsignedShort
public abstract int readUnsignedShort();
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.action.OF13SetFieldActionSerializerTest.java
License:Open Source License
/** * Test identify ExperimenterClass serializer *//*from www.jav a2s . c om*/ @Test public void test() { OF13SetFieldActionSerializer ser = new OF13SetFieldActionSerializer(); ser.injectSerializerRegistry(registry); org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder actionBuilder = new ActionBuilder(); long experimenterId = 12L; ExperimenterIdCaseBuilder expCaseBuilder = new ExperimenterIdCaseBuilder(); ExperimenterBuilder expBuilder = new ExperimenterBuilder(); expBuilder.setExperimenter(new ExperimenterId(experimenterId)); expCaseBuilder.setExperimenter(expBuilder.build()); MatchEntryBuilder meb = new MatchEntryBuilder(); meb.setOxmClass(ExperimenterClass.class); meb.setOxmMatchField(OxmMatchFieldClass.class); meb.setMatchEntryValue(expCaseBuilder.build()); List<MatchEntry> matchEntry = new ArrayList<>(); MatchEntry me = meb.build(); matchEntry.add(me); SetFieldCaseBuilder caseBuilder = new SetFieldCaseBuilder(); SetFieldActionBuilder setFieldBuilder = new SetFieldActionBuilder(); setFieldBuilder.setMatchEntry(matchEntry); caseBuilder.setSetFieldAction(setFieldBuilder.build()); actionBuilder.setActionChoice(caseBuilder.build()); MatchEntrySerializerKey<?, ?> key = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, ExperimenterClass.class, OxmMatchFieldClass.class); key.setExperimenterId(experimenterId); registry.registerSerializer(key, serializerMock); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); ser.serialize(actionBuilder.build(), out); Mockito.verify(serializerMock, Mockito.times(1)).serialize((MatchEntry) Mockito.anyObject(), (ByteBuf) Mockito.anyObject()); int lenght = out.readableBytes(); Assert.assertEquals("Wrong - bad field code", ActionConstants.SET_FIELD_CODE, out.readUnsignedShort()); Assert.assertEquals("Wrong - bad lenght", lenght, out.readUnsignedShort()); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.AbstractBundleMessageFactoryTest.java
License:Open Source License
@Test public void writeBundleFlags() throws Exception { ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); AbstractBundleMessageFactory.writeBundleFlags(new BundleFlags(true, true), out); Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort()); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.BundleAddMessageFactoryTest.java
License:Open Source License
@Test public void testSerializeWithoutProperties() { BundleAddMessageBuilder builder = new BundleAddMessageBuilder(); builder.setBundleId(new BundleId(1L)); builder.setFlags(new BundleFlags(true, false)); Message innerMessage = AbstractBundleMessageFactoryTest.createPortModCase(); builder.setMessage(innerMessage);//from w ww . j a v a2s .c o m ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); Mockito.when(registry.getSerializer(Matchers.any(MessageTypeKey.class))).thenReturn(portModSerializer); ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry); factory.serialize(builder.build(), out); Assert.assertEquals("Wrong bundle ID", 1L, out.readUnsignedInt()); long padding = out.readUnsignedShort(); Assert.assertEquals("Wrong flags", 1, out.readUnsignedShort()); Mockito.verify(portModSerializer, Mockito.times(1)).serialize((PortMod) innerMessage, out); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.BundleAddMessageFactoryTest.java
License:Open Source License
@Test public void testSerializeWithExperimenterProperty() { BundleAddMessageBuilder builder = new BundleAddMessageBuilder(); builder.setBundleId(new BundleId(2L)); builder.setFlags(new BundleFlags(true, false)); Message innerMessage = AbstractBundleMessageFactoryTest.createPortModCase(); builder.setMessage(innerMessage);/*from ww w .ja va2 s. c om*/ BundleExperimenterPropertyData data = AbstractBundleMessageFactoryTest .createBundleExperimenterPropertyData(); builder.setBundleProperty(AbstractBundleMessageFactoryTest.createListWithBundleExperimenterProperty(data)); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); Mockito.when(registry.getSerializer(Matchers.any(MessageTypeKey.class))).thenReturn(portModSerializer) .thenReturn(propertySerializer); ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry); factory.serialize(builder.build(), out); Assert.assertEquals("Wrong bundle ID", 2L, out.readUnsignedInt()); long padding = out.readUnsignedShort(); Assert.assertEquals("Wrong flags", 1, out.readUnsignedShort()); Mockito.verify(portModSerializer, Mockito.times(1)).serialize((PortMod) innerMessage, out); Mockito.verify(propertySerializer, Mockito.times(1)).serialize(data, out); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.BundleControlFactoryTest.java
License:Open Source License
@Test public void testSerializeWithoutProperties() { BundleControlBuilder builder = new BundleControlBuilder(); builder.setBundleId(new BundleId(1L)); builder.setType(BundleControlType.ONFBCTOPENREQUEST); builder.setFlags(new BundleFlags(true, true)); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(builder.build(), out); Assert.assertEquals("Wrong bundle ID", 1L, out.readUnsignedInt()); Assert.assertEquals("Wrong type", BundleControlType.ONFBCTOPENREQUEST.getIntValue(), out.readUnsignedShort()); Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort()); Assert.assertTrue("Unexpected data", out.readableBytes() == 0); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.BundleControlFactoryTest.java
License:Open Source License
@Test public void testSerializeWithExperimenterProperty() { BundleControlBuilder builder = new BundleControlBuilder(); builder.setBundleId(new BundleId(3L)); builder.setType(BundleControlType.ONFBCTCOMMITREQUEST); builder.setFlags(new BundleFlags(false, true)); BundleExperimenterPropertyData data = AbstractBundleMessageFactoryTest .createBundleExperimenterPropertyData(); builder.setBundleProperty(AbstractBundleMessageFactoryTest.createListWithBundleExperimenterProperty(data)); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); Mockito.when(registry.getSerializer(Matchers.any(MessageTypeKey.class))).thenReturn(serializer); ((SerializerRegistryInjector) factory).injectSerializerRegistry(registry); factory.serialize(builder.build(), out); Assert.assertEquals("Wrong bundle ID", 3L, out.readUnsignedInt()); Assert.assertEquals("Wrong type", BundleControlType.ONFBCTCOMMITREQUEST.getIntValue(), out.readUnsignedShort()); Assert.assertEquals("Wrong flags", 2, out.readUnsignedShort()); Assert.assertEquals("Wrong property type", BundlePropertyType.ONFETBPTEXPERIMENTER.getIntValue(), out.readUnsignedShort());/* ww w . ja va2 s .co m*/ int length = out.readUnsignedShort(); Assert.assertEquals("Wrong experimenter ID", 1, out.readUnsignedInt()); Assert.assertEquals("Wrong experimenter type", 2, out.readUnsignedInt()); Mockito.verify(serializer, Mockito.times(1)).serialize(data, out); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.FlowModInputMessageFactoryTest.java
License:Open Source License
/** * @throws Exception //from ww w. ja va2s . c o m * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO */ @Test public void testFlowModInputMessageFactory() throws Exception { FlowModInputBuilder builder = new FlowModInputBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 }; builder.setCookie(new BigInteger(1, cookie)); byte[] cookieMask = new byte[] { (byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30 }; builder.setCookieMask(new BigInteger(1, cookieMask)); builder.setTableId(new TableId(65L)); builder.setCommand(FlowModCommand.forValue(2)); builder.setIdleTimeout(12); builder.setHardTimeout(0); builder.setPriority(126); builder.setBufferId(2L); builder.setOutPort(new PortNumber(4422L)); builder.setOutGroup(98L); builder.setFlags(new FlowModFlags(true, false, true, false, true)); MatchBuilder matchBuilder = new MatchBuilder(); matchBuilder.setType(OxmMatchType.class); List<MatchEntries> entries = new ArrayList<>(); MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(InPhyPort.class); entriesBuilder.setHasMask(false); PortNumberMatchEntryBuilder portNumberBuilder = new PortNumberMatchEntryBuilder(); portNumberBuilder.setPortNumber(new PortNumber(42L)); entriesBuilder.addAugmentation(PortNumberMatchEntry.class, portNumberBuilder.build()); entries.add(entriesBuilder.build()); entriesBuilder.setOxmClass(Nxm0Class.class); entriesBuilder.setOxmMatchField(IpEcn.class); entriesBuilder.setHasMask(false); EcnMatchEntryBuilder ecnBuilder = new EcnMatchEntryBuilder(); ecnBuilder.setEcn((short) 4); entriesBuilder.addAugmentation(EcnMatchEntry.class, ecnBuilder.build()); entries.add(entriesBuilder.build()); matchBuilder.setMatchEntries(entries); builder.setMatch(matchBuilder.build()); List<Instruction> instructions = new ArrayList<>(); InstructionBuilder insBuilder = new InstructionBuilder(); insBuilder.setType(GotoTable.class); TableIdInstructionBuilder idBuilder = new TableIdInstructionBuilder(); idBuilder.setTableId((short) 43); insBuilder.addAugmentation(TableIdInstruction.class, idBuilder.build()); instructions.add(insBuilder.build()); insBuilder.setType(WriteMetadata.class); MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder(); metaBuilder.setMetadata(cookie); metaBuilder.setMetadataMask(cookieMask); insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build()); instructions.add(insBuilder.build()); insBuilder = new InstructionBuilder(); insBuilder.setType(ApplyActions.class); insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build()); instructions.add(insBuilder.build()); builder.setInstruction(instructions); FlowModInput message = builder.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); FlowModInputMessageFactory factory = FlowModInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message)); cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES]; out.readBytes(cookie); Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie)); cookieMask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES]; out.readBytes(cookieMask); Assert.assertEquals("Wrong cookieMask", message.getCookieMask(), new BigInteger(1, cookieMask)); Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readUnsignedByte()); Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedByte()); Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort()); Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort()); Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readUnsignedShort()); Assert.assertEquals("Wrong bufferId", message.getBufferId().intValue(), out.readUnsignedInt()); Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt()); Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt()); Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFlagsFromBitmap(out.readUnsignedShort())); out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE); Assert.assertEquals("Wrong match type", 1, out.readUnsignedShort()); out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort()); short fieldAndMask = out.readUnsignedByte(); Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1); Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1); out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES); Assert.assertEquals("Wrong oxm value", 42, out.readUnsignedInt()); Assert.assertEquals("Wrong oxm class", 0, out.readUnsignedShort()); fieldAndMask = out.readUnsignedByte(); Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1); Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1); out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES); Assert.assertEquals("Wrong oxm value", 4, out.readUnsignedByte()); out.skipBytes(7); Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort()); out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); Assert.assertEquals("Wrong instruction value", 43, out.readUnsignedByte()); out.skipBytes(3); Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort()); out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); out.skipBytes(EncodeConstants.SIZE_OF_INT_IN_BYTES); byte[] cookieRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES]; out.readBytes(cookieRead); byte[] cookieMaskRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES]; out.readBytes(cookieMaskRead); Assert.assertArrayEquals("Wrong metadata", cookie, cookieRead); Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead); Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort()); out.skipBytes(4); Assert.assertTrue("Unread data", out.readableBytes() == 0); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.FlowRemovedMessageFactoryTest.java
License:Open Source License
@Test public void testSerialize() throws Exception { FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setCookie(BigInteger.valueOf(1234L)); builder.setPriority(1234);/*from ww w . ja v a2 s . c o m*/ builder.setReason(FlowRemovedReason.forValue(2)); builder.setTableId(new TableId(65L)); builder.setDurationSec(1234L); builder.setDurationNsec(1234L); builder.setIdleTimeout(1234); builder.setHardTimeout(1234); builder.setPacketCount(BigInteger.valueOf(1234L)); builder.setByteCount(BigInteger.valueOf(1234L)); MatchBuilder matchBuilder = new MatchBuilder(); matchBuilder.setType(OxmMatchType.class); List<MatchEntry> entries = new ArrayList<>(); MatchEntryBuilder entriesBuilder = new MatchEntryBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(InPhyPort.class); entriesBuilder.setHasMask(false); InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder(); InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder(); inPhyPortBuilder.setPortNumber(new PortNumber(42L)); inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build()); entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build()); entries.add(entriesBuilder.build()); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(IpEcn.class); entriesBuilder.setHasMask(false); IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder(); IpEcnBuilder ipEcnBuilder = new IpEcnBuilder(); ipEcnBuilder.setEcn((short) 4); ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build()); entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build()); entries.add(entriesBuilder.build()); matchBuilder.setMatchEntry(entries); builder.setMatch(matchBuilder.build()); FlowRemovedMessage message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 72); Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong priority", message.getPriority().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readByte()); Assert.assertEquals("Wrong Table ID", message.getTableId().getValue().intValue(), serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong duration sec", message.getDurationSec().intValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong duration nsec", message.getDurationNsec().intValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong Idle timeout", message.getIdleTimeout().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong Hard timeout", message.getIdleTimeout().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong Packet count", message.getPacketCount().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong Byte count", message.getByteCount().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort()); short fieldAndMask = serializedBuffer.readUnsignedByte(); Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1); Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1); serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES); Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort()); fieldAndMask = serializedBuffer.readUnsignedByte(); Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1); Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1); serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES); Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(7); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.GroupModInputMessageFactoryTest.java
License:Open Source License
/** * @throws Exception//from w w w. j a v a 2 s. com * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO */ @Test public void testGroupModInputMessage() throws Exception { GroupModInputBuilder builder = new GroupModInputBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setCommand(GroupModCommand.forValue(2)); builder.setType(GroupType.forValue(3)); builder.setGroupId(new GroupId(256L)); List<BucketsList> exp = createBucketsList(); builder.setBucketsList(exp); GroupModInput message = builder.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); GroupModInputMessageFactory factory = GroupModInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message)); Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort()); Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte()); out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE); Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt()); List<BucketsList> rec = createBucketsListFromBufer(out); Assert.assertArrayEquals("Wrong bucketList", exp.toArray(), rec.toArray()); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.GroupModInputMessageFactoryTest.java
License:Open Source License
private static List<BucketsList> createBucketsListFromBufer(ByteBuf out) { List<BucketsList> bucketsList = new ArrayList<>(); BucketsListBuilder bucketsBuilder = new BucketsListBuilder(); BucketsList bucket;/*from w w w .j a v a 2s. c om*/ out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); bucketsBuilder.setWeight(out.readUnsignedShort()); bucketsBuilder.setWatchPort(new PortNumber(out.readUnsignedInt())); bucketsBuilder.setWatchGroup(out.readUnsignedInt()); out.skipBytes(4); bucket = bucketsBuilder.build(); bucketsList.add(bucket); return bucketsList; }