/**
 * Copyright (c) 2015 Bosch Software Innovations GmbH 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.eclipse.hawkbit.amqp;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.hawkbit.dmf.amqp.api.AmqpSettings;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * The spring AMQP configuration which is enabled by using the profile
 * {@code amqp} to use a AMQP for communication with SP enabled devices.
 *
 *
 *
 */
@EnableConfigurationProperties(AmqpProperties.class)
public class AmqpConfiguration {

    @Autowired
    protected AmqpProperties amqpProperties;

    @Autowired
    private ConnectionFactory connectionFactory;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * Method to set the Jackson2JsonMessageConverter.
     *
     * @return the Jackson2JsonMessageConverter
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        final Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
        return jackson2JsonMessageConverter;
    }

    /**
     * Create the sp receiver queue.
     *
     * @return the receiver queue
     */
    @Bean
    public Queue receiverQueue() {
        return new Queue(amqpProperties.getReceiverQueue(), true, false, false, getDeadLetterExchangeArgs());
    }

    /**
     * Create the dead letter fanout exchange.
     *
     * @return the fanout exchange
     */
    @Bean
    public FanoutExchange senderExchange() {
        return new FanoutExchange(AmqpSettings.DMF_EXCHANGE);
    }

    /**
     * Create dead letter queue.
     *
     * @return the queue
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(amqpProperties.getDeadLetterQueue());
    }

    /**
     * Create the dead letter fanout exchange.
     *
     * @return the fanout exchange
     */
    @Bean
    public FanoutExchange exchangeDeadLetter() {
        return new FanoutExchange(amqpProperties.getDeadLetterExchange());
    }

    /**
     * Create the Binding deadLetterQueue to exchangeDeadLetter.
     *
     * @return the binding
     */
    @Bean
    public Binding bindDeadLetterQueueToLwm2mExchange() {
        return BindingBuilder.bind(deadLetterQueue()).to(exchangeDeadLetter());
    }

    /**
     * Create the Binding {@link AmqpConfiguration#receiverQueueFromSp()} to
     * {@link AmqpConfiguration#senderConnectorToSpExchange()}.
     *
     * @return the binding and create the queue and exchange
     */
    @Bean
    public Binding bindSenderExchangeToSpQueue() {
        return BindingBuilder.bind(receiverQueue()).to(senderExchange());
    }

    /**
     * Create amqp handler service bean.
     *
     * @return handler service bean
     */
    @Bean
    public AmqpMessageHandlerService amqpMessageHandlerService() {
        return new AmqpMessageHandlerService(rabbitTemplate);
    }

    /**
     * Create default amqp sender service bean.
     *
     * @return the default amqp sender service bean
     */
    @Bean
    @ConditionalOnMissingBean
    public AmqpSenderService amqpSenderServiceBean() {
        return new DefaultAmqpSenderService(rabbitTemplate);
    }

    /**
     * Returns the Listener factory.
     *
     * @return the {@link SimpleMessageListenerContainer} that gets used receive
     *         AMQP messages
     */
    @Bean(name = { "listenerContainerFactory" })
    public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
        final SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
        containerFactory.setDefaultRequeueRejected(false);
        containerFactory.setConnectionFactory(connectionFactory);
        containerFactory.setMissingQueuesFatal(amqpProperties.isMissingQueuesFatal());
        return containerFactory;
    }

    private Map<String, Object> getDeadLetterExchangeArgs() {
        final Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", amqpProperties.getDeadLetterExchange());
        return args;
    }

}

@@ -1027,76 +1027,8 @@
 er;%0A
-import org.springframework.amqp.support.converter.MessageConverter;%0A
 impo
@@ -1086,16 +1086,16 @@
 owired;%0A
+
 import o
@@ -1454,22 +1454,16 @@
 ces.%0A *%0A
- *%0A *%0A
  */%0A@Ena
@@ -1675,67 +1675,8 @@
 y;%0A%0A
-    @Autowired%0A    private RabbitTemplate rabbitTemplate;%0A%0A
     
@@ -1822,45 +1822,37 @@
 lic 
-MessageConverter jsonMessageConver
+RabbitTemplate rabbitTempla
 te
-r
 () %7B
@@ -1870,101 +1870,76 @@
 nal 
-Jackson2JsonMessageConverter jackson2JsonMessageConver
+RabbitTemplate rabbitTempla
 te
-r
  = new 
-Jackson2JsonMessageConverter(
+RabbitTemplate(connectionFactory
 );%0A 
@@ -1980,17 +1980,21 @@
 nverter(
-j
+new J
 ackson2J
@@ -2012,16 +2012,18 @@
 onverter
+()
 );%0A     
@@ -2036,36 +2036,22 @@
 urn 
-jackson2JsonMessageConver
+rabbitTempla
 te
-r
 ;%0A  
@@ -3849,32 +3849,34 @@
 e(rabbitTemplate
+()
 );%0A    %7D%0A%0A    /*
@@ -4145,16 +4145,18 @@
 Template
+()
 );%0A    %7D

Create rabbitTemplate bean with jackson message converter