Merge changes I8c23739a,Ia0e70828
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / integrationtest / consumer-service / src / main / java / org / opendaylight / controller / sample / zeromq / consumer / ExampleConsumer.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 package org.opendaylight.controller.sample.zeromq.consumer;
9
10 import java.io.FileNotFoundException;
11 import java.io.InputStream;
12 import java.net.URI;
13 import java.util.Hashtable;
14 import java.util.concurrent.*;
15
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;
28 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
29
30 import javax.xml.stream.XMLStreamException;
31
32 public class ExampleConsumer extends AbstractConsumer {
33
34   private final URI namespace = URI.create("http://cisco.com/example");
35   private final QName QNAME = new QName(namespace, "heartbeat");
36
37   private ConsumerSession session;
38
39   private ServiceRegistration<ExampleConsumer> thisReg;
40   private Logger _logger = LoggerFactory.getLogger(ExampleConsumer.class);
41
42   @Override
43   public void onSessionInitiated(ConsumerSession session) {
44     this.session = session;
45   }
46
47   public RpcResult<CompositeNode> invokeRpc(QName qname, CompositeNode input) {
48     _logger.info("Invoking RPC:[{}] with Input:[{}]", qname.getLocalName(), input);
49     RpcResult<CompositeNode> result = null;
50     Future<RpcResult<CompositeNode>> future = ExampleConsumer.this.session.rpc(qname, input);
51     try {
52       result = future.get();
53     } catch (Exception e) {
54       e.printStackTrace();
55     }
56     _logger.info("Returning Result:[{}]", result);
57     return result;
58   }
59
60   @Override
61   protected void startImpl(BundleContext context){
62     thisReg = context.registerService(ExampleConsumer.class, this, new Hashtable<String,String>());
63   }
64   @Override
65   protected void stopImpl(BundleContext context) {
66     super.stopImpl(context);
67     thisReg.unregister();
68   }
69
70   public CompositeNode getValidCompositeNodeWithOneSimpleChild() throws FileNotFoundException {
71     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleChild.xml");
72     return loadCompositeNode(xmlStream);
73   }
74
75   public CompositeNode getValidCompositeNodeWithTwoSimpleChildren() throws FileNotFoundException {
76     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoSimpleChildren.xml");
77     return loadCompositeNode(xmlStream);
78   }
79
80   public CompositeNode getValidCompositeNodeWithFourSimpleChildren() throws FileNotFoundException {
81     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/FourSimpleChildren.xml");
82     return loadCompositeNode(xmlStream);
83   }
84
85   public CompositeNode getValidCompositeNodeWithOneSimpleOneCompositeChild() throws FileNotFoundException {
86     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleOneCompositeChild.xml");
87     return loadCompositeNode(xmlStream);
88   }
89
90   public CompositeNode getValidCompositeNodeWithTwoCompositeChildren() throws FileNotFoundException {
91     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoCompositeChildren.xml");
92     return loadCompositeNode(xmlStream);
93   }
94
95   public CompositeNode getInvalidCompositeNodeSimpleChild() throws FileNotFoundException {
96     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidSimpleChild.xml");
97     return loadCompositeNode(xmlStream);
98   }
99
100   public CompositeNode getInvalidCompositeNodeCompositeChild() throws FileNotFoundException {
101     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidCompositeChild.xml");
102     return loadCompositeNode(xmlStream);
103   }
104
105   //Note to self:  Stolen from TestUtils
106   ///Users/alefan/odl/controller4/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
107   // Figure out how to include TestUtils through pom ...was getting errors
108   private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
109     if (xmlInputStream == null) {
110       throw new IllegalArgumentException();
111     }
112     Node<?> dataTree;
113     try {
114       dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
115     } catch (XMLStreamException e) {
116       _logger.error("Error during building data tree from XML", e);
117       return null;
118     }
119     if (dataTree == null) {
120       _logger.error("data tree is null");
121       return null;
122     }
123     if (dataTree instanceof SimpleNode) {
124       _logger.error("RPC XML was resolved as SimpleNode");
125       return null;
126     }
127     return (CompositeNode) dataTree;
128   }
129 }