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.RpcProviderRegistry;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
24 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
25 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.StaleMeterKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import test.mock.util.FRMTest;
43 import test.mock.util.RpcProviderRegistryMock;
44 import test.mock.util.SalMeterServiceMock;
46 @RunWith(MockitoJUnitRunner.class)
47 public class MeterListenerTest extends FRMTest {
48 private ForwardingRulesManagerImpl forwardingRulesManager;
49 private final static NodeId NODE_ID = new NodeId("testnode:1");
50 private final static NodeKey s1Key = new NodeKey(NODE_ID);
51 RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
53 ClusterSingletonServiceProvider clusterSingletonService;
55 DeviceMastershipManager deviceMastershipManager;
59 forwardingRulesManager = new ForwardingRulesManagerImpl(
61 rpcProviderRegistryMock,
63 clusterSingletonService);
64 forwardingRulesManager.start();
65 // TODO consider tests rewrite (added because of complicated access)
66 forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
67 Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
71 public void addTwoMetersTest() throws Exception {
72 addFlowCapableNode(s1Key);
74 MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
75 InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
76 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
77 Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
79 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
80 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
81 assertCommit(writeTx.submit());
82 SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
83 List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
84 assertEquals(1, addMeterCalls.size());
85 assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
87 meterKey = new MeterKey(new MeterId((long) 2001));
88 meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
89 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
90 meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
91 writeTx = getDataBroker().newWriteOnlyTransaction();
92 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
93 assertCommit(writeTx.submit());
94 salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
95 addMeterCalls = salMeterService.getAddMeterCalls();
96 assertEquals(2, addMeterCalls.size());
97 assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
98 assertEquals(meterII, addMeterCalls.get(1).getMeterRef().getValue());
102 public void updateMeterTest() throws Exception {
103 addFlowCapableNode(s1Key);
105 MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
106 InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
107 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
108 Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").setBarrier(false).build();
110 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
111 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
112 assertCommit(writeTx.submit());
113 SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
114 List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
115 assertEquals(1, addMeterCalls.size());
116 assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
118 meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
119 writeTx = getDataBroker().newWriteOnlyTransaction();
120 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
121 assertCommit(writeTx.submit());
122 salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
123 List<UpdateMeterInput> updateMeterCalls = salMeterService.getUpdateMeterCalls();
124 assertEquals(1, updateMeterCalls.size());
125 assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
126 assertEquals(meterII, updateMeterCalls.get(0).getMeterRef().getValue());
130 public void removeMeterTest() throws Exception {
131 addFlowCapableNode(s1Key);
133 MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
134 InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
135 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
136 Meter meter = new MeterBuilder().setKey(meterKey).setMeterName("meter_one").build();
138 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
139 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
140 assertCommit(writeTx.submit());
141 SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
142 List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
143 assertEquals(1, addMeterCalls.size());
144 assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
146 writeTx = getDataBroker().newWriteOnlyTransaction();
147 writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
148 assertCommit(writeTx.submit());
149 salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
150 List<RemoveMeterInput> removeMeterCalls = salMeterService.getRemoveMeterCalls();
151 assertEquals(1, removeMeterCalls.size());
152 assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
153 assertEquals(meterII, removeMeterCalls.get(0).getMeterRef().getValue());
158 public void staleMeterCreationTest() throws Exception {
159 addFlowCapableNode(s1Key);
161 StaleMeterKey meterKey = new StaleMeterKey(new MeterId((long) 2000));
162 InstanceIdentifier<StaleMeter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
163 .augmentation(FlowCapableNode.class).child(StaleMeter.class, meterKey);
164 StaleMeter meter = new StaleMeterBuilder().setKey(meterKey).setMeterName("stale_meter_one").build();
166 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
167 writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
168 assertCommit(writeTx.submit());
172 public void tearDown() throws Exception {
173 forwardingRulesManager.close();