Enhancements to remote rpc server. Using zmq router-dealer bridge to make the server...
[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.impl.RoutingTableImpl;
8 import org.opendaylight.controller.sal.connector.remoterpc.util.XmlUtils;
9 import org.opendaylight.controller.sample.zeromq.consumer.ExampleConsumer;
10 import org.opendaylight.controller.sample.zeromq.provider.ExampleProvider;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.common.RpcResult;
13 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.osgi.framework.*;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.ws.rs.GET;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType;
23 import java.io.Serializable;
24 import java.net.URI;
25 import java.util.Set;
26
27 @Path("router")
28 public class Router {
29   private Logger _logger = LoggerFactory.getLogger(Router.class);
30   private final URI namespace = URI.create("http://cisco.com/example");
31   private final QName QNAME = new QName(namespace, "heartbeat");
32
33
34   @GET
35   @Path("/hello")
36   @Produces(MediaType.TEXT_PLAIN)
37   public String hello() {
38     return "Hello";
39   }
40
41   @GET
42   @Path("/announce")
43   @Produces(MediaType.TEXT_PLAIN)
44   public String announce() {
45     _logger.info("Announce request received");
46
47     BundleContext ctx = getBundleContext();
48     ServiceReference providerRef = ctx.getServiceReference(ExampleProvider.class);
49     if (providerRef == null) {
50       _logger.debug("Could not get provider reference");
51       return "Could not get provider reference";
52     }
53
54     ExampleProvider provider = (ExampleProvider) ctx.getService(providerRef);
55     if (provider == null) {
56       _logger.info("Could not get provider service");
57       return "Could not get provider service";
58     }
59
60     provider.announce(QNAME);
61     return "Announcement sent ";
62
63   }
64
65   @GET
66   @Path("/rpc")
67   @Produces(MediaType.TEXT_PLAIN)
68   public String invokeRpc() throws Exception {
69     _logger.info("Invoking RPC");
70
71     ExampleConsumer consumer = getConsumer();
72     RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithOneSimpleChild());
73     _logger.info("Result [{}]", result.isSuccessful());
74
75     return stringify(result);
76   }
77
78   @GET
79   @Path("/rpc-success")
80   @Produces(MediaType.TEXT_PLAIN)
81   public String invokeRpcSuccess() throws Exception {
82     ExampleConsumer consumer = getConsumer();
83     RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getValidCompositeNodeWithFourSimpleChildren()); //TODO: Change this
84     _logger.info("Result [{}]", result.isSuccessful());
85
86     return stringify(result);
87   }
88
89   @GET
90   @Path("/rpc-failure")
91   @Produces(MediaType.TEXT_PLAIN)
92   public String invokeRpcFailure() throws Exception {
93     ExampleConsumer consumer = getConsumer();
94     //RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, consumer.getInvalidCompositeNodeCompositeChild()); //TODO: Change this
95     RpcResult<CompositeNode> result = consumer.invokeRpc(QNAME, null); //TODO: Change this
96     _logger.info("Result [{}]", result.isSuccessful());
97
98     return stringify(result);
99   }
100
101   @GET
102   @Path("/routingtable")
103   @Produces(MediaType.TEXT_PLAIN)
104   public String invokeRoutingTable() {
105     _logger.info("Invoking adding an entry in routing table");
106
107     BundleContext ctx = getBundleContext();
108     ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
109     if (routingTableServiceReference == null) {
110       _logger.debug("Could not get routing table impl reference");
111       return "Could not get routingtable referen ";
112     }
113     RoutingTableImpl routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
114     if (routingTable == null) {
115       _logger.info("Could not get routing table service");
116       return "Could not get routing table service";
117     }
118
119
120     RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
121     try {
122       routingTable.addGlobalRoute(rii.toString(), "172.27.12.1:5000");
123     } catch (RoutingTableException e) {
124       _logger.error("error in adding routing identifier" + e.getMessage());
125
126     } catch (SystemException e) {
127       _logger.error("error in adding routing identifier" + e.getMessage());
128     }
129
130     String result = routingTable.dumpRoutingTableCache();
131
132
133
134
135     _logger.info("Result [{}] routes added for route" + rii + result);
136
137     return result;
138   }
139
140   @GET
141   @Path("/routingtabledelete")
142   @Produces(MediaType.TEXT_PLAIN)
143   public String invokeDeleteRoutingTable() {
144     _logger.info("Invoking adding an entry in routing table");
145
146     BundleContext ctx = getBundleContext();
147     ServiceReference routingTableServiceReference = ctx.getServiceReference(RoutingTable.class);
148     if (routingTableServiceReference == null) {
149       _logger.debug("Could not get routing table impl reference");
150       return "Could not get routingtable referen ";
151     }
152     RoutingTable routingTable = (RoutingTableImpl) ctx.getService(routingTableServiceReference);
153     if (routingTable == null) {
154       _logger.info("Could not get routing table service");
155       return "Could not get routing table service";
156     }
157
158
159     RoutingIdentifierImpl rii = new RoutingIdentifierImpl();
160     try {
161       routingTable.removeGlobalRoute(rii.toString());
162     } catch (RoutingTableException e) {
163       _logger.error("error in adding routing identifier" + e.getMessage());
164
165     } catch (SystemException e) {
166       _logger.error("error in adding routing identifier" + e.getMessage());
167     }
168
169     Set<String> routes = routingTable.getRoutes(rii.toString());
170
171     StringBuilder stringBuilder = new StringBuilder();
172     if (routes != null) {
173       for (String route : routes) {
174         stringBuilder.append(route);
175       }
176     } else {
177       stringBuilder.append(" successfully");
178     }
179
180     _logger.info("Result [{}] routes removed for route" + rii + stringBuilder.toString());
181
182     return stringBuilder.toString();
183   }
184
185   private String stringify(RpcResult<CompositeNode> result) {
186     CompositeNode node = result.getResult();
187     StringBuilder builder = new StringBuilder("result:").append(XmlUtils.compositeNodeToXml(node)).append("\n")
188         .append("error:").append(result.getErrors()).append("\n");
189
190     return builder.toString();
191   }
192
193   private BundleContext getBundleContext() {
194     ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
195     Bundle bundle = null;
196
197     if (tlcl instanceof BundleReference) {
198       bundle = ((BundleReference) tlcl).getBundle();
199     } else {
200       _logger.info("Unable to determine the bundle context based on " +
201           "thread context classloader.");
202       bundle = FrameworkUtil.getBundle(this.getClass());
203     }
204     return (bundle == null ? null : bundle.getBundleContext());
205   }
206
207   private ExampleConsumer getConsumer() {
208     BundleContext ctx = getBundleContext();
209     ServiceReference consumerRef = ctx.getServiceReference(ExampleConsumer.class);
210     if (consumerRef == null) {
211       _logger.debug("Could not get consumer reference");
212       throw new NullPointerException("Could not get consumer reference");
213     }
214     ExampleConsumer consumer = (ExampleConsumer) ctx.getService(consumerRef);
215     if (consumer == null) {
216       _logger.info("Could not get consumer service");
217       throw new NullPointerException("Could not get consumer service");
218     }
219     return consumer;
220   }
221
222   class RoutingIdentifierImpl implements RpcRouter.RouteIdentifier, Serializable {
223
224     private final URI namespace = URI.create("http://cisco.com/example");
225     private final QName QNAME = new QName(namespace, "global");
226     private final QName instance = new QName(URI.create("127.0.0.1"), "local");
227
228     @Override
229     public QName getContext() {
230       return QNAME;
231     }
232
233     @Override
234     public QName getType() {
235       return QNAME;
236     }
237
238     @Override
239     public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier getRoute() {
240       return InstanceIdentifier.of(instance);
241     }
242
243       @Override
244       public boolean equals(Object o) {
245           if (this == o) return true;
246           if (o == null || getClass() != o.getClass()) return false;
247
248           RoutingIdentifierImpl that = (RoutingIdentifierImpl) o;
249
250           if (!QNAME.equals(that.QNAME)) return false;
251           if (!instance.equals(that.instance)) return false;
252           if (!namespace.equals(that.namespace)) return false;
253
254           return true;
255       }
256
257       @Override
258       public int hashCode() {
259           int result = namespace.hashCode();
260           result = 31 * result + QNAME.hashCode();
261           result = 31 * result + instance.hashCode();
262           return result;
263       }
264   }
265 }