ce381f688a981840c03edf03ffa00c4ee4c6555e
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronSFCFlowClassifiersNorthbound.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. 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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
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.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.opendaylight.neutron.spi.INeutronSFCFlowClassifierCRUD;
29 import org.opendaylight.neutron.spi.NeutronSFCFlowClassifier;
30 import org.ops4j.pax.cdi.api.OsgiService;
31
32 /**
33  * Neutron Northbound REST APIs for OpenStack SFC Flow Classifier.
34  */
35 @Singleton
36 @Path("/sfc/flowclassifiers")
37 public final class NeutronSFCFlowClassifiersNorthbound extends AbstractNeutronNorthbound<NeutronSFCFlowClassifier,
38         NeutronSFCFlowClassifierRequest, INeutronSFCFlowClassifierCRUD> {
39
40     private static final String RESOURCE_NAME = "Sfc Flow Classifier";
41
42     @Inject
43     public NeutronSFCFlowClassifiersNorthbound(@OsgiService INeutronSFCFlowClassifierCRUD neutronCRUD) {
44         super(neutronCRUD);
45     }
46
47     @Override
48     protected String getResourceName() {
49         return RESOURCE_NAME;
50     }
51
52     /**
53      * Returns a list of all SFC Flow Classifiers.
54      */
55     @GET
56     @Produces({ MediaType.APPLICATION_JSON })
57     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
58             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
59             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
60             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
61     public Response listSFCFlowClassifiers(
62             // return fields
63             @QueryParam("fields") List<String> fields,
64             // note: openstack isn't clear about filtering on lists, so we aren't handling them
65             @QueryParam("id") String queryID,
66             @QueryParam("name") String queryName,
67             @QueryParam("ethertype") String queryEthertype,
68             @QueryParam("protocol") String queryProtocol,
69             @QueryParam("source_port_range_min") Integer querySourcePortRangeMin,
70             @QueryParam("source_port_range_max") Integer querySourcePortRangeMax,
71             @QueryParam("tenant_id") String queryTenantID,
72             @QueryParam("destination_port_range_min") Integer queryDestinationPortRangeMin,
73             @QueryParam("destination_port_range_max") Integer queryDestinationPortRangeMax,
74             @QueryParam("source_ip_prefix") String querySourceIpPrefix,
75             @QueryParam("destination_ip_prefix") String queryDestinationIpPrefix,
76             @QueryParam("logical_source_port") String queryLogicalSourcePort,
77             @QueryParam("logical_destination_port") String queryLogicalDestinationPort) {
78         INeutronSFCFlowClassifierCRUD sfcFlowClassifierInterface = getNeutronCRUD();
79         List<NeutronSFCFlowClassifier> allSFCFlowClassifier = sfcFlowClassifierInterface.getAll();
80         List<NeutronSFCFlowClassifier> ans = new ArrayList<>();
81         for (NeutronSFCFlowClassifier classifier : allSFCFlowClassifier) {
82             if ((queryID == null || queryID.equals(classifier.getID()))
83                     && (queryName == null || queryName.equals(classifier.getName()))
84                     && (queryEthertype == null || queryEthertype.equals(classifier.getEthertype()))
85                     && (queryProtocol == null || queryProtocol.equals(classifier.getProtocol()))
86                     && (querySourcePortRangeMin == null
87                         || querySourcePortRangeMin.equals(classifier.getSourcePortRangeMin()))
88                     && (querySourcePortRangeMax == null
89                         || querySourcePortRangeMax.equals(classifier.getSourcePortRangeMax()))
90                     && (queryDestinationPortRangeMin == null
91                             || queryDestinationPortRangeMin.equals(classifier.getDestinationPortRangeMin()))
92                     && (queryDestinationPortRangeMax == null
93                             || queryDestinationPortRangeMax.equals(classifier.getDestinationPortRangeMax()))
94                     && (querySourceIpPrefix == null || querySourceIpPrefix.equals(classifier.getSourceIpPrefix()))
95                     && (queryDestinationIpPrefix == null
96                             || queryDestinationIpPrefix.equals(classifier.getDestinationIpPrefix()))
97                     && (queryLogicalSourcePort == null
98                             || queryLogicalSourcePort.equals(classifier.getLogicalSourcePortUUID()))
99                     && (queryLogicalDestinationPort == null
100                             || queryLogicalDestinationPort.equals(classifier.getLogicalDestinationPortUUID()))
101                     && (queryTenantID == null || queryTenantID.equals(classifier.getTenantID()))) {
102                 if (fields.size() > 0) {
103                     ans.add(classifier.extractFields(fields));
104                 } else {
105                     ans.add(classifier);
106                 }
107             }
108         }
109
110         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSFCFlowClassifierRequest(ans)).build();
111
112     }
113
114     /**
115      * Returns a specific SFC Flow Classifier.
116      */
117     @Path("{flowClassifierUUID}")
118     @GET
119     @Produces({ MediaType.APPLICATION_JSON })
120     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
121             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
122             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
123             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
124             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
125     public Response showSFCFlowClassifier(@PathParam("flowClassifierUUID") String sfcFlowClassifierUUID,
126             // return fields
127             @QueryParam("fields") List<String> fields) {
128         return show(sfcFlowClassifierUUID, fields);
129     }
130
131     /**
132      * Creates new SFC Flow Classifier.
133      */
134     @POST
135     @Produces({ MediaType.APPLICATION_JSON })
136     @Consumes({ MediaType.APPLICATION_JSON })
137     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
138             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
139     public Response createSFCFlowClassifier(final NeutronSFCFlowClassifierRequest input) {
140         return create(input);
141     }
142
143     @Override
144     protected void updateDelta(String uuid, NeutronSFCFlowClassifier delta, NeutronSFCFlowClassifier original) {
145         /*
146          * note: what we get appears to not be a delta but
147          * rather an incomplete updated object.  So we need to set
148          * the ID to complete the object and then send that down
149          * for folks to check
150          */
151         delta.setID(uuid);
152         delta.setTenantID(original.getTenantID());
153     }
154
155     /**
156      * Updates an existing SFC Flow Classifier.
157      */
158     @Path("{flowClassifierUUID}")
159     @PUT
160     @Produces({ MediaType.APPLICATION_JSON })
161     @Consumes({ MediaType.APPLICATION_JSON })
162     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
163             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
164             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
165     public Response updateSFCFlowClassifier(@PathParam("flowClassifierUUID") String sfcFlowClassifierUUID,
166             final NeutronSFCFlowClassifierRequest input) {
167         return update(sfcFlowClassifierUUID, input);
168     }
169
170     /**
171      * Deletes the SFC Flow Classifier.
172      */
173     @Path("{flowClassifierUUID}")
174     @DELETE
175     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
176             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
177             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
178     public Response deleteSFCFlowClassifier(@PathParam("flowClassifierUUID") String sfcFlowClassifierUUID) {
179         return delete(sfcFlowClassifierUUID);
180     }
181 }