propagate datastore exceptions all the way to northbound
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronMeteringLabelRulesNorthbound.java
1 /*
2  * Copyright (c) 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 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.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
28 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
29 import org.opendaylight.yangtools.yang.common.OperationFailedException;
30 import org.ops4j.pax.cdi.api.OsgiService;
31
32 /**
33  * Neutron Northbound REST APIs for Metering Lable Rules.<br>
34  */
35 @Singleton
36 @Path("/metering/metering-label-rules")
37 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
38         NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
39
40     private static final String RESOURCE_NAME = "Metering Label Rule";
41
42     @Inject
43     public NeutronMeteringLabelRulesNorthbound(@OsgiService INeutronMeteringLabelRuleCRUD 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 metering label rules.
54      */
55     @GET
56     @Produces({ MediaType.APPLICATION_JSON })
57     //@TypeHint(OpenStackNetworks.class)
58     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
59             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
60             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
61             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
62     public Response listMeteringLabelRules(
63             // return fields
64             @QueryParam("fields") List<String> fields,
65             // filter fields
66             @QueryParam("id") String queryID,
67             @QueryParam("direction") String queryDirection,
68             @QueryParam("remote_ip_prefix") String queryRemoteIpPrefix,
69             @QueryParam("metering_label_id") String queryLabelID
70     // pagination and sorting are TODO
71     ) {
72         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
73         List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
74         List<NeutronMeteringLabelRule> ans = new ArrayList<>();
75         for (NeutronMeteringLabelRule rule : allNeutronMeteringLabelRule) {
76             if ((queryID == null || queryID.equals(rule.getID()))
77                     && (queryDirection == null || queryDirection.equals(rule.getMeteringLabelRuleDirection()))
78                     && (queryRemoteIpPrefix == null
79                             || queryRemoteIpPrefix.equals(rule.getMeteringLabelRuleRemoteIpPrefix()))
80                     && (queryLabelID == null || queryLabelID.equals(rule.getMeteringLabelRuleLabelID()))) {
81                 if (fields.size() > 0) {
82                     ans.add(rule.extractFields(fields));
83                 } else {
84                     ans.add(rule);
85                 }
86             }
87         }
88         //TODO: apply pagination to results
89         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
90     }
91
92     /**
93      * Returns a specific metering label rule.
94      */
95     @Path("{ruleUUID}")
96     @GET
97     @Produces({ MediaType.APPLICATION_JSON })
98     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
99             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
100             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
101             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
102             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
103             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
104     public Response showMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID,
105             // return fields
106             @QueryParam("fields") List<String> fields) {
107         return show(ruleUUID, fields);
108     }
109
110     /**
111      * Creates new metering label rule.
112      */
113     @POST
114     @Produces({ MediaType.APPLICATION_JSON })
115     @Consumes({ MediaType.APPLICATION_JSON })
116     //@TypeHint(NeutronNetwork.class)
117     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
118             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
119     public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
120         if (input.isSingleton()) {
121             NeutronMeteringLabelRule singleton = input.getSingleton();
122             try {
123                 // add meteringLabelRule to the cache
124                 INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
125                 meteringLabelRuleInterface.add(singleton);
126                 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
127
128             } catch (OperationFailedException e) {
129                 throw new DatastoreOperationFailedWebApplicationException(e);
130             }
131         } else {
132             // only singleton meteringLabelRule creates supported
133             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
134         }
135     }
136
137     /**
138      * Deletes a Metering Label rule.
139      */
140     @Path("{ruleUUID}")
141     @DELETE
142     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
143             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
144             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
145     public Response deleteMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID) {
146         return delete(ruleUUID);
147     }
148 }