// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.LoggingRequestHandler;
import com.yahoo.restapi.ErrorResponse;
import com.yahoo.vespa.hosted.provision.NoSuchNodeException;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.yolean.Exceptions;

import javax.inject.Inject;
import java.util.logging.Level;

/**
 * @author mpolden
 */
public class LoadBalancersV1ApiHandler extends LoggingRequestHandler {

    private final NodeRepository nodeRepository;

    @Inject
    public LoadBalancersV1ApiHandler(LoggingRequestHandler.Context parentCtx, NodeRepository nodeRepository) {
        super(parentCtx);
        this.nodeRepository = nodeRepository;
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        try {
            switch (request.getMethod()) {
                case GET: return handleGET(request);
                default: return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported");
            }
        }
        catch (NotFoundException | NoSuchNodeException e) {
            return ErrorResponse.notFoundError(Exceptions.toMessageString(e));
        }
        catch (IllegalArgumentException e) {
            return ErrorResponse.badRequest(Exceptions.toMessageString(e));
        }
        catch (RuntimeException e) {
            log.log(Level.WARNING, "Unexpected error handling '" + request.getUri() + "'", e);
            return ErrorResponse.internalServerError(Exceptions.toMessageString(e));
        }
    }

    private HttpResponse handleGET(HttpRequest request) {
        String path = request.getUri().getPath();
        if (path.matches("/loadbalancers/v1/?")) return new LoadBalancersResponse(request, nodeRepository);
        throw new NotFoundException("Nothing at path '" + path + "'");
    }
}

@@ -161,54 +161,8 @@
 i;%0A%0A
-import com.yahoo.container.jdisc.HttpRequest;%0A
 impo
@@ -285,29 +285,23 @@
 restapi.
-ErrorResponse
+RestApi
 ;%0Aimport
@@ -315,50 +315,37 @@
 hoo.
-vespa.hosted.provision.NoSuchNodeException
+restapi.RestApiRequestHandler
 ;%0Aim
@@ -402,103 +402,35 @@
 ry;%0A
-import com.yahoo.yolean.Exceptions;%0A%0Aimport javax.inject.Inject;%0Aimport java.util.logging.Level
+%0Aimport javax.inject.Inject
 ;%0A%0A/
@@ -451,16 +451,33 @@
 mpolden%0A
+ * @author jonmv%0A
  */%0Apubl
@@ -519,23 +519,23 @@
 extends 
-Logging
+RestApi
 RequestH
@@ -540,16 +540,43 @@
 tHandler
+%3CLoadBalancersV1ApiHandler%3E
  %7B%0A%0A    
@@ -767,16 +767,68 @@
 arentCtx
+, LoadBalancersV1ApiHandler::createRestApiDefinition
 );%0A     
@@ -883,302 +883,121 @@
     
-@Override%0A    public HttpResponse handle(HttpRequest request) %7B%0A        try %7B%0A            switch (request.getMethod()) %7B%0A                case GET: return handleGET(request);%0A                default: return ErrorResponse.methodNotAllowed(%22Method '%22 + request.getMethod() + %22' is not supported%22);
+private static RestApi createRestApiDefinition(LoadBalancersV1ApiHandler self) %7B%0A        return RestApi.builder()
 %0A   
@@ -1005,26 +1005,24 @@
         
-%7D%0A
         
 %7D%0A      
@@ -1017,288 +1017,76 @@
     
-%7D%0A        catch (NotFoundException %7C NoSuchNodeException e) %7B%0A            return ErrorResponse.notFoundError(Exceptions.toMessageString(e));%0A        %7D%0A        catch (IllegalArgumentException e) %7B%0A            return ErrorResponse.badRequest(Exceptions.toMessageString(e));%0A
+  .addRoute(RestApi.route(%22/loadbalancers/v1%22)%0A             
         
 %7D%0A  
@@ -1085,18 +1085,16 @@
     
-%7D%0A
         
 catc
@@ -1093,390 +1093,170 @@
     
-catch (RuntimeException e) %7B%0A            log.log(Level.WARNING, %22Unexpected error handling '%22 + request.getUri() + %22'%22, e);%0A            return ErrorResponse.internalServerError(Exceptions.toMessageString(e));%0A        %7D%0A    %7D%0A%0A    private HttpResponse handleGET(HttpRequest request) %7B%0A        String path = request.getUri().getPath();%0A        if (path.matches(%22/loadbalancers/v1/?%22))
+          .get(self::getLoadBalancers))%0A                      .build();%0A    %7D%0A%0A    private HttpResponse getLoadBalancers(RestApi.RequestContext context) %7B%0A       
  ret
@@ -1289,15 +1289,25 @@
 nse(
+context.
 request
+()
 , no
@@ -1329,79 +1329,9 @@
     
-    throw new NotFoundException(%22Nothing at path '%22 + path + %22'%22);%0A    
 %7D
+%0A
 %0A%7D%0A

Rewrite to use RestApi