dd1b6ad9cdaf2b62bfdf56ace50d1b542fc0642d
[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.ops4j.pax.cdi.api.OsgiService;
30
31 /**
32  * Neutron Northbound REST APIs for Metering Lable Rules.<br>
33  */
34 @Singleton
35 @Path("/metering/metering-label-rules")
36 public final class NeutronMeteringLabelRulesNorthbound extends AbstractNeutronNorthbound<NeutronMeteringLabelRule,
37         NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD> {
38
39     private static final String RESOURCE_NAME = "Metering Label Rule";
40
41     @Inject
42     public NeutronMeteringLabelRulesNorthbound(@OsgiService INeutronMeteringLabelRuleCRUD neutronCRUD) {
43         super(neutronCRUD);
44     }
45
46     @Override
47     protected String getResourceName() {
48         return RESOURCE_NAME;
49     }
50
51     /**
52      * Returns a list of all metering label rules.
53      */
54     @GET
55     @Produces({ MediaType.APPLICATION_JSON })
56     //@TypeHint(OpenStackNetworks.class)
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 listMeteringLabelRules(
62             // return fields
63             @QueryParam("fields") List<String> fields,
64             // filter fields
65             @QueryParam("id") String queryID,
66             @QueryParam("direction") String queryDirection,
67             @QueryParam("remote_ip_prefix") String queryRemoteIpPrefix,
68             @QueryParam("metering_label_id") String queryLabelID
69     // pagination and sorting are TODO
70     ) {
71         INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
72         List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
73         List<NeutronMeteringLabelRule> ans = new ArrayList<>();
74         for (NeutronMeteringLabelRule rule : allNeutronMeteringLabelRule) {
75             if ((queryID == null || queryID.equals(rule.getID()))
76                     && (queryDirection == null || queryDirection.equals(rule.getMeteringLabelRuleDirection()))
77                     && (queryRemoteIpPrefix == null
78                             || queryRemoteIpPrefix.equals(rule.getMeteringLabelRuleRemoteIpPrefix()))
79                     && (queryLabelID == null || queryLabelID.equals(rule.getMeteringLabelRuleLabelID()))) {
80                 if (fields.size() > 0) {
81                     ans.add(rule.extractFields(fields));
82                 } else {
83                     ans.add(rule);
84                 }
85             }
86         }
87         //TODO: apply pagination to results
88         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronMeteringLabelRuleRequest(ans)).build();
89     }
90
91     /**
92      * Returns a specific metering label rule.
93      */
94     @Path("{ruleUUID}")
95     @GET
96     @Produces({ MediaType.APPLICATION_JSON })
97     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
98             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
99             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
100             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
101             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
102             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
103     public Response showMeteringLabelRule(@PathParam("ruleUUID") String ruleUUID,
104             // return fields
105             @QueryParam("fields") List<String> fields) {
106         return show(ruleUUID, fields);
107     }
108
109     /**
110      * Creates new metering label rule.
111      */
112     @POST
113     @Produces({ MediaType.APPLICATION_JSON })
114     @Consumes({ MediaType.APPLICATION_JSON })
115     //@TypeHint(NeutronNetwork.class)
116     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
117             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
118     public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
119         INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
120         if (input.isSingleton()) {
121             NeutronMeteringLabelRule singleton = input.getSingleton();
122
123             /*
124              * add meteringLabelRule to the cache
125              */
126             meteringLabelRuleInterface.add(singleton);
127         } else {
128
129             /*
130              * only singleton meteringLabelRule creates supported
131              */
132             throw new BadRequestException("Only singleton meteringLabelRule creates supported");
133         }
134         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
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 }