1 package org.opendaylight.unimgr.impl;
3 import static org.mockito.Mockito.mock;
4 import static org.mockito.Mockito.spy;
5 import static org.mockito.Mockito.times;
6 import static org.mockito.Mockito.verify;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.function.Consumer;
13 import org.junit.Test;
14 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriver;
15 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriverBuilder;
16 import org.opendaylight.unimgr.mef.nrp.impl.ActivationDriverRepoServiceImpl;
17 import org.opendaylight.unimgr.utils.ActivationDriverMocks;
18 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.forwarding.constructs.ForwardingConstruct;
19 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.forwarding.constructs.ForwardingConstructBuilder;
20 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPort;
21 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPortBuilder;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
27 class TestBusinessEx extends RuntimeException {
28 public TestBusinessEx() {
29 super("expected exception");
34 * @author bartosz.michalik@amartus.com
36 public class FcRouteActivatorServiceTest {
39 private static final TopologyId topoA = new TopologyId("a");
40 private static final TopologyId topoZ = new TopologyId("z");
42 public ForwardingConstructActivatorService createService(List<ActivationDriverBuilder> builders) {
43 return new ForwardingConstructActivatorService(new ActivationDriverRepoServiceImpl(builders));
47 public void testActivateSingleNode() throws Exception {
50 final ActivationDriver d1 = mock(ActivationDriver.class);
52 ForwardingConstructActivatorService service = createService(Arrays.asList(
53 ActivationDriverMocks.prepareDriver((port1, port2) -> topoA.equals(port1.getTopology()) ? d1 : null),
54 ActivationDriverMocks.prepareDriver((port1, port2) -> null)
58 service.activate(singleNode());
61 verify(d1).activate();
66 public void testActivateTwoNodesSingleVendor() throws Exception {
68 final ActivationDriver d1 = mock(ActivationDriver.class);
70 ForwardingConstructActivatorService service = createService(Collections.singletonList(
71 ActivationDriverMocks.prepareDriver(port -> d1)
75 service.activate(twoNodes());
78 verify(d1, times(2)).activate();
79 verify(d1, times(2)).commit();
83 public void testActivateTwoNodesMultiVendor() throws Exception {
86 final ActivationDriver d1 = mock(ActivationDriver.class);
87 final ActivationDriver d2 = mock(ActivationDriver.class);
89 ForwardingConstructActivatorService service = createService(Arrays.asList(
90 ActivationDriverMocks.prepareDriver(port -> topoA.equals(port.getTopology()) ? d1 : null),
91 ActivationDriverMocks.prepareDriver(port -> topoZ.equals(port.getTopology()) ? d2 : null)
95 service.activate(twoNodes());
98 verify(d1).activate();
100 verify(d2).activate();
105 public void testActivateSingleNodeFailure() throws Exception {
108 final ActivationDriver d1 = spy(new FailingActivationDriver(p -> { if(p.getTopology().equals(topoA)) throw new TestBusinessEx();}));
110 ForwardingConstructActivatorService service = createService(Collections.singletonList(
111 ActivationDriverMocks.prepareDriver((p1,p2) -> d1)
115 service.activate(singleNode());
118 verify(d1, times(1)).rollback();
122 public void testActivateMultiNodeFailure() throws Exception {
125 final ActivationDriver d1 = spy(new FailingActivationDriver(p -> { if(p.getTopology().equals(topoA)) throw new TestBusinessEx();}));
127 ForwardingConstructActivatorService service = createService(Collections.singletonList(
128 ActivationDriverMocks.prepareDriver(p1 -> d1)
132 service.activate(twoNodes());
135 verify(d1, times(1)).activate();
136 verify(d1, times(2)).rollback();
140 public void testDeactivateSingleNodeFailure() throws Exception {
143 final ActivationDriver d1 = spy(new FailingActivationDriver(p -> { if(p.getTopology().equals(topoA)) throw new TestBusinessEx();}));
145 ForwardingConstructActivatorService service = createService(Arrays.asList(
146 ActivationDriverMocks.prepareDriver((p1,p2) -> null),
147 ActivationDriverMocks.prepareDriver((p1,p2) -> d1)
151 service.deactivate(singleNode());
154 verify(d1, times(1)).deactivate();
155 verify(d1, times(1)).rollback();
159 public void testDeactivateTwoNodesSingleVendor() throws Exception {
161 final ActivationDriver d1 = mock(ActivationDriver.class);
163 ForwardingConstructActivatorService service = createService(Collections.singletonList(
164 ActivationDriverMocks.prepareDriver(port -> d1)
168 service.deactivate(twoNodes());
171 verify(d1, times(2)).deactivate();
172 verify(d1, times(2)).commit();
175 private ForwardingConstruct singleNode() {
177 port("a", "localhost", "80"),
178 port("z", "localhost", "8080")
182 private ForwardingConstruct twoNodes() {
184 port("a", "192.168.1.1", "80"),
185 port("z", "192.168.1.2", "80")
189 private ForwardingConstruct fc(FcPort... ports) {
190 return new ForwardingConstructBuilder()
191 .setFcPort(Arrays.asList(ports))
195 FcPort port(String topo, String host, String port) {
196 return new FcPortBuilder()
197 .setTopology(new TopologyId(topo))
198 .setNode(new NodeId(host))
199 .setTp(new TpId(port))
203 private static class FailingActivationDriver implements ActivationDriver {
205 private final Consumer<FcPort> consumer;
208 FailingActivationDriver(Consumer<FcPort> portConsumer) {
209 this.consumer = portConsumer;
213 public void commit() {
218 public void rollback() {
223 public void initialize(FcPort from, FcPort to, ForwardingConstruct context) {
224 if(this.from == null)
229 public void activate() {
230 consumer.accept(from);
234 public void deactivate() {
235 consumer.accept(from);
239 public int priority() {