package quilt.config.generator.agent;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.openshift.restclient.ClientFactory;
import com.openshift.restclient.IClient;
import com.openshift.restclient.NoopSSLCertificateCallback;
import com.openshift.restclient.authorization.TokenAuthorizationStrategy;
import io.vertx.core.Vertx;
import io.vertx.proton.ProtonClient;
import io.vertx.proton.ProtonConnection;
import io.vertx.proton.ProtonDelivery;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.message.Message;
import quilt.config.admin.ConfigManager;
import quilt.config.generator.ConfigGenerator;
import quilt.config.model.Config;
import quilt.config.model.parser.ConfigParser;
import quilt.config.openshift.OpenshiftClient;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author lulf
 */
public class GeneratorAgent implements Runnable, AutoCloseable {
    private static final Logger log = Logger.getLogger(GeneratorAgent.class.getName());

    private final ProtonClient client;
    private final GeneratorAgentOptions options;
    private volatile ProtonConnection connection;

    private final ConfigParser parser = new ConfigParser();
    private final ConfigManager configManager;

    private final ObjectMapper mapper = new ObjectMapper();
    private final Vertx vertx;

    public GeneratorAgent(GeneratorAgentOptions options) throws IOException {
        this.vertx = Vertx.vertx();
        client = ProtonClient.create(vertx);

        IClient osClient = new ClientFactory().create(options.openshiftUrl(), new NoopSSLCertificateCallback());
        osClient.setAuthorizationStrategy(new TokenAuthorizationStrategy(options.openshiftToken()));

        configManager = new ConfigManager(new OpenshiftClient(osClient, options.openshiftNamespace()), new ConfigGenerator(osClient, options.brokerProperties()));
        this.options = options;
    }

    public void run() {
        client.connect(options.configHost(), options.configPort(), connectionHandle -> {
            if (connectionHandle.succeeded()) {
                connection = connectionHandle.result();
                connection.open();

                connection.createReceiver("maas").handler(this::handleMessage).open();
                log.log(Level.INFO, "Created receiver");
            } else {
                log.log(Level.INFO, "Connect failed: " + connectionHandle.cause().getMessage());
                vertx.close();
            }
        });
    }

    private void handleMessage(ProtonDelivery protonDelivery, Message message) {
        try {
            if (message.getBody() instanceof AmqpValue) {
                JsonNode root = mapper.readTree((String) ((AmqpValue) message.getBody()).getValue());
                Config config = parser.parse(root.get("json"));
                log.log(Level.INFO, "Configuration was updated");

                configManager.configUpdated(config);
            }
        } catch (IOException e) {
            log.log(Level.INFO, "Error handling config update", e);
        }
    }

    @Override
    public void close() throws Exception {
        if (connection != null) {
            connection.close();
        }
    }
}

@@ -170,23 +170,23 @@
 t.Client
-Factory
+Builder
 ;%0Aimport
@@ -224,68 +224,8 @@
 nt;%0A
-import com.openshift.restclient.NoopSSLCertificateCallback;%0A
 impo
@@ -1571,24 +1571,15 @@
 ient
-Factory().create
+Builder
 (opt
@@ -1601,143 +1601,203 @@
 rl()
-, new NoopSSLCertificateCallback());%0A        osClient.setAuthorizationStrategy(new TokenAuthorizationStrategy(options.openshiftToken())
+)%0A                .resourceFactory(new TokenAuthorizationStrategy(options.openshiftToken())) // TODO: Use new method when new version of openshift-restclient-java is used.%0A                .build(
 );%0A%0A

Use non-deprecated code for constructing rest client