X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fserialization%2Ffactories%2FOF10FlowModInputMessageFactory.java;h=7ba6e7ebef1e6480c047f647a288cc2eb2a57d55;hb=60ad94454217d7d1f6f9e65f39f72ae111ecc845;hp=7821cd4979ad354993b1c37f63fd822c8a1da9a4;hpb=09a089ee658f6fc1627b5eafdbb29249e7c7ffb9;p=openflowjava.git diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/OF10FlowModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/OF10FlowModInputMessageFactory.java index 7821cd49..7ba6e7eb 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/OF10FlowModInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/OF10FlowModInputMessageFactory.java @@ -1,75 +1,67 @@ -/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ -package org.opendaylight.openflowjava.protocol.impl.serialization.factories; - -import io.netty.buffer.ByteBuf; - -import java.util.HashMap; -import java.util.Map; - -import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer; -import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils; -import org.opendaylight.openflowjava.protocol.impl.util.OF10ActionsSerializer; -import org.opendaylight.openflowjava.protocol.impl.util.OF10MatchSerializer; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; - -/** - * Translates FlowMod messages - * @author michal.polkorab - */ -public class OF10FlowModInputMessageFactory implements OFSerializer { - - private static final byte MESSAGE_TYPE = 14; - private static final int MESSAGE_LENGTH = 72; - - private static OF10FlowModInputMessageFactory instance; - - private OF10FlowModInputMessageFactory() { - // singleton - } - - /** - * @return singleton factory - */ - public static synchronized OF10FlowModInputMessageFactory getInstance() { - if(instance == null) { - instance = new OF10FlowModInputMessageFactory(); - } - return instance; - } - - @Override - public void messageToBuffer(short version, ByteBuf out, FlowModInput message) { - ByteBufUtils.writeOFHeader(instance, message, out); - OF10MatchSerializer.encodeMatchV10(out, message.getMatchV10()); - out.writeLong(message.getCookie().longValue()); - out.writeShort(message.getCommand().getIntValue()); - out.writeShort(message.getIdleTimeout().intValue()); - out.writeShort(message.getHardTimeout().intValue()); - out.writeShort(message.getPriority()); - out.writeInt(message.getBufferId().intValue()); - out.writeShort(message.getOutPort().getValue().intValue()); - out.writeShort(createFlowModFlagsBitmask(message.getFlagsV10())); - OF10ActionsSerializer.encodeActionsV10(out, message.getActionsList()); - } - - @Override - public int computeLength(FlowModInput message) { - return MESSAGE_LENGTH + OF10ActionsSerializer.computeActionsLength(message.getActionsList()); - } - - @Override - public byte getMessageType() { - return MESSAGE_TYPE; - } - - private static int createFlowModFlagsBitmask(FlowModFlagsV10 flags) { - int flowModFlagBitmask = 0; - Map flowModFlagsMap = new HashMap<>(); - flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM()); - flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP()); - flowModFlagsMap.put(2, flags.isOFPFFEMERG()); - flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap); - return flowModFlagBitmask; - } -} +/* + * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * 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 + */ + +package org.opendaylight.openflowjava.protocol.impl.serialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector; +import org.opendaylight.openflowjava.util.ByteBufUtils; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker; +import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory; +import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; + +/** + * Translates FlowMod messages + * @author michal.polkorab + */ +public class OF10FlowModInputMessageFactory implements OFSerializer, SerializerRegistryInjector { + + private static final byte MESSAGE_TYPE = 14; + private static final TypeKeyMaker ACTION_KEY_MAKER = + TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF10_VERSION_ID); + private SerializerRegistry registry; + + @Override + public void serialize(final FlowModInput message, final ByteBuf outBuffer) { + ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH); + OFSerializer matchSerializer = registry.getSerializer(new MessageTypeKey<>( + message.getVersion(), MatchV10.class)); + matchSerializer.serialize(message.getMatchV10(), outBuffer); + outBuffer.writeLong(message.getCookie().longValue()); + outBuffer.writeShort(message.getCommand().getIntValue()); + outBuffer.writeShort(message.getIdleTimeout().intValue()); + outBuffer.writeShort(message.getHardTimeout().intValue()); + outBuffer.writeShort(message.getPriority()); + outBuffer.writeInt(message.getBufferId().intValue()); + outBuffer.writeShort(message.getOutPort().getValue().intValue()); + outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlagsV10())); + ListSerializer.serializeList(message.getAction(), ACTION_KEY_MAKER, registry, outBuffer); + ByteBufUtils.updateOFHeaderLength(outBuffer); + } + + private static int createFlowModFlagsBitmask(final FlowModFlagsV10 flags) { + return ByteBufUtils.fillBitMask(0, + flags.isOFPFFSENDFLOWREM(), + flags.isOFPFFCHECKOVERLAP(), + flags.isOFPFFEMERG()); + } + + @Override + public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) { + this.registry = serializerRegistry; + } +}