Merge "Added conflict handling between configuration and state choice nodes. -unique...
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / integrationtest / consumer-service / src / main / java / org / opendaylight / controller / sample / zeromq / consumer / ExampleConsumer.java
1 package org.opendaylight.controller.sample.zeromq.consumer;
2
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
5 import java.net.URI;
6 import java.util.Hashtable;
7 import java.util.concurrent.*;
8
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;
22
23 import javax.xml.stream.XMLStreamException;
24
25 public class ExampleConsumer extends AbstractConsumer {
26
27   private final URI namespace = URI.create("http://cisco.com/example");
28   private final QName QNAME = new QName(namespace, "heartbeat");
29
30   private ConsumerSession session;
31
32   private ServiceRegistration<ExampleConsumer> thisReg;
33   private Logger _logger = LoggerFactory.getLogger(ExampleConsumer.class);
34
35   @Override
36   public void onSessionInitiated(ConsumerSession session) {
37     this.session = session;
38   }
39
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);
44     try {
45       result = future.get();
46     } catch (Exception e) {
47       e.printStackTrace();
48     }
49     _logger.info("Returning Result:[{}]", result);
50     return result;
51   }
52
53   @Override
54   protected void startImpl(BundleContext context){
55     thisReg = context.registerService(ExampleConsumer.class, this, new Hashtable<String,String>());
56   }
57   @Override
58   protected void stopImpl(BundleContext context) {
59     super.stopImpl(context);
60     thisReg.unregister();
61   }
62
63   public CompositeNode getValidCompositeNodeWithOneSimpleChild() throws FileNotFoundException {
64     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleChild.xml");
65     return loadCompositeNode(xmlStream);
66   }
67
68   public CompositeNode getValidCompositeNodeWithTwoSimpleChildren() throws FileNotFoundException {
69     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoSimpleChildren.xml");
70     return loadCompositeNode(xmlStream);
71   }
72
73   public CompositeNode getValidCompositeNodeWithFourSimpleChildren() throws FileNotFoundException {
74     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/FourSimpleChildren.xml");
75     return loadCompositeNode(xmlStream);
76   }
77
78   public CompositeNode getValidCompositeNodeWithOneSimpleOneCompositeChild() throws FileNotFoundException {
79     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleOneCompositeChild.xml");
80     return loadCompositeNode(xmlStream);
81   }
82
83   public CompositeNode getValidCompositeNodeWithTwoCompositeChildren() throws FileNotFoundException {
84     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoCompositeChildren.xml");
85     return loadCompositeNode(xmlStream);
86   }
87
88   public CompositeNode getInvalidCompositeNodeSimpleChild() throws FileNotFoundException {
89     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidSimpleChild.xml");
90     return loadCompositeNode(xmlStream);
91   }
92
93   public CompositeNode getInvalidCompositeNodeCompositeChild() throws FileNotFoundException {
94     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidCompositeChild.xml");
95     return loadCompositeNode(xmlStream);
96   }
97
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();
104     }
105     Node<?> dataTree;
106     try {
107       dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
108     } catch (XMLStreamException e) {
109       _logger.error("Error during building data tree from XML", e);
110       return null;
111     }
112     if (dataTree == null) {
113       _logger.error("data tree is null");
114       return null;
115     }
116     if (dataTree instanceof SimpleNode) {
117       _logger.error("RPC XML was resolved as SimpleNode");
118       return null;
119     }
120     return (CompositeNode) dataTree;
121   }
122 }