395eefd3dd3de4f4d592e69ffb6684afee776403
[neutron.git] /
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 org.codehaus.enunciate.jaxrs.ResponseCode;
11 import org.codehaus.enunciate.jaxrs.StatusCodes;
12 import org.opendaylight.neutron.spi.INeutronSFCPortChainCRUD;
13 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
14 import org.opendaylight.neutron.spi.NeutronSFCPortChain;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
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.MediaType;
26 import javax.ws.rs.core.Response;
27 import java.net.HttpURLConnection;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 /**
33  * Neutron Northbound REST APIs for OpenStack SFC Port Chain.<br>
34  * This class provides REST APIs for managing OpenStack SFC Port Chain
35  *
36  * <br>
37  * <br>
38  * Authentication scheme : <b>HTTP Basic</b><br>
39  * Authentication realm : <b>opendaylight</b><br>
40  * Transport : <b>HTTP and HTTPS</b><br>
41  * <br>
42  * HTTPS Authentication is disabled by default. Administrator can enable it in
43  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
44  * trusted authority.<br>
45  * More info :
46  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
47  *
48  */
49
50 @Path("/sfc/portchains")
51 public class NeutronSFCPortChainsNorthbound
52         extends AbstractNeutronNorthbound<NeutronSFCPortChain,
53         NeutronSFCPortChainRequest, INeutronSFCPortChainCRUD> {
54
55     private static final String RESOURCE_NAME = "Sfc Port Chain";
56
57     @Override
58     protected String getResourceName() {
59         return RESOURCE_NAME;
60     }
61
62     @Override
63     protected NeutronSFCPortChain extractFields(NeutronSFCPortChain o, List<String> fields) {
64         return o.extractFields(fields);
65     }
66
67     @Override
68     protected NeutronSFCPortChainRequest newNeutronRequest(NeutronSFCPortChain o) {
69         return new NeutronSFCPortChainRequest(o);
70     }
71
72     @Override
73     protected INeutronSFCPortChainCRUD getNeutronCRUD() {
74         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSFCPortChainCRUD(this);
75         if (answer.getSFCPortChainInterface() == null) {
76             throw new ServiceUnavailableException(serviceUnavailable());
77         }
78         return answer.getSFCPortChainInterface();
79     }
80
81     /**
82      * Returns a list of all SFC Port Chains*/
83
84     @GET
85     @Produces({ MediaType.APPLICATION_JSON })
86     @StatusCodes({
87             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
88             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
89             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
90             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
91     public Response listSFCPortChains(
92             // return fields
93             @QueryParam("fields") List<String> fields,
94             @QueryParam("id") String queryID,
95             @QueryParam("name") String queryName,
96             @QueryParam("tenant_id") String queryTenantID) {
97         INeutronSFCPortChainCRUD sfcPortChainInterface = getNeutronCRUD();
98         List<NeutronSFCPortChain> allSFCPortChain = sfcPortChainInterface.getAll();
99         List<NeutronSFCPortChain> ans = new ArrayList<>();
100         Iterator<NeutronSFCPortChain> i = allSFCPortChain.iterator();
101         while (i.hasNext()) {
102             NeutronSFCPortChain oSFCPC = i.next();
103             if ((queryID == null || queryID.equals(oSFCPC.getID())) &&
104                     (queryName == null || queryName.equals(oSFCPC.getName())) &&
105                     (queryTenantID == null || queryTenantID.equals(oSFCPC.getTenantID()))) {
106                 if (fields.size() > 0) {
107                     ans.add(extractFields(oSFCPC,fields));
108                 } else {
109                     ans.add(oSFCPC);
110                 }
111             }
112         }
113
114         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSFCPortChainRequest(ans)).build();
115
116     }
117
118     /**
119      * Returns a specific SFC Port Chain */
120
121     @Path("{portChainUUID}")
122     @GET
123     @Produces({ MediaType.APPLICATION_JSON })
124     @StatusCodes({
125             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
126             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
127             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
128             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
129             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130     public Response showSFCPortChain(
131             @PathParam("portChainUUID") String sfcPortChainUUID,
132             // return fields
133             @QueryParam("fields") List<String> fields) {
134         return show(sfcPortChainUUID, fields);
135     }
136
137     /**
138      * Creates new SFC Port Chain*/
139     @POST
140     @Produces({ MediaType.APPLICATION_JSON })
141     @Consumes({ MediaType.APPLICATION_JSON })
142     @StatusCodes({
143             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
144             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
145     public Response createSFCPortChain(final NeutronSFCPortChainRequest input) {
146         return create(input);
147     }
148
149     @Override
150     protected void updateDelta(String uuid, NeutronSFCPortChain delta, NeutronSFCPortChain original) {
151         /*
152          *  note: what we get appears to not be a delta but
153          * rather an incomplete updated object.  So we need to set
154          * the ID to complete the object and then send that down
155          * for folks to check
156          */
157
158         delta.setID(uuid);
159         delta.setTenantID(original.getTenantID());
160     }
161
162     /**
163      * Updates an existing SFC Port Chain */
164     @Path("{portChainUUID}")
165     @PUT
166     @Produces({ MediaType.APPLICATION_JSON })
167     @Consumes({ MediaType.APPLICATION_JSON })
168     @StatusCodes({
169             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
170             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
171             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
172     public Response updateSFCPortChain(
173             @PathParam("portChainUUID") String sfcPortChainUUID, final NeutronSFCPortChainRequest input) {
174         return update(sfcPortChainUUID, input);
175     }
176
177     /**
178      * Deletes the SFC Port Chain */
179     @Path("{portChainUUID}")
180     @DELETE
181     @StatusCodes({
182             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
183             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
184             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
185     public Response deleteSFCPortChain(
186             @PathParam("portChainUUID") String sfcPortChainUUID) {
187         return delete(sfcPortChainUUID);
188     }
189 }