Merge "Simplify method isMutualExclusive in Subnet. Remove redundant 'if' statements."
[controller.git] / opendaylight / md-sal / remoterpc-routingtable / integrationtest / test-nb / src / main / java / org / opendaylight / controller / tests / zmqroutingtable / rest / Router.java
1 package org.opendaylight.controller.tests.zmqroutingtable.rest;
2
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;
15
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;
22 import java.net.URI;
23
24 @Path("router")
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");
29
30
31   @GET
32   @Path("/hello")
33   @Produces(MediaType.TEXT_PLAIN)
34   public String hello() {
35     return "Hello";
36   }
37
38
39
40
41     @GET
42     @Path("/rtadd")
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");
46
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 ";
52         }
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";
57         }
58
59
60         RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
61         try {
62             routingTable.addGlobalRoute(rii, instance+":"+ port);
63         } catch (RoutingTableException e) {
64             _logger.error("error in adding routing identifier" + e.getMessage());
65
66         } catch (SystemException e) {
67             _logger.error("error in adding routing identifier" + e.getMessage());
68         }
69
70         StringBuilder stringBuilder = new StringBuilder();
71         stringBuilder.append("Result of adding route:").append("\n")
72                      .append(routingTable.dumpRoutingTableCache());
73         return stringBuilder.toString();
74     }
75
76   @GET
77   @Path("/rtdelete")
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");
81
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 ";
87     }
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";
92     }
93
94
95     RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
96     try {
97       routingTable.removeGlobalRoute(rii);
98     } catch (RoutingTableException e) {
99       _logger.error("error in adding routing identifier" + e.getMessage());
100
101     } catch (SystemException e) {
102       _logger.error("error in adding routing identifier" + e.getMessage());
103     }
104
105
106     StringBuilder stringBuilder = new StringBuilder();
107     stringBuilder.append("Result of deleting route:").append("\n")
108               .append(routingTable.dumpRoutingTableCache());
109
110     return stringBuilder.toString();
111   }
112
113     @GET
114     @Path("/routingtable")
115     @Produces(MediaType.TEXT_PLAIN)
116     public String invokeGetRoutingTable() {
117         _logger.info("Invoking getting of routing table");
118
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 ";
124         }
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";
129         }
130
131
132         StringBuilder stringBuilder = new StringBuilder();
133         stringBuilder.append("Result of getting routetable:").append("\n")
134                 .append(routingTable.dumpRoutingTableCache());
135
136         return stringBuilder.toString();
137     }
138
139
140
141   private BundleContext getBundleContext() {
142     ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
143     Bundle bundle = null;
144
145     if (tlcl instanceof BundleReference) {
146       bundle = ((BundleReference) tlcl).getBundle();
147     } else {
148       _logger.info("Unable to determine the bundle context based on " +
149           "thread context classloader.");
150       bundle = FrameworkUtil.getBundle(this.getClass());
151     }
152     return (bundle == null ? null : bundle.getBundleContext());
153   }
154
155
156
157 }