Merge "BUG-5636: making table features configurable for the He plugin. DEFAULT will...
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / MeterForwarderTest.java
1 /**
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
11 import java.math.BigInteger;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49
50 /**
51  * Test for {@link MeterForwarder}.
52  */
53 @RunWith(MockitoJUnitRunner.class)
54 public class MeterForwarderTest {
55
56     private final NodeKey s1Key = new NodeKey(new NodeId("S1"));
57     private final MeterId meterId = new MeterId(42L);
58     private final MeterKey meterKey = new MeterKey(meterId);
59     private final Meter meter = new MeterBuilder()
60             .setMeterId(meterId)
61             .setMeterName("test-meter")
62             .build();
63
64     private final KeyedInstanceIdentifier<Node, NodeKey> nodePath = InstanceIdentifier.create(Nodes.class)
65             .child(Node.class, s1Key);
66     private final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = nodePath
67             .augmentation(FlowCapableNode.class);
68     private final InstanceIdentifier<Meter> meterPath = flowCapableNodePath.child(Meter.class, meterKey);
69
70     @Mock
71     private SalMeterService salMeterService;
72     @Captor
73     private ArgumentCaptor<AddMeterInput> addMeterInputCpt;
74     @Captor
75     private ArgumentCaptor<RemoveMeterInput> removeMeterInputCpt;
76     @Captor
77     private ArgumentCaptor<UpdateMeterInput> updateMeterInputCpt;
78
79     private TransactionId txId;
80
81     private MeterForwarder meterForwarder;
82
83     @Before
84     public void setUp() throws Exception {
85         meterForwarder = new MeterForwarder(salMeterService);
86         txId = new TransactionId(BigInteger.ONE);
87     }
88
89     @Test
90     public void testRemove() throws Exception {
91         Mockito.when(salMeterService.removeMeter(removeMeterInputCpt.capture())).thenReturn(
92                 RpcResultBuilder.success(new RemoveMeterOutputBuilder()
93                         .setTransactionId(txId)
94                         .build()).buildFuture()
95         );
96
97         Meter removeMeter = new MeterBuilder(meter).build();
98
99         final Future<RpcResult<RemoveMeterOutput>> removeResult = meterForwarder.remove(meterPath, removeMeter, flowCapableNodePath);
100         Mockito.verify(salMeterService).removeMeter(Matchers.<RemoveMeterInput>any());
101
102         Assert.assertTrue(removeResult.isDone());
103         final RpcResult<RemoveMeterOutput> meterResult = removeResult.get(2, TimeUnit.SECONDS);
104         Assert.assertTrue(meterResult.isSuccessful());
105
106         Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
107
108         final RemoveMeterInput removeMeterInput = removeMeterInputCpt.getValue();
109         Assert.assertEquals(meterPath, removeMeterInput.getMeterRef().getValue());
110         Assert.assertEquals(nodePath, removeMeterInput.getNode().getValue());
111         Assert.assertEquals("test-meter", removeMeterInput.getMeterName());
112     }
113
114     @Test
115     public void testUpdate() throws Exception {
116         Mockito.when(salMeterService.updateMeter(updateMeterInputCpt.capture())).thenReturn(
117                 RpcResultBuilder.success(new UpdateMeterOutputBuilder()
118                         .setTransactionId(txId)
119                         .build()).buildFuture()
120         );
121
122         Meter meterOriginal = new MeterBuilder(meter).build();
123         Meter meterUpdate = new MeterBuilder(meter)
124                 .setMeterName("another-test")
125                 .build();
126
127         final Future<RpcResult<UpdateMeterOutput>> updateResult = meterForwarder.update(meterPath, meterOriginal, meterUpdate,
128                 flowCapableNodePath);
129         Mockito.verify(salMeterService).updateMeter(Matchers.<UpdateMeterInput>any());
130
131         Assert.assertTrue(updateResult.isDone());
132         final RpcResult<UpdateMeterOutput> meterResult = updateResult.get(2, TimeUnit.SECONDS);
133         Assert.assertTrue(meterResult.isSuccessful());
134
135         Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
136
137         final UpdateMeterInput updateMeterInput = updateMeterInputCpt.getValue();
138         Assert.assertEquals(meterPath, updateMeterInput.getMeterRef().getValue());
139         Assert.assertEquals(nodePath, updateMeterInput.getNode().getValue());
140
141         Assert.assertEquals("test-meter", updateMeterInput.getOriginalMeter().getMeterName());
142         Assert.assertEquals("another-test", updateMeterInput.getUpdatedMeter().getMeterName());
143     }
144
145     @Test
146     public void testAdd() throws Exception {
147         Mockito.when(salMeterService.addMeter(addMeterInputCpt.capture())).thenReturn(
148                 RpcResultBuilder.success(new AddMeterOutputBuilder()
149                         .setTransactionId(txId)
150                         .build()).buildFuture()
151         );
152
153         final Future<RpcResult<AddMeterOutput>> addResult = meterForwarder.add(meterPath, meter, flowCapableNodePath);
154         Mockito.verify(salMeterService).addMeter(Matchers.<AddMeterInput>any());
155
156         Assert.assertTrue(addResult.isDone());
157         final RpcResult<AddMeterOutput> meterResult = addResult.get(2, TimeUnit.SECONDS);
158         Assert.assertTrue(meterResult.isSuccessful());
159
160         Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
161
162         final AddMeterInput addMeterInput = addMeterInputCpt.getValue();
163         Assert.assertEquals(meterPath, addMeterInput.getMeterRef().getValue());
164         Assert.assertEquals(nodePath, addMeterInput.getNode().getValue());
165         Assert.assertEquals("test-meter", addMeterInput.getMeterName());
166     }
167 }