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.*;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.core.MediaType;
30 import java.io.Serializable;
36 private Logger _logger = LoggerFactory.getLogger(Router.class);
37 private final URI namespace = URI.create("http://cisco.com/example");
38 private final QName QNAME = new QName(namespace, "heartbeat");
43 @Produces(MediaType.TEXT_PLAIN)
44 public String hello() {
50 @Produces(MediaType.TEXT_PLAIN)
51 public String announce() {
52 _logger.info("Announce request received");
54 BundleContext ctx = getBundleContext();
55 ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class);
56 if (providerRef == null) {
57 _logger.debug("Could not get provider reference");
58 return "Could not get provider reference";
61 ExampleProvider provider = (ExampleProvider) ctx.getService(providerRef);
62 if (provider == null) {
63 _logger.info("Could not get provider service");
64 return "Could not get provider service";
67 provider.announce(QNAME);
68 return "Announcement sent ";
74 @Produces(MediaType.TEXT_PLAIN)
75 public String invokeRpc() throws Exception {
76 _logger.info("Invoking RPC");
78 ExampleConsumer consumer = getConsumer();
79 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithOneSimpleChild());
80 _logger.info("Result [{}]", result.isSuccessful());
82 return stringify(result);
87 @Produces(MediaType.TEXT_PLAIN)
88 public String invokeRpcSuccess() throws Exception {
89 ExampleConsumer consumer = getConsumer();
90 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithFourSimpleChildren()); //TODO: Change this
91 _logger.info("Result [{}]", result.isSuccessful());
93 return stringify(result);
98 @Produces(MediaType.TEXT_PLAIN)
99 public String invokeRpcFailure() throws Exception {
100 ExampleConsumer consumer = getConsumer();
101 //RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getInvalidCompositeNodeCompositeChild()); //TODO: Change this
102 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, null); //TODO: Change this
103 _logger.info("Result [{}]", result.isSuccessful());
105 return stringify(result);
109 @Path("/routingtable")
110 @Produces(MediaType.TEXT_PLAIN)
111 public String invokeRoutingTable() {
112 _logger.info("Invoking adding an entry in routing table");
114 BundleContext ctx = getBundleContext();
115 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
116 if (routingTableServiceReference == null) {
117 _logger.debug("Could not get routing table impl reference");
118 return "Could not get routingtable referen ";
120 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
121 if (routingTable == null) {
122 _logger.info("Could not get routing table service");
123 return "Could not get routing table service";
127 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
129 routingTable.addGlobalRoute(rii.toString(), "172.27.12.1:5000");
130 } catch (RoutingTableException e) {
131 _logger.error("error in adding routing identifier" + e.getMessage());
133 } catch (SystemException e) {
134 _logger.error("error in adding routing identifier" + e.getMessage());
137 String result = routingTable.dumpRoutingTableCache();
142 _logger.info("Result [{}] routes added for route" + rii + result);
148 @Path("/routingtabledelete")
149 @Produces(MediaType.TEXT_PLAIN)
150 public String invokeDeleteRoutingTable() {
151 _logger.info("Invoking adding an entry in routing table");
153 BundleContext ctx = getBundleContext();
154 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
155 if (routingTableServiceReference == null) {
156 _logger.debug("Could not get routing table impl reference");
157 return "Could not get routingtable referen ";
159 RoutingTable routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
160 if (routingTable == null) {
161 _logger.info("Could not get routing table service");
162 return "Could not get routing table service";
166 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
168 routingTable.removeGlobalRoute(rii.toString());
169 } catch (RoutingTableException e) {
170 _logger.error("error in adding routing identifier" + e.getMessage());
172 } catch (SystemException e) {
173 _logger.error("error in adding routing identifier" + e.getMessage());
176 Set<String> routes = routingTable.getRoutes(rii.toString());
178 StringBuilder stringBuilder = new StringBuilder();
179 if (routes != null) {
180 for (String route : routes) {
181 stringBuilder.append(route);
184 stringBuilder.append(" successfully");
187 _logger.info("Result [{}] routes removed for route" + rii + stringBuilder.toString());
189 return stringBuilder.toString();
192 private String stringify(RpcResult<CompositeNode> result) {
193 CompositeNode node = result.getResult();
194 StringBuilder builder = new StringBuilder("result:").append(XmlUtils.compositeNodeToXml(node)).append("\n")
195 .append("error:").append(result.getErrors()).append("\n");
197 return builder.toString();
200 private BundleContext getBundleContext() {
201 ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
202 Bundle bundle = null;
204 if (tlcl instanceof BundleReference) {
205 bundle = ((BundleReference) tlcl).getBundle();
207 _logger.info("Unable to determine the bundle context based on " +
208 "thread context classloader.");
209 bundle = FrameworkUtil.getBundle(this.getClass());
211 return (bundle == null ? null : bundle.getBundleContext());
214 private ExampleConsumer getConsumer() {
215 BundleContext ctx = getBundleContext();
216 ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class);
217 if (consumerRef == null) {
218 _logger.debug("Could not get consumer reference");
219 throw new NullPointerException("Could not get consumer reference");
221 ExampleConsumer consumer = (ExampleConsumer) ctx.getService(consumerRef);
222 if (consumer == null) {
223 _logger.info("Could not get consumer service");
224 throw new NullPointerException("Could not get consumer service");
229 class RoutingIdentifierImpl implements RpcRouter.RouteIdentifier, Serializable {
231 private final URI namespace = URI.create("http://cisco.com/example");
232 private final QName QNAME = new QName(namespace, "global");
233 private final QName instance = new QName(URI.create("127.0.0.1"), "local");
236 public QName getContext() {
241 public QName getType() {
246 public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier getRoute() {
247 return InstanceIdentifier.of(instance);
251 public boolean equals(Object o) {
252 if (this == o) return true;
253 if (o == null || getClass() != o.getClass()) return false;
255 RoutingIdentifierImpl that = (RoutingIdentifierImpl) o;
257 if (!QNAME.equals(that.QNAME)) return false;
258 if (!instance.equals(that.instance)) return false;
259 if (!namespace.equals(that.namespace)) return false;
265 public int hashCode() {
266 int result = namespace.hashCode();
267 result = 31 * result + QNAME.hashCode();
268 result = 31 * result + instance.hashCode();