2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sample.zeromq.consumer;
10 import java.io.FileNotFoundException;
11 import java.io.InputStream;
13 import java.util.Hashtable;
14 import java.util.concurrent.Future;
16 import org.opendaylight.controller.sal.core.api.AbstractConsumer;
17 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 import org.opendaylight.yangtools.yang.data.api.Node;
22 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceRegistration;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
29 import javax.xml.stream.XMLStreamException;
31 public class ExampleConsumer extends AbstractConsumer {
33 private final URI namespace = URI.create("http://cisco.com/example");
34 private final QName QNAME = new QName(namespace, "heartbeat");
36 private ConsumerSession session;
38 private ServiceRegistration<ExampleConsumer> thisReg;
39 private Logger _logger = LoggerFactory.getLogger(ExampleConsumer.class);
42 public void onSessionInitiated(ConsumerSession session) {
43 this.session = session;
46 public RpcResult<CompositeNode> invokeRpc(QName qname, CompositeNode input) {
47 _logger.info("Invoking RPC:[{}] with Input:[{}]", qname.getLocalName(), input);
48 RpcResult<CompositeNode> result = null;
49 Future<RpcResult<CompositeNode>> future = ExampleConsumer.this.session.rpc(qname, input);
51 result = future.get();
52 } catch (Exception e) {
55 _logger.info("Returning Result:[{}]", result);
60 protected void startImpl(BundleContext context){
61 thisReg = context.registerService(ExampleConsumer.class, this, new Hashtable<String,String>());
64 protected void stopImpl(BundleContext context) {
65 super.stopImpl(context);
69 public CompositeNode getValidCompositeNodeWithOneSimpleChild() throws FileNotFoundException {
70 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleChild.xml");
71 return loadCompositeNode(xmlStream);
74 public CompositeNode getValidCompositeNodeWithTwoSimpleChildren() throws FileNotFoundException {
75 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoSimpleChildren.xml");
76 return loadCompositeNode(xmlStream);
79 public CompositeNode getValidCompositeNodeWithFourSimpleChildren() throws FileNotFoundException {
80 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/FourSimpleChildren.xml");
81 return loadCompositeNode(xmlStream);
84 public CompositeNode getValidCompositeNodeWithOneSimpleOneCompositeChild() throws FileNotFoundException {
85 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleOneCompositeChild.xml");
86 return loadCompositeNode(xmlStream);
89 public CompositeNode getValidCompositeNodeWithTwoCompositeChildren() throws FileNotFoundException {
90 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoCompositeChildren.xml");
91 return loadCompositeNode(xmlStream);
94 public CompositeNode getInvalidCompositeNodeSimpleChild() throws FileNotFoundException {
95 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidSimpleChild.xml");
96 return loadCompositeNode(xmlStream);
99 public CompositeNode getInvalidCompositeNodeCompositeChild() throws FileNotFoundException {
100 InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidCompositeChild.xml");
101 return loadCompositeNode(xmlStream);
104 //Note to self: Stolen from TestUtils
105 ///Users/alefan/odl/controller4/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
106 // Figure out how to include TestUtils through pom ...was getting errors
107 private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
108 if (xmlInputStream == null) {
109 throw new IllegalArgumentException();
113 dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
114 } catch (XMLStreamException e) {
115 _logger.error("Error during building data tree from XML", e);
118 if (dataTree == null) {
119 _logger.error("data tree is null");
122 if (dataTree instanceof SimpleNode) {
123 _logger.error("RPC XML was resolved as SimpleNode");
126 return (CompositeNode) dataTree;