d5f1351e9c5f72a3fe193ddc5f7563657b792129
[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 final static NodeId NODE_ID = new NodeId("testnode:1");
52     private final static NodeKey s1Key = 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
64     @Before
65     public void setUp() {
66         forwardingRulesManager = new ForwardingRulesManagerImpl(
67                 getDataBroker(),
68                 rpcProviderRegistryMock,
69                 getConfig(),
70                 clusterSingletonService,
71                 notificationService,
72                 getConfigurationService(),
73                 reconciliationManager);
74
75         forwardingRulesManager.start();
76         // TODO consider tests rewrite (added because of complicated access)
77         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
78         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
79     }
80
81     @Test
82     public void addTwoMetersTest() throws Exception {
83         addFlowCapableNode(s1Key);
84
85         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
86         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
87                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
88         Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
89
90         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
91         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
92         assertCommit(writeTx.submit());
93         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
94         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
95         assertEquals(1, addMeterCalls.size());
96         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
97
98         meterKey = new MeterKey(new MeterId((long) 2001));
99         meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
100                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
101         meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
102         writeTx = getDataBroker().newWriteOnlyTransaction();
103         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
104         assertCommit(writeTx.submit());
105         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
106         addMeterCalls = salMeterService.getAddMeterCalls();
107         assertEquals(2, addMeterCalls.size());
108         assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
109         assertEquals(meterII, addMeterCalls.get(1).getMeterRef().getValue());
110     }
111
112     @Test
113     public void updateMeterTest() throws Exception {
114         addFlowCapableNode(s1Key);
115
116         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
117         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
118                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
119         Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").setBarrier(false).build();
120
121         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
122         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
123         assertCommit(writeTx.submit());
124         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
125         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
126         assertEquals(1, addMeterCalls.size());
127         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
128
129         meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
130         writeTx = getDataBroker().newWriteOnlyTransaction();
131         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
132         assertCommit(writeTx.submit());
133         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
134         List<UpdateMeterInput> updateMeterCalls = salMeterService.getUpdateMeterCalls();
135         assertEquals(1, updateMeterCalls.size());
136         assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
137         assertEquals(meterII, updateMeterCalls.get(0).getMeterRef().getValue());
138     }
139
140     @Test
141     public void removeMeterTest() throws Exception {
142         addFlowCapableNode(s1Key);
143
144         MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
145         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
146                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
147         Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
148
149         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
150         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
151         assertCommit(writeTx.submit());
152         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
153         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
154         assertEquals(1, addMeterCalls.size());
155         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
156
157         writeTx = getDataBroker().newWriteOnlyTransaction();
158         writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
159         assertCommit(writeTx.submit());
160         salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
161         List<RemoveMeterInput> removeMeterCalls = salMeterService.getRemoveMeterCalls();
162         assertEquals(1, removeMeterCalls.size());
163         assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
164         assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
165     }
166
167
168     @Test
169     public void staleMeterCreationTest() throws Exception {
170         addFlowCapableNode(s1Key);
171
172         StaleMeterKey meterKey = new StaleMeterKey(new MeterId((long) 2000));
173         InstanceIdentifier<StaleMeter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
174                 .augmentation(FlowCapableNode.class).child(StaleMeter.class, meterKey);
175         StaleMeter meter = new StaleMeterBuilder().setKey(meterKey).setMeterName("stale_meter_one").build();
176
177         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
178         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
179         assertCommit(writeTx.submit());
180     }
181
182     @After
183     public void tearDown() throws Exception {
184         forwardingRulesManager.close();
185     }
186
187 }