Bulk-add copyright headers to java files
[controller.git] / opendaylight / md-sal / remoterpc-routingtable / integrationtest / test-nb / src / main / java / org / opendaylight / controller / tests / zmqroutingtable / rest / Router.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.tests.zmqroutingtable.rest;
9
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;
22
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;
29 import java.net.URI;
30
31 @Path("router")
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");
36
37
38   @GET
39   @Path("/hello")
40   @Produces(MediaType.TEXT_PLAIN)
41   public String hello() {
42     return "Hello";
43   }
44
45
46
47
48     @GET
49     @Path("/rtadd")
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");
53
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 ";
59         }
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";
64         }
65
66
67         RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
68         try {
69             routingTable.addGlobalRoute(rii, instance+":"+ port);
70         } catch (RoutingTableException e) {
71             _logger.error("error in adding routing identifier" + e.getMessage());
72
73         } catch (SystemException e) {
74             _logger.error("error in adding routing identifier" + e.getMessage());
75         }
76
77         StringBuilder stringBuilder = new StringBuilder();
78         stringBuilder.append("Result of adding route:").append("\n")
79                      .append(routingTable.dumpRoutingTableCache());
80         return stringBuilder.toString();
81     }
82
83   @GET
84   @Path("/rtdelete")
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");
88
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 ";
94     }
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";
99     }
100
101
102     RouteIdentifierImpl rii = new RouteIdentifierImpl(namespace,instance);
103     try {
104       routingTable.removeGlobalRoute(rii);
105     } catch (RoutingTableException e) {
106       _logger.error("error in adding routing identifier" + e.getMessage());
107
108     } catch (SystemException e) {
109       _logger.error("error in adding routing identifier" + e.getMessage());
110     }
111
112
113     StringBuilder stringBuilder = new StringBuilder();
114     stringBuilder.append("Result of deleting route:").append("\n")
115               .append(routingTable.dumpRoutingTableCache());
116
117     return stringBuilder.toString();
118   }
119
120     @GET
121     @Path("/routingtable")
122     @Produces(MediaType.TEXT_PLAIN)
123     public String invokeGetRoutingTable() {
124         _logger.info("Invoking getting of routing table");
125
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 ";
131         }
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";
136         }
137
138
139         StringBuilder stringBuilder = new StringBuilder();
140         stringBuilder.append("Result of getting routetable:").append("\n")
141                 .append(routingTable.dumpRoutingTableCache());
142
143         return stringBuilder.toString();
144     }
145
146
147
148   private BundleContext getBundleContext() {
149     ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
150     Bundle bundle = null;
151
152     if (tlcl instanceof BundleReference) {
153       bundle = ((BundleReference) tlcl).getBundle();
154     } else {
155       _logger.info("Unable to determine the bundle context based on " +
156           "thread context classloader.");
157       bundle = FrameworkUtil.getBundle(this.getClass());
158     }
159     return (bundle == null ? null : bundle.getBundleContext());
160   }
161
162
163
164 }