Bug 6357 - FRM integration with singleton 36/43236/9
authorAndrej Leitner <andrej.leitner@pantheon.tech>
Fri, 5 Aug 2016 11:03:20 +0000 (13:03 +0200)
committerJozef Bacigal <jozef.bacigal@pantheon.tech>
Mon, 15 Aug 2016 08:51:57 +0000 (08:51 +0000)
 - using ClusterSingletonServiceProvider instead if EOS
 - junit tests update

Change-Id: I07033627ba08e64f58fff117be57dcfe0f030d52
Signed-off-by: Andrej Leitner <andrej.leitner@pantheon.tech>
applications/forwardingrules-manager/pom.xml
applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastership.java [new file with mode: 0644]
applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManager.java [new file with mode: 0644]
applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/ForwardingRulesManagerImpl.java
applications/forwardingrules-manager/src/main/resources/org/opendaylight/blueprint/forwardingrules-manager.xml
applications/forwardingrules-manager/src/test/java/test/mock/FlowListenerTest.java
applications/forwardingrules-manager/src/test/java/test/mock/GroupListenerTest.java
applications/forwardingrules-manager/src/test/java/test/mock/MeterListenerTest.java
applications/forwardingrules-manager/src/test/java/test/mock/NodeListenerTest.java
applications/forwardingrules-manager/src/test/java/test/mock/TableFeaturesListenerTest.java
applications/forwardingrules-manager/src/test/java/test/mock/util/EntityOwnershipServiceMock.java [deleted file]

index 81e9f37fe438453404d3ff6c89e3bca1f05f9d56..4de1aabdc119836e015c7b0eecdf784ab76518d4 100644 (file)
       <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>
diff --git a/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastership.java b/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastership.java
new file mode 100644 (file)
index 0000000..fc41178
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ * 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;
+    }
+
+}
diff --git a/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManager.java b/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManager.java
new file mode 100644 (file)
index 0000000..1530e46
--- /dev/null
@@ -0,0 +1,56 @@
+/**
+ * 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();
+    }
+
+}
index f22a57bc8aa1f26cc42c78393c0d6bad69f1fdd3..c058b093648a1386c99ae22850a674e9fc9479d3 100644 (file)
@@ -8,6 +8,7 @@
 
 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;
@@ -17,12 +18,10 @@ import java.util.Set;
 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;
@@ -32,7 +31,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 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;
@@ -74,15 +72,17 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
 
     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 !");
 
@@ -98,18 +98,15 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
 
     @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
@@ -183,6 +180,7 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
                             Sets.newHashSet(activeNodes);
                     set.add(ident);
                     activeNodes = Collections.unmodifiableSet(set);
+                    deviceMastershipManager.onDeviceConnected(ident.firstKeyOf(Node.class).getId());
                 }
             }
         }
@@ -197,6 +195,7 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
                             Sets.newHashSet(activeNodes);
                     set.remove(ident);
                     activeNodes = Collections.unmodifiableSet(set);
+                    deviceMastershipManager.onDeviceDisconnected(ident.firstKeyOf(Node.class).getId());
                 }
             }
         }
@@ -259,13 +258,13 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
 
     @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;
     }
+
 }
 
index 1902cbac5c3189b93397020bf446bcb80aae5537..b6f154e06524e1cca1ad31685f648da321d310da 100644 (file)
@@ -5,7 +5,7 @@
 
   <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"/>
@@ -15,6 +15,6 @@
     <argument ref="dataBroker"/>
     <argument ref="rpcRegistry"/>
     <argument ref="frmConfig"/>
-    <argument ref="entityOwnershipService"/>
+    <argument ref="clusterSingletonService"/>
   </bean>
 </blueprint>
\ No newline at end of file
index 31ab45a413e55de27119fdd0e0e8c282d2d9c5ef..acada3752301bb3fb142bcb17a5b23d808f746f4 100644 (file)
@@ -8,13 +8,21 @@
 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;
@@ -40,27 +48,37 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 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"));
@@ -93,19 +111,10 @@ public class FlowListenerTest extends FRMTest {
         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"));
@@ -139,19 +148,10 @@ public class FlowListenerTest extends FRMTest {
         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"));
@@ -188,18 +188,10 @@ public class FlowListenerTest extends FRMTest {
         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"));
@@ -228,8 +220,6 @@ public class FlowListenerTest extends FRMTest {
         assertEquals("DOM-1", removeFlowCalls.get(0).getTransactionUri().getValue());
         assertEquals(flowII, removeFlowCalls.get(0).getFlowRef().getValue());
         assertEquals(Boolean.TRUE, removeFlowCalls.get(0).isStrict());
-
-        forwardingRulesManager.close();
     }
 
 
@@ -250,8 +240,11 @@ public class FlowListenerTest extends FRMTest {
         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
         writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
         assertCommit(writeTx.submit());
+    }
 
-
-
+    @After
+    public void tearDown() throws Exception {
+        forwardingRulesManager.close();
     }
+
 }
index 9e9dff9121b1f777c6989a63415c60b94f97bd4b..e33a88d52aca9eb6ec7ea83342a8d3e3ea84f493 100644 (file)
@@ -7,45 +7,68 @@
  */
 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));
@@ -72,18 +95,10 @@ public class GroupListenerTest extends FRMTest {
         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));
@@ -107,18 +122,10 @@ public class GroupListenerTest extends FRMTest {
         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));
@@ -141,8 +148,6 @@ public class GroupListenerTest extends FRMTest {
         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
         assertEquals(1, removeGroupCalls.size());
         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
-
-        forwardingRulesManager.close();
     }
 
     @Test
@@ -158,4 +163,10 @@ public class GroupListenerTest extends FRMTest {
         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
         assertCommit(writeTx.submit());
     }
+
+    @After
+    public void tearDown() throws Exception {
+        forwardingRulesManager.close();
+    }
+
 }
index 457c5dd3c96c019dadc6bc647ece3443cb379e32..bab64fc88575015472ad3c50cd87936797447373 100644 (file)
@@ -7,14 +7,29 @@
  */
 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;
@@ -24,30 +39,36 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.Rem
 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));
@@ -75,18 +96,10 @@ public class MeterListenerTest extends FRMTest {
         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));
@@ -111,18 +124,10 @@ public class MeterListenerTest extends FRMTest {
         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));
@@ -146,8 +151,6 @@ public class MeterListenerTest extends FRMTest {
         assertEquals(1, removeMeterCalls.size());
         assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
         assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
-
-        forwardingRulesManager.close();
     }
 
 
@@ -165,4 +168,9 @@ public class MeterListenerTest extends FRMTest {
         assertCommit(writeTx.submit());
     }
 
+    @After
+    public void tearDown() throws Exception {
+        forwardingRulesManager.close();
+    }
+
 }
index d2c153c62ea1a75c7e41e3ce269af8ccba597c1f..c0ee47212444c710ad5c9c0b2c717e5df19bca6d 100644 (file)
@@ -9,9 +9,15 @@ package test.mock;
 
 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;
@@ -19,40 +25,43 @@ 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;
 
+@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();
+    }
 
 }
index 26160a737f559264de05e2d1f813f921fb078315..1dd9697173988dcd40e8888a004701514f5e33b8 100644 (file)
@@ -10,11 +10,18 @@ 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.tables.TableKey;
@@ -27,27 +34,38 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table
 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);
 
@@ -67,7 +85,11 @@ public class TableFeaturesListenerTest extends FRMTest {
         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();
     }
+
 }
diff --git a/applications/forwardingrules-manager/src/test/java/test/mock/util/EntityOwnershipServiceMock.java b/applications/forwardingrules-manager/src/test/java/test/mock/util/EntityOwnershipServiceMock.java
deleted file mode 100644 (file)
index 6df960f..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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;
-    }
-}