Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / MeterListenerTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package test.mock;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.List;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
26 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import test.mock.util.FRMTest;
44 import test.mock.util.RpcProviderRegistryMock;
45 import test.mock.util.SalMeterServiceMock;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class MeterListenerTest extends FRMTest {
49     private ForwardingRulesManagerImpl forwardingRulesManager;
50     private final static NodeId NODE_ID = new NodeId("testnode:1");
51     private final static NodeKey s1Key = new NodeKey(NODE_ID);
52     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
53     @Mock
54     ClusterSingletonServiceProvider clusterSingletonService;
55     @Mock
56     DeviceMastershipManager deviceMastershipManager;
57     @Mock
58     private NotificationProviderService notificationService;
59
60     @Before
61     public void setUp() {
62         forwardingRulesManager = new ForwardingRulesManagerImpl(
63                 getDataBroker(),
64                 rpcProviderRegistryMock,
65                 getConfig(),
66                 clusterSingletonService,
67                 notificationService);
68         forwardingRulesManager.start();
69         // TODO consider tests rewrite (added because of complicated access)
70         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
71         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
72     }
73
74     @Test
75     public void addTwoMetersTest() throws Exception {
76         addFlowCapableNode(s1Key);
77
78         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
79         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
80                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
81         Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
82
83         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
84         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
85         assertCommit(writeTx.submit());
86         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
87         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
88         assertEquals(1, addMeterCalls.size());
89         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
90
91         meterKey = new MeterKey(new MeterId((long) 2001));
92         meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
93                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
94         meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
95         writeTx = getDataBroker().newWriteOnlyTransaction();
96         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
97         assertCommit(writeTx.submit());
98         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
99         addMeterCalls = salMeterService.getAddMeterCalls();
100         assertEquals(2, addMeterCalls.size());
101         assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
102         assertEquals(meterII, addMeterCalls.get(1).getMeterRef().getValue());
103     }
104
105     @Test
106     public void updateMeterTest() throws Exception {
107         addFlowCapableNode(s1Key);
108
109         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
110         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
111                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
112         Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").setBarrier(false).build();
113
114         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
115         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
116         assertCommit(writeTx.submit());
117         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
118         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
119         assertEquals(1, addMeterCalls.size());
120         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
121
122         meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
123         writeTx = getDataBroker().newWriteOnlyTransaction();
124         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
125         assertCommit(writeTx.submit());
126         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
127         List<UpdateMeterInput> updateMeterCalls = salMeterService.getUpdateMeterCalls();
128         assertEquals(1, updateMeterCalls.size());
129         assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
130         assertEquals(meterII, updateMeterCalls.get(0).getMeterRef().getValue());
131     }
132
133     @Test
134     public void removeMeterTest() throws Exception {
135         addFlowCapableNode(s1Key);
136
137         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
138         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
139                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
140         Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
141
142         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
143         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
144         assertCommit(writeTx.submit());
145         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
146         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
147         assertEquals(1, addMeterCalls.size());
148         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
149
150         writeTx = getDataBroker().newWriteOnlyTransaction();
151         writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
152         assertCommit(writeTx.submit());
153         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
154         List<RemoveMeterInput> removeMeterCalls = salMeterService.getRemoveMeterCalls();
155         assertEquals(1, removeMeterCalls.size());
156         assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
157         assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
158     }
159
160
161     @Test
162     public void staleMeterCreationTest() throws Exception {
163         addFlowCapableNode(s1Key);
164
165         StaleMeterKey meterKey = new StaleMeterKey(new MeterId((long) 2000));
166         InstanceIdentifier<StaleMeter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
167                 .augmentation(FlowCapableNode.class).child(StaleMeter.class, meterKey);
168         StaleMeter meter = new StaleMeterBuilder().setKey(meterKey).setMeterName("stale_meter_one").build();
169
170         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
171         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
172         assertCommit(writeTx.submit());
173     }
174
175     @After
176     public void tearDown() throws Exception {
177         forwardingRulesManager.close();
178     }
179
180 }