List of usage examples for io.netty.buffer ByteBuf readShort
public abstract short readShort();
From source file:org.opendaylight.openflowjava.nx.codec.action.LearnCodecUtil.java
License:Open Source License
private static FlowMods readFlowToPort(ByteBuf message, short numBits) { FlowModOutputToPortBuilder builder = new FlowModOutputToPortBuilder(); builder.setSrcField((long) message.readInt()); builder.setSrcOfs((int) message.readShort()); builder.setFlowModNumBits((int) numBits); length -= TO_PORT_LENGTH - EncodeConstants.SIZE_OF_SHORT_IN_BYTES; FlowModsBuilder flowModsBuilder = new FlowModsBuilder(); FlowModOutputToPortCaseBuilder caseBuilder = new FlowModOutputToPortCaseBuilder(); caseBuilder.setFlowModOutputToPort(builder.build()); flowModsBuilder.setFlowModSpec(caseBuilder.build()); return flowModsBuilder.build(); }
From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10PacketOutInputMessageFactory.java
License:Open Source License
@Override public PacketOutInput deserialize(ByteBuf rawMessage) { PacketOutInputBuilder builder = new PacketOutInputBuilder(); builder.setVersion((short) EncodeConstants.OF10_VERSION_ID); builder.setXid(rawMessage.readUnsignedInt()); builder.setBufferId(rawMessage.readUnsignedInt()); builder.setInPort(new PortNumber((long) rawMessage.readUnsignedShort())); int actions_len = rawMessage.readShort(); CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF10_VERSION_ID); List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF10_VERSION_ID, actions_len, rawMessage, keyMaker, registry); builder.setAction(actions);/* www. ja v a 2 s . c o m*/ byte[] data = new byte[rawMessage.readableBytes()]; rawMessage.readBytes(data); if (data != null) { builder.setData(data); } return builder.build(); }
From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.PacketOutInputMessageFactory.java
License:Open Source License
@Override public PacketOutInput deserialize(ByteBuf rawMessage) { PacketOutInputBuilder builder = new PacketOutInputBuilder(); builder.setVersion((short) EncodeConstants.OF13_VERSION_ID); builder.setXid(rawMessage.readUnsignedInt()); builder.setBufferId(rawMessage.readUnsignedInt()); builder.setInPort(new PortNumber(rawMessage.readUnsignedInt())); int actions_len = rawMessage.readShort(); rawMessage.skipBytes(PADDING);/*w ww. j a va2s. c o m*/ CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID); List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID, actions_len, rawMessage, keyMaker, registry); builder.setAction(actions); byte[] data = new byte[rawMessage.readableBytes()]; rawMessage.readBytes(data); if (data != null) { builder.setData(data); } return builder.build(); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.ErrorMessageFactoryTest.java
License:Open Source License
@Test public void testSerialize() throws Exception { ErrorMessageBuilder builder = new ErrorMessageBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setType(10);/*from w w w .ja v a 2 s. c om*/ builder.setCode(20); byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"); builder.setData(data); ErrorMessage message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 28); Assert.assertEquals("Wrong Type", message.getType().intValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong Code", message.getCode().intValue(), serializedBuffer.readShort()); byte[] readData = new byte[serializedBuffer.readableBytes()]; serializedBuffer.readBytes(readData); Assert.assertArrayEquals("Wrong data", message.getData(), readData); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.FlowModInputMessageFactoryTest.java
License:Open Source License
/** * @throws Exception /*from www.j a v a2 s .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 w w w. j a 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.GetConfigReplyMessageFactoryTest.java
License:Open Source License
@Test public void testSerialize() throws Exception { GetConfigOutputBuilder builder = new GetConfigOutputBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setFlags(SwitchConfigFlag.forValue(2)); builder.setMissSendLen(20);//w w w. j a v a 2 s. c om GetConfigOutput message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 12); Assert.assertEquals("Wrong Type", message.getFlags().getIntValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong Code", message.getMissSendLen().intValue(), serializedBuffer.readShort()); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.MeterModInputMessageFactoryTest.java
License:Open Source License
/** * @throws Exception /*from w w w. jav a 2 s. com*/ * Testing of {@link MeterModInputMessageFactory} for correct translation from POJO */ @Test public void testMeterModInputMessage() throws Exception { MeterModInputBuilder builder = new MeterModInputBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setCommand(MeterModCommand.forValue(1)); builder.setFlags(new MeterFlags(false, true, true, false)); builder.setMeterId(new MeterId(2248L)); builder.setBands(createBandsList()); MeterModInput message = builder.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); MeterModInputMessageFactory factory = MeterModInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); BufferHelper.checkHeaderV13(out, factory.getMessageType(), 64); Assert.assertEquals("Wrong meterModCommand", message.getCommand().getIntValue(), out.readUnsignedShort()); Assert.assertEquals("Wrong meterFlags", message.getFlags(), decodeMeterModFlags(out.readShort())); Assert.assertEquals("Wrong meterId", message.getMeterId().getValue().intValue(), out.readUnsignedInt()); Assert.assertEquals("Wrong bands", message.getBands(), decodeBandsList(out)); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactoryTest.java
License:Open Source License
@Test public void testMultipartRequestTableFeaturesMessageFactory() throws Exception { MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setFlags(new MultipartRequestFlags(true)); builder.setType(MultipartType.forValue(12)); MultipartReplyTableFeaturesCaseBuilder caseBuilder = new MultipartReplyTableFeaturesCaseBuilder(); MultipartReplyTableFeaturesBuilder featuresBuilder = new MultipartReplyTableFeaturesBuilder(); List<TableFeatures> tableFeaturesList = new ArrayList<>(); TableFeaturesBuilder tableFeaturesBuilder = new TableFeaturesBuilder(); tableFeaturesBuilder.setTableId((short) 8); tableFeaturesBuilder.setName("AAAABBBBCCCCDDDDEEEEFFFFGGGG"); tableFeaturesBuilder.setMetadataMatch(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01 }); tableFeaturesBuilder.setMetadataWrite(new byte[] { 0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01 }); tableFeaturesBuilder.setConfig(new TableConfig(true)); tableFeaturesBuilder.setMaxEntries(65L); List<TableFeatureProperties> properties = new ArrayList<>(); TableFeaturePropertiesBuilder propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTNEXTTABLES); NextTableRelatedTableFeaturePropertyBuilder nextPropBuilder = new NextTableRelatedTableFeaturePropertyBuilder(); List<NextTableIds> nextIds = new ArrayList<>(); nextIds.add(new NextTableIdsBuilder().setTableId((short) 1).build()); nextIds.add(new NextTableIdsBuilder().setTableId((short) 2).build()); nextPropBuilder.setNextTableIds(nextIds); propBuilder.addAugmentation(NextTableRelatedTableFeatureProperty.class, nextPropBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTNEXTTABLESMISS); nextPropBuilder = new NextTableRelatedTableFeaturePropertyBuilder(); nextIds = new ArrayList<>(); nextPropBuilder.setNextTableIds(nextIds); propBuilder.addAugmentation(NextTableRelatedTableFeatureProperty.class, nextPropBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTINSTRUCTIONS); InstructionRelatedTableFeaturePropertyBuilder insPropBuilder = new InstructionRelatedTableFeaturePropertyBuilder(); List<Instruction> insIds = new ArrayList<>(); InstructionBuilder insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new WriteActionsCaseBuilder().build()); insIds.add(insBuilder.build());/*from w ww .j a va2 s. co m*/ insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new GotoTableCaseBuilder().build()); insIds.add(insBuilder.build()); insPropBuilder.setInstruction(insIds); propBuilder.addAugmentation(InstructionRelatedTableFeatureProperty.class, insPropBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTINSTRUCTIONSMISS); insPropBuilder = new InstructionRelatedTableFeaturePropertyBuilder(); insIds = new ArrayList<>(); insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new WriteMetadataCaseBuilder().build()); insIds.add(insBuilder.build()); insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new ApplyActionsCaseBuilder().build()); insIds.add(insBuilder.build()); insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new MeterCaseBuilder().build()); insIds.add(insBuilder.build()); insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new ClearActionsCaseBuilder().build()); insIds.add(insBuilder.build()); insBuilder = new InstructionBuilder(); insBuilder.setInstructionChoice(new GotoTableCaseBuilder().build()); insIds.add(insBuilder.build()); insPropBuilder.setInstruction(insIds); propBuilder.addAugmentation(InstructionRelatedTableFeatureProperty.class, insPropBuilder.build()); properties.add(propBuilder.build()); tableFeaturesBuilder.setTableFeatureProperties(properties); tableFeaturesList.add(tableFeaturesBuilder.build()); tableFeaturesBuilder = new TableFeaturesBuilder(); tableFeaturesBuilder.setTableId((short) 8); tableFeaturesBuilder.setName("AAAABBBBCCCCDDDDEEEEFFFFGGGG"); byte[] metadataMatch = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01 }; tableFeaturesBuilder.setMetadataMatch(metadataMatch); byte[] metadataWrite = new byte[] { 0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01 }; tableFeaturesBuilder.setMetadataWrite(metadataWrite); tableFeaturesBuilder.setConfig(new TableConfig(true)); tableFeaturesBuilder.setMaxEntries(67L); properties = new ArrayList<>(); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTWRITEACTIONS); ActionRelatedTableFeaturePropertyBuilder actBuilder = new ActionRelatedTableFeaturePropertyBuilder(); List<Action> actions = new ArrayList<>(); ActionBuilder actionBuilder = new ActionBuilder(); actionBuilder.setActionChoice(new OutputActionCaseBuilder().build()); actions.add(actionBuilder.build()); actBuilder.setAction(actions); propBuilder.addAugmentation(ActionRelatedTableFeatureProperty.class, actBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTWRITEACTIONSMISS); actBuilder = new ActionRelatedTableFeaturePropertyBuilder(); actions = new ArrayList<>(); actBuilder.setAction(actions); propBuilder.addAugmentation(ActionRelatedTableFeatureProperty.class, actBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTAPPLYACTIONS); actBuilder = new ActionRelatedTableFeaturePropertyBuilder(); actions = new ArrayList<>(); actBuilder.setAction(actions); propBuilder.addAugmentation(ActionRelatedTableFeatureProperty.class, actBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTAPPLYACTIONSMISS); actBuilder = new ActionRelatedTableFeaturePropertyBuilder(); actions = new ArrayList<>(); actBuilder.setAction(actions); propBuilder.addAugmentation(ActionRelatedTableFeatureProperty.class, actBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTMATCH); OxmRelatedTableFeaturePropertyBuilder oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder(); List<MatchEntry> entries = new ArrayList<>(); MatchEntryBuilder entriesBuilder = new MatchEntryBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(InPhyPort.class); entriesBuilder.setHasMask(false); entries.add(entriesBuilder.build()); entriesBuilder = new MatchEntryBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(InPort.class); entriesBuilder.setHasMask(false); entries.add(entriesBuilder.build()); oxmBuilder.setMatchEntry(entries); propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTWILDCARDS); oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder(); entries = new ArrayList<>(); oxmBuilder.setMatchEntry(entries); propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTWRITESETFIELD); oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder(); entries = new ArrayList<>(); oxmBuilder.setMatchEntry(entries); propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTWRITESETFIELDMISS); oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder(); entries = new ArrayList<>(); oxmBuilder.setMatchEntry(entries); propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTAPPLYSETFIELD); oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder(); entries = new ArrayList<>(); entriesBuilder = new MatchEntryBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(IpProto.class); entriesBuilder.setHasMask(false); entries.add(entriesBuilder.build()); entriesBuilder = new MatchEntryBuilder(); entriesBuilder.setOxmClass(OpenflowBasicClass.class); entriesBuilder.setOxmMatchField(IpEcn.class); entriesBuilder.setHasMask(false); entries.add(entriesBuilder.build()); oxmBuilder.setMatchEntry(entries); propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build()); properties.add(propBuilder.build()); propBuilder = new TableFeaturePropertiesBuilder(); propBuilder.setType(TableFeaturesPropType.OFPTFPTAPPLYSETFIELDMISS); oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder(); entries = new ArrayList<>(); oxmBuilder.setMatchEntry(entries); propBuilder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build()); properties.add(propBuilder.build()); tableFeaturesBuilder.setTableFeatureProperties(properties); tableFeaturesList.add(tableFeaturesBuilder.build()); featuresBuilder.setTableFeatures(tableFeaturesList); caseBuilder.setMultipartReplyTableFeatures(featuresBuilder.build()); builder.setMultipartReplyBody(caseBuilder.build()); MultipartReplyMessage message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 520); Assert.assertEquals("Wrong type", MultipartType.OFPMPTABLEFEATURES.getIntValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong flags", message.getFlags(), createMultipartRequestFlags(serializedBuffer.readShort())); serializedBuffer.skipBytes(PADDING); Assert.assertEquals("Wrong length", 232, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong registry-id", 8, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(5); Assert.assertEquals("Wrong name", "AAAABBBBCCCCDDDDEEEEFFFFGGGG", ByteBufUtils.decodeNullTerminatedString(serializedBuffer, 32)); byte[] metadataMatchOutput = new byte[metadataMatch.length]; serializedBuffer.readBytes(metadataMatchOutput); Assert.assertArrayEquals("Wrong metadata-match", new byte[] { 0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01 }, metadataMatchOutput); serializedBuffer.skipBytes(64 - metadataMatch.length); byte[] metadataWriteOutput = new byte[metadataWrite.length]; serializedBuffer.readBytes(metadataWriteOutput); Assert.assertArrayEquals("Wrong metadata-write", new byte[] { 0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01 }, metadataWriteOutput); serializedBuffer.skipBytes(64 - metadataWrite.length); Assert.assertEquals("Wrong config", 1, serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong max-entries", 65, serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong property type", 2, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 6, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong next-registry-id", 1, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong next-registry-id", 2, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(2); Assert.assertEquals("Wrong property type", 3, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 0, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 12, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 3, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 1, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 1, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 24, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 2, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 6, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 5, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction type", 1, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong instruction length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong length", 272, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong registry-id", 8, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(5); Assert.assertEquals("Wrong name", "AAAABBBBCCCCDDDDEEEEFFFFGGGG", ByteBufUtils.decodeNullTerminatedString(serializedBuffer, 32)); metadataMatchOutput = new byte[metadataMatch.length]; serializedBuffer.readBytes(metadataMatchOutput); serializedBuffer.skipBytes(64 - metadataMatch.length); Assert.assertArrayEquals("Wrong metadata-match", new byte[] { 0x00, 0x01, 0x02, 0x03, 0x01, 0x04, 0x08, 0x01 }, metadataMatchOutput); metadataWriteOutput = new byte[metadataWrite.length]; serializedBuffer.readBytes(metadataWriteOutput); serializedBuffer.skipBytes(64 - metadataWrite.length); Assert.assertArrayEquals("Wrong metadata-write", new byte[] { 0x00, 0x07, 0x01, 0x05, 0x01, 0x00, 0x03, 0x01 }, metadataWriteOutput); Assert.assertEquals("Wrong config", 1, serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong max-entries", 67, serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong property type", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 8, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong action type", 0, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong action length", 4, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property type", 5, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 6, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 7, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 8, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 12, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong match class", 0x8000, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong match field&mask", 2, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong match length", 4, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong match class", 0x8000, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong match field&mask", 0, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong match length", 4, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 10, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 12, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 13, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 14, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 12, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong match class", 0x8000, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong match field&mask", 20, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong match length", 1, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong match class", 0x8000, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong match field&mask", 18, serializedBuffer.readUnsignedByte()); Assert.assertEquals("Wrong match length", 1, serializedBuffer.readUnsignedByte()); serializedBuffer.skipBytes(4); Assert.assertEquals("Wrong property type", 15, serializedBuffer.readUnsignedShort()); Assert.assertEquals("Wrong property length", 4, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(4); Assert.assertTrue("Unread data", serializedBuffer.readableBytes() == 0); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactoryTest.java
License:Open Source License
@Test public void testPortDescSerialize() throws Exception { MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setFlags(new MultipartRequestFlags(true)); builder.setType(MultipartType.forValue(13)); MultipartReplyPortDescCaseBuilder portDescCase = new MultipartReplyPortDescCaseBuilder(); MultipartReplyPortDescBuilder portDesc = new MultipartReplyPortDescBuilder(); portDesc.setPorts(createPortList()); portDescCase.setMultipartReplyPortDesc(portDesc.build()); builder.setMultipartReplyBody(portDescCase.build()); MultipartReplyMessage message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 80); Assert.assertEquals("Wrong type", MultipartType.OFPMPPORTDESC.getIntValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong flags", message.getFlags(), createMultipartRequestFlags(serializedBuffer.readShort())); serializedBuffer.skipBytes(PADDING); MultipartReplyPortDescCase body = (MultipartReplyPortDescCase) message.getMultipartReplyBody(); MultipartReplyPortDesc messageOutput = body.getMultipartReplyPortDesc(); Ports port = messageOutput.getPorts().get(0); Assert.assertEquals("Wrong PortNo", port.getPortNo().intValue(), serializedBuffer.readUnsignedInt()); serializedBuffer.skipBytes(4);/*from ww w . j ava 2s. co m*/ byte[] address = new byte[6]; serializedBuffer.readBytes(address); Assert.assertEquals("Wrong MacAddress", port.getHwAddr().getValue().toLowerCase(), new MacAddress(ByteBufUtils.macAddressToString(address)).getValue().toLowerCase()); serializedBuffer.skipBytes(2); byte[] name = new byte[16]; serializedBuffer.readBytes(name); Assert.assertEquals("Wrong name", port.getName(), new String(name).trim()); Assert.assertEquals("Wrong config", port.getConfig(), createPortConfig(serializedBuffer.readInt())); Assert.assertEquals("Wrong state", port.getState(), createPortState(serializedBuffer.readInt())); Assert.assertEquals("Wrong current", port.getCurrentFeatures(), createPortFeatures(serializedBuffer.readInt())); Assert.assertEquals("Wrong advertised", port.getAdvertisedFeatures(), createPortFeatures(serializedBuffer.readInt())); Assert.assertEquals("Wrong supported", port.getSupportedFeatures(), createPortFeatures(serializedBuffer.readInt())); Assert.assertEquals("Wrong peer", port.getPeerFeatures(), createPortFeatures(serializedBuffer.readInt())); Assert.assertEquals("Wrong Current speed", port.getCurrSpeed().longValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong Max speed", port.getMaxSpeed().longValue(), serializedBuffer.readInt()); }