OpenDaylight Controller functional modules.
[controller.git] / opendaylight / northbound / topology / src / main / java / org / opendaylight / controller / topology / northbound / TopologyNorthboundJAXRS.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.topology.northbound;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import javax.ws.rs.GET;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.PathParam;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.core.MediaType;
22
23 import org.codehaus.enunciate.jaxrs.ResponseCode;
24 import org.codehaus.enunciate.jaxrs.StatusCodes;
25 import org.codehaus.enunciate.jaxrs.TypeHint;
26 import org.opendaylight.controller.northbound.commons.RestMessages;
27 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
28 import org.opendaylight.controller.sal.core.Edge;
29 import org.opendaylight.controller.sal.core.Property;
30 import org.opendaylight.controller.sal.utils.ServiceHelper;
31 import org.opendaylight.controller.topologymanager.ITopologyManager;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Topology Northbound REST API
37  * 
38  * <br><br>
39  * Authentication scheme : <b>HTTP Basic</b><br>
40  * Authentication realm : <b>opendaylight</b><br>
41  * Transport : <b>HTTP and HTTPS</b><br>
42  * <br>
43  * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding 
44  * a proper keystore / SSL certificate from a trusted authority.<br>
45  * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
46  */
47
48 @Path("/")
49 public class TopologyNorthboundJAXRS {
50     private static Logger logger = LoggerFactory
51             .getLogger(TopologyNorthboundJAXRS.class);
52
53     /**
54      *
55      * Retrieve the Topology for the
56      *
57      * @param containerName The container for which we want to retrieve the topology
58      *
59      * @return A List of EdgeProps each EdgeProp represent an Edge of
60      * the grap with the corresponding properties attached to it.
61      */
62     @Path("/{containerName}")
63     @GET
64     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
65     @TypeHint(Topology.class)
66     @StatusCodes( { @ResponseCode(code = 404, condition = "The containerName passed was not found") })
67     public Topology getTopology(
68             @PathParam("containerName") String containerName) {
69         ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
70                 .getInstance(ITopologyManager.class, containerName, this);
71         if (topologyManager == null) {
72             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
73                     .toString());
74         }
75
76         Map<Edge, Set<Property>> topo = topologyManager.getEdges();
77         if (topo != null) {
78             List<EdgeProperties> res = new ArrayList<EdgeProperties>();
79             for (Map.Entry<Edge, Set<Property>> entry : topo.entrySet()) {
80                 EdgeProperties el = new EdgeProperties(entry.getKey(), entry.getValue());
81                 res.add(el);
82             }
83             return new Topology(res);
84         }
85
86         return null;
87     }
88 }