// 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;
    }

}

@@ -209,64 +209,8 @@
 or;%0A
-import ai.vespa.models.handler.ModelsEvaluationHandler;%0A
 impo
@@ -258,16 +258,16 @@
 tModel;%0A
+
 import c
@@ -755,16 +755,82 @@
 ucer %7B%0A%0A
+    private final static String BUNDLE_NAME = %22model-evaluation%22;%0A
     priv
@@ -947,32 +947,57 @@
 _HANDLER_NAME = 
+%22ai.vespa.models.handler.
 ModelsEvaluation
@@ -1003,32 +1003,17 @@
 nHandler
-.class.getName()
+%22
 ;%0A    pr
@@ -1034,35 +1034,36 @@
 atic String 
-BUNDLE_NAME
+REST_BINDING
  = %22model-ev
@@ -1070,16 +1070,19 @@
 aluation
+/v1
 %22;%0A%0A    
@@ -2049,110 +2049,8 @@
 ));%0A
-        String binding = ModelsEvaluationHandler.API_ROOT + %22/%22 + ModelsEvaluationHandler.VERSION_V1;%0A
     
@@ -2085,39 +2085,44 @@
 s(%22http://*/%22 + 
-binding
+REST_BINDING
 ,%0A              
@@ -2156,23 +2156,28 @@
 //*/%22 + 
-binding
+REST_BINDING
 ,%0A      
@@ -2214,31 +2214,36 @@
 ttp://*/%22 + 
-binding
+REST_BINDING
  + %22/*%22,%0A   
@@ -2292,15 +2292,20 @@
 %22 + 
-binding
+REST_BINDING
  + %22

Remove code dependencies on model evaluation handler