X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fdeserialization%2FDeserializationFactory.java;h=afc01f621e55939a23fc1aa3a3cc815aa45b1b85;hb=7424f6fec6016081265f98bdd9c5b17f671c0d88;hp=db137fb7dd54d97c37eb50dd21c418c5dfec5574;hpb=7d9b6038060779d819e17d5add8556dcc097fcea;p=openflowjava.git diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DeserializationFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DeserializationFactory.java index db137fb7..afc01f62 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DeserializationFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DeserializationFactory.java @@ -1,39 +1,65 @@ -/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ -package org.opendaylight.openflowjava.protocol.impl.deserialization; - -import io.netty.buffer.ByteBuf; - -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author michal.polkorab - * @author timotej.kubas - */ -public abstract class DeserializationFactory { - - private static final Logger LOGGER = LoggerFactory - .getLogger(DeserializationFactory.class); - - /** - * Transforms ByteBuf into correct POJO message - * @param rawMessage - * @param version version decoded from OpenFlow protocol message - * @return correct POJO as DataObject - */ - public static DataObject bufferToMessage(ByteBuf rawMessage, short version) { - DataObject dataObject = null; - short type = rawMessage.readUnsignedByte(); - rawMessage.skipBytes(Short.SIZE / Byte.SIZE); - - MessageTypeCodeKey msgTypeCodeKey = new MessageTypeCodeKey(version, type); - OFDeserializer decoder = DecoderTable.getInstance().getDecoder(msgTypeCodeKey); - if (decoder != null) { - dataObject = decoder.bufferToMessage(rawMessage, version); - } else { - LOGGER.warn("No correct decoder found in DecoderTable for arguments: " + msgTypeCodeKey.toString()); - } - return dataObject; - } -} +/* + * 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.deserialization; + +import io.netty.buffer.ByteBuf; + +import java.util.HashMap; +import java.util.Map; + +import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry; +import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer; +import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * @author michal.polkorab + * @author timotej.kubas + */ +public class DeserializationFactory { + + private DeserializerRegistry registry; + private Map> messageClassMap; + + /** + * Constructor + */ + public DeserializationFactory() { + messageClassMap = new HashMap<>(); + TypeToClassMapInitializer.initializeTypeToClassMap(messageClassMap); + } + + /** + * Transforms ByteBuf into correct POJO message + * @param rawMessage + * @param version version decoded from OpenFlow protocol message + * @return correct POJO as DataObject + */ + public DataObject deserialize(ByteBuf rawMessage, short version) { + DataObject dataObject = null; + int type = rawMessage.readUnsignedByte(); + Class clazz = messageClassMap.get(new TypeToClassKey(version, type)); + rawMessage.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); + OFDeserializer deserializer = registry.getDeserializer( + new MessageCodeKey(version, type, clazz)); + if (deserializer != null) { + dataObject = deserializer.deserialize(rawMessage); + } + return dataObject; + } + + /** + * @param registry + */ + public void setRegistry(DeserializerRegistry registry) { + this.registry = registry; + } + +}