2 * Copyright (c) 2014 NEC Corporation 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
9 package org.opendaylight.controller.sal.implementation.internal;
12 import java.util.concurrent.ConcurrentMap;
14 import org.slf4j.Logger;
16 import org.opendaylight.controller.sal.utils.GlobalConstants;
19 * An instance of this class keeps a protocol plugin service handler.
21 * @param <T> Type of protocol plugin service.
23 public final class ProtocolService<T> {
25 * Default priority value.
27 private static final int DEFAULT_PRIORITY = 0;
30 * A protocol plugin service handler.
32 private final T service;
35 * A priority value assigned to this protocol plugin.
37 private final int priority;
40 * Set protocol plugin service.
42 * @param map A map that keeps protocol plugin services.
43 * @param props Service properties.
44 * @param s Protocol plugin service.
45 * @param logger A logger instance.
46 * @param <S> Type of protocol plugin service.
48 public static <S> void set(ConcurrentMap<String, ProtocolService<S>> map,
49 Map<?, ?> props, S s, Logger logger) {
51 logger.error("Protocol plugin service store is null.");
55 logger.error("Protocol plugin service is null.");
59 logger.error("Service property is null.");
63 if (logger.isTraceEnabled()) {
64 logger.trace("Received set service request: {}", s);
65 for (Map.Entry<?, ?> entry: props.entrySet()) {
66 logger.trace("Prop key:({}) value:({})", entry.getKey(),
71 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
72 if (!(value instanceof String)) {
73 logger.error("Unexpected protocol type: {}", value);
77 String type = (String)value;
78 ProtocolService<S> service = new ProtocolService<S>(props, s);
79 ProtocolService<S> old = map.putIfAbsent(type, service);
81 // Compare priority value.
82 if (old.getPriority() >= service.getPriority()) {
83 logger.trace("Protocol plugin service for {} is already set: " +
84 "current={}, requested={}", type, old, service);
88 if (map.replace(type, old, service)) {
91 old = map.putIfAbsent(type, service);
94 logger.debug("Stored protocol plugin service for {}: {}",
99 * Unset protocol plugin service.
101 * @param map A map that keeps protocol plugin services.
102 * @param props Service properties.
103 * @param s Protocol plugin service.
104 * @param logger A logger instance.
105 * @param <S> Type of protocol plugin service.
107 public static <S> void unset(ConcurrentMap<String, ProtocolService<S>> map,
108 Map<?, ?> props, S s, Logger logger) {
110 logger.error("Protocol plugin service store is null.");
114 logger.error("Protocol plugin service is null.");
118 logger.error("Service property is null.");
122 if (logger.isTraceEnabled()) {
123 logger.trace("Received unset service request: {}", s);
124 for (Map.Entry<?, ?> entry: props.entrySet()) {
125 logger.trace("Prop key:({}) value:({})",
126 entry.getKey(), entry.getValue());
130 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
131 if (!(value instanceof String)) {
132 logger.error("Unexpected protocol type {}: service={}", value, s);
136 String type = (String)value;
137 ProtocolService<S> plugin = new ProtocolService<S>(props, s);
138 if (map.remove(type, plugin)) {
139 logger.debug("Removed protocol plugin service for {}: {}",
142 logger.trace("Ignore unset service request for {}: {}",
150 * @param props Protocol plugin service properties.
151 * @param s A protocol plugin service handler.
153 public ProtocolService(Map<?, ?> props, T s) {
156 String key = GlobalConstants.PROTOCOLPLUGINPRIORITY.toString();
157 Object value = props.get(key);
158 if (value instanceof Integer) {
159 priority = ((Integer)value).intValue();
161 priority = DEFAULT_PRIORITY;
166 * Return a protocol plugin service handler.
168 * @return A protocol plugin service handler.
170 public T getService() {
175 * Return a priority value assigned to this protocol plugin.
177 * @return A priority value.
179 public int getPriority() {
184 * Determine whether the given object is identical to this object.
186 * @param o An object to be compared.
187 * @return {@code true} if identical. Otherwise {@code false}.
190 public boolean equals(Object o) {
194 if (o == null || o.getClass() != getClass()) {
198 ProtocolService<?> plugin = (ProtocolService<?>)o;
199 return (service.equals(plugin.service) && priority == plugin.priority);
203 * Return the hash code of this object.
205 * @return The hash code.
208 public int hashCode() {
209 return service.hashCode() + (priority * 31);
213 * Return a string representation of this instance.
215 * @return A string representation of this instance.
218 public String toString() {
219 StringBuilder builder = new StringBuilder("[service=");
220 return builder.append(service).append(", priority=").append(priority).
221 append(']').toString();