5f1933b9b1c625032a095ab33317d54753c8fc10
[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 import java.util.concurrent.ConcurrentMap;
17
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;
28
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;
43
44 /**
45  * Topology Northbound REST API
46  * 
47  * <br><br>
48  * Authentication scheme : <b>HTTP Basic</b><br>
49  * Authentication realm : <b>opendaylight</b><br>
50  * Transport : <b>HTTP and HTTPS</b><br>
51  * <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
55  */
56
57 @Path("/")
58 public class TopologyNorthboundJAXRS {
59     private static Logger logger = LoggerFactory
60             .getLogger(TopologyNorthboundJAXRS.class);
61
62     /**
63      *
64      * Retrieve the Topology
65      *
66      * @param containerName The container for which we want to retrieve the topology
67      *
68      * @return A List of EdgeProps each EdgeProp represent an Edge of
69      * the grap with the corresponding properties attached to it.
70      */
71     @Path("/{containerName}")
72     @GET
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
82                     .toString());
83         }
84
85         Map<Edge, Set<Property>> topo = topologyManager.getEdges();
86         if (topo != null) {
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());
90                 res.add(el);
91             }
92             return new Topology(res);
93         }
94
95         return null;
96     }
97
98     /**
99     * Retrieve the user configured links 
100     *
101     * @param containerName The container for which we want to retrieve the user links
102     *
103     * @return A List of user configured links
104     */
105    @Path("/{containerName}/userLink")
106    @GET
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
116                    .toString());
117        }
118
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);
123        }
124
125        return null;
126    }
127    
128    /**
129     * Add an User Link
130     *
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
134     */
135
136    @Path("/{containerName}/userLink")
137    @POST
138    @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
139    @StatusCodes( {
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) {
148
149                 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
150                                 .getInstance(ITopologyManager.class, containerName, this);
151                 if (topologyManager == null) {
152                         throw new ResourceNotFoundException(RestMessages.NOCONTAINER
153                                         .toString());
154                 }
155
156                 Status status = topologyManager.addUserLink(userLinkConfig.getValue());
157                 if (status.isSuccess()) {
158                         return Response.status(Response.Status.CREATED).build();
159                 }
160                 throw new InternalServerErrorException(status.getDescription());
161    }
162
163    /**
164     * Delete an User Link
165     *
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
169     */
170
171    @Path("/{containerName}/userLink/{name}")
172    @DELETE
173    @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
174    @StatusCodes( {
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) {
181
182                 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
183                                 .getInstance(ITopologyManager.class, containerName, this);
184            if (topologyManager == null) {
185                    throw new ResourceNotFoundException(RestMessages.NOCONTAINER
186                                    .toString());
187            }
188
189        Status ret = topologyManager.deleteUserLink(name);
190        if (ret.isSuccess()) {
191            return Response.ok().build();
192        }
193        throw new ResourceNotFoundException(ret.getDescription());
194    }
195 }