changing activation service logic to use presto-api module
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / impl / FcRouteActivatorServiceTest.java
1 package org.opendaylight.unimgr.impl;
2
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;
7
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.function.Consumer;
12
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;
25
26
27 class TestBusinessEx extends RuntimeException {
28     public TestBusinessEx() {
29         super("expected exception");
30     }
31 }
32
33 /**
34  * @author bartosz.michalik@amartus.com
35  */
36 public class FcRouteActivatorServiceTest {
37
38
39     private static final TopologyId topoA = new TopologyId("a");
40     private static final TopologyId topoZ = new TopologyId("z");
41
42     public ForwardingConstructActivatorService createService(List<ActivationDriverBuilder> builders) {
43         return new ForwardingConstructActivatorService(new ActivationDriverRepoServiceImpl(builders));
44     }
45
46     @Test
47     public void testActivateSingleNode() throws Exception {
48
49         //having
50         final ActivationDriver d1 = mock(ActivationDriver.class);
51
52         ForwardingConstructActivatorService service = createService(Arrays.asList(
53                 ActivationDriverMocks.prepareDriver((port1, port2) -> topoA.equals(port1.getTopology()) ? d1 : null),
54                 ActivationDriverMocks.prepareDriver((port1, port2) -> null)
55         ));
56
57         //when
58         service.activate(singleNode());
59
60         //then
61         verify(d1).activate();
62         verify(d1).commit();
63     }
64
65     @Test
66     public void testActivateTwoNodesSingleVendor() throws Exception {
67         //having
68         final ActivationDriver d1 = mock(ActivationDriver.class);
69
70         ForwardingConstructActivatorService service = createService(Collections.singletonList(
71                 ActivationDriverMocks.prepareDriver(port -> d1)
72         ));
73
74         //when
75         service.activate(twoNodes());
76
77         //then
78         verify(d1, times(2)).activate();
79         verify(d1, times(2)).commit();
80     }
81
82     @Test
83     public void testActivateTwoNodesMultiVendor() throws Exception {
84
85         //having
86         final ActivationDriver d1 = mock(ActivationDriver.class);
87         final ActivationDriver d2 = mock(ActivationDriver.class);
88
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)
92         ));
93
94         //when
95         service.activate(twoNodes());
96
97         //then
98         verify(d1).activate();
99         verify(d1).commit();
100         verify(d2).activate();
101         verify(d2).commit();
102     }
103
104     @Test
105     public void testActivateSingleNodeFailure() throws Exception {
106
107         //having
108         final ActivationDriver d1 = spy(new FailingActivationDriver(p -> { if(p.getTopology().equals(topoA)) throw new TestBusinessEx();}));
109
110         ForwardingConstructActivatorService service = createService(Collections.singletonList(
111                 ActivationDriverMocks.prepareDriver((p1,p2) -> d1)
112         ));
113
114         //when
115         service.activate(singleNode());
116
117         //then
118         verify(d1, times(1)).rollback();
119     }
120
121     @Test
122     public void testActivateMultiNodeFailure() throws Exception {
123
124         //having
125         final ActivationDriver d1 = spy(new FailingActivationDriver(p -> { if(p.getTopology().equals(topoA)) throw new TestBusinessEx();}));
126
127         ForwardingConstructActivatorService service = createService(Collections.singletonList(
128                 ActivationDriverMocks.prepareDriver(p1 -> d1)
129         ));
130
131         //when
132         service.activate(twoNodes());
133
134         //then
135         verify(d1, times(1)).activate();
136         verify(d1, times(2)).rollback();
137     }
138
139     @Test
140     public void testDeactivateSingleNodeFailure() throws Exception {
141
142         //having
143         final ActivationDriver d1 = spy(new FailingActivationDriver(p -> { if(p.getTopology().equals(topoA)) throw new TestBusinessEx();}));
144
145         ForwardingConstructActivatorService service = createService(Arrays.asList(
146                 ActivationDriverMocks.prepareDriver((p1,p2) -> null),
147                 ActivationDriverMocks.prepareDriver((p1,p2) -> d1)
148         ));
149
150         //when
151         service.deactivate(singleNode());
152
153         //then
154         verify(d1, times(1)).deactivate();
155         verify(d1, times(1)).rollback();
156     }
157
158     @Test
159     public void testDeactivateTwoNodesSingleVendor() throws Exception {
160         //having
161         final ActivationDriver d1 = mock(ActivationDriver.class);
162
163         ForwardingConstructActivatorService service = createService(Collections.singletonList(
164                 ActivationDriverMocks.prepareDriver(port -> d1)
165         ));
166
167         //when
168         service.deactivate(twoNodes());
169
170         //then
171         verify(d1, times(2)).deactivate();
172         verify(d1, times(2)).commit();
173     }
174
175     private ForwardingConstruct singleNode() {
176         return fc(
177                 port("a", "localhost", "80"),
178                 port("z", "localhost", "8080")
179         );
180     }
181
182     private ForwardingConstruct twoNodes() {
183         return fc(
184                 port("a", "192.168.1.1", "80"),
185                 port("z", "192.168.1.2", "80")
186         );
187     }
188
189     private ForwardingConstruct fc(FcPort... ports) {
190         return new ForwardingConstructBuilder()
191                 .setFcPort(Arrays.asList(ports))
192                 .build();
193     }
194
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))
200                 .build();
201     }
202
203     private static class FailingActivationDriver implements ActivationDriver {
204
205         private final Consumer<FcPort> consumer;
206         private FcPort from;
207
208         FailingActivationDriver(Consumer<FcPort> portConsumer) {
209             this.consumer  = portConsumer;
210         }
211
212         @Override
213         public void commit() {
214
215         }
216
217         @Override
218         public void rollback() {
219
220         }
221
222         @Override
223         public void initialize(FcPort from, FcPort to, ForwardingConstruct context) {
224             if(this.from == null)
225                 this.from = from;
226         }
227
228         @Override
229         public void activate() {
230             consumer.accept(from);
231         }
232
233         @Override
234         public void deactivate() {
235             consumer.accept(from);
236         }
237
238         @Override
239         public int priority() {
240             return 0;
241         }
242     }
243 }