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