31f6dd51f5044622fd46f0b02ab1b9cf0c8182ee
[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.ArgumentMatchers;
20 import org.mockito.Captor;
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 =
99                 meterForwarder.remove(meterPath, removeMeter, flowCapableNodePath);
100         Mockito.verify(salMeterService).removeMeter(ArgumentMatchers.<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 =
128                 meterForwarder.update(meterPath, meterOriginal, meterUpdate,
129                 flowCapableNodePath);
130         Mockito.verify(salMeterService).updateMeter(ArgumentMatchers.<UpdateMeterInput>any());
131
132         Assert.assertTrue(updateResult.isDone());
133         final RpcResult<UpdateMeterOutput> meterResult = updateResult.get(2, TimeUnit.SECONDS);
134         Assert.assertTrue(meterResult.isSuccessful());
135
136         Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
137
138         final UpdateMeterInput updateMeterInput = updateMeterInputCpt.getValue();
139         Assert.assertEquals(meterPath, updateMeterInput.getMeterRef().getValue());
140         Assert.assertEquals(nodePath, updateMeterInput.getNode().getValue());
141
142         Assert.assertEquals("test-meter", updateMeterInput.getOriginalMeter().getMeterName());
143         Assert.assertEquals("another-test", updateMeterInput.getUpdatedMeter().getMeterName());
144     }
145
146     @Test
147     public void testAdd() throws Exception {
148         Mockito.when(salMeterService.addMeter(addMeterInputCpt.capture())).thenReturn(
149                 RpcResultBuilder.success(new AddMeterOutputBuilder()
150                         .setTransactionId(txId)
151                         .build()).buildFuture()
152         );
153
154         final Future<RpcResult<AddMeterOutput>> addResult = meterForwarder.add(meterPath, meter, flowCapableNodePath);
155         Mockito.verify(salMeterService).addMeter(ArgumentMatchers.<AddMeterInput>any());
156
157         Assert.assertTrue(addResult.isDone());
158         final RpcResult<AddMeterOutput> meterResult = addResult.get(2, TimeUnit.SECONDS);
159         Assert.assertTrue(meterResult.isSuccessful());
160
161         Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
162
163         final AddMeterInput addMeterInput = addMeterInputCpt.getValue();
164         Assert.assertEquals(meterPath, addMeterInput.getMeterRef().getValue());
165         Assert.assertEquals(nodePath, addMeterInput.getNode().getValue());
166         Assert.assertEquals("test-meter", addMeterInput.getMeterName());
167     }
168 }