3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.topology.northbound;
12 import java.util.ArrayList;
13 import java.util.List;
16 import java.util.concurrent.ConcurrentMap;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.DELETE;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.POST;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.xml.bind.JAXBElement;
29 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.codehaus.enunciate.jaxrs.TypeHint;
32 import org.opendaylight.controller.northbound.commons.RestMessages;
33 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
34 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
35 import org.opendaylight.controller.sal.core.Edge;
36 import org.opendaylight.controller.sal.core.Property;
37 import org.opendaylight.controller.sal.utils.ServiceHelper;
38 import org.opendaylight.controller.sal.utils.Status;
39 import org.opendaylight.controller.topologymanager.ITopologyManager;
40 import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Topology Northbound REST API
48 * Authentication scheme : <b>HTTP Basic</b><br>
49 * Authentication realm : <b>opendaylight</b><br>
50 * Transport : <b>HTTP and HTTPS</b><br>
52 * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
53 * a proper keystore / SSL certificate from a trusted authority.<br>
54 * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
58 public class TopologyNorthboundJAXRS {
59 private static Logger logger = LoggerFactory
60 .getLogger(TopologyNorthboundJAXRS.class);
64 * Retrieve the Topology
66 * @param containerName The container for which we want to retrieve the topology
68 * @return A List of EdgeProps each EdgeProp represent an Edge of
69 * the grap with the corresponding properties attached to it.
71 @Path("/{containerName}")
73 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
74 @TypeHint(Topology.class)
75 @StatusCodes( { @ResponseCode(code = 404, condition = "The Container Name passed was not found") })
76 public Topology getTopology(
77 @PathParam("containerName") String containerName) {
78 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
79 .getInstance(ITopologyManager.class, containerName, this);
80 if (topologyManager == null) {
81 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
85 Map<Edge, Set<Property>> topo = topologyManager.getEdges();
87 List<EdgeProperties> res = new ArrayList<EdgeProperties>();
88 for (Map.Entry<Edge, Set<Property>> entry : topo.entrySet()) {
89 EdgeProperties el = new EdgeProperties(entry.getKey(), entry.getValue());
92 return new Topology(res);
99 * Retrieve the user configured links
101 * @param containerName The container for which we want to retrieve the user links
103 * @return A List of user configured links
105 @Path("/{containerName}/userLink")
107 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
108 @TypeHint(TopologyUserLinks.class)
109 @StatusCodes( { @ResponseCode(code = 404, condition = "The Container Name passed was not found") })
110 public TopologyUserLinks getUserLinks(
111 @PathParam("containerName") String containerName) {
112 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
113 .getInstance(ITopologyManager.class, containerName, this);
114 if (topologyManager == null) {
115 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
119 ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topologyManager.getUserLinks();
120 if ((userLinks != null) && (userLinks.values() != null)) {
121 List<TopologyUserLinkConfig> res = new ArrayList<TopologyUserLinkConfig>(userLinks.values());
122 return new TopologyUserLinks(res);
131 * @param containerName Name of the Container. The base Container is "default".
132 * @param TopologyUserLinkConfig in JSON or XML format
133 * @return Response as dictated by the HTTP Response Status code
136 @Path("/{containerName}/userLink")
138 @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
140 @ResponseCode(code = 201, condition = "User Link added successfully"),
141 @ResponseCode(code = 404, condition = "The Container Name passed was not found"),
142 @ResponseCode(code = 409, condition = "Failed to add User Link due to Conflicting Name"),
143 @ResponseCode(code = 500, condition = "Failed to add User Link. Failure Reason included in HTTP Error response"),
144 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
145 public Response addUserLink(
146 @PathParam(value = "containerName") String containerName,
147 @TypeHint(TopologyUserLinkConfig.class) JAXBElement<TopologyUserLinkConfig> userLinkConfig) {
149 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
150 .getInstance(ITopologyManager.class, containerName, this);
151 if (topologyManager == null) {
152 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
156 Status status = topologyManager.addUserLink(userLinkConfig.getValue());
157 if (status.isSuccess()) {
158 return Response.status(Response.Status.CREATED).build();
160 throw new InternalServerErrorException(status.getDescription());
164 * Delete an User Link
166 * @param containerName Name of the Container. The base Container is "default".
167 * @param name Name of the Link Configuration
168 * @return Response as dictated by the HTTP Response Status code
171 @Path("/{containerName}/userLink/{name}")
173 @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
175 @ResponseCode(code = 200, condition = "Operation successful"),
176 @ResponseCode(code = 404, condition = "The Container Name or Link Configuration Name was not found"),
177 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
178 public Response deleteUserLink(
179 @PathParam("containerName") String containerName,
180 @PathParam("name") String name) {
182 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
183 .getInstance(ITopologyManager.class, containerName, this);
184 if (topologyManager == null) {
185 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
189 Status ret = topologyManager.deleteUserLink(name);
190 if (ret.isSuccess()) {
191 return Response.ok().build();
193 throw new ResourceNotFoundException(ret.getDescription());