Remove deprecated MD-SAL APIs
[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.junit.Assert.assertSame;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16
17 import com.google.common.util.concurrent.Futures;
18 import java.util.Set;
19 import javax.inject.Inject;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
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.ObjectRegistration;
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
45     private static final Logger LOG = LoggerFactory
46             .getLogger(RoutedServiceIT.class);
47
48     protected OpendaylightTestRoutedRpcService odlRoutedService1;
49     protected OpendaylightTestRoutedRpcService odlRoutedService2;
50
51     @Inject
52     @Filter(timeout = 120 * 1000)
53     RpcProviderService rpcProviderService;
54
55     @Inject
56     @Filter(timeout = 120 * 1000)
57     RpcConsumerRegistry rpcConsumerRegistry;
58
59     /**
60      * Prepare mocks.
61      */
62     @Before
63     public void setUp() {
64         odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service");
65         odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service");
66         Mockito.when(odlRoutedService1.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
67             .thenReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null));
68         Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
69             .thenReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null));
70     }
71
72     @Test
73     public void testServiceRegistration() {
74         LOG.info("Register provider 1 with first implementation of routeSimpleService - service1 of node 1");
75         final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
76         final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
77
78         ObjectRegistration<OpendaylightTestRoutedRpcService> firstReg = rpcProviderService.registerRpcImplementation(
79             OpendaylightTestRoutedRpcService.class, odlRoutedService1,  Set.of(nodeOnePath));
80         assertNotNull("Registration should not be null", firstReg);
81         assertSame(odlRoutedService1, firstReg.getInstance());
82
83         LOG.info("Register provider 2 with second implementation of routeSimpleService - service2 of node 2");
84
85         ObjectRegistration<OpendaylightTestRoutedRpcService> secondReg = rpcProviderService.registerRpcImplementation(
86             OpendaylightTestRoutedRpcService.class, odlRoutedService2, Set.of(nodeTwo));
87         assertNotNull("Registration should not be null", firstReg);
88         assertSame(odlRoutedService2, secondReg.getInstance());
89         assertNotSame(secondReg, firstReg);
90
91         OpendaylightTestRoutedRpcService consumerService =
92                 rpcConsumerRegistry.getRpcService(OpendaylightTestRoutedRpcService.class);
93         assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
94         assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1,
95                 consumerService);
96
97         /**
98          * Consumer creates addFlow message for node one and sends it to the MD-SAL.
99          */
100         final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
101         consumerService.routedSimpleRoute(simpleRouteFirstFoo);
102
103         /**
104          * Verifies that implementation of the first provider received the same message from MD-SAL.
105          */
106         verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
107         /**
108          * Verifies that second instance was not invoked with first message
109          */
110         verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
111
112         /**
113          * Consumer sends message to nodeTwo for three times. Should be processed by second instance.
114          */
115         final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
116         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
117         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
118         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
119
120         /**
121          * Verifies that second instance was invoked 3 times with second message and first instance wasn't invoked.
122          */
123         verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
124         verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
125
126         LOG.info("Unregistration of the path for the node one in the first provider");
127         firstReg.close();
128
129         LOG.info("Provider 2 registers path of node 1");
130         secondReg.close();
131         secondReg = rpcProviderService.registerRpcImplementation(
132             OpendaylightTestRoutedRpcService.class, odlRoutedService2, Set.of(nodeOnePath));
133
134         /**
135          * A consumer sends third message to node 1.
136          */
137         final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
138         consumerService.routedSimpleRoute(simpleRouteThirdFoo);
139
140         /**
141          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1 time.
142          * TODO: fix unregister path
143          */
144         //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
145         verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
146     }
147
148     /**
149      * Returns node reference from string which represents path.
150      *
151      * @param string string with key(path)
152      * @return instance identifier to {@link UnorderedList}
153      */
154     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
155         return InstanceIdentifier.builder(Lists.class)
156                 .child(UnorderedContainer.class)
157                 .child(UnorderedList.class, new UnorderedListKey(string))
158                 .build();
159     }
160
161     /**
162      * Creates flow AddFlowInput for which only node and cookie are set.
163      *
164      * @param node NodeRef value
165      * @return simpleRouteInput instance
166      */
167     static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
168         return new RoutedSimpleRouteInputBuilder().setRoute(node).build();
169     }
170 }