Fixup Augmentable and Identifiable methods changing
[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.openflowplugin.applications.reconciliation.ReconciliationManager;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import test.mock.util.FRMTest;
45 import test.mock.util.RpcProviderRegistryMock;
46 import test.mock.util.SalMeterServiceMock;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class MeterListenerTest extends FRMTest {
50     private ForwardingRulesManagerImpl forwardingRulesManager;
51     private static final NodeId NODE_ID = new NodeId("testnode:1");
52     private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
53     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
54     @Mock
55     ClusterSingletonServiceProvider clusterSingletonService;
56     @Mock
57     DeviceMastershipManager deviceMastershipManager;
58     @Mock
59     private NotificationProviderService notificationService;
60     @Mock
61     private ReconciliationManager reconciliationManager;
62
63     @Before
64     public void setUp() {
65         forwardingRulesManager = new ForwardingRulesManagerImpl(
66                 getDataBroker(),
67                 rpcProviderRegistryMock,
68                 getConfig(),
69                 clusterSingletonService,
70                 notificationService,
71                 getConfigurationService(),
72                 reconciliationManager);
73
74         forwardingRulesManager.start();
75         // TODO consider tests rewrite (added because of complicated access)
76         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
77         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
78     }
79
80     @Test
81     public void addTwoMetersTest() throws Exception {
82         addFlowCapableNode(NODE_KEY);
83
84         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
85         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
86                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
87         Meter meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_one").build();
88
89         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
90         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
91         assertCommit(writeTx.submit());
92         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
93         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
94         assertEquals(1, addMeterCalls.size());
95         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
96
97         meterKey = new MeterKey(new MeterId((long) 2001));
98         meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
99                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
100         meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
101         writeTx = getDataBroker().newWriteOnlyTransaction();
102         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
103         assertCommit(writeTx.submit());
104         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
105         addMeterCalls = salMeterService.getAddMeterCalls();
106         assertEquals(2, addMeterCalls.size());
107         assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
108         assertEquals(meterII, addMeterCalls.get(1).getMeterRef().getValue());
109     }
110
111     @Test
112     public void updateMeterTest() throws Exception {
113         addFlowCapableNode(NODE_KEY);
114
115         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
116         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
117                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
118         Meter meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_one").setBarrier(false).build();
119
120         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
121         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
122         assertCommit(writeTx.submit());
123         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
124         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
125         assertEquals(1, addMeterCalls.size());
126         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
127
128         meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
129         writeTx = getDataBroker().newWriteOnlyTransaction();
130         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
131         assertCommit(writeTx.submit());
132         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
133         List<UpdateMeterInput> updateMeterCalls = salMeterService.getUpdateMeterCalls();
134         assertEquals(1, updateMeterCalls.size());
135         assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
136         assertEquals(meterII, updateMeterCalls.get(0).getMeterRef().getValue());
137     }
138
139     @Test
140     public void removeMeterTest() throws Exception {
141         addFlowCapableNode(NODE_KEY);
142
143         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
144         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
145                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
146         Meter meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_one").build();
147
148         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
149         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
150         assertCommit(writeTx.submit());
151         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
152         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
153         assertEquals(1, addMeterCalls.size());
154         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
155
156         writeTx = getDataBroker().newWriteOnlyTransaction();
157         writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
158         assertCommit(writeTx.submit());
159         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
160         List<RemoveMeterInput> removeMeterCalls = salMeterService.getRemoveMeterCalls();
161         assertEquals(1, removeMeterCalls.size());
162         assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
163         assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
164     }
165
166     @Test
167     public void staleMeterCreationTest() throws Exception {
168         addFlowCapableNode(NODE_KEY);
169
170         StaleMeterKey meterKey = new StaleMeterKey(new MeterId((long) 2000));
171         InstanceIdentifier<StaleMeter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
172                 .augmentation(FlowCapableNode.class).child(StaleMeter.class, meterKey);
173         StaleMeter meter = new StaleMeterBuilder().withKey(meterKey).setMeterName("stale_meter_one").build();
174
175         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
176         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
177         assertCommit(writeTx.submit());
178     }
179
180     @After
181     public void tearDown() throws Exception {
182         forwardingRulesManager.close();
183     }
184 }