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.tests.zmqrouter.rest;
10 import org.opendaylight.controller.sal.connector.api.RpcRouter;
11 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
12 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
13 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
14 import org.opendaylight.controller.sal.connector.remoterpc.impl.RoutingTableImpl;
15 import org.opendaylight.controller.sal.connector.remoterpc.util.XmlUtils;
16 import org.opendaylight.controller.sample.zeromq.consumer.ExampleConsumer;
17 import org.opendaylight.controller.sample.zeromq.provider.ExampleProvider;
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.InstanceIdentifier;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.ServiceReference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.MediaType;
31 import java.io.Serializable;
37 private Logger _logger = LoggerFactory.getLogger(Router.class);
38 private final URI namespace = URI.create("http://cisco.com/example");
39 private final QName QNAME = new QName(namespace, "heartbeat");
44 @Produces(MediaType.TEXT_PLAIN)
45 public String hello() {
51 @Produces(MediaType.TEXT_PLAIN)
52 public String announce() {
53 _logger.info("Announce request received");
55 BundleContext ctx = getBundleContext();
56 ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class);
57 if (providerRef == null) {
58 _logger.debug("Could not get provider reference");
59 return "Could not get provider reference";
62 ExampleProvider provider = (ExampleProvider) ctx.getService(providerRef);
63 if (provider == null) {
64 _logger.info("Could not get provider service");
65 return "Could not get provider service";
68 provider.announce(QNAME);
69 return "Announcement sent ";
75 @Produces(MediaType.TEXT_PLAIN)
76 public String invokeRpc() throws Exception {
77 _logger.info("Invoking RPC");
79 ExampleConsumer consumer = getConsumer();
80 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithOneSimpleChild());
81 _logger.info("Result [{}]", result.isSuccessful());
83 return stringify(result);
88 @Produces(MediaType.TEXT_PLAIN)
89 public String invokeRpcSuccess() throws Exception {
90 ExampleConsumer consumer = getConsumer();
91 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithFourSimpleChildren()); //TODO: Change this
92 _logger.info("Result [{}]", result.isSuccessful());
94 return stringify(result);
99 @Produces(MediaType.TEXT_PLAIN)
100 public String invokeRpcFailure() throws Exception {
101 ExampleConsumer consumer = getConsumer();
102 //RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getInvalidCompositeNodeCompositeChild()); //TODO: Change this
103 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, null); //TODO: Change this
104 _logger.info("Result [{}]", result.isSuccessful());
106 return stringify(result);
110 @Path("/routingtable")
111 @Produces(MediaType.TEXT_PLAIN)
112 public String invokeRoutingTable() {
113 _logger.info("Invoking adding an entry in routing table");
115 BundleContext ctx = getBundleContext();
116 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
117 if (routingTableServiceReference == null) {
118 _logger.debug("Could not get routing table impl reference");
119 return "Could not get routingtable referen ";
121 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
122 if (routingTable == null) {
123 _logger.info("Could not get routing table service");
124 return "Could not get routing table service";
128 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
130 routingTable.addGlobalRoute(rii.toString(), "172.27.12.1:5000");
131 } catch (RoutingTableException e) {
132 _logger.error("error in adding routing identifier" + e.getMessage());
134 } catch (SystemException e) {
135 _logger.error("error in adding routing identifier" + e.getMessage());
138 String result = routingTable.dumpRoutingTableCache();
143 _logger.info("Result [{}] routes added for route" + rii + result);
149 @Path("/routingtabledelete")
150 @Produces(MediaType.TEXT_PLAIN)
151 public String invokeDeleteRoutingTable() {
152 _logger.info("Invoking adding an entry in routing table");
154 BundleContext ctx = getBundleContext();
155 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
156 if (routingTableServiceReference == null) {
157 _logger.debug("Could not get routing table impl reference");
158 return "Could not get routingtable referen ";
160 RoutingTable routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
161 if (routingTable == null) {
162 _logger.info("Could not get routing table service");
163 return "Could not get routing table service";
167 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
169 routingTable.removeGlobalRoute(rii.toString());
170 } catch (RoutingTableException e) {
171 _logger.error("error in adding routing identifier" + e.getMessage());
173 } catch (SystemException e) {
174 _logger.error("error in adding routing identifier" + e.getMessage());
177 Set<String> routes = routingTable.getRoutes(rii.toString());
179 StringBuilder stringBuilder = new StringBuilder();
180 if (routes != null) {
181 for (String route : routes) {
182 stringBuilder.append(route);
185 stringBuilder.append(" successfully");
188 _logger.info("Result [{}] routes removed for route" + rii + stringBuilder.toString());
190 return stringBuilder.toString();
193 private String stringify(RpcResult<CompositeNode> result) {
194 CompositeNode node = result.getResult();
195 StringBuilder builder = new StringBuilder("result:").append(XmlUtils.compositeNodeToXml(node)).append("\n")
196 .append("error:").append(result.getErrors()).append("\n");
198 return builder.toString();
201 private BundleContext getBundleContext() {
202 ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
203 Bundle bundle = null;
205 if (tlcl instanceof BundleReference) {
206 bundle = ((BundleReference) tlcl).getBundle();
208 _logger.info("Unable to determine the bundle context based on " +
209 "thread context classloader.");
210 bundle = FrameworkUtil.getBundle(this.getClass());
212 return (bundle == null ? null : bundle.getBundleContext());
215 private ExampleConsumer getConsumer() {
216 BundleContext ctx = getBundleContext();
217 ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class);
218 if (consumerRef == null) {
219 _logger.debug("Could not get consumer reference");
220 throw new NullPointerException("Could not get consumer reference");
222 ExampleConsumer consumer = (ExampleConsumer) ctx.getService(consumerRef);
223 if (consumer == null) {
224 _logger.info("Could not get consumer service");
225 throw new NullPointerException("Could not get consumer service");
230 class RoutingIdentifierImpl implements RpcRouter.RouteIdentifier, Serializable {
232 private final URI namespace = URI.create("http://cisco.com/example");
233 private final QName QNAME = new QName(namespace, "global");
234 private final QName instance = new QName(URI.create("127.0.0.1"), "local");
237 public QName getContext() {
242 public QName getType() {
247 public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier getRoute() {
248 return InstanceIdentifier.of(instance);
252 public boolean equals(Object o) {
253 if (this == o) return true;
254 if (o == null || getClass() != o.getClass()) return false;
256 RoutingIdentifierImpl that = (RoutingIdentifierImpl) o;
258 if (!QNAME.equals(that.QNAME)) return false;
259 if (!instance.equals(that.instance)) return false;
260 if (!namespace.equals(that.namespace)) return false;
266 public int hashCode() {
267 int result = namespace.hashCode();
268 result = 31 * result + QNAME.hashCode();
269 result = 31 * result + instance.hashCode();