// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container;

import ai.vespa.models.evaluation.ModelsEvaluator;
import ai.vespa.models.handler.ModelsEvaluationHandler;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.searchdefinition.derived.RankProfileList;
import com.yahoo.vespa.config.search.RankProfilesConfig;
import com.yahoo.vespa.config.search.core.RankingConstantsConfig;
import com.yahoo.vespa.model.container.component.Handler;

import java.util.List;
import java.util.Objects;

/**
 * Configuration of components for stateless model evaluation
 *
 * @author bratseth
 */
public class ContainerModelEvaluation implements RankProfilesConfig.Producer, RankingConstantsConfig.Producer {

    private final static String EVALUATOR_NAME = ModelsEvaluator.class.getName();
    private final static String REST_HANDLER_NAME = ModelsEvaluationHandler.class.getName();
    private final static String BUNDLE_NAME = "model-evaluation";

    /** Global rank profiles, aka models */
    private final RankProfileList rankProfileList;

    public ContainerModelEvaluation(ContainerCluster cluster, RankProfileList rankProfileList) {
        this.rankProfileList = Objects.requireNonNull(rankProfileList, "rankProfileList cannot be null");
        cluster.addSimpleComponent(EVALUATOR_NAME, null, BUNDLE_NAME);
        cluster.addComponent(ContainerModelEvaluation.getHandler());
    }

    public void prepare(List<Container> containers) {
        rankProfileList.sendConstantsTo(containers);
    }

    @Override
    public void getConfig(RankProfilesConfig.Builder builder) {
        rankProfileList.getConfig(builder);
    }

    @Override
    public void getConfig(RankingConstantsConfig.Builder builder) {
        rankProfileList.getConfig(builder);
    }

    public static Handler<?> getHandler() {
        Handler<?> handler = new Handler<>(new ComponentModel(REST_HANDLER_NAME, null, BUNDLE_NAME));
        String binding = ModelsEvaluationHandler.API_ROOT + "/" + ModelsEvaluationHandler.VERSION_V1;
        handler.addServerBindings("http://*/" + binding,
                                  "https://*/" + binding,
                                  "http://*/" + binding + "/*",
                                  "https://*/" + binding + "/*");
        return handler;
    }

}

// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container;

import ai.vespa.models.evaluation.ModelsEvaluator;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.searchdefinition.derived.RankProfileList;
import com.yahoo.vespa.config.search.RankProfilesConfig;
import com.yahoo.vespa.config.search.core.RankingConstantsConfig;
import com.yahoo.vespa.model.container.component.Handler;

import java.util.List;
import java.util.Objects;

/**
 * Configuration of components for stateless model evaluation
 *
 * @author bratseth
 */
public class ContainerModelEvaluation implements RankProfilesConfig.Producer, RankingConstantsConfig.Producer {

    private final static String BUNDLE_NAME = "model-evaluation";
    private final static String EVALUATOR_NAME = ModelsEvaluator.class.getName();
    private final static String REST_HANDLER_NAME = "ai.vespa.models.handler.ModelsEvaluationHandler";
    private final static String REST_BINDING = "model-evaluation/v1";

    /** Global rank profiles, aka models */
    private final RankProfileList rankProfileList;

    public ContainerModelEvaluation(ContainerCluster cluster, RankProfileList rankProfileList) {
        this.rankProfileList = Objects.requireNonNull(rankProfileList, "rankProfileList cannot be null");
        cluster.addSimpleComponent(EVALUATOR_NAME, null, BUNDLE_NAME);
        cluster.addComponent(ContainerModelEvaluation.getHandler());
    }

    public void prepare(List<Container> containers) {
        rankProfileList.sendConstantsTo(containers);
    }

    @Override
    public void getConfig(RankProfilesConfig.Builder builder) {
        rankProfileList.getConfig(builder);
    }

    @Override
    public void getConfig(RankingConstantsConfig.Builder builder) {
        rankProfileList.getConfig(builder);
    }

    public static Handler<?> getHandler() {
        Handler<?> handler = new Handler<>(new ComponentModel(REST_HANDLER_NAME, null, BUNDLE_NAME));
        handler.addServerBindings("http://*/" + REST_BINDING,
                                  "https://*/" + REST_BINDING,
                                  "http://*/" + REST_BINDING + "/*",
                                  "https://*/" + REST_BINDING + "/*");
        return handler;
    }

}

Remove code dependencies on model evaluation handler