Merge "Neutron LBaaS v2.0 API support"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RpcBrokerTest.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
9 package org.opendaylight.controller.remote.rpc;
10
11
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.testkit.JavaTestKit;
15 import com.google.common.util.concurrent.Futures;
16 import junit.framework.Assert;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.remote.rpc.messages.AddRoutedRpc;
22 import org.opendaylight.controller.remote.rpc.messages.AddRpc;
23 import org.opendaylight.controller.remote.rpc.messages.ErrorResponse;
24 import org.opendaylight.controller.remote.rpc.messages.InvokeRoutedRpc;
25 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
26 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
27 import org.opendaylight.controller.remote.rpc.registry.ClusterWrapper;
28 import org.opendaylight.controller.remote.rpc.registry.RpcRegistryOld;
29 import org.opendaylight.controller.sal.common.util.Rpcs;
30 import org.opendaylight.controller.sal.connector.api.RpcRouter;
31 import org.opendaylight.controller.sal.core.api.Broker;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
36 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
37 import org.opendaylight.yangtools.yang.data.api.Node;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41
42 import java.net.URI;
43 import java.net.URISyntaxException;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.HashSet;
47 import java.util.Set;
48 import java.util.concurrent.Future;
49
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
52
53 public class RpcBrokerTest {
54
55   static ActorSystem system;
56
57
58   @BeforeClass
59   public static void setup() {
60     system = ActorSystem.create();
61   }
62
63   @AfterClass
64   public static void teardown() {
65     JavaTestKit.shutdownActorSystem(system);
66     system = null;
67   }
68
69   @Test
70   public void testInvokeRpcError() throws URISyntaxException {
71     new JavaTestKit(system) {{
72       ActorRef rpcRegistry = system.actorOf(RpcRegistryOld.props(Mockito.mock(ClusterWrapper.class)));
73       Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
74       SchemaContext schemaContext = mock(SchemaContext.class);
75       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
76       QName rpc = new QName(new URI("noactor1"), "noactor1");
77       CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "no child"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
78       InvokeRpc invokeMsg = new InvokeRpc(rpc, input);
79       rpcBroker.tell(invokeMsg, getRef());
80
81       Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
82         protected Boolean match(Object in) {
83           if (in instanceof ErrorResponse) {
84             ErrorResponse reply = (ErrorResponse)in;
85             return reply.getException().getMessage().contains("No remote actor found for rpc execution of :");
86           } else {
87             throw noMatch();
88           }
89         }
90       }.get(); // this extracts the received message
91
92       Assert.assertTrue(getMsg);
93     }};
94   }
95
96   /**
97    * This test method invokes and executes the remote rpc
98    */
99
100   @Test
101   public void testInvokeRpc() throws URISyntaxException {
102     new JavaTestKit(system) {{
103       ActorRef rpcRegistry = system.actorOf(RpcRegistryOld.props(mock(ClusterWrapper.class)));
104       Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
105       SchemaContext schemaContext = mock(SchemaContext.class);
106       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
107       ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor1");
108       // Add RPC in table
109       QName rpc = new QName(new URI("actor1"), "actor1");
110       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
111       final String route = rpcBrokerRemote.path().toString();
112       AddRpc rpcMsg = new AddRpc(routeId, route);
113       rpcRegistry.tell(rpcMsg, getRef());
114       expectMsgEquals(duration("2 second"), "Success");
115
116       // invoke rpc
117       CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
118       CompositeNode invokeRpcResult = mock(CompositeNode.class);
119       Collection<RpcError> errors = new ArrayList<>();
120       RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
121       Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
122       when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
123       InvokeRpc invokeMsg = new InvokeRpc(rpc, input);
124       rpcBroker.tell(invokeMsg, getRef());
125
126       //verify response msg
127       Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
128         protected Boolean match(Object in) {
129           if (in instanceof RpcResponse) {
130             return true;
131           } else {
132             throw noMatch();
133           }
134         }
135       }.get(); // this extracts the received message
136
137       Assert.assertTrue(getMsg);
138     }};
139   }
140
141   @Test
142   public void testInvokeRoutedRpcError() throws URISyntaxException {
143     new JavaTestKit(system) {{
144       ActorRef rpcRegistry = system.actorOf(RpcRegistryOld.props(Mockito.mock(ClusterWrapper.class)));
145       Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
146       SchemaContext schemaContext = mock(SchemaContext.class);
147       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
148       QName rpc = new QName(new URI("actor1"), "actor1");
149       CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
150       InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(rpc)), input);
151       rpcBroker.tell(invokeMsg, getRef());
152
153       Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
154         protected Boolean match(Object in) {
155           if (in instanceof ErrorResponse) {
156             ErrorResponse reply = (ErrorResponse)in;
157             return "No remote actor found for rpc execution.".equals(reply.getException().getMessage());
158           } else {
159             throw noMatch();
160           }
161         }
162       }.get(); // this extracts the received message
163
164       Assert.assertTrue(getMsg);
165     }};
166   }
167
168   /**
169    * This test method invokes and executes the remote routed rpc
170    */
171
172   @Test
173   public void testInvokeRoutedRpc() throws URISyntaxException {
174     new JavaTestKit(system) {{
175       ActorRef rpcRegistry = system.actorOf(RpcRegistryOld.props(mock(ClusterWrapper.class)));
176       Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
177       SchemaContext schemaContext = mock(SchemaContext.class);
178       ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
179       ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor2");
180       // Add Routed RPC in table
181       QName rpc = new QName(new URI("actor2"), "actor2");
182       YangInstanceIdentifier identifier = YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(rpc));
183       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, identifier);
184       final String route = rpcBrokerRemote.path().toString();
185       Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
186       routeIds.add(routeId);
187
188       AddRoutedRpc rpcMsg = new AddRoutedRpc(routeIds, route);
189       rpcRegistry.tell(rpcMsg, getRef());
190       expectMsgEquals(duration("2 second"), "Success");
191
192       // invoke rpc
193       CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
194       CompositeNode invokeRpcResult = mock(CompositeNode.class);
195       Collection<RpcError> errors = new ArrayList<>();
196       RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
197       Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
198       when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
199       InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, identifier, input);
200       rpcBroker.tell(invokeMsg, getRef());
201
202       //verify response msg
203       Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
204         protected Boolean match(Object in) {
205           if (in instanceof RpcResponse) {
206             return true;
207           } else {
208             throw noMatch();
209           }
210         }
211       }.get(); // this extracts the received message
212
213       Assert.assertTrue(getMsg);
214     }};
215   }
216
217 }