/*
 * Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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.hawkular.metrics.api.jaxrs;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.badRequest;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.executeAsync;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;

import org.hawkular.metrics.api.jaxrs.util.ApiUtils;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricType;
import org.hawkular.metrics.core.api.MetricsService;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;


/**
 * Interface to deal with metrics
 * @author Heiko W. Rupp
 */
@Path("/")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Api(value = "/", description = "Metrics related REST interface")
public class MetricHandler {
    @Inject
    private MetricsService metricsService;

    @GET
    @Path("/{tenantId}/metrics")
    @ApiOperation(value = "Find tenant's metric definitions.", notes = "Does not include any metric values. ",
            response = List.class, responseContainer = "List")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved at least one metric "
            + "definition."),
            @ApiResponse(code = 204, message = "No metrics found."),
            @ApiResponse(code = 400, message = "Given type is not a valid type.", response = ApiError.class),
            @ApiResponse(code = 500, message = "Failed to retrieve metrics due to unexpected error.",
                         response = ApiError.class)
    })
    public void findMetrics(
            @Suspended final AsyncResponse asyncResponse,
            @PathParam("tenantId") final String tenantId,
        @ApiParam(value = "Queried metric type", required = true, allowableValues = "[guage, avail, log]")
        @QueryParam("type") String type) {

        executeAsync(
                asyncResponse, () -> {
                    MetricType metricType = null;
                    try {
                        metricType = MetricType.fromTextCode(type);
                    } catch (IllegalArgumentException e) {
                        return badRequest(
                                new ApiError("[" + type + "] is not a valid type. Accepted values are num|avail|log")
                        );
                    }
                    ListenableFuture<List<Metric<?>>> future = metricsService.findMetrics(tenantId, metricType);
                    return Futures.transform(future, ApiUtils.MAP_COLLECTION);
                }
        );
    }
}

@@ -1026,16 +1026,41 @@
 rs.GET;%0A
+import javax.ws.rs.POST;%0A
 import j
@@ -1076,16 +1076,16 @@
 s.Path;%0A
-
 import j
@@ -1249,16 +1249,50 @@
 spended;
+%0Aimport javax.ws.rs.core.Response;
 %0A%0Aimport
@@ -1432,16 +1432,16 @@
 icType;%0A
-
 import o
@@ -1484,16 +1484,83 @@
 Service;
+%0Aimport org.hawkular.metrics.core.impl.request.MixedMetricsRequest;
 %0A%0Aimport
@@ -3892,10 +3892,1106 @@
 ;%0A    %7D%0A
+%0A    @POST%0A    @Path(%22/%7BtenantId%7D/data%22)%0A    @ApiOperation(value = %22Add data for multiple metrics in a single call.%22)%0A    @ApiResponses(value = %7B%0A            @ApiResponse(code = 200, message = %22Adding data succeeded.%22),%0A            @ApiResponse(code = 500, message = %22Unexpected error happened while storing the data%22,%0A                response = ApiError.class) %7D)%0A    public void addMetricsData(@Suspended final AsyncResponse asyncResponse, @PathParam(%22tenantId%22) String tenantId,%0A            @ApiParam(value = %22List of metrics%22, required = true) MixedMetricsRequest metricsRequest) %7B%0A        executeAsync(asyncResponse, () -%3E %7B%0A            if (metricsRequest.getGuageMetrics() == null %7C%7C metricsRequest.getGuageMetrics().isEmpty()) %7B%0A                return Futures.immediateFuture(Response.ok().build());%0A            %7D%0A            metricsRequest.getGuageMetrics().forEach(m -%3E m.setTenantId(tenantId));%0A            ListenableFuture%3CVoid%3E future = metricsService.addGuageData(metricsRequest.getGuageMetrics());%0A            return Futures.transform(future, ApiUtils.MAP_VOID);%0A        %7D);%0A    %7D%0A%0A
 %7D%0A

Add POST for inserting mixed metrics, right now only guages are supported.