2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.genius.alivenessmonitor.protocols.impl;
10 import java.util.EnumMap;
11 import java.util.HashMap;
13 import javax.inject.Singleton;
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;
24 * Implementation of AlivenessProtocolHandlerRegistry.
25 * Implementations of this interface are expected to be thread-safe.
27 * @author Michael Vorburger.ch
31 @Service(classes = AlivenessProtocolHandlerRegistry.class)
32 public class AlivenessProtocolHandlerRegistryImpl implements AlivenessProtocolHandlerRegistry {
34 private static final Logger LOG = LoggerFactory.getLogger(AlivenessProtocolHandlerRegistryImpl.class);
36 private final Map<MonitorProtocolType, AlivenessProtocolHandler<?>> protocolTypeToProtocolHandler =
37 new EnumMap<>(MonitorProtocolType.class);
38 private final Map<Class<?>, AlivenessProtocolHandler<?>> packetTypeToProtocolHandler = new HashMap<>();
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,
49 public synchronized AlivenessProtocolHandler<?> getOpt(MonitorProtocolType protocolType) {
50 return protocolTypeToProtocolHandler.get(protocolType);
53 @SuppressWarnings("unchecked")
55 public synchronized <T extends Packet> AlivenessProtocolHandler<T> getOpt(Class<T> packetClass) {
56 return (AlivenessProtocolHandler<T>) packetTypeToProtocolHandler.get(packetClass);
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);
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);