1 package org.opendaylight.controller.tests.zmqroutingtable.rest;
3 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
4 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
5 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
6 import org.opendaylight.controller.sal.connector.remoterpc.impl.RoutingTableImpl;
7 import org.opendaylight.yangtools.yang.common.QName;
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleContext;
10 import org.osgi.framework.BundleReference;
11 import org.osgi.framework.FrameworkUtil;
12 import org.osgi.framework.ServiceReference;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.QueryParam;
20 import javax.ws.rs.core.MediaType;
21 import java.io.Serializable;
25 public class Router implements Serializable {
26 private Logger _logger = LoggerFactory.getLogger(Router.class);
27 private final URI namespace = URI.create("http://cisco.com/example");
28 private final QName QNAME = new QName(namespace, "heartbeat");
33 @Produces(MediaType.TEXT_PLAIN)
34 public String hello() {
43 @Produces(MediaType.TEXT_PLAIN)
44 public String addToRoutingTable(@QueryParam("nsp") String namespace,@QueryParam("inst") String instance,@QueryParam("port") String port) {
45 _logger.info("Invoking adding an entry in routing table");
47 BundleContext ctx = getBundleContext();
48 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
49 if (routingTableServiceReference == null) {
50 _logger.debug("Could not get routing table impl reference");
51 return "Could not get routingtable referen ";
53 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
54 if (routingTable == null) {
55 _logger.info("Could not get routing table service");
56 return "Could not get routing table service";
60 RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
62 routingTable.addGlobalRoute(rii, instance+":"+ port);
63 } catch (RoutingTableException e) {
64 _logger.error("error in adding routing identifier" + e.getMessage());
66 } catch (SystemException e) {
67 _logger.error("error in adding routing identifier" + e.getMessage());
70 StringBuilder stringBuilder = new StringBuilder();
71 stringBuilder.append("Result of adding route:").append("\n")
72 .append(routingTable.dumpRoutingTableCache());
73 return stringBuilder.toString();
78 @Produces(MediaType.TEXT_PLAIN)
79 public String invokeDeleteRoutingTable(@QueryParam("nsp") String namespace,@QueryParam("inst") String instance) {
80 _logger.info("Invoking delete an entry in routing table");
82 BundleContext ctx = getBundleContext();
83 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
84 if (routingTableServiceReference == null) {
85 _logger.debug("Could not get routing table impl reference");
86 return "Could not get routingtable referen ";
88 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
89 if (routingTable == null) {
90 _logger.info("Could not get routing table service");
91 return "Could not get routing table service";
95 RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
97 routingTable.removeGlobalRoute(rii);
98 } catch (RoutingTableException e) {
99 _logger.error("error in adding routing identifier" + e.getMessage());
101 } catch (SystemException e) {
102 _logger.error("error in adding routing identifier" + e.getMessage());
106 StringBuilder stringBuilder = new StringBuilder();
107 stringBuilder.append("Result of deleting route:").append("\n")
108 .append(routingTable.dumpRoutingTableCache());
110 return stringBuilder.toString();
114 @Path("/routingtable")
115 @Produces(MediaType.TEXT_PLAIN)
116 public String invokeGetRoutingTable() {
117 _logger.info("Invoking getting of routing table");
119 BundleContext ctx = getBundleContext();
120 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
121 if (routingTableServiceReference == null) {
122 _logger.debug("Could not get routing table impl reference");
123 return "Could not get routingtable referen ";
125 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
126 if (routingTable == null) {
127 _logger.info("Could not get routing table service");
128 return "Could not get routing table service";
132 StringBuilder stringBuilder = new StringBuilder();
133 stringBuilder.append("Result of getting routetable:").append("\n")
134 .append(routingTable.dumpRoutingTableCache());
136 return stringBuilder.toString();
141 private BundleContext getBundleContext() {
142 ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
143 Bundle bundle = null;
145 if (tlcl instanceof BundleReference) {
146 bundle = ((BundleReference) tlcl).getBundle();
148 _logger.info("Unable to determine the bundle context based on " +
149 "thread context classloader.");
150 bundle = FrameworkUtil.getBundle(this.getClass());
152 return (bundle == null ? null : bundle.getBundleContext());