6c9ec4e788827a6c375d4da4964578622a9ea47b
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / integrationtest / test-nb / src / main / java / org / opendaylight / controller / tests / zmqrouter / rest / Router.java
1 package org.opendaylight.controller.tests.zmqrouter.rest;
2
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;
19
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;
25 import java.net.URI;
26 import java.util.Set;
27
28 @Path("router")
29 public class Router {
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");
33
34
35   @GET
36   @Path("/hello")
37   @Produces(MediaType.TEXT_PLAIN)
38   public String hello() {
39     return "Hello";
40   }
41
42   @GET
43   @Path("/announce")
44   @Produces(MediaType.TEXT_PLAIN)
45   public String announce() {
46     _logger.info("Announce request received");
47
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";
53     }
54
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";
59     }
60
61     provider.announce(QNAME);
62     return "Announcement sent ";
63
64   }
65
66   @GET
67   @Path("/rpc")
68   @Produces(MediaType.TEXT_PLAIN)
69   public String invokeRpc() throws Exception {
70     _logger.info("Invoking RPC");
71
72     ExampleConsumer consumer = getConsumer();
73     RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, new CompositeNodeImpl());
74     _logger.info("Result [{}]", result.isSuccessful());
75
76     return stringify(result);
77   }
78
79   @GET
80   @Path("/rpc-success")
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());
86
87     return stringify(result);
88   }
89
90   @GET
91   @Path("/rpc-failure")
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());
98
99     return stringify(result);
100   }
101
102   @GET
103   @Path("/routingtable")
104   @Produces(MediaType.TEXT_PLAIN)
105   public String invokeRoutingTable() {
106     _logger.info("Invoking adding an entry in routing table");
107
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 ";
113     }
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";
118     }
119
120
121     RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
122     try {
123       routingTable.addGlobalRoute(rii.toString(), "172.27.12.1:5000");
124     } catch (RoutingTableException e) {
125       _logger.error("error in adding routing identifier" + e.getMessage());
126
127     } catch (SystemException e) {
128       _logger.error("error in adding routing identifier" + e.getMessage());
129     }
130
131     Set<String> routes = routingTable.getRoutes(rii.toString());
132
133     StringBuilder stringBuilder = new StringBuilder();
134     for (String route : routes) {
135       stringBuilder.append(route);
136     }
137
138     _logger.info("Result [{}] routes added for route" + rii + stringBuilder.toString());
139
140     return stringBuilder.toString();
141   }
142
143   @GET
144   @Path("/routingtabledelete")
145   @Produces(MediaType.TEXT_PLAIN)
146   public String invokeDeleteRoutingTable() {
147     _logger.info("Invoking adding an entry in routing table");
148
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 ";
154     }
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";
159     }
160
161
162     RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
163     try {
164       routingTable.removeGlobalRoute(rii.toString());
165     } catch (RoutingTableException e) {
166       _logger.error("error in adding routing identifier" + e.getMessage());
167
168     } catch (SystemException e) {
169       _logger.error("error in adding routing identifier" + e.getMessage());
170     }
171
172     Set<String> routes = routingTable.getRoutes(rii.toString());
173
174     StringBuilder stringBuilder = new StringBuilder();
175     if (routes != null) {
176       for (String route : routes) {
177         stringBuilder.append(route);
178       }
179     } else {
180       stringBuilder.append(" successfully");
181     }
182
183     _logger.info("Result [{}] routes removed for route" + rii + stringBuilder.toString());
184
185     return stringBuilder.toString();
186   }
187
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");
192
193     return builder.toString();
194   }
195
196   private BundleContext getBundleContext() {
197     ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
198     Bundle bundle = null;
199
200     if (tlcl instanceof BundleReference) {
201       bundle = ((BundleReference) tlcl).getBundle();
202     } else {
203       _logger.info("Unable to determine the bundle context based on " +
204           "thread context classloader.");
205       bundle = FrameworkUtil.getBundle(this.getClass());
206     }
207     return (bundle == null ? null : bundle.getBundleContext());
208   }
209
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");
216     }
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");
221     }
222     return consumer;
223   }
224
225   class RoutingIdentifierImpl implements RpcRouter.RouteIdentifier, Serializable {
226
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");
230
231     @Override
232     public QName getContext() {
233       return QNAME;
234     }
235
236     @Override
237     public QName getType() {
238       return QNAME;
239     }
240
241     @Override
242     public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier getRoute() {
243       return InstanceIdentifier.of(instance);
244     }
245   }
246 }