2 * Copyright (c) 2014 SDN Hub. All rights reserved.
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
12 package org.sdnhub.dnsguard.northbound;
14 import java.util.List;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.Context;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.SecurityContext;
26 import javax.ws.rs.core.UriInfo;
28 import org.codehaus.enunciate.jaxrs.StatusCodes;
29 import org.codehaus.enunciate.jaxrs.TypeHint;
30 import org.opendaylight.controller.northbound.commons.RestMessages;
31 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
32 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
33 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
34 import org.opendaylight.controller.sal.authorization.Privilege;
35 import org.opendaylight.controller.sal.utils.ServiceHelper;
36 import org.sdnhub.dnsguard.DnsReply;
37 import org.sdnhub.dnsguard.IDnsGuard;
38 import org.sdnhub.dnsguard.renders.D3pieData;
39 import org.sdnhub.dnsguard.renders.DataTableObject;
40 import org.sdnhub.dnsguard.renders.DnsRecordReply;
41 import org.sdnhub.dnsguard.renders.DnsUsage;
42 import org.sdnhub.dnsguard.renders.Violator;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.gson.Gson;
47 import com.google.gson.GsonBuilder;
52 * This entire web class can be accessed via /northbound prefix as specified in
57 * Authentication scheme : <b>HTTP Basic</b><br>
58 * Authentication realm : <b>opendaylight</b><br>
59 * Transport : <b>HTTP and HTTPS</b><br>
61 * HTTPS Authentication is disabled by default.
64 public class AppNorthbound {
66 protected static final Logger log = LoggerFactory.getLogger(AppNorthbound.class);
69 private UriInfo _uriInfo;
70 private String username;
73 public void setSecurityContext(SecurityContext context) {
74 if (context != null && context.getUserPrincipal() != null) {
75 username = context.getUserPrincipal().getName();
79 protected String getUserName() {
85 * Sample REST API call
87 * @return A response string
93 * http://127.0.0.1:8080/dnsguard/northbound/test
95 * Response body in XML:
96 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
97 * Sample Northbound API
99 * Response body in JSON:
100 * Sample Northbound API
105 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
106 @TypeHint(String.class)
108 public String getTest() {
109 String result = "<xml><output>Sample Northbound API from module DnsGuard</output></xml>";
115 * Sample REST API call
117 * @return A response string
123 * http://127.0.0.1:8080/dnsguard/northbound/echo/{echo}
125 * Response body in XML:
126 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
127 * Sample Northbound API
129 * Response body in JSON:
130 * Sample Northbound API
133 @Path("/echo/{echo}")
135 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
136 @TypeHint(String.class)
138 public String getEcho(@PathParam("echo") String echo) {
140 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
141 throw new UnauthorizedException("User is not authorized to perform this operation");
144 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
146 if (dnshandler == null) {
147 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
150 String result = "<xml><output>Query from module DnsGuard " + dnshandler.echo(echo) + " </output></xml>";
158 * Sample REST API call
160 * @return A response string
166 * http://127.0.0.1:8080/dnsguard/northbound/resolv
168 * Response body in XML:
169 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
170 * Sample Northbound API
172 * Response body in JSON:
173 * Sample Northbound API
176 @Path("/resolv/{IpFrom}/{AppIp}")
178 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
179 @TypeHint(String.class)
181 public String getQuery(@PathParam("IpFrom") String ipFrom, @PathParam("AppIp") String appIP) {
183 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
184 throw new UnauthorizedException("User is not authorized to perform this operation");
187 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
189 if (dnshandler == null) {
190 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
193 String result = "<xml><output>ipFrom: " + dnshandler.echo(ipFrom) + " appIP: " + dnshandler.echo(appIP) + " </output></xml>";
200 * Sample REST API call
202 * @return A response string
208 * http://127.0.0.1:8080/dnsguard/northbound/lazyresolv/appIp
210 * Response body in XML:
211 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
212 * Sample Northbound API
214 * Response body in JSON:
215 * Sample Northbound API
218 @Path("/lazyresolv/{appIp}")
220 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
221 @TypeHint(String.class)
223 public String lazyresolv(@PathParam("appIp") String appIp) {
225 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
226 throw new UnauthorizedException("User is not authorized to perform this operation");
229 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
231 if (dnshandler == null) {
232 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
236 String json = new Gson().toJson( dnshandler.lazyresolv(appIp) );
243 * @param http://127.0.0.1:8080/dnsguard/northbound/appsbyip/{sourceIp}
244 * @return The domains visited by an internal IP
246 @Path("/appsbyip/{sourceIp}")
248 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
249 @TypeHint(String.class)
251 public String appsbyip(@PathParam("sourceIp") String sourceIp) {
253 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
254 throw new UnauthorizedException("User is not authorized to perform this operation");
257 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
259 if (dnshandler == null) {
260 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
263 String json = new Gson().toJson( dnshandler.appsbyip(sourceIp) );
270 * Returns the violators of the local dns server
272 * @return A response string
278 * http://127.0.0.1:8080/dnsguard/northbound/getviolators
280 * Response body in XML:
281 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
282 * Sample Northbound API
284 * Response body in JSON:
285 * Sample Northbound API
288 @Path("/getviolators")
290 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
291 @TypeHint(String.class)
293 public String getViolators() {
295 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
296 throw new UnauthorizedException("User is not authorized to perform this operation");
299 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
301 if (dnshandler == null) {
302 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
305 List<Violator> violators = dnshandler.getViolators();
307 DataTableObject dataTableObject = new DataTableObject();
308 dataTableObject.setAaData(violators);
310 Gson gson = new GsonBuilder().setPrettyPrinting().create();
311 String json = gson.toJson( dataTableObject );
319 * Returns the violators of the local dns server
321 * @return A response string
327 * http://127.0.0.1:8080/dnsguard/northbound/getrecords
329 * Response body in XML:
330 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
331 * Sample Northbound API
333 * Response body in JSON:
334 * Sample Northbound API
339 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
340 @TypeHint(String.class)
342 public String getRecords() {
344 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
345 throw new UnauthorizedException("User is not authorized to perform this operation");
348 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
350 if (dnshandler == null) {
351 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
354 List<DnsRecordReply> records = dnshandler.getDatabaseDnsRecords(100, 0);
356 DataTableObject dataTableObject = new DataTableObject();
357 dataTableObject.setAaData(records);
359 Gson gson = new GsonBuilder().setPrettyPrinting().create();
360 String json = gson.toJson( dataTableObject );
369 * @return A response string
375 * http://127.0.0.1:8080/dnsguard/northbound/getrecords
377 * Response body in XML:
378 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
379 * Sample Northbound API
381 * Response body in JSON:
382 * Sample Northbound API
385 @Path("/findrecords/")
387 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
388 @TypeHint(String.class)
390 public String findRecords(@FormParam("draw") String draw, @FormParam("start") String start, @FormParam("length") String length, @FormParam("search[value]") String search) {
392 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
393 throw new UnauthorizedException("User is not authorized to perform this operation");
396 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
398 if (dnshandler == null) {
399 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
402 log.info("draw " + draw + " start " + start + " length " + length + " search " + search);
404 //TODO: LC, change behaivor
405 List<DnsRecordReply> records = dnshandler.getDatabaseDnsRecords(100, 0);
407 DataTableObject dataTableObject = new DataTableObject();
408 dataTableObject.setAaData(records);
409 dataTableObject.setiDraw(1);
411 Gson gson = new GsonBuilder().setPrettyPrinting().create();
412 String json = gson.toJson( dataTableObject );
420 * Returns the violators of the local dns server
422 * @return A response string
428 * http://localhost:8080/dnsguard/northbound/getTopExternalDnsUsage
430 * Response body in XML:
431 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
432 * Sample Northbound API
434 * Response body in JSON:
435 * Sample Northbound API
438 @Path("/getTopExternalDnsUsage")
440 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
441 @TypeHint(String.class)
443 public String getTopExternalDnsUsage() {
445 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
446 throw new UnauthorizedException("User is not authorized to perform this operation");
449 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
451 if (dnshandler == null) {
452 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
455 List<DnsUsage> topdns = dnshandler.getExternalDnsUsage(5);
457 D3pieData pieDataObject = new D3pieData();
458 pieDataObject.setContent(topdns);
460 Gson gson = new GsonBuilder().setPrettyPrinting().create();
461 String json = gson.toJson( pieDataObject );
468 * @param http://127.0.0.1:8080/dnsguard/northbound/setlocaldns/{localdns}
469 * @return The domains visited by an internal IP
471 @Path("/setlocaldns/{localdns}")
473 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
474 @TypeHint(String.class)
476 public String setLocalDnsServer(@PathParam("localdns") String local_dns) {
478 // TODO Auto-generated method stub
479 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
480 throw new UnauthorizedException("User is not authorized to perform this operation");
483 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
485 if (dnshandler == null) {
486 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
490 String json = new Gson().toJson( dnshandler.setLocalDnsServer(local_dns) );
497 * @param http://127.0.0.1:8080/dnsguard/northbound/getlocaldns
498 * @return The domains visited by an internal IP
500 @Path("/getlocaldns")
502 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON })
503 @TypeHint(String.class)
505 public String getLocalDnsServer() {
507 // TODO Auto-generated method stub
508 if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.READ, this)) {
509 throw new UnauthorizedException("User is not authorized to perform this operation");
512 IDnsGuard dnshandler = (IDnsGuard) ServiceHelper.getInstance(IDnsGuard.class, "default", this);
514 if (dnshandler == null) {
515 throw new ServiceUnavailableException("DnsHandler Service " + RestMessages.SERVICEUNAVAILABLE.toString());
519 String json = new Gson().toJson( dnshandler.getLocalDnsServer() );
527 @Path("/updateconfig")
528 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
529 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
530 public String updateconfig(@FormParam("dbserver") String dbserver, @FormParam("dbport") String dbport, @FormParam("dbname") String dbname, @FormParam("dbuser") String dbuser, @FormParam("dbpasswd") String dbpasswd, @FormParam("ib_size") String ib_size, @FormParam("ib_max") String ib_max) throws Exception {
532 String result = "<xml><output>Query from module DnsGuard </output></xml>";