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