2 * Copyright (c) 2013 Cisco Systems, Inc. and others. 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
9 package org.opendaylight.controller.topology.northbound;
11 import java.util.ArrayList;
12 import java.util.List;
15 import java.util.concurrent.ConcurrentMap;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.SecurityContext;
28 import javax.xml.bind.JAXBElement;
30 import org.codehaus.enunciate.jaxrs.ResponseCode;
31 import org.codehaus.enunciate.jaxrs.StatusCodes;
32 import org.codehaus.enunciate.jaxrs.TypeHint;
33 import org.opendaylight.controller.northbound.commons.RestMessages;
34 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
35 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
36 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
37 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
38 import org.opendaylight.controller.sal.authorization.Privilege;
39 import org.opendaylight.controller.sal.core.Edge;
40 import org.opendaylight.controller.sal.core.Property;
41 import org.opendaylight.controller.sal.utils.ServiceHelper;
42 import org.opendaylight.controller.sal.utils.Status;
43 import org.opendaylight.controller.topologymanager.ITopologyManager;
44 import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
47 * Topology Northbound REST API
51 * Authentication scheme : <b>HTTP Basic</b><br>
52 * Authentication realm : <b>opendaylight</b><br>
53 * Transport : <b>HTTP and HTTPS</b><br>
55 * HTTPS Authentication is disabled by default. Administrator can enable it in
56 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
57 * trusted authority.<br>
59 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
63 public class TopologyNorthboundJAXRS {
65 private String username;
68 public void setSecurityContext(SecurityContext context) {
69 username = context.getUserPrincipal().getName();
72 protected String getUserName() {
78 * Retrieve the Topology
80 * @param containerName
81 * The container for which we want to retrieve the topology
83 * @return A List of EdgeProps each EdgeProp represent an Edge of the grap
84 * with the corresponding properties attached to it.
86 @Path("/{containerName}")
88 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
89 @TypeHint(Topology.class)
90 @StatusCodes({ @ResponseCode(code = 404, condition = "The Container Name passed was not found") })
91 public Topology getTopology(@PathParam("containerName") String containerName) {
93 if (!NorthboundUtils.isAuthorized(
94 getUserName(), containerName, Privilege.READ, this)) {
95 throw new UnauthorizedException(
96 "User is not authorized to perform this operation on container "
99 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
100 .getInstance(ITopologyManager.class, containerName, this);
101 if (topologyManager == null) {
102 throw new ResourceNotFoundException(
103 RestMessages.NOCONTAINER.toString());
106 Map<Edge, Set<Property>> topo = topologyManager.getEdges();
108 List<EdgeProperties> res = new ArrayList<EdgeProperties>();
109 for (Map.Entry<Edge, Set<Property>> entry : topo.entrySet()) {
110 EdgeProperties el = new EdgeProperties(entry.getKey(),
114 return new Topology(res);
121 * Retrieve the user configured links
123 * @param containerName
124 * The container for which we want to retrieve the user links
126 * @return A List of user configured links
128 @Path("/{containerName}/userLink")
130 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
131 @TypeHint(TopologyUserLinks.class)
132 @StatusCodes({ @ResponseCode(code = 404, condition = "The Container Name passed was not found") })
133 public TopologyUserLinks getUserLinks(
134 @PathParam("containerName") String containerName) {
136 if (!NorthboundUtils.isAuthorized(
137 getUserName(), containerName, Privilege.READ, this)) {
138 throw new UnauthorizedException(
139 "User is not authorized to perform this operation on container "
142 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
143 .getInstance(ITopologyManager.class, containerName, this);
144 if (topologyManager == null) {
145 throw new ResourceNotFoundException(
146 RestMessages.NOCONTAINER.toString());
149 ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topologyManager
151 if ((userLinks != null) && (userLinks.values() != null)) {
152 List<TopologyUserLinkConfig> res = new ArrayList<TopologyUserLinkConfig>(
154 return new TopologyUserLinks(res);
163 * @param containerName
164 * Name of the Container. The base Container is "default".
165 * @param TopologyUserLinkConfig
166 * in JSON or XML format
167 * @return Response as dictated by the HTTP Response Status code
170 @Path("/{containerName}/userLink")
172 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
174 @ResponseCode(code = 201, condition = "User Link added successfully"),
175 @ResponseCode(code = 404, condition = "The Container Name passed was not found"),
176 @ResponseCode(code = 409, condition = "Failed to add User Link due to Conflicting Name"),
177 @ResponseCode(code = 500, condition = "Failed to add User Link. Failure Reason included in HTTP Error response"),
178 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
179 public Response addUserLink(
180 @PathParam(value = "containerName") String containerName,
181 @TypeHint(TopologyUserLinkConfig.class) JAXBElement<TopologyUserLinkConfig> userLinkConfig) {
183 if (!NorthboundUtils.isAuthorized(
184 getUserName(), containerName, Privilege.WRITE, this)) {
185 throw new UnauthorizedException(
186 "User is not authorized to perform this operation on container "
189 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
190 .getInstance(ITopologyManager.class, containerName, this);
191 if (topologyManager == null) {
192 throw new ResourceNotFoundException(
193 RestMessages.NOCONTAINER.toString());
196 Status status = topologyManager.addUserLink(userLinkConfig.getValue());
197 if (status.isSuccess()) {
198 NorthboundUtils.auditlog("User Link", username, "added", userLinkConfig.getValue().getName(), containerName);
199 return Response.status(Response.Status.CREATED).build();
201 throw new InternalServerErrorException(status.getDescription());
205 * Delete an User Link
207 * @param containerName
208 * Name of the Container. The base Container is "default".
210 * Name of the Link Configuration
211 * @return Response as dictated by the HTTP Response Status code
214 @Path("/{containerName}/userLink/{name}")
216 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
218 @ResponseCode(code = 200, condition = "Operation successful"),
219 @ResponseCode(code = 404, condition = "The Container Name or Link Configuration Name was not found"),
220 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
221 public Response deleteUserLink(
222 @PathParam("containerName") String containerName,
223 @PathParam("name") String name) {
225 if (!NorthboundUtils.isAuthorized(
226 getUserName(), containerName, Privilege.WRITE, this)) {
227 throw new UnauthorizedException(
228 "User is not authorized to perform this operation on container "
231 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
232 .getInstance(ITopologyManager.class, containerName, this);
233 if (topologyManager == null) {
234 throw new ResourceNotFoundException(
235 RestMessages.NOCONTAINER.toString());
238 Status ret = topologyManager.deleteUserLink(name);
239 if (ret.isSuccess()) {
240 NorthboundUtils.auditlog("User Link", username, "removed", name, containerName);
241 return Response.ok().build();
243 throw new ResourceNotFoundException(ret.getDescription());