/*
 * Copyright (c) 2015 Fraunhofer FOKUS
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openbaton.common.vnfm_sdk.amqp;

import com.google.gson.Gson;
import org.openbaton.catalogue.nfvo.messages.Interfaces.NFVMessage;
import org.openbaton.common.vnfm_sdk.AbstractVnfm;
import org.openbaton.common.vnfm_sdk.VnfmHelper;
import org.openbaton.common.vnfm_sdk.amqp.configuration.RabbitConfiguration;
import org.openbaton.common.vnfm_sdk.exception.BadFormatException;
import org.openbaton.common.vnfm_sdk.exception.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.ContextClosedEvent;

/**
 * Created by lto on 28/05/15.
 */

@SpringBootApplication
@ComponentScan(basePackages = "org.openbaton")
public abstract class AbstractVnfmSpringAmqp extends AbstractVnfm implements ApplicationListener<ContextClosedEvent> {

    @Autowired
    private Gson gson;
    @Autowired
    private ConfigurableApplicationContext context;

    public void onAction(String message) throws NotFoundException, BadFormatException {

        NFVMessage nfvMessage = gson.fromJson(message, NFVMessage.class);

        this.onAction(nfvMessage);
    }

    @Override
    protected void setup() {
        vnfmHelper = (VnfmHelper) context.getBean("vnfmSpringHelperRabbit");
        super.setup();
    }

    protected abstract void checkEmsStarted(String hostname) throws RuntimeException;

    @Override
    protected void unregister() {
        ((VnfmSpringHelperRabbit) vnfmHelper).sendMessageToQueue(RabbitConfiguration.queueName_vnfmUnregister, vnfmManagerEndpoint);
    }

    @Override
    protected void register() {
        ((VnfmSpringHelperRabbit) vnfmHelper).sendMessageToQueue(RabbitConfiguration.queueName_vnfmRegister, vnfmManagerEndpoint);
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        unregister();
    }
}


@@ -666,24 +666,54 @@
 .gson.Gson;%0A
+import com.rabbitmq.client.*;%0A
 import org.o
@@ -768,16 +768,16 @@
 essage;%0A
-
 import o
@@ -1456,16 +1456,45 @@
 Event;%0A%0A
+import java.io.IOException;%0A%0A
 /**%0A * C
@@ -2304,32 +2304,50 @@
  unregister() %7B%0A
+        try %7B%0A    
         ((VnfmSp
@@ -2455,32 +2455,899 @@
 nagerEndpoint);%0A
+        %7D catch (IllegalStateException e) %7B%0A            log.warn(%22Got exception while unregistering trying to do it manually%22);%0A            ConnectionFactory factory = new ConnectionFactory();%0A            factory.setHost(%22localhost%22);%0A            Connection connection = null;%0A            try %7B%0A                connection = factory.newConnection();%0A%0A                Channel channel = connection.createChannel();%0A%0A                String message = gson.toJson(vnfmManagerEndpoint);%0A                channel.basicPublish(%22openbaton-exchange%22, RabbitConfiguration.queueName_vnfmUnregister, MessageProperties.TEXT_PLAIN, message.getBytes(%22UTF-8%22));%0A                log.debug(%22Sent '%22 + message + %22'%22);%0A%0A                channel.close();%0A                connection.close();%0A            %7D catch (IOException e1) %7B%0A                e1.printStackTrace();%0A            %7D%0A        %7D%0A
     %7D%0A%0A    @Over

Fix unregistration of gradled vnfm-sdk