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 org.junit.Test;
11 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
15 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.*;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import test.mock.util.EntityOwnershipServiceMock;
29 import test.mock.util.FRMTest;
30 import test.mock.util.RpcProviderRegistryMock;
31 import test.mock.util.SalMeterServiceMock;
33 import java.util.List;
35 import static org.junit.Assert.assertEquals;
37 public class MeterListenerTest extends FRMTest {
38 RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
39 EntityOwnershipService eos = new EntityOwnershipServiceMock();
41 NodeKey s1Key = new NodeKey(new NodeId("S1"));
44 public void addTwoMetersTest() throws Exception {
45 ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
47 rpcProviderRegistryMock,
49 forwardingRulesManager.start();
51 addFlowCapableNode(s1Key);
53 MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
54 InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
55 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
56 Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
58 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
59 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
60 assertCommit(writeTx.submit());
61 SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
62 List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
63 assertEquals(1, addMeterCalls.size());
64 assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
66 meterKey = new MeterKey(new MeterId((long) 2001));
67 meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
68 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
69 meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
70 writeTx = getDataBroker().newWriteOnlyTransaction();
71 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
72 assertCommit(writeTx.submit());
73 salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
74 addMeterCalls = salMeterService.getAddMeterCalls();
75 assertEquals(2, addMeterCalls.size());
76 assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
77 assertEquals(meterII, addMeterCalls.get(1).getMeterRef().getValue());
79 forwardingRulesManager.close();
83 public void updateMeterTest() throws Exception {
84 ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
86 rpcProviderRegistryMock,
88 forwardingRulesManager.start();
90 addFlowCapableNode(s1Key);
92 MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
93 InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
94 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
95 Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").setBarrier(false).build();
97 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
98 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
99 assertCommit(writeTx.submit());
100 SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
101 List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
102 assertEquals(1, addMeterCalls.size());
103 assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
105 meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
106 writeTx = getDataBroker().newWriteOnlyTransaction();
107 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
108 assertCommit(writeTx.submit());
109 salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
110 List<UpdateMeterInput> updateMeterCalls = salMeterService.getUpdateMeterCalls();
111 assertEquals(1, updateMeterCalls.size());
112 assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
113 assertEquals(meterII, updateMeterCalls.get(0).getMeterRef().getValue());
115 forwardingRulesManager.close();
119 public void removeMeterTest() throws Exception {
120 ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
122 rpcProviderRegistryMock,
124 forwardingRulesManager.start();
126 addFlowCapableNode(s1Key);
128 MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
129 InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
130 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
131 Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
133 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
134 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
135 assertCommit(writeTx.submit());
136 SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
137 List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
138 assertEquals(1, addMeterCalls.size());
139 assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
141 writeTx = getDataBroker().newWriteOnlyTransaction();
142 writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
143 assertCommit(writeTx.submit());
144 salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
145 List<RemoveMeterInput> removeMeterCalls = salMeterService.getRemoveMeterCalls();
146 assertEquals(1, removeMeterCalls.size());
147 assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
148 assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
150 forwardingRulesManager.close();
155 public void staleMeterCreationTest() throws Exception {
156 addFlowCapableNode(s1Key);
158 StaleMeterKey meterKey = new StaleMeterKey(new MeterId((long) 2000));
159 InstanceIdentifier<StaleMeter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
160 .augmentation(FlowCapableNode.class).child(StaleMeter.class, meterKey);
161 StaleMeter meter = new StaleMeterBuilder().setKey(meterKey).setMeterName("stale_meter_one").build();
163 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
164 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
165 assertCommit(writeTx.submit());