checkstyle: enable SummaryJavadoc
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronNetworksNorthbound.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.DefaultValue;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
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.UriInfo;
28 import org.codehaus.enunciate.jaxrs.ResponseCode;
29 import org.codehaus.enunciate.jaxrs.StatusCodes;
30 import org.codehaus.enunciate.jaxrs.TypeHint;
31 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
32 import org.opendaylight.neutron.spi.NeutronNetwork;
33
34 /**
35  * Neutron Northbound REST APIs for Network.<br>
36  * This class provides REST APIs for managing neutron Networks
37  *
38  * <br>
39  * <br>
40  * Authentication scheme : <b>HTTP Basic</b><br>
41  * Authentication realm : <b>opendaylight</b><br>
42  * Transport : <b>HTTP and HTTPS</b><br>
43  * <br>
44  * HTTPS Authentication is disabled by default. Administrator can enable it in
45  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
46  * trusted authority.<br>
47  * More info :
48  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
49  *
50  */
51
52 @Path("/networks")
53 public final class NeutronNetworksNorthbound
54         extends AbstractNeutronNorthbound<NeutronNetwork, NeutronNetworkRequest, INeutronNetworkCRUD> {
55
56     @Context
57     UriInfo uriInfo;
58
59     private static final String RESOURCE_NAME = "Network";
60
61     @Override
62     protected String getResourceName() {
63         return RESOURCE_NAME;
64     }
65
66     /**
67      * Returns a list of all Networks.
68      */
69
70     @GET
71     @Produces({ MediaType.APPLICATION_JSON })
72     //@TypeHint(OpenStackNetworks.class)
73     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
74             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
75             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
76             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
77     public Response listNetworks(
78             // return fields
79             @QueryParam("fields") List<String> fields,
80             // note: openstack isn't clear about filtering on lists, so we aren't handling them
81             @QueryParam("id") String queryID,
82             @QueryParam("name") String queryName,
83             @QueryParam("admin_state_up") String queryAdminStateUp,
84             @QueryParam("status") String queryStatus,
85             @QueryParam("shared") String queryShared,
86             @QueryParam("tenant_id") String queryTenantID,
87             @QueryParam("router_external") String queryRouterExternal,
88             @QueryParam("provider_network_type") String queryProviderNetworkType,
89             @QueryParam("provider_physical_network") String queryProviderPhysicalNetwork,
90             @QueryParam("provider_segmentation_id") String queryProviderSegmentationID,
91             @QueryParam("qos_policy_id") String queryQosPolicyId,
92             // linkTitle
93             @QueryParam("limit") Integer limit,
94             @QueryParam("marker") String marker,
95             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
96     // sorting not supported
97     ) {
98         INeutronNetworkCRUD networkInterface = getNeutronCRUD();
99         List<NeutronNetwork> allNetworks = networkInterface.getAll();
100         List<NeutronNetwork> ans = new ArrayList<>();
101         for (NeutronNetwork network : allNetworks) {
102             //match filters: TODO provider extension
103             Boolean adminStateUp = null;
104             Boolean shared = null;
105             Boolean routerExternal = null;
106             if (queryAdminStateUp != null) {
107                 adminStateUp = Boolean.valueOf(queryAdminStateUp);
108             }
109             if (queryShared != null) {
110                 shared = Boolean.valueOf(queryShared);
111             }
112             if (queryRouterExternal != null) {
113                 routerExternal = Boolean.valueOf(queryRouterExternal);
114             }
115             if ((queryID == null || queryID.equals(network.getID()))
116                     && (queryName == null || queryName.equals(network.getName()))
117                     && (adminStateUp == null || adminStateUp.booleanValue() == network.isAdminStateUp())
118                     && (queryStatus == null || queryStatus.equals(network.getStatus()))
119                     && (shared == null || shared.booleanValue() == network.isShared())
120                     && (routerExternal == null || routerExternal.booleanValue() == network.isRouterExternal())
121                     && (queryTenantID == null || queryTenantID.equals(network.getTenantID()))
122                     && (queryQosPolicyId == null || queryQosPolicyId.equals(network.getQosPolicyId()))) {
123                 if (fields.size() > 0) {
124                     ans.add(network.extractFields(fields));
125                 } else {
126                     ans.add(network);
127                 }
128             }
129         }
130
131         if (limit != null && ans.size() > 1) {
132             // Return a paginated request
133             NeutronNetworkRequest request = (NeutronNetworkRequest) PaginatedRequestFactory.createRequest(limit, marker,
134                     pageReverse, uriInfo, ans, NeutronNetwork.class);
135             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
136         }
137
138         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronNetworkRequest(ans)).build();
139
140     }
141
142     /**
143      * Returns a specific Network.
144      */
145
146     @Path("{netUUID}")
147     @GET
148     @Produces({ MediaType.APPLICATION_JSON })
149     //@TypeHint(OpenStackNetworks.class)
150     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
151             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
152             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
153             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
154             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
155     public Response showNetwork(@PathParam("netUUID") String netUUID,
156             // return fields
157             @QueryParam("fields") List<String> fields) {
158         return show(netUUID, fields);
159     }
160
161     /**
162      * Creates new Networks.
163      */
164     @POST
165     @Produces({ MediaType.APPLICATION_JSON })
166     @Consumes({ MediaType.APPLICATION_JSON })
167     @TypeHint(NeutronNetwork.class)
168     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
169             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
170     public Response createNetworks(final NeutronNetworkRequest input) {
171         return create(input);
172     }
173
174     @Override
175     protected void updateDelta(String uuid, NeutronNetwork delta, NeutronNetwork original) {
176         /*
177          *  note: what we get appears to not be a delta but
178          * rather an incomplete updated object.  So we need to set
179          * the ID to complete the object and then send that down
180          * for folks to check
181          */
182
183         delta.setID(uuid);
184         delta.setTenantID(original.getTenantID());
185     }
186
187     /**
188      * Updates a Network.
189      */
190     @Path("{netUUID}")
191     @PUT
192     @Produces({ MediaType.APPLICATION_JSON })
193     @Consumes({ MediaType.APPLICATION_JSON })
194     //@TypeHint(OpenStackNetworks.class)
195     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
196             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
197             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
198     public Response updateNetwork(@PathParam("netUUID") String netUUID, final NeutronNetworkRequest input) {
199         return update(netUUID, input);
200     }
201
202     /**
203      * Deletes a Network.
204      */
205
206     @Path("{netUUID}")
207     @DELETE
208     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
209             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
210             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
211     public Response deleteNetwork(@PathParam("netUUID") String netUUID) {
212         return delete(netUUID);
213     }
214 }