All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rackspacecloud.blueflood.internal.InternalAPIFactory Maven / Gradle / Ivy

/*
 * Copyright 2013 Rackspace
 *
 *    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 com.rackspacecloud.blueflood.internal;

import com.rackspacecloud.blueflood.rollup.Granularity;
import com.rackspacecloud.blueflood.utils.TimeValue;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class InternalAPIFactory {
    public static final String BASE_PATH = "/v1.0";
    private static int PAGINATION_LIMIT = 1000;
    private static int PAGINATION_RETRY_COUNT = 3;

    static final Map SAFETY_TTLS = new HashMap() {{
        for (Granularity gran : Granularity.granularities())
            put(gran, new TimeValue(gran.getTTL().getValue() * 5, gran.getTTL().getUnit()));
    }};

    private static final Account DEFAULT_ACCOUNT = new Account() {
        @Override
        public TimeValue getMetricTtl(String resolution) {
            return SAFETY_TTLS.get(Granularity.fromString(resolution));
        }
    };
    
    static ClientConnectionManager buildConnectionManager(int concurrency) {
        final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(concurrency);
        connectionManager.setMaxTotal(concurrency);
        return connectionManager;
    }
    
    public static InternalAPI create(final int concurrency, final String clusterString) {
        return new InternalAPI() {
            // NOTE: some tests (InternalAPITest) make assumptions about these members being here.
            final ClientConnectionManager connectionManager = buildConnectionManager(concurrency);  
            final JsonResource jsonResource = new HttpJsonResource(connectionManager, clusterString, BASE_PATH);
            
            public Account fetchAccount(String tenantId) throws IOException {
                return Account.fromJSON(jsonResource.getResource("/accounts/" + tenantId));
            }

            public List listAccountMapEntries() throws IOException {
                List entries = new LinkedList();
                String nextMarker = null;

                do {
                    String path = "/accounts?limit=" + PAGINATION_LIMIT;

                    if (nextMarker != null) {
                        path += "&marker=" + nextMarker;
                    }

                    PaginatedAccountMap result = PaginatedAccountMap.fromJSON(jsonResource.getResource(path));
                    entries.addAll(result.getEntries());
                    nextMarker = result.getNextMarker();

                } while (nextMarker != null);

                return entries;
            }
        };
    }

    public static InternalAPI createDefaultTTLProvider() {
        return new InternalAPI() {
            public Account fetchAccount(String tenantId) throws IOException {
                return DEFAULT_ACCOUNT;
            }

            public List listAccountMapEntries() throws IOException {
                throw new RuntimeException("Not implemented");
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy