import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
/**
* Service (per device) for registration in singleton provider.
*/
-public class DeviceMastership implements ClusterSingletonService {
+public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
private final NodeId nodeId;
private final ServiceGroupIdentifier identifier;
- private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+ private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
private boolean deviceMastered;
- public DeviceMastership(final NodeId nodeId) {
+ public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
this.nodeId = nodeId;
this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
this.deviceMastered = false;
+ clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
}
@Override
return identifier;
}
- public boolean isDeviceMastered() {
- return deviceMastered;
- }
-
- public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
- this.clusterSingletonServiceRegistration = registration;
+ @Override
+ public void close() {
+ if (clusterSingletonServiceRegistration != null) {
+ try {
+ clusterSingletonServiceRegistration.close();
+ } catch (Exception e) {
+ LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
+ }
+ }
}
- public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
- return clusterSingletonServiceRegistration;
+ public boolean isDeviceMastered() {
+ return deviceMastered;
}
}
package org.opendaylight.openflowplugin.applications.frm.impl;
+import com.google.common.annotations.VisibleForTesting;
import java.util.concurrent.ConcurrentHashMap;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
public void onDeviceConnected(final NodeId nodeId) {
- final DeviceMastership mastership = new DeviceMastership(nodeId);
- final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
- mastership.setClusterSingletonServiceRegistration(registration);
+ final DeviceMastership mastership = new DeviceMastership(nodeId, clusterSingletonService);
deviceMasterships.put(nodeId, mastership);
- LOG.debug("FRS service registered for: {}", nodeId.getValue());
+ LOG.debug("FRM service registered for: {}", nodeId.getValue());
}
-
public void onDeviceDisconnected(final NodeId nodeId) {
final DeviceMastership mastership = deviceMasterships.remove(nodeId);
- final ClusterSingletonServiceRegistration registration = mastership.getClusterSingletonServiceRegistration();
- if (registration != null) {
- try {
- registration.close();
- } catch (Exception e) {
- LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
- }
+ if (mastership != null) {
+ mastership.close();
}
- LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
+ LOG.debug("FRM service unregistered for: {}", nodeId.getValue());
}
public boolean isDeviceMastered(final NodeId nodeId) {
return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
}
+ @VisibleForTesting
+ ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
+ return deviceMasterships;
+ }
+
}
--- /dev/null
+/**
+ * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.openflowplugin.applications.frm.impl;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+/**
+ * Test for {@link DeviceMastershipManager}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class DeviceMastershipManagerTest {
+ private static final NodeId NODE_ID = new NodeId("testNode");
+ private DeviceMastershipManager deviceMastershipManager;
+ @Mock
+ private ClusterSingletonServiceRegistration registration;
+ @Mock
+ private ClusterSingletonServiceProvider clusterSingletonService;
+
+ @Before
+ public void setUp() throws Exception {
+ deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService);
+ Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
+ .thenReturn(registration);
+ }
+
+ @Test
+ public void testOnDeviceConnectedAndDisconnected() throws Exception {
+ // no context
+ Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
+ // create context - register
+ deviceMastershipManager.onDeviceConnected(NODE_ID);
+ DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
+ Assert.assertNotNull(serviceInstance);
+ Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
+ // destroy context - unregister
+ deviceMastershipManager.onDeviceDisconnected(NODE_ID);
+ Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
+ Mockito.verify(registration).close();
+ }
+
+ @Test
+ public void testIsDeviceMasteredOrSlaved() {
+ // no context
+ Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
+ deviceMastershipManager.onDeviceConnected(NODE_ID);
+ // is master
+ deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
+ Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
+ // is not master
+ deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
+ Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/**
+ * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.openflowplugin.applications.frm.impl;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+/**
+ * Test for {@link DeviceMastership}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class DeviceMastershipTest {
+ private static final NodeId NODE_ID = new NodeId("testNode");
+ private DeviceMastership deviceMastership;
+
+ @Mock
+ private DeviceMastershipManager deviceMastershipManager;
+
+ @Before
+ public void setUp() throws Exception {
+ deviceMastership = new DeviceMastership(NODE_ID, Mockito.mock(ClusterSingletonServiceProvider.class));
+ }
+
+ @Test
+ public void testInstantiateServiceInstance() {
+ deviceMastership.instantiateServiceInstance();
+ Assert.assertTrue(deviceMastership.isDeviceMastered());
+ }
+
+ @Test
+ public void testCloseServiceInstance() {
+ deviceMastership.closeServiceInstance();
+ Assert.assertFalse(deviceMastership.isDeviceMastered());
+ }
+
+}
\ No newline at end of file
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
/**
* {@link ClusterSingletonService} clusterSingletonServiceRegistration per connected device.
*/
-public class DeviceMastership implements ClusterSingletonService {
+public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
private final NodeId nodeId;
private final ServiceGroupIdentifier identifier;
private final ReconciliationRegistry reconciliationRegistry;
- private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+ private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
private boolean deviceMastered;
- public DeviceMastership(final NodeId nodeId, final ReconciliationRegistry reconciliationRegistry) {
+ public DeviceMastership(final NodeId nodeId,
+ final ReconciliationRegistry reconciliationRegistry,
+ final ClusterSingletonServiceProvider clusterSingletonService) {
this.nodeId = nodeId;
this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
this.reconciliationRegistry = reconciliationRegistry;
this.deviceMastered = false;
+ clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
}
@Override
return identifier;
}
- public boolean isDeviceMastered() {
- return deviceMastered;
- }
-
- public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
- this.clusterSingletonServiceRegistration = registration;
+ @Override
+ public void close() {
+ if (clusterSingletonServiceRegistration != null) {
+ try {
+ clusterSingletonServiceRegistration.close();
+ } catch (Exception e) {
+ LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
+ }
+ }
}
- public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
- return clusterSingletonServiceRegistration;
+ public boolean isDeviceMastered() {
+ return deviceMastered;
}
}
-/*
+/**
* Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
import com.google.common.annotations.VisibleForTesting;
import java.util.concurrent.ConcurrentHashMap;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.slf4j.Logger;
}
public void onDeviceConnected(final NodeId nodeId) {
- final DeviceMastership mastership = new DeviceMastership(nodeId, reconciliationRegistry);
- final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
- mastership.setClusterSingletonServiceRegistration(registration);
+ final DeviceMastership mastership = new DeviceMastership(nodeId, reconciliationRegistry, clusterSingletonService);
deviceMasterships.put(nodeId, mastership);
LOG.debug("FRS service registered for: {}", nodeId.getValue());
}
-
public void onDeviceDisconnected(final NodeId nodeId) {
final DeviceMastership mastership = deviceMasterships.remove(nodeId);
- final ClusterSingletonServiceRegistration registration = mastership.getClusterSingletonServiceRegistration();
- if (registration != null) {
- try {
- registration.close();
- } catch (Exception e) {
- LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
- }
+ if (mastership != null) {
+ mastership.close();
}
LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
}
ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
return deviceMasterships;
}
+
}
-/*
+/**
* Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
}
@Test
- public void testOnDeviceConnectedAndDisconnected() {
+ public void testOnDeviceConnectedAndDisconnected() throws Exception {
// no context
Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
// create context - register
deviceMastershipManager.onDeviceConnected(NODE_ID);
- DeviceMastership registration = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
- Assert.assertNotNull(registration);
- Mockito.verify(clusterSingletonService).registerClusterSingletonService(registration);
+ DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
+ Assert.assertNotNull(serviceInstance);
+ Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
// destroy context - unregister
deviceMastershipManager.onDeviceDisconnected(NODE_ID);
Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
+ Mockito.verify(registration).close();
}
@Test
-/*
+/**
* Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@Before
public void setUp() throws Exception {
- deviceMastership = new DeviceMastership(NODE_ID, reconciliationRegistry);
+ deviceMastership = new DeviceMastership(NODE_ID, reconciliationRegistry, Mockito.mock(ClusterSingletonServiceProvider.class));
}
@Test
- public void instantiateServiceInstance() {
+ public void testInstantiateServiceInstance() {
deviceMastership.instantiateServiceInstance();
Mockito.verify(reconciliationRegistry).register(NODE_ID);
Assert.assertTrue(deviceMastership.isDeviceMastered());
}
@Test
- public void closeServiceInstance() {
+ public void testCloseServiceInstance() {
deviceMastership.closeServiceInstance();
Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
Assert.assertFalse(deviceMastership.isDeviceMastered());
}
+
}
\ No newline at end of file