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