Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManagerTest.java
1 /**
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 /**
33  * Test for {@link DeviceMastershipManager}.
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class DeviceMastershipManagerTest {
37     private static final NodeId NODE_ID = new NodeId("testNode");
38     private DeviceMastershipManager deviceMastershipManager;
39     @Mock
40     private ClusterSingletonServiceRegistration registration;
41     @Mock
42     private ClusterSingletonServiceProvider clusterSingletonService;
43     @Mock
44     private NotificationProviderService notificationService;
45
46     @Before
47     public void setUp() throws Exception {
48         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService,
49                 notificationService);
50         Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
51                 .thenReturn(registration);
52     }
53
54     @Test
55     public void testOnDeviceConnectedAndDisconnected() throws Exception {
56         // no context
57         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
58         NodeUpdatedBuilder nodeUpdatedBuilder = new NodeUpdatedBuilder();
59         nodeUpdatedBuilder.setId(NODE_ID);
60         deviceMastershipManager.onNodeUpdated(nodeUpdatedBuilder.build());
61         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
62         Assert.assertNotNull(serviceInstance);
63         // destroy context - unregister
64         deviceMastershipManager.onDeviceDisconnected(NODE_ID);
65         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
66         NodeRemovedBuilder nodeRemovedBuilder = new NodeRemovedBuilder();
67         InstanceIdentifier<Node> nodeIId = InstanceIdentifier.create(Nodes.class).
68                 child(Node.class, new NodeKey(NODE_ID));
69         nodeRemovedBuilder.setNodeRef(new NodeRef(nodeIId));
70         deviceMastershipManager.onNodeRemoved(nodeRemovedBuilder.build());
71         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
72     }
73
74     @Test
75     public void testIsDeviceMasteredOrSlaved() {
76         // no context
77         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
78         NodeUpdatedBuilder nodeUpdatedBuilder = new NodeUpdatedBuilder();
79         nodeUpdatedBuilder.setId(NODE_ID);
80         deviceMastershipManager.onNodeUpdated(nodeUpdatedBuilder.build());
81         // is master
82         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
83         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
84         // is not master
85         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
86         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
87     }
88
89 }