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