/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.sample.zeromq.test.it; import junit.framework.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.controller.sal.connector.remoterpc.RemoteRpcClient; import org.opendaylight.controller.sal.connector.remoterpc.dto.Message; import org.opendaylight.controller.sal.core.api.Broker; import org.opendaylight.controller.sample.zeromq.consumer.ExampleConsumer; import org.opendaylight.controller.sample.zeromq.provider.ExampleProvider; import org.opendaylight.controller.test.sal.binding.it.TestHelper; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.junit.PaxExam; import org.ops4j.pax.exam.util.Filter; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; import org.osgi.framework.ServiceReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.zeromq.ZMQ; import javax.inject.Inject; import java.io.IOException; import java.net.URI; import static org.opendaylight.controller.test.sal.binding.it.TestHelper.baseModelBundles; import static org.opendaylight.controller.test.sal.binding.it.TestHelper.bindingAwareSalBundles; //import static org.ops4j.pax.exam.CoreOptions.*; @RunWith(PaxExam.class) public class RouterTest { private Logger _logger = LoggerFactory.getLogger(RouterTest.class); public static final String ODL = "org.opendaylight.controller"; public static final String YANG = "org.opendaylight.yangtools"; public static final String SAMPLE = "org.opendaylight.controller.tests"; private final URI namespace = URI.create("http://cisco.com/example"); private final QName QNAME = new QName(namespace, "heartbeat"); @Inject org.osgi.framework.BundleContext ctx; @Inject @Filter(timeout=60*1000) Broker broker; private ZMQ.Context zmqCtx = ZMQ.context(1); //private Server router; //private ExampleProvider provider; //@Test public void testInvokeRpc() throws Exception{ //Thread.sleep(1000); //Send announcement ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); _logger.debug("Provider sends announcement [{}]", "heartbeat"); provider.announce(QNAME); ServiceReference routerRef = ctx.getServiceReference(RemoteRpcClient.class); RemoteRpcClient router = (RemoteRpcClient) ctx.getService(routerRef); _logger.debug("Found router[{}]", router); _logger.debug("Invoking RPC [{}]", QNAME); for (int i = 0; i < 3; i++) { RpcResult result = router.invokeRpc(QNAME, consumer.getValidCompositeNodeWithOneSimpleChild()); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertNotNull(result); } } @Test public void testInvokeRpcWithValidSimpleNode() throws Exception{ //Thread.sleep(1500); ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); // Provider sends announcement _logger.debug("Provider sends announcement [{}]", "heartbeat"); provider.announce(QNAME); // Consumer invokes RPC _logger.debug("Invoking RPC [{}]", QNAME); CompositeNode input = consumer.getValidCompositeNodeWithOneSimpleChild(); for (int i = 0; i < 3; i++) { RpcResult result = consumer.invokeRpc(QNAME, input); Assert.assertNotNull(result); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertTrue(result.isSuccessful()); Assert.assertNotNull(result.getResult()); Assert.assertEquals(0, result.getErrors().size()); Assert.assertEquals(input.getChildren().size()+1, result.getResult().getChildren().size()); } } @Test public void testInvokeRpcWithValidSimpleNodes() throws Exception{ //Thread.sleep(1500); ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); // Provider sends announcement _logger.debug("Provider sends announcement [{}]", "heartbeat"); provider.announce(QNAME); // Consumer invokes RPC _logger.debug("Invoking RPC [{}]", QNAME); CompositeNode input = consumer.getValidCompositeNodeWithFourSimpleChildren(); for (int i = 0; i < 3; i++) { RpcResult result = consumer.invokeRpc(QNAME, input); Assert.assertNotNull(result); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertTrue(result.isSuccessful()); Assert.assertNotNull(result.getResult()); Assert.assertEquals(0, result.getErrors().size()); Assert.assertEquals(input.getChildren().size()+1, result.getResult().getChildren().size()); } } @Test public void testInvokeRpcWithValidCompositeNode() throws Exception{ //Thread.sleep(1500); ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); // Provider sends announcement _logger.debug("Provider sends announcement [{}]", "heartbeat"); provider.announce(QNAME); // Consumer invokes RPC _logger.debug("Invoking RPC [{}]", QNAME); CompositeNode input = consumer.getValidCompositeNodeWithTwoCompositeChildren(); for (int i = 0; i < 3; i++) { RpcResult result = consumer.invokeRpc(QNAME, input); Assert.assertNotNull(result); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertTrue(result.isSuccessful()); Assert.assertNotNull(result.getResult()); Assert.assertEquals(0, result.getErrors().size()); Assert.assertEquals(input.getChildren().size()+1, result.getResult().getChildren().size()); } } @Test public void testInvokeRpcWithNullInput() throws Exception{ //Thread.sleep(1500); ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); // Provider sends announcement _logger.debug("Provider sends announcement [{}]", QNAME.getLocalName()); provider.announce(QNAME); // Consumer invokes RPC _logger.debug("Invoking RPC [{}]", QNAME); for (int i = 0; i < 3; i++) { RpcResult result = consumer.invokeRpc(QNAME, null); Assert.assertNotNull(result); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertFalse(result.isSuccessful()); Assert.assertNull(result.getResult()); Assert.assertEquals(1, result.getErrors().size()); Assert.assertEquals(RpcError.ErrorSeverity.WARNING, ((RpcError)result.getErrors().toArray()[0]).getSeverity()); } } @Test public void testInvokeRpcWithInvalidSimpleNode() throws Exception{ //Thread.sleep(1500); ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); // Provider sends announcement _logger.debug("Provider sends announcement [{}]", QNAME.getLocalName()); provider.announce(QNAME); // Consumer invokes RPC _logger.debug("Invoking RPC [{}]", QNAME); CompositeNode input = consumer.getInvalidCompositeNodeSimpleChild(); for (int i = 0; i < 3; i++) { RpcResult result = consumer.invokeRpc(QNAME, input); Assert.assertNotNull(result); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertFalse(result.isSuccessful()); Assert.assertNull(result.getResult()); Assert.assertEquals(1, result.getErrors().size()); Assert.assertEquals(RpcError.ErrorSeverity.ERROR, ((RpcError)result.getErrors().toArray()[0]).getSeverity()); } } @Test public void testInvokeRpcWithInvalidCompositeNode() throws Exception{ //Thread.sleep(1500); ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); Assert.assertNotNull(providerRef); ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); Assert.assertNotNull(provider); ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); Assert.assertNotNull(consumerRef); ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); Assert.assertNotNull(consumer); // Provider sends announcement _logger.debug("Provider sends announcement [{}]", QNAME.getLocalName()); provider.announce(QNAME); // Consumer invokes RPC _logger.debug("Invoking RPC [{}]", QNAME); CompositeNode input = consumer.getInvalidCompositeNodeCompositeChild(); for (int i = 0; i < 3; i++) { RpcResult result = consumer.invokeRpc(QNAME, input); Assert.assertNotNull(result); _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); Assert.assertFalse(result.isSuccessful()); Assert.assertNull(result.getResult()); Assert.assertEquals(1, result.getErrors().size()); Assert.assertEquals(RpcError.ErrorSeverity.ERROR, ((RpcError)result.getErrors().toArray()[0]).getSeverity()); } } //@Test // This method is UNTESTED -- need to get around the bundling issues before I know if this even work // public void testInvokeRpcWithValidCompositeNode() throws Exception{ // Thread.sleep(10000); // //Send announcement // ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class); // Assert.assertNotNull(providerRef); // // ExampleProvider provider = (ExampleProvider)ctx.getService(providerRef); // Assert.assertNotNull(provider); // // ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class); // Assert.assertNotNull(consumerRef); // // ExampleConsumer consumer = (ExampleConsumer)ctx.getService(consumerRef); // Assert.assertNotNull(consumer); // // _logger.debug("Provider sends announcement [{}]", "heartbeat"); // provider.announce(QNAME); // ServiceReference routerRef = ctx.getServiceReference(Client.class); // Client router = (Client) ctx.getService(routerRef); // _logger.debug("Found router[{}]", router); // _logger.debug("Invoking RPC [{}]", QNAME); // for (int i = 0; i < 3; i++) { // RpcResult result = router.getInstance().invokeRpc(QNAME, consumer.getValidCompositeNodeWithOneSimpleChild()); // _logger.debug("{}-> Result is: Successful:[{}], Payload:[{}], Errors: [{}]", i, result.isSuccessful(), result.getResult(), result.getErrors()); // Assert.assertNotNull(result); // } // } private Message send(Message msg) throws IOException { ZMQ.Socket reqSocket = zmqCtx.socket(ZMQ.REQ); reqSocket.connect("tcp://localhost:5555"); reqSocket.send(Message.serialize(msg)); Message response = parseMessage(reqSocket); return response; } /** * @param socket * @return */ private Message parseMessage(ZMQ.Socket socket) { Message msg = null; try { byte[] bytes = socket.recv(); _logger.debug("Received bytes:[{}]", bytes.length); msg = (Message) Message.deserialize(bytes); } catch (Throwable t) { t.printStackTrace(); } return msg; } private void printState(){ Bundle[] b = ctx.getBundles(); _logger.debug("\n\nNumber of bundles [{}]\n\n]", b.length); for (int i=0;i