return VTNConfigManager.CONFIG_IDENT;
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
return VTNConfigManager.CONFIG_IDENT;
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
child(VtnFlowCondition.class).build();
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
augmentation(FlowCapableNodeConnector.class).build();
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
augmentation(FlowCapableNode.class).build();
}
- /**
- * {@inheritDoc}
- */
- @Override
- protected Logger getLogger() {
- return LOG;
- }
-
/**
* Return a set of {@link VtnUpdateType} instances that specifies
* event types to be listened.
protected Set<VtnUpdateType> getRequiredEvents() {
return REQUIRED_EVENTS;
}
+
+ // CloseableContainer
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected Logger getLogger() {
+ return LOG;
+ }
}
child(Topology.class, topoKey).child(Link.class).build();
}
- /**
- * {@inheritDoc}
- */
- @Override
- protected Logger getLogger() {
- return LOG;
- }
-
/**
* Return a set of {@link VtnUpdateType} instances that specifies
* event types to be listened.
protected Set<VtnUpdateType> getRequiredEvents() {
return REQUIRED_EVENTS;
}
+
+ // CloseableContainer
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected Logger getLogger() {
+ return LOG;
+ }
}
child(VtnNode.class).build();
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
}
}
- // SalNotificationListener
+ // CloseableContainer
/**
* {@inheritDoc}
child(VtnPathMap.class).build();
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
child(VtnPathPolicy.class).build();
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
child(VtnLink.class).build();
}
- /**
- * {@inheritDoc}
- */
- @Override
- protected Logger getLogger() {
- return LOG;
- }
-
/**
* Return a set of {@link VtnUpdateType} instances that specifies
* event types to be listened.
return REQUIRED_EVENTS;
}
+ // CloseableContainer
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected Logger getLogger() {
+ return LOG;
+ }
+
// VTNConfigProvider
/**
import java.util.Map;
import java.util.Set;
-import org.slf4j.Logger;
-
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
* @param <C> Type of event context.
*/
public abstract class AbstractDataChangeListener<T extends DataObject, C>
- implements AutoCloseable, DataChangeListener {
+ extends CloseableContainer implements DataChangeListener {
/**
* The type of the target data.
*/
private final Class<T> targetType;
- /**
- * A closeable objects to be closed on a {@link #close()} call.
- */
- private final CompositeAutoCloseable closeables;
-
/**
* Construct a new instance.
*
*/
protected AbstractDataChangeListener(Class<T> cls) {
targetType = cls;
- closeables = new CompositeAutoCloseable(getLogger());
}
/**
}
}
- /**
- * Add the given closeable object to the closeable object set.
- *
- * <p>
- * The given object will be closed on a {@link #close()} call.
- * </p>
- *
- * @param ac An {@link AutoCloseable} instance.
- */
- protected final void addCloseable(AutoCloseable ac) {
- closeables.add(ac);
- }
-
/**
* Verify an instance identifier notified by a data change event.
*
*/
protected abstract InstanceIdentifier<T> getWildcardPath();
- /**
- * Return a logger instance.
- *
- * @return A {@link Logger} instance.
- */
- protected abstract Logger getLogger();
-
- // AutoCloseable
-
- /**
- * Close this listener.
- */
- @Override
- public void close() {
- closeables.close();
- }
-
// DataChangeListener
/**
--- /dev/null
+/*
+ * Copyright (c) 2015 NEC Corporation
+ * All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this
+ * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.vtn.manager.internal.util;
+
+import org.slf4j.Logger;
+
+/**
+ * {@code CloseableContainer} describes a container that contains one or
+ * more {@link AutoCloseable} instances.
+ */
+public abstract class CloseableContainer implements AutoCloseable {
+ /**
+ * A closeable objects to be closed on a {@link #close()} call.
+ */
+ private final CompositeAutoCloseable closeables;
+
+ /**
+ * Construct a new instance.
+ */
+ protected CloseableContainer() {
+ closeables = new CompositeAutoCloseable(getLogger());
+ }
+
+ /**
+ * Add the given closeable object to the closeable object set.
+ *
+ * <p>
+ * The given object will be closed on the first {@link #close()} call.
+ * </p>
+ *
+ * @param ac An {@link AutoCloseable} instance.
+ */
+ protected final void addCloseable(AutoCloseable ac) {
+ closeables.add(ac);
+ }
+
+ /**
+ * Return a logger instance.
+ *
+ * <p>
+ * Note that this method will be called before the constructor returns.
+ * </p>
+ *
+ * @return A {@link Logger} instance.
+ */
+ protected abstract Logger getLogger();
+
+ // AutoCloseable
+
+ /**
+ * Close all the closeables in this instance.
+ */
+ @Override
+ public void close() {
+ closeables.close();
+ }
+}
package org.opendaylight.vtn.manager.internal.util;
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.slf4j.Logger;
-
import org.opendaylight.controller.sal.binding.api.NotificationService;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.NotificationListener;
/**
* Abstract base class for MD-SAL notification listener.
*/
-public abstract class SalNotificationListener
- implements AutoCloseable, NotificationListener {
- /**
- * Registration of the notification listener.
- */
- private final AtomicReference<ListenerRegistration<NotificationListener>> registration =
- new AtomicReference<>();
-
- /**
- * Construct a new instance.
- */
- protected SalNotificationListener() {
- }
-
+public abstract class SalNotificationListener extends CloseableContainer
+ implements NotificationListener {
/**
* Register this instance as a SAL notification listener.
*
*/
protected final void registerListener(NotificationService nsv) {
try {
- registration.set(nsv.registerNotificationListener(this));
+ addCloseable(nsv.registerNotificationListener(this));
} catch (Exception e) {
String msg = "Failed to register notification listener: " +
getClass().getName();
throw new IllegalStateException(msg, e);
}
}
-
- /**
- * Return a logger instance.
- *
- * @return A {@link Logger} instance.
- */
- protected abstract Logger getLogger();
-
- // AutoCloseable
-
- /**
- * Close this listener.
- */
- @Override
- public void close() {
- ListenerRegistration<NotificationListener> reg =
- registration.getAndSet(null);
- if (reg != null) {
- try {
- reg.close();
- } catch (Exception e) {
- String msg = "Failed to unregister notification listener: " +
- getClass().getName();
- getLogger().error(msg, e);
- }
- }
- }
}
child(Vtn.class).build();
}
+ // CloseableContainer
+
/**
* {@inheritDoc}
*/
/**
* Logger instance.
*/
- private final Logger logger = Mockito.mock(Logger.class);
+ private Logger logger;
/**
* {@inheritDoc}
*/
+ @Override
protected Logger getLogger() {
- return logger;
+ Logger log = logger;
+ if (log == null) {
+ log = Mockito.mock(Logger.class);
+ logger = log;
+ }
+ return log;
}
}
Mockito.verify(logger, Mockito.never()).error(Mockito.anyString());
// Unregister a listener.
- String msg = "Failed to unregister notification listener: " +
- listener.getClass().getName();
+ String msg = "Failed to close instance: " + reg;
IllegalArgumentException iae =
new IllegalArgumentException("Bad argument");
Mockito.doThrow(iae).when(reg).close();