e659f2192a87307e70658e88781ed532c8955460
[genius.git] / alivenessmonitor / alivenessmonitor-impl / src / main / java / org / opendaylight / genius / alivenessmonitor / protocols / impl / AlivenessProtocolHandlerRegistryImpl.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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 package org.opendaylight.genius.alivenessmonitor.protocols.impl;
9
10 import java.util.EnumMap;
11 import java.util.HashMap;
12 import java.util.Map;
13 import javax.inject.Singleton;
14
15 import org.apache.aries.blueprint.annotation.service.Service;
16 import org.opendaylight.genius.alivenessmonitor.protocols.AlivenessProtocolHandler;
17 import org.opendaylight.genius.alivenessmonitor.protocols.AlivenessProtocolHandlerRegistry;
18 import org.opendaylight.openflowplugin.libraries.liblldp.Packet;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProtocolType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Implementation of AlivenessProtocolHandlerRegistry.
25  * Implementations of this interface are expected to be thread-safe.
26  *
27  * @author Michael Vorburger.ch
28  */
29
30 @Singleton
31 @Service(classes = AlivenessProtocolHandlerRegistry.class)
32 public class AlivenessProtocolHandlerRegistryImpl implements AlivenessProtocolHandlerRegistry {
33
34     private static final Logger LOG = LoggerFactory.getLogger(AlivenessProtocolHandlerRegistryImpl.class);
35
36     private final Map<MonitorProtocolType, AlivenessProtocolHandler<?>> protocolTypeToProtocolHandler =
37             new EnumMap<>(MonitorProtocolType.class);
38     private final Map<Class<?>, AlivenessProtocolHandler<?>> packetTypeToProtocolHandler = new HashMap<>();
39
40     @Override
41     public synchronized void register(MonitorProtocolType protocolType, AlivenessProtocolHandler<?> protocolHandler) {
42         protocolTypeToProtocolHandler.put(protocolType, protocolHandler);
43         packetTypeToProtocolHandler.put(protocolHandler.getPacketClass(), protocolHandler);
44         LOG.trace("Registered AlivenessProtocolHandler protocolType={}, protocolHandler={}", protocolType,
45                 protocolHandler);
46     }
47
48     @Override
49     public synchronized AlivenessProtocolHandler<?> getOpt(MonitorProtocolType protocolType) {
50         return protocolTypeToProtocolHandler.get(protocolType);
51     }
52
53     @SuppressWarnings("unchecked")
54     @Override
55     public synchronized <T extends Packet> AlivenessProtocolHandler<T> getOpt(Class<T> packetClass) {
56         return (AlivenessProtocolHandler<T>) packetTypeToProtocolHandler.get(packetClass);
57     }
58
59     @Override
60     public AlivenessProtocolHandler<?> get(MonitorProtocolType protocolType) {
61         AlivenessProtocolHandler<?> handler = getOpt(protocolType);
62         if (handler == null) {
63             throw new IllegalStateException("No handler registered for protocolType: " + protocolType);
64         }
65         return handler;
66     }
67
68     public AlivenessProtocolHandler<?> get(Class<?> packetClass) {
69         AlivenessProtocolHandler<?> handler = packetTypeToProtocolHandler.get(packetClass);
70         if (handler == null) {
71             throw new IllegalStateException("No handler registered for packetClass: " + packetClass);
72         }
73         return handler;
74     }
75
76 }