2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
10 import static org.junit.Assert.assertEquals;
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;
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();
55 ClusterSingletonServiceProvider clusterSingletonService;
57 DeviceMastershipManager deviceMastershipManager;
59 private NotificationProviderService notificationService;
61 private ReconciliationManager reconciliationManager;
66 forwardingRulesManager = new ForwardingRulesManagerImpl(
68 rpcProviderRegistryMock,
70 clusterSingletonService,
72 getConfigurationService(),
73 reconciliationManager);
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);
82 public void addTwoMetersTest() throws Exception {
83 addFlowCapableNode(s1Key);
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();
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());
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());
113 public void updateMeterTest() throws Exception {
114 addFlowCapableNode(s1Key);
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();
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());
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());
141 public void removeMeterTest() throws Exception {
142 addFlowCapableNode(s1Key);
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();
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());
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());
169 public void staleMeterCreationTest() throws Exception {
170 addFlowCapableNode(s1Key);
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();
177 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
178 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
179 assertCommit(writeTx.submit());
183 public void tearDown() throws Exception {
184 forwardingRulesManager.close();