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.zmqroutingtable.rest;
10 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
11 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
12 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
13 import org.opendaylight.controller.sal.connector.remoterpc.impl.RoutingTableImpl;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.osgi.framework.Bundle;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.BundleReference;
18 import org.osgi.framework.FrameworkUtil;
19 import org.osgi.framework.ServiceReference;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import javax.ws.rs.GET;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.QueryParam;
27 import javax.ws.rs.core.MediaType;
28 import java.io.Serializable;
32 public class Router implements Serializable {
33 private Logger _logger = LoggerFactory.getLogger(Router.class);
34 private final URI namespace = URI.create("http://cisco.com/example");
35 private final QName QNAME = new QName(namespace, "heartbeat");
40 @Produces(MediaType.TEXT_PLAIN)
41 public String hello() {
50 @Produces(MediaType.TEXT_PLAIN)
51 public String addToRoutingTable(@QueryParam("nsp") String namespace,@QueryParam("inst") String instance,@QueryParam("port") String port) {
52 _logger.info("Invoking adding an entry in routing table");
54 BundleContext ctx = getBundleContext();
55 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
56 if (routingTableServiceReference == null) {
57 _logger.debug("Could not get routing table impl reference");
58 return "Could not get routingtable referen ";
60 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
61 if (routingTable == null) {
62 _logger.info("Could not get routing table service");
63 return "Could not get routing table service";
67 RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
69 routingTable.addGlobalRoute(rii, instance+":"+ port);
70 } catch (RoutingTableException e) {
71 _logger.error("error in adding routing identifier" + e.getMessage());
73 } catch (SystemException e) {
74 _logger.error("error in adding routing identifier" + e.getMessage());
77 StringBuilder stringBuilder = new StringBuilder();
78 stringBuilder.append("Result of adding route:").append("\n")
79 .append(routingTable.dumpRoutingTableCache());
80 return stringBuilder.toString();
85 @Produces(MediaType.TEXT_PLAIN)
86 public String invokeDeleteRoutingTable(@QueryParam("nsp") String namespace,@QueryParam("inst") String instance) {
87 _logger.info("Invoking delete an entry in routing table");
89 BundleContext ctx = getBundleContext();
90 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
91 if (routingTableServiceReference == null) {
92 _logger.debug("Could not get routing table impl reference");
93 return "Could not get routingtable referen ";
95 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
96 if (routingTable == null) {
97 _logger.info("Could not get routing table service");
98 return "Could not get routing table service";
102 RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
104 routingTable.removeGlobalRoute(rii);
105 } catch (RoutingTableException e) {
106 _logger.error("error in adding routing identifier" + e.getMessage());
108 } catch (SystemException e) {
109 _logger.error("error in adding routing identifier" + e.getMessage());
113 StringBuilder stringBuilder = new StringBuilder();
114 stringBuilder.append("Result of deleting route:").append("\n")
115 .append(routingTable.dumpRoutingTableCache());
117 return stringBuilder.toString();
121 @Path("/routingtable")
122 @Produces(MediaType.TEXT_PLAIN)
123 public String invokeGetRoutingTable() {
124 _logger.info("Invoking getting of routing table");
126 BundleContext ctx = getBundleContext();
127 ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
128 if (routingTableServiceReference == null) {
129 _logger.debug("Could not get routing table impl reference");
130 return "Could not get routingtable referen ";
132 RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
133 if (routingTable == null) {
134 _logger.info("Could not get routing table service");
135 return "Could not get routing table service";
139 StringBuilder stringBuilder = new StringBuilder();
140 stringBuilder.append("Result of getting routetable:").append("\n")
141 .append(routingTable.dumpRoutingTableCache());
143 return stringBuilder.toString();
148 private BundleContext getBundleContext() {
149 ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
150 Bundle bundle = null;
152 if (tlcl instanceof BundleReference) {
153 bundle = ((BundleReference) tlcl).getBundle();
155 _logger.info("Unable to determine the bundle context based on " +
156 "thread context classloader.");
157 bundle = FrameworkUtil.getBundle(this.getClass());
159 return (bundle == null ? null : bundle.getBundleContext());