Implement finding a primary based on the shard name and do basic wiring of Distribute...
[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.Future;
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
29 import javax.xml.stream.XMLStreamException;
30
31 public class ExampleConsumer extends AbstractConsumer {
32
33   private final URI namespace = URI.create("http://cisco.com/example");
34   private final QName QNAME = new QName(namespace, "heartbeat");
35
36   private ConsumerSession session;
37
38   private ServiceRegistration<ExampleConsumer> thisReg;
39   private Logger _logger = LoggerFactory.getLogger(ExampleConsumer.class);
40
41   @Override
42   public void onSessionInitiated(ConsumerSession session) {
43     this.session = session;
44   }
45
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);
50     try {
51       result = future.get();
52     } catch (Exception e) {
53       e.printStackTrace();
54     }
55     _logger.info("Returning Result:[{}]", result);
56     return result;
57   }
58
59   @Override
60   protected void startImpl(BundleContext context){
61     thisReg = context.registerService(ExampleConsumer.class, this, new Hashtable<String,String>());
62   }
63   @Override
64   protected void stopImpl(BundleContext context) {
65     super.stopImpl(context);
66     thisReg.unregister();
67   }
68
69   public CompositeNode getValidCompositeNodeWithOneSimpleChild() throws FileNotFoundException {
70     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleChild.xml");
71     return loadCompositeNode(xmlStream);
72   }
73
74   public CompositeNode getValidCompositeNodeWithTwoSimpleChildren() throws FileNotFoundException {
75     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoSimpleChildren.xml");
76     return loadCompositeNode(xmlStream);
77   }
78
79   public CompositeNode getValidCompositeNodeWithFourSimpleChildren() throws FileNotFoundException {
80     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/FourSimpleChildren.xml");
81     return loadCompositeNode(xmlStream);
82   }
83
84   public CompositeNode getValidCompositeNodeWithOneSimpleOneCompositeChild() throws FileNotFoundException {
85     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/OneSimpleOneCompositeChild.xml");
86     return loadCompositeNode(xmlStream);
87   }
88
89   public CompositeNode getValidCompositeNodeWithTwoCompositeChildren() throws FileNotFoundException {
90     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/TwoCompositeChildren.xml");
91     return loadCompositeNode(xmlStream);
92   }
93
94   public CompositeNode getInvalidCompositeNodeSimpleChild() throws FileNotFoundException {
95     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidSimpleChild.xml");
96     return loadCompositeNode(xmlStream);
97   }
98
99   public CompositeNode getInvalidCompositeNodeCompositeChild() throws FileNotFoundException {
100     InputStream xmlStream = ExampleConsumer.class.getResourceAsStream("/InvalidCompositeChild.xml");
101     return loadCompositeNode(xmlStream);
102   }
103
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();
110     }
111     Node<?> dataTree;
112     try {
113       dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
114     } catch (XMLStreamException e) {
115       _logger.error("Error during building data tree from XML", e);
116       return null;
117     }
118     if (dataTree == null) {
119       _logger.error("data tree is null");
120       return null;
121     }
122     if (dataTree instanceof SimpleNode) {
123       _logger.error("RPC XML was resolved as SimpleNode");
124       return null;
125     }
126     return (CompositeNode) dataTree;
127   }
128 }