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.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;
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();
54 ClusterSingletonServiceProvider clusterSingletonService;
56 DeviceMastershipManager deviceMastershipManager;
58 private NotificationProviderService notificationService;
62 forwardingRulesManager = new ForwardingRulesManagerImpl(
64 rpcProviderRegistryMock,
66 clusterSingletonService,
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);
75 public void addTwoMetersTest() throws Exception {
76 addFlowCapableNode(s1Key);
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();
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());
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());
106 public void updateMeterTest() throws Exception {
107 addFlowCapableNode(s1Key);
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();
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());
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());
134 public void removeMeterTest() throws Exception {
135 addFlowCapableNode(s1Key);
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();
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());
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());
162 public void staleMeterCreationTest() throws Exception {
163 addFlowCapableNode(s1Key);
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();
170 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
171 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
172 assertCommit(writeTx.submit());
176 public void tearDown() throws Exception {
177 forwardingRulesManager.close();