Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / RoutedServiceIT.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.test.sal.binding.it;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNotSame;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import com.google.common.util.concurrent.Futures;
19 import java.util.Set;
20 import javax.inject.Inject;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.api.RpcProviderService;
24 import org.opendaylight.mdsal.binding.api.RpcService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRoute;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
33 import org.opendaylight.yangtools.concepts.Registration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.ops4j.pax.exam.util.Filter;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Covers routed rpc creation, registration, invocation, unregistration.
42  */
43 public class RoutedServiceIT extends AbstractIT {
44     private static final Logger LOG = LoggerFactory.getLogger(RoutedServiceIT.class);
45
46     protected RoutedSimpleRoute routedSimpleRouteRpc1;
47     protected RoutedSimpleRoute routedSimpleRouteRpc2;
48
49     @Inject
50     @Filter(timeout = 120 * 1000)
51     RpcProviderService rpcProviderService;
52
53     @Inject
54     @Filter(timeout = 120 * 1000)
55     RpcService rpcService;
56
57     /**
58      * Prepare mocks.
59      */
60     @Before
61     public void setUp() {
62         routedSimpleRouteRpc1 = mock(RoutedSimpleRoute.class, "First Flow Rpc");
63         doReturn(RoutedSimpleRoute.class).when(routedSimpleRouteRpc1).implementedInterface();
64         doReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null)).when(routedSimpleRouteRpc1)
65             .invoke(any());
66
67         routedSimpleRouteRpc2 = mock(RoutedSimpleRoute.class, "Second Flow Rpc");
68         doReturn(RoutedSimpleRoute.class).when(routedSimpleRouteRpc2).implementedInterface();
69         doReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null)).when(routedSimpleRouteRpc2)
70             .invoke(any());
71     }
72
73     @Test
74     public void testServiceRegistration() {
75         LOG.info("Register provider 1 with first implementation of routeSimpleService - rpc1 of node 1");
76         final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
77         final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
78
79         Registration firstReg = rpcProviderService.registerRpcImplementation(routedSimpleRouteRpc1,
80             Set.of(nodeOnePath));
81         assertNotNull("Registration should not be null", firstReg);
82
83         LOG.info("Register provider 2 with second implementation of routeSimpleService - rpc2 of node 2");
84
85         Registration secondReg = rpcProviderService.registerRpcImplementation(routedSimpleRouteRpc2, Set.of(nodeTwo));
86         assertNotNull("Registration should not be null", firstReg);
87         assertNotSame(secondReg, firstReg);
88
89         RoutedSimpleRoute consumerService = rpcService.getRpc(RoutedSimpleRoute.class);
90         assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
91         assertNotSame("Provider instance and consumer instance should not be same.", routedSimpleRouteRpc1,
92                 consumerService);
93
94         /**
95          * Consumer creates addFlow message for node one and sends it to the MD-SAL.
96          */
97         final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
98         consumerService.invoke(simpleRouteFirstFoo);
99
100         /**
101          * Verifies that implementation of the first instance received the same message from MD-SAL.
102          */
103         verify(routedSimpleRouteRpc1).invoke(simpleRouteFirstFoo);
104         /**
105          * Verifies that second instance was not invoked with first message
106          */
107         verify(routedSimpleRouteRpc2, times(0)).invoke(simpleRouteFirstFoo);
108
109         /**
110          * Consumer sends message to nodeTwo for three times. Should be processed by second instance.
111          */
112         final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
113         consumerService.invoke(simpleRouteSecondFoo);
114         consumerService.invoke(simpleRouteSecondFoo);
115         consumerService.invoke(simpleRouteSecondFoo);
116
117         /**
118          * Verifies that second instance was invoked 3 times with second message and first instance wasn't invoked.
119          */
120         verify(routedSimpleRouteRpc2, times(3)).invoke(simpleRouteSecondFoo);
121         verify(routedSimpleRouteRpc1, times(0)).invoke(simpleRouteSecondFoo);
122
123         LOG.info("Unregistration of the path for the node one in the first provider");
124         firstReg.close();
125
126         LOG.info("Provider 2 registers path of node 1");
127         secondReg.close();
128         secondReg = rpcProviderService.registerRpcImplementation(routedSimpleRouteRpc2, Set.of(nodeOnePath));
129
130         /**
131          * A consumer sends third message to node 1.
132          */
133         final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
134         consumerService.invoke(simpleRouteThirdFoo);
135
136         /**
137          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1 time.
138          * TODO: fix unregister path
139          */
140         verify(routedSimpleRouteRpc2).invoke(simpleRouteThirdFoo);
141     }
142
143     /**
144      * Returns node reference from string which represents path.
145      *
146      * @param string string with key(path)
147      * @return instance identifier to {@link UnorderedList}
148      */
149     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
150         return InstanceIdentifier.builder(Lists.class)
151                 .child(UnorderedContainer.class)
152                 .child(UnorderedList.class, new UnorderedListKey(string))
153                 .build();
154     }
155
156     /**
157      * Creates flow AddFlowInput for which only node and cookie are set.
158      *
159      * @param node NodeRef value
160      * @return simpleRouteInput instance
161      */
162     static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
163         return new RoutedSimpleRouteInputBuilder().setRoute(node).build();
164     }
165 }