1 package org.opendaylight.controller.tests.zmqrouter.rest;
3 import org.opendaylight.controller.sal.connector.api.RpcRouter;
4 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
5 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
6 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
7 import org.opendaylight.controller.sal.connector.remoterpc.impl.RoutingTableImpl;
8 import org.opendaylight.controller.sal.connector.remoterpc.util.XmlUtils;
9 import org.opendaylight.controller.sample.zeromq.consumer.ExampleConsumer;
10 import org.opendaylight.controller.sample.zeromq.provider.ExampleProvider;
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.InstanceIdentifier;
15 import org.osgi.framework.*;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType;
23 import java.io.Serializable;
29 private Logger _logger = LoggerFactory.getLogger(Router.class);
30 private final URI namespace = URI.create("http://cisco.com/example");
31 private final QName QNAME = new QName(namespace, "heartbeat");
36 @Produces(MediaType.TEXT_PLAIN)
37 public String hello() {
43 @Produces(MediaType.TEXT_PLAIN)
44 public String announce() {
45 _logger.info("Announce request received");
47 BundleContext ctx = getBundleContext();
48 ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class);
49 if (providerRef == null) {
50 _logger.debug("Could not get provider reference");
51 return "Could not get provider reference";
54 ExampleProvider provider = (ExampleProvider) ctx.getService(providerRef);
55 if (provider == null) {
56 _logger.info("Could not get provider service");
57 return "Could not get provider service";
60 provider.announce(QNAME);
61 return "Announcement sent ";
67 @Produces(MediaType.TEXT_PLAIN)
68 public String invokeRpc() throws Exception {
69 _logger.info("Invoking RPC");
71 ExampleConsumer consumer = getConsumer();
72 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithOneSimpleChild());
73 _logger.info("Result [{}]", result.isSuccessful());
75 return stringify(result);
80 @Produces(MediaType.TEXT_PLAIN)
81 public String invokeRpcSuccess() throws Exception {
82 ExampleConsumer consumer = getConsumer();
83 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithFourSimpleChildren()); //TODO: Change this
84 _logger.info("Result [{}]", result.isSuccessful());
86 return stringify(result);
91 @Produces(MediaType.TEXT_PLAIN)
92 public String invokeRpcFailure() throws Exception {
93 ExampleConsumer consumer = getConsumer();
94 //RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getInvalidCompositeNodeCompositeChild()); //TODO: Change this
95 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, null); //TODO: Change this
96 _logger.info("Result [{}]", result.isSuccessful());
98 return stringify(result);
102 @Path("/routingtable")
103 @Produces(MediaType.TEXT_PLAIN)
104 public String invokeRoutingTable() {
105 _logger.info("Invoking adding an entry in routing table");
107 BundleContext ctx = getBundleContext();
108 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
109 if (routingTableServiceReference == null) {
110 _logger.debug("Could not get routing table impl reference");
111 return "Could not get routingtable referen ";
113 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
114 if (routingTable == null) {
115 _logger.info("Could not get routing table service");
116 return "Could not get routing table service";
120 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
122 routingTable.addGlobalRoute(rii.toString(), "172.27.12.1:5000");
123 } catch (RoutingTableException e) {
124 _logger.error("error in adding routing identifier" + e.getMessage());
126 } catch (SystemException e) {
127 _logger.error("error in adding routing identifier" + e.getMessage());
130 String result = routingTable.dumpRoutingTableCache();
135 _logger.info("Result [{}] routes added for route" + rii + result);
141 @Path("/routingtabledelete")
142 @Produces(MediaType.TEXT_PLAIN)
143 public String invokeDeleteRoutingTable() {
144 _logger.info("Invoking adding an entry in routing table");
146 BundleContext ctx = getBundleContext();
147 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
148 if (routingTableServiceReference == null) {
149 _logger.debug("Could not get routing table impl reference");
150 return "Could not get routingtable referen ";
152 RoutingTable routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
153 if (routingTable == null) {
154 _logger.info("Could not get routing table service");
155 return "Could not get routing table service";
159 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
161 routingTable.removeGlobalRoute(rii.toString());
162 } catch (RoutingTableException e) {
163 _logger.error("error in adding routing identifier" + e.getMessage());
165 } catch (SystemException e) {
166 _logger.error("error in adding routing identifier" + e.getMessage());
169 Set<String> routes = routingTable.getRoutes(rii.toString());
171 StringBuilder stringBuilder = new StringBuilder();
172 if (routes != null) {
173 for (String route : routes) {
174 stringBuilder.append(route);
177 stringBuilder.append(" successfully");
180 _logger.info("Result [{}] routes removed for route" + rii + stringBuilder.toString());
182 return stringBuilder.toString();
185 private String stringify(RpcResult<CompositeNode> result) {
186 CompositeNode node = result.getResult();
187 StringBuilder builder = new StringBuilder("result:").append(XmlUtils.compositeNodeToXml(node)).append("\n")
188 .append("error:").append(result.getErrors()).append("\n");
190 return builder.toString();
193 private BundleContext getBundleContext() {
194 ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
195 Bundle bundle = null;
197 if (tlcl instanceof BundleReference) {
198 bundle = ((BundleReference) tlcl).getBundle();
200 _logger.info("Unable to determine the bundle context based on " +
201 "thread context classloader.");
202 bundle = FrameworkUtil.getBundle(this.getClass());
204 return (bundle == null ? null : bundle.getBundleContext());
207 private ExampleConsumer getConsumer() {
208 BundleContext ctx = getBundleContext();
209 ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class);
210 if (consumerRef == null) {
211 _logger.debug("Could not get consumer reference");
212 throw new NullPointerException("Could not get consumer reference");
214 ExampleConsumer consumer = (ExampleConsumer) ctx.getService(consumerRef);
215 if (consumer == null) {
216 _logger.info("Could not get consumer service");
217 throw new NullPointerException("Could not get consumer service");
222 class RoutingIdentifierImpl implements RpcRouter.RouteIdentifier, Serializable {
224 private final URI namespace = URI.create("http://cisco.com/example");
225 private final QName QNAME = new QName(namespace, "global");
226 private final QName instance = new QName(URI.create("127.0.0.1"), "local");
229 public QName getContext() {
234 public QName getType() {
239 public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier getRoute() {
240 return InstanceIdentifier.of(instance);
244 public boolean equals(Object o) {
245 if (this == o) return true;
246 if (o == null || getClass() != o.getClass()) return false;
248 RoutingIdentifierImpl that = (RoutingIdentifierImpl) o;
250 if (!QNAME.equals(that.QNAME)) return false;
251 if (!instance.equals(that.instance)) return false;
252 if (!namespace.equals(that.namespace)) return false;
258 public int hashCode() {
259 int result = namespace.hashCode();
260 result = 31 * result + QNAME.hashCode();
261 result = 31 * result + instance.hashCode();