private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
private final MBeanServerConnection mBeanServerConnection;
- private final ConfigPersisterNotificationListener listener;
+ private final NotificationListener listener;
- public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection,
- Persister persisterAggregator) {
+ public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) {
+ this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator));
+ }
+
+ public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
this.mBeanServerConnection = mBeanServerConnection;
- listener = new ConfigPersisterNotificationListener(persisterAggregator);
+ this.listener = notificationListener;
registerAsJMXListener(mBeanServerConnection, listener);
-
}
- private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) {
+ private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
logger.trace("Called registerAsJMXListener");
try {
mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
@Override
public synchronized void close() {
// unregister from JMX
- ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
+ final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
try {
if (mBeanServerConnection.isRegistered(on)) {
mBeanServerConnection.removeNotificationListener(on, listener);
}
- } catch (Exception e) {
+ } catch (final Exception e) {
logger.warn("Unable to unregister {} as listener for {}", listener, on, e);
}
}
private final Persister persisterAggregator;
- ConfigPersisterNotificationListener(Persister persisterAggregator) {
+ ConfigPersisterNotificationListener(final Persister persisterAggregator) {
this.persisterAggregator = persisterAggregator;
}
@Override
- public void handleNotification(Notification notification, Object handback) {
+ public void handleNotification(final Notification notification, final Object handback) {
if (!(notification instanceof NetconfJMXNotification))
return;
if (notification instanceof CommitJMXNotification) {
try {
handleAfterCommitNotification((CommitJMXNotification) notification);
- } catch (Exception e) {
+ } catch (final Exception e) {
// log exceptions from notification Handler here since
// notificationBroadcastSupport logs only DEBUG level
logger.warn("Failed to handle notification {}", notification, e);
persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
notification.getCapabilities()));
logger.trace("Configuration persisted successfully");
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new RuntimeException("Unable to persist configuration snapshot", e);
}
}
public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
if (!persisterWithConfiguration.readOnly){
- logger.debug("Calling {}.persistConfig",persisterWithConfiguration.storage);
- persisterWithConfiguration.storage.persistConfig(holder);
+ logger.debug("Calling {}.persistConfig", persisterWithConfiguration.getStorage());
+ persisterWithConfiguration.getStorage().persistConfig(holder);
}
}
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. 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.controller.netconf.persist.impl;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import javax.management.MBeanServerConnection;
+
+import javax.management.NotificationFilter;
+import javax.management.NotificationListener;
+import javax.management.ObjectName;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.persist.api.Persister;
+
+public class ConfigPersisterNotificationHandlerTest {
+
+ @Mock
+ private MBeanServerConnection mBeanServer;
+ @Mock
+ private Persister notificationListener;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ doNothing().when(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
+ any(NotificationFilter.class), anyObject());
+ }
+
+ @Test
+ public void testNotificationHandler() throws Exception {
+ doReturn(true).when(mBeanServer).isRegistered(any(ObjectName.class));
+ doThrow(Exception.class).when(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
+
+ final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
+ verify(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
+ any(NotificationFilter.class), anyObject());
+
+ testedHandler.close();
+ verify(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
+ }
+
+ @Test
+ public void testNotificationHandlerCloseNotRegistered() throws Exception {
+ doReturn(false).when(mBeanServer).isRegistered(any(ObjectName.class));
+
+ final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
+
+ testedHandler.close();
+ verify(mBeanServer, times(0)).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. 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.controller.netconf.persist.impl;
+
+import java.util.Collections;
+
+import javax.management.Notification;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
+import org.opendaylight.controller.config.persist.api.Persister;
+import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
+import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
+import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+
+import com.google.common.collect.Lists;
+
+public class ConfigPersisterNotificationListenerTest {
+
+ @Mock
+ private Persister mockPersister;
+ private PersisterAggregator persisterAggregator;
+
+ @Mock
+ private NetconfJMXNotification unknownNetconfNotif;
+ @Mock
+ private CommitJMXNotification commitNetconfNotif;
+ @Mock
+ private Notification unknownNotif;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ Mockito.doNothing().when(mockPersister).persistConfig(Matchers.any(ConfigSnapshotHolder.class));
+ Mockito.doReturn("persister").when(mockPersister).toString();
+ final PersisterAggregator.PersisterWithConfiguration withCfg = new PersisterAggregator.PersisterWithConfiguration(mockPersister, false);
+ persisterAggregator = new PersisterAggregator(Lists.newArrayList(withCfg));
+
+ Mockito.doReturn("netconfUnknownNotification").when(unknownNetconfNotif).toString();
+ Mockito.doReturn("netconfCommitNotification").when(commitNetconfNotif).toString();
+
+ Mockito.doReturn(XmlUtil.readXmlToElement("<config-snapshot/>")).when(commitNetconfNotif).getConfigSnapshot();
+ Mockito.doReturn(Collections.emptySet()).when(commitNetconfNotif).getCapabilities();
+
+ }
+
+ @Test
+ public void testNotificationListenerUnknownNotification() throws Exception {
+ final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator);
+ testeListener.handleNotification(unknownNotif, null);
+ Mockito.verifyZeroInteractions(mockPersister);
+ }
+
+ @Test
+ public void testNotificationListenerUnknownNetconfNotification() throws Exception {
+ final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator);
+ try {
+ testeListener.handleNotification(unknownNetconfNotif, null);
+ Assert.fail("Unknown netconf notification should fail");
+ } catch (final IllegalStateException e) {
+ Mockito.verifyZeroInteractions(mockPersister);
+ }
+ }
+
+ @Test
+ public void testNotificationListenerCommitNetconfNotification() throws Exception {
+ final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator);
+ testeListener.handleNotification(commitNetconfNotif, null);
+ Mockito.verify(mockPersister).persistConfig(Matchers.any(ConfigSnapshotHolder.class));
+ }
+}
package org.opendaylight.controller.netconf.persist.impl;
+import com.google.common.collect.Lists;
+
import org.junit.Test;
import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
import org.opendaylight.controller.config.persist.api.Persister;
assertEquals(1, DummyAdapter.props);
}
+ @Test
+ public void testNoopAdapter() throws Exception {
+ final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
+ final PersisterAggregator persisterAggregator =
+ new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)));
+
+ noOpStorageAdapter.instantiate(null);
+
+ persisterAggregator.persistConfig(null);
+ persisterAggregator.loadLastConfigs();
+ persisterAggregator.persistConfig(null);
+ persisterAggregator.loadLastConfigs();
+
+ noOpStorageAdapter.close();
+ }
+
@Test
public void testLoadFromPropertyFile() throws Exception {
PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));