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.dto.CompositeNodeImpl;
8 import org.opendaylight.controller.sal.connector.remoterpc.impl.RoutingTableImpl;
9 import org.opendaylight.controller.sal.connector.remoterpc.util.XmlUtils;
10 import org.opendaylight.controller.sample.zeromq.consumer.ExampleConsumer;
11 import org.opendaylight.controller.sample.zeromq.provider.ExampleProvider;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.RpcResult;
14 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
16 import org.osgi.framework.*;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.MediaType;
24 import java.io.Serializable;
30 private Logger _logger = LoggerFactory.getLogger(Router.class);
31 private final URI namespace = URI.create("http://cisco.com/example");
32 private final QName QNAME = new QName(namespace, "heartbeat");
37 @Produces(MediaType.TEXT_PLAIN)
38 public String hello() {
44 @Produces(MediaType.TEXT_PLAIN)
45 public String announce() {
46 _logger.info("Announce request received");
48 BundleContext ctx = getBundleContext();
49 ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class);
50 if (providerRef == null) {
51 _logger.debug("Could not get provider reference");
52 return "Could not get provider reference";
55 ExampleProvider provider = (ExampleProvider) ctx.getService(providerRef);
56 if (provider == null) {
57 _logger.info("Could not get provider service");
58 return "Could not get provider service";
61 provider.announce(QNAME);
62 return "Announcement sent ";
68 @Produces(MediaType.TEXT_PLAIN)
69 public String invokeRpc() throws Exception {
70 _logger.info("Invoking RPC");
72 ExampleConsumer consumer = getConsumer();
73 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, new CompositeNodeImpl());
74 _logger.info("Result [{}]", result.isSuccessful());
76 return stringify(result);
81 @Produces(MediaType.TEXT_PLAIN)
82 public String invokeRpcSuccess() throws Exception {
83 ExampleConsumer consumer = getConsumer();
84 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithFourSimpleChildren()); //TODO: Change this
85 _logger.info("Result [{}]", result.isSuccessful());
87 return stringify(result);
92 @Produces(MediaType.TEXT_PLAIN)
93 public String invokeRpcFailure() throws Exception {
94 ExampleConsumer consumer = getConsumer();
95 //RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getInvalidCompositeNodeCompositeChild()); //TODO: Change this
96 RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, null); //TODO: Change this
97 _logger.info("Result [{}]", result.isSuccessful());
99 return stringify(result);
103 @Path("/routingtable")
104 @Produces(MediaType.TEXT_PLAIN)
105 public String invokeRoutingTable() {
106 _logger.info("Invoking adding an entry in routing table");
108 BundleContext ctx = getBundleContext();
109 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
110 if (routingTableServiceReference == null) {
111 _logger.debug("Could not get routing table impl reference");
112 return "Could not get routingtable referen ";
114 RoutingTable routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
115 if (routingTable == null) {
116 _logger.info("Could not get routing table service");
117 return "Could not get routing table service";
121 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
123 routingTable.addGlobalRoute(rii.toString(), "172.27.12.1:5000");
124 } catch (RoutingTableException e) {
125 _logger.error("error in adding routing identifier" + e.getMessage());
127 } catch (SystemException e) {
128 _logger.error("error in adding routing identifier" + e.getMessage());
131 Set<String> routes = routingTable.getRoutes(rii.toString());
133 StringBuilder stringBuilder = new StringBuilder();
134 for (String route : routes) {
135 stringBuilder.append(route);
138 _logger.info("Result [{}] routes added for route" + rii + stringBuilder.toString());
140 return stringBuilder.toString();
144 @Path("/routingtabledelete")
145 @Produces(MediaType.TEXT_PLAIN)
146 public String invokeDeleteRoutingTable() {
147 _logger.info("Invoking adding an entry in routing table");
149 BundleContext ctx = getBundleContext();
150 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
151 if (routingTableServiceReference == null) {
152 _logger.debug("Could not get routing table impl reference");
153 return "Could not get routingtable referen ";
155 RoutingTable routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
156 if (routingTable == null) {
157 _logger.info("Could not get routing table service");
158 return "Could not get routing table service";
162 RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
164 routingTable.removeGlobalRoute(rii.toString());
165 } catch (RoutingTableException e) {
166 _logger.error("error in adding routing identifier" + e.getMessage());
168 } catch (SystemException e) {
169 _logger.error("error in adding routing identifier" + e.getMessage());
172 Set<String> routes = routingTable.getRoutes(rii.toString());
174 StringBuilder stringBuilder = new StringBuilder();
175 if (routes != null) {
176 for (String route : routes) {
177 stringBuilder.append(route);
180 stringBuilder.append(" successfully");
183 _logger.info("Result [{}] routes removed for route" + rii + stringBuilder.toString());
185 return stringBuilder.toString();
188 private String stringify(RpcResult<CompositeNode> result) {
189 CompositeNode node = result.getResult();
190 StringBuilder builder = new StringBuilder("result:").append(XmlUtils.compositeNodeToXml(node)).append("\n")
191 .append("error:").append(result.getErrors()).append("\n");
193 return builder.toString();
196 private BundleContext getBundleContext() {
197 ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
198 Bundle bundle = null;
200 if (tlcl instanceof BundleReference) {
201 bundle = ((BundleReference) tlcl).getBundle();
203 _logger.info("Unable to determine the bundle context based on " +
204 "thread context classloader.");
205 bundle = FrameworkUtil.getBundle(this.getClass());
207 return (bundle == null ? null : bundle.getBundleContext());
210 private ExampleConsumer getConsumer() {
211 BundleContext ctx = getBundleContext();
212 ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class);
213 if (consumerRef == null) {
214 _logger.debug("Could not get consumer reference");
215 throw new NullPointerException("Could not get consumer reference");
217 ExampleConsumer consumer = (ExampleConsumer) ctx.getService(consumerRef);
218 if (consumer == null) {
219 _logger.info("Could not get consumer service");
220 throw new NullPointerException("Could not get consumer service");
225 class RoutingIdentifierImpl implements RpcRouter.RouteIdentifier, Serializable {
227 private final URI namespace = URI.create("http://cisco.com/example");
228 private final QName QNAME = new QName(namespace, "global");
229 private final QName instance = new QName(URI.create("127.0.0.1"), "local");
232 public QName getContext() {
237 public QName getType() {
242 public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier getRoute() {
243 return InstanceIdentifier.of(instance);