daecefb2fa024666924f6615bbb1038837596653
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / OpendaylightDirectStatisticsServiceProvider.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Optional;
14
15 /**
16  * The Opendaylight direct statistics service provider.
17  */
18 public class OpendaylightDirectStatisticsServiceProvider {
19     private Map<Class<? extends AbstractDirectStatisticsService>, AbstractDirectStatisticsService> services = new HashMap<>();
20
21     /**
22      * Register direct statistics service.
23      *
24      * @param type    the service type
25      * @param service the service instance
26      */
27     public void register(Class<? extends AbstractDirectStatisticsService> type, AbstractDirectStatisticsService service) {
28         if (services.containsKey(type)) return;
29
30         services.put(type, service);
31     }
32
33     /**
34      * Lookup direct statistics service.
35      *
36      * @param <T>  the type parameter
37      * @param type the service type
38      * @return the service instance
39      */
40     public <T extends AbstractDirectStatisticsService> Optional<T> lookup(Class<T> type) {
41         if (!services.containsKey(type)) return Optional.empty();
42
43         return Optional.of(type.cast(services.get(type)));
44     }
45 }