1 package org.opendaylight.controller.sample.zeromq.consumer;
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
6 import java.util.Hashtable;
7 import java.util.concurrent.*;
9 import org.opendaylight.controller.sal.core.api.AbstractConsumer;
10 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.common.RpcResult;
13 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
14 import org.opendaylight.yangtools.yang.data.api.Node;
15 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceRegistration;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
23 import javax.xml.stream.XMLStreamException;
25 public class ExampleConsumer extends AbstractConsumer {
27 private final URI namespace = URI.create("http://cisco.com/example");
28 private final QName QNAME = new QName(namespace, "heartbeat");
30 private ConsumerSession session;
32 private ServiceRegistration<ExampleConsumer> thisReg;
33 private Logger _logger = LoggerFactory.getLogger(ExampleConsumer.class);
36 public void onSessionInitiated(ConsumerSession session) {
37 this.session = session;
40 public RpcResult<CompositeNode> invokeRpc(QName qname, CompositeNode input) {
41 _logger.info("Invoking RPC:[{}] with Input:[{}]", qname.getLocalName(), input);
42 RpcResult<CompositeNode> result = null;
43 Future<RpcResult<CompositeNode>> future = ExampleConsumer.this.session.rpc(qname, input);
45 result = future.get();
46 } catch (Exception e) {
49 _logger.info("Returning Result:[{}]", result);
54 protected void startImpl(BundleContext context){
55 thisReg = context.registerService(ExampleConsumer.class, this, new Hashtable<String,String>());
58 protected void stopImpl(BundleContext context) {
59 super.stopImpl(context);
63 public CompositeNode getValidCompositeNodeWithOneSimpleChild() throws FileNotFoundException {
64 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleChild.xml");
65 return loadCompositeNode(xmlStream);
68 public CompositeNode getValidCompositeNodeWithTwoSimpleChildren() throws FileNotFoundException {
69 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoSimpleChildren.xml");
70 return loadCompositeNode(xmlStream);
73 public CompositeNode getValidCompositeNodeWithFourSimpleChildren() throws FileNotFoundException {
74 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/FourSimpleChildren.xml");
75 return loadCompositeNode(xmlStream);
78 public CompositeNode getValidCompositeNodeWithOneSimpleOneCompositeChild() throws FileNotFoundException {
79 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleOneCompositeChild.xml");
80 return loadCompositeNode(xmlStream);
83 public CompositeNode getValidCompositeNodeWithTwoCompositeChildren() throws FileNotFoundException {
84 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoCompositeChildren.xml");
85 return loadCompositeNode(xmlStream);
88 public CompositeNode getInvalidCompositeNodeSimpleChild() throws FileNotFoundException {
89 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidSimpleChild.xml");
90 return loadCompositeNode(xmlStream);
93 public CompositeNode getInvalidCompositeNodeCompositeChild() throws FileNotFoundException {
94 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidCompositeChild.xml");
95 return loadCompositeNode(xmlStream);
98 //Note to self: Stolen from TestUtils
99 ///Users/alefan/odl/controller4/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
100 // Figure out how to include TestUtils through pom ...was getting errors
101 private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
102 if (xmlInputStream == null) {
103 throw new IllegalArgumentException();
107 dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
108 } catch (XMLStreamException e) {
109 _logger.error("Error during building data tree from XML", e);
112 if (dataTree == null) {
113 _logger.error("data tree is null");
116 if (dataTree instanceof SimpleNode) {
117 _logger.error("RPC XML was resolved as SimpleNode");
120 return (CompositeNode) dataTree;