<groupId>org.opendaylight.controller</groupId>
<artifactId>sal-common-util</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-singleton-common-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.opendaylight.controller</groupId>
<artifactId>sal-binding-broker-impl</artifactId>
--- /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 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.ClusterSingletonServiceRegistration;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Service (per device) for registration in singleton provider.
+ */
+public class DeviceMastership implements ClusterSingletonService {
+ private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
+ private final NodeId nodeId;
+ private final ServiceGroupIdentifier identifier;
+ private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+ private boolean deviceMastered;
+
+ public DeviceMastership(final NodeId nodeId) {
+ this.nodeId = nodeId;
+ this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
+ this.deviceMastered = false;
+ }
+
+ @Override
+ public void instantiateServiceInstance() {
+ LOG.debug("FRM started for: {}", nodeId.getValue());
+ deviceMastered = true;
+ }
+
+ @Override
+ public ListenableFuture<Void> closeServiceInstance() {
+ LOG.debug("FRM stopped for: {}", nodeId.getValue());
+ deviceMastered = false;
+ return Futures.immediateFuture(null);
+ }
+
+ @Override
+ public ServiceGroupIdentifier getIdentifier() {
+ return identifier;
+ }
+
+ public boolean isDeviceMastered() {
+ return deviceMastered;
+ }
+
+ public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
+ this.clusterSingletonServiceRegistration = registration;
+ }
+
+ public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
+ return clusterSingletonServiceRegistration;
+ }
+
+}
--- /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 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;
+
+/**
+ * Manager for clustering service registrations of {@link DeviceMastership}.
+ */
+public class DeviceMastershipManager {
+ private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
+ private final ClusterSingletonServiceProvider clusterSingletonService;
+ private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
+
+ public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService) {
+ this.clusterSingletonService = clusterSingletonService;
+ }
+
+ public void onDeviceConnected(final NodeId nodeId) {
+ final DeviceMastership mastership = new DeviceMastership(nodeId);
+ final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
+ mastership.setClusterSingletonServiceRegistration(registration);
+ 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);
+ }
+ }
+ LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
+ }
+
+ public boolean isDeviceMastered(final NodeId nodeId) {
+ return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
+ }
+
+}
package org.opendaylight.openflowplugin.applications.frm.impl;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import java.util.concurrent.atomic.AtomicLong;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
private final ForwardingRulesManagerConfig forwardingRulesManagerConfig;
private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
- private final EntityOwnershipService entityOwnershipService;
+ private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
+ private DeviceMastershipManager deviceMastershipManager;
public ForwardingRulesManagerImpl(final DataBroker dataBroker,
final RpcConsumerRegistry rpcRegistry,
final ForwardingRulesManagerConfig config,
- final EntityOwnershipService eos) {
+ final ClusterSingletonServiceProvider clusterSingletonService) {
this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
this.forwardingRulesManagerConfig = Preconditions.checkNotNull(config, "Configuration for FRM cannot be null");
- this.entityOwnershipService = Preconditions.checkNotNull(eos, "EntityOwnership service can not be null");
+ this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
+ "ClusterSingletonService provider can not be null");
Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
@Override
public void start() {
-
this.flowListener = new FlowForwarder(this, dataService);
-
this.groupListener = new GroupForwarder(this, dataService);
this.meterListener = new MeterForwarder(this, dataService);
-
this.tableListener = new TableForwarder(this, dataService);
+ this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider);
this.nodeListener = new FlowNodeReconciliationImpl(this, dataService);
- flowNodeConnectorInventoryTranslatorImpl =
- new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
+ flowNodeConnectorInventoryTranslatorImpl =
+ new FlowNodeConnectorInventoryTranslatorImpl(this,dataService);
LOG.info("ForwardingRulesManager has started successfully.");
-
}
@Override
Sets.newHashSet(activeNodes);
set.add(ident);
activeNodes = Collections.unmodifiableSet(set);
+ deviceMastershipManager.onDeviceConnected(ident.firstKeyOf(Node.class).getId());
}
}
}
Sets.newHashSet(activeNodes);
set.remove(ident);
activeNodes = Collections.unmodifiableSet(set);
+ deviceMastershipManager.onDeviceDisconnected(ident.firstKeyOf(Node.class).getId());
}
}
}
@Override
public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
- NodeId nodeId = ident.firstKeyOf(Node.class).getId();
- Entity entity = new Entity("openflow", nodeId.getValue());
- Optional<EntityOwnershipState> eState = this.entityOwnershipService.getOwnershipState(entity);
- if(eState.isPresent()) {
- return eState.get().isOwner();
- }
- return false;
+ return deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
+ }
+
+ @VisibleForTesting
+ public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
+ this.deviceMastershipManager = deviceMastershipManager;
}
+
}
<reference id="dataBroker" interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"/>
<reference id="rpcRegistry" interface="org.opendaylight.controller.sal.binding.api.RpcProviderRegistry"/>
- <reference id="entityOwnershipService" interface="org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService"/>
+ <reference id="clusterSingletonService" interface="org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider"/>
<odl:clustered-app-config id="frmConfig"
binding-class="org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig"/>
<argument ref="dataBroker"/>
<argument ref="rpcRegistry"/>
<argument ref="frmConfig"/>
- <argument ref="entityOwnershipService"/>
+ <argument ref="clusterSingletonService"/>
</bean>
</blueprint>
\ No newline at end of file
package test.mock;
import static org.junit.Assert.assertEquals;
+
import java.util.Collections;
import java.util.List;
+import org.junit.After;
+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.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import test.mock.util.EntityOwnershipServiceMock;
import test.mock.util.FRMTest;
import test.mock.util.RpcProviderRegistryMock;
import test.mock.util.SalFlowServiceMock;
+@RunWith(MockitoJUnitRunner.class)
public class FlowListenerTest extends FRMTest {
+ private ForwardingRulesManagerImpl forwardingRulesManager;
+ private final static NodeId NODE_ID = new NodeId("testnode:1");
+ private final static NodeKey s1Key = new NodeKey(NODE_ID);
RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
- EntityOwnershipService eos = new EntityOwnershipServiceMock();
-
- NodeKey s1Key = new NodeKey(new NodeId("S1"));
TableKey tableKey = new TableKey((short) 2);
-
- @Test
- public void addTwoFlowsTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
+ @Mock
+ ClusterSingletonServiceProvider clusterSingletonService;
+ @Mock
+ DeviceMastershipManager deviceMastershipManager;
+
+ @Before
+ public void setUp() {
+ forwardingRulesManager = new ForwardingRulesManagerImpl(
getDataBroker(),
rpcProviderRegistryMock,
getConfig(),
- eos);
+ clusterSingletonService);
forwardingRulesManager.start();
+ // TODO consider tests rewrite (added because of complicated access)
+ forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
+ Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
+ }
+ @Test
+ public void addTwoFlowsTest() throws Exception {
addFlowCapableNode(s1Key);
FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
assertEquals("DOM-1", addFlowCalls.get(1).getTransactionUri().getValue());
assertEquals(2, addFlowCalls.get(1).getTableId().intValue());
assertEquals(flowII, addFlowCalls.get(1).getFlowRef().getValue());
-
- forwardingRulesManager.close();
- }
+}
@Test
public void updateFlowTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(),
- eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getOriginalFlow().isStrict());
assertEquals(Boolean.TRUE, updateFlowCalls.get(0).getUpdatedFlow().isStrict());
-
- forwardingRulesManager.close();
}
@Test
public void updateFlowScopeTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(),
- eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
assertEquals("DOM-1", updateFlowCalls.get(0).getTransactionUri().getValue());
assertEquals(flowII, updateFlowCalls.get(0).getFlowRef().getValue());
assertEquals(ipMatch, updateFlowCalls.get(0).getUpdatedFlow().getMatch().getIpMatch());
- forwardingRulesManager.close();
}
@Test
public void deleteFlowTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(),
- eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
FlowKey flowKey = new FlowKey(new FlowId("test_Flow"));
assertEquals("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue());
assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict());
-
- forwardingRulesManager.close();
}
writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
assertCommit(writeTx.submit());
+ }
-
-
+ @After
+ public void tearDown() throws Exception {
+ forwardingRulesManager.close();
}
+
}
*/
package test.mock;
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+import org.junit.After;
+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.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.*;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-import test.mock.util.EntityOwnershipServiceMock;
import test.mock.util.FRMTest;
import test.mock.util.RpcProviderRegistryMock;
import test.mock.util.SalGroupServiceMock;
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-
+@RunWith(MockitoJUnitRunner.class)
public class GroupListenerTest extends FRMTest {
+ private ForwardingRulesManagerImpl forwardingRulesManager;
+ private final static NodeId NODE_ID = new NodeId("testnode:1");
+ private final static NodeKey s1Key = new NodeKey(NODE_ID);
RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
- EntityOwnershipService eos = new EntityOwnershipServiceMock();
-
- NodeKey s1Key = new NodeKey(new NodeId("S1"));
+ @Mock
+ ClusterSingletonServiceProvider clusterSingletonService;
+ @Mock
+ DeviceMastershipManager deviceMastershipManager;
+
+ @Before
+ public void setUp() {
+ forwardingRulesManager = new ForwardingRulesManagerImpl(
+ getDataBroker(),
+ rpcProviderRegistryMock,
+ getConfig(),
+ clusterSingletonService);
+ forwardingRulesManager.start();
+ // TODO consider tests rewrite (added because of complicated access)
+ forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
+ Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
+ }
@Test
public void addTwoGroupsTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock,
- getConfig(), eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
GroupKey groupKey = new GroupKey(new GroupId((long) 255));
addGroupCalls = salGroupService.getAddGroupCalls();
assertEquals(2, addGroupCalls.size());
assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
-
- forwardingRulesManager.close();
}
@Test
public void updateGroupTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(), eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
GroupKey groupKey = new GroupKey(new GroupId((long) 255));
List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
assertEquals(1, updateGroupCalls.size());
assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
-
- forwardingRulesManager.close();
}
@Test
public void removeGroupTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(), eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
GroupKey groupKey = new GroupKey(new GroupId((long) 255));
List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
assertEquals(1, removeGroupCalls.size());
assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
-
- forwardingRulesManager.close();
}
@Test
writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
assertCommit(writeTx.submit());
}
+
+ @After
+ public void tearDown() throws Exception {
+ forwardingRulesManager.close();
+ }
+
}
*/
package test.mock;
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+import org.junit.After;
+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.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.*;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-import test.mock.util.EntityOwnershipServiceMock;
import test.mock.util.FRMTest;
import test.mock.util.RpcProviderRegistryMock;
import test.mock.util.SalMeterServiceMock;
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-
+@RunWith(MockitoJUnitRunner.class)
public class MeterListenerTest extends FRMTest {
+ private ForwardingRulesManagerImpl forwardingRulesManager;
+ private final static NodeId NODE_ID = new NodeId("testnode:1");
+ private final static NodeKey s1Key = new NodeKey(NODE_ID);
RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
- EntityOwnershipService eos = new EntityOwnershipServiceMock();
-
- NodeKey s1Key = new NodeKey(new NodeId("S1"));
-
- @Test
- public void addTwoMetersTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
+ @Mock
+ ClusterSingletonServiceProvider clusterSingletonService;
+ @Mock
+ DeviceMastershipManager deviceMastershipManager;
+
+ @Before
+ public void setUp() {
+ forwardingRulesManager = new ForwardingRulesManagerImpl(
getDataBroker(),
rpcProviderRegistryMock,
- getConfig(), eos);
+ getConfig(),
+ clusterSingletonService);
forwardingRulesManager.start();
+ // TODO consider tests rewrite (added because of complicated access)
+ forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
+ Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
+ }
+ @Test
+ public void addTwoMetersTest() throws Exception {
addFlowCapableNode(s1Key);
MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
assertEquals(2, addMeterCalls.size());
assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
assertEquals(meterII, addMeterCalls.get(1).getMeterRef().getValue());
-
- forwardingRulesManager.close();
}
@Test
public void updateMeterTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(), eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
assertEquals(1, updateMeterCalls.size());
assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
assertEquals(meterII, updateMeterCalls.get(0).getMeterRef().getValue());
-
- forwardingRulesManager.close();
}
@Test
public void removeMeterTest() throws Exception {
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
- getDataBroker(),
- rpcProviderRegistryMock,
- getConfig(), eos);
- forwardingRulesManager.start();
-
addFlowCapableNode(s1Key);
MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
assertEquals(1, removeMeterCalls.size());
assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
-
- forwardingRulesManager.close();
}
assertCommit(writeTx.submit());
}
+ @After
+ public void tearDown() throws Exception {
+ forwardingRulesManager.close();
+ }
+
}
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import test.mock.util.EntityOwnershipServiceMock;
import test.mock.util.FRMTest;
import test.mock.util.RpcProviderRegistryMock;
+@RunWith(MockitoJUnitRunner.class)
public class NodeListenerTest extends FRMTest {
-
+ private ForwardingRulesManagerImpl forwardingRulesManager;
+ private final static NodeKey s1Key = new NodeKey(new NodeId("testnode:1"));
RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
- EntityOwnershipService eos = new EntityOwnershipServiceMock();
-
- NodeKey s1Key = new NodeKey(new NodeId("S1"));
+ @Mock
+ ClusterSingletonServiceProvider clusterSingletonService;
- @Test
- public void addRemoveNodeTest() throws Exception {
- try (ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
+ @Before
+ public void setUp() {
+ forwardingRulesManager = new ForwardingRulesManagerImpl(
getDataBroker(),
rpcProviderRegistryMock,
getConfig(),
- eos)) {
- forwardingRulesManager.start();
-
- addFlowCapableNode(s1Key);
-
- InstanceIdentifier<FlowCapableNode> nodeII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
- .augmentation(FlowCapableNode.class);
-
- boolean nodeActive = forwardingRulesManager.isNodeActive(nodeII);
- assertTrue(nodeActive);
+ clusterSingletonService);
+ forwardingRulesManager.start();
+ }
- removeNode(s1Key);
+ @Test
+ public void addRemoveNodeTest() throws Exception {
+ addFlowCapableNode(s1Key);
- nodeActive = forwardingRulesManager.isNodeActive(nodeII);
- assertFalse(nodeActive);
- }
+ InstanceIdentifier<FlowCapableNode> nodeII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
+ .augmentation(FlowCapableNode.class);
+ boolean nodeActive = forwardingRulesManager.isNodeActive(nodeII);
+ assertTrue(nodeActive);
+ removeNode(s1Key);
+ nodeActive = forwardingRulesManager.isNodeActive(nodeII);
+ assertFalse(nodeActive);
}
+ @After
+ public void tearDown() throws Exception {
+ forwardingRulesManager.close();
+ }
}
import static org.junit.Assert.assertEquals;
import java.util.List;
+import org.junit.After;
+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.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import test.mock.util.EntityOwnershipServiceMock;
import test.mock.util.FRMTest;
import test.mock.util.RpcProviderRegistryMock;
import test.mock.util.SalTableServiceMock;
+@RunWith(MockitoJUnitRunner.class)
public class TableFeaturesListenerTest extends FRMTest {
+ private ForwardingRulesManagerImpl forwardingRulesManager;
+ private final static NodeId NODE_ID = new NodeId("testnode:1");
+ private final static NodeKey s1Key = new NodeKey(NODE_ID);
RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
- EntityOwnershipService eos = new EntityOwnershipServiceMock();
+ @Mock
+ ClusterSingletonServiceProvider clusterSingletonService;
+ @Mock
+ DeviceMastershipManager deviceMastershipManager;
-
- @Test
- public void updateFlowTest() throws Exception {
- NodeKey s1Key = new NodeKey(new NodeId("S1"));
- TableKey tableKey = new TableKey((short) 2);
- TableFeaturesKey tableFeaturesKey = new TableFeaturesKey(tableKey.getId());
- ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
+ @Before
+ public void setUp() {
+ forwardingRulesManager = new ForwardingRulesManagerImpl(
getDataBroker(),
rpcProviderRegistryMock,
getConfig(),
- eos);
+ clusterSingletonService);
forwardingRulesManager.start();
+ // TODO consider tests rewrite (added because of complicated access)
+ forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
+ Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
+ }
+
+ @Test
+ public void updateFlowTest() {
+ TableKey tableKey = new TableKey((short) 2);
+ TableFeaturesKey tableFeaturesKey = new TableFeaturesKey(tableKey.getId());
addTable(tableKey, s1Key);
List<UpdateTableInput> updateTableInputs = salTableServiceMock.getUpdateTableInput();
assertEquals(1, updateTableInputs.size());
assertEquals("DOM-0", updateTableInputs.get(0).getTransactionUri().getValue());
+ }
+ @After
+ public void tearDown() throws Exception {
forwardingRulesManager.close();
}
+
}
+++ /dev/null
-/*
- * Copyright (c) 2016 Brocade Communications 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 test.mock.util;
-
-import com.google.common.base.Optional;
-import org.opendaylight.controller.md.sal.common.api.clustering.*;
-
-import javax.annotation.Nonnull;
-
-/**
- * Created by vishnoianil on 2/4/16.
- */
-public class EntityOwnershipServiceMock implements EntityOwnershipService {
- @Override
- public EntityOwnershipCandidateRegistration registerCandidate(@Nonnull Entity entity) throws CandidateAlreadyRegisteredException {
- return null;
- }
-
- @Override
- public EntityOwnershipListenerRegistration registerListener(@Nonnull String entityType, @Nonnull EntityOwnershipListener listener) {
- return null;
- }
-
- @Override
- public Optional<EntityOwnershipState> getOwnershipState(@Nonnull Entity forEntity) {
- return Optional.of(new EntityOwnershipState(true, true));
- }
-
- @Override
- public boolean isCandidateRegistered(@Nonnull Entity entity) {
- return false;
- }
-}