[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / resources / DcaeLocationResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.resources;
22
23
24 import io.swagger.annotations.Api;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28
29 import java.util.List;
30
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.GenericEntity;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.Response.Status;
43
44 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
45 import org.onap.dmaap.dbcapi.model.ApiError;
46 import org.onap.dmaap.dbcapi.model.DcaeLocation;
47 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
48
49 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
50 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
51
52
53 @Path("/dcaeLocations")
54 @Api( value= "dcaeLocations", description = "an OpenStack tenant purposed for OpenDCAE (i.e. where OpenDCAE components might be deployed)" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 @Authorization
58 public class DcaeLocationResource extends BaseLoggingClass {
59         private DcaeLocationService locationService = new DcaeLocationService();
60         private ResponseBuilder responseBuilder = new ResponseBuilder();
61         
62         @GET
63         @ApiOperation( value = "return dcaeLocation details", 
64                 notes = "Returns array of  `dcaeLocation` objects.  All objects managed by DMaaP are deployed in some `dcaeLocation` which is a unique identifier for an *OpenStack* tenant purposed for a *dcaeLayer*  (ecomp or edge).", 
65                 response = DcaeLocation.class)
66     @ApiResponses( value = {
67         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
68         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
69     })
70         public Response getDcaeLocations() {
71                 List<DcaeLocation> locs = locationService.getAllDcaeLocations();
72
73                 GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) {};
74         return responseBuilder.success(list);
75         }
76         
77         @POST
78         @ApiOperation( value = "return dcaeLocation details", 
79                 notes = "Create some `dcaeLocation` which is a unique identifier for an *OpenStack* tenant purposed for a *dcaeLayer*  (ecomp or edge).", 
80                 response = DcaeLocation.class)
81     @ApiResponses( value = {
82         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
83         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
84     })
85         public Response addDcaeLocation(DcaeLocation location) {
86
87                 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) != null ) {
88                         return responseBuilder.error(new ApiError(Status.CONFLICT.getStatusCode(),
89                                         "dcaeLocation already exists", "dcaeLocation"));
90                 }
91                 DcaeLocation loc = locationService.addDcaeLocation(location);
92                 return responseBuilder.success(Status.CREATED.getStatusCode(), loc);
93         }
94         
95         @PUT
96         @ApiOperation( value = "return dcaeLocation details", 
97                 notes = "update the openStackAvailabilityZone of a dcaeLocation", 
98                 response = DcaeLocation.class)
99     @ApiResponses( value = {
100         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
101         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
102     })
103         @Path("/{locationName}")
104         public Response updateDcaeLocation( 
105                         @PathParam("locationName") String name, DcaeLocation location) {
106
107                 location.setDcaeLocationName(name);
108                 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) == null ) {
109                         return responseBuilder.notFound();
110
111                 }
112                 DcaeLocation loc = locationService.updateDcaeLocation(location);
113                 return responseBuilder.success(Status.CREATED.getStatusCode(), loc );
114         }
115         
116         @DELETE
117         @ApiOperation( value = "return dcaeLocation details", notes = "delete a dcaeLocation", response = DcaeLocation.class)
118     @ApiResponses( value = {
119         @ApiResponse( code = 204, message = "Success", response = DcaeLocation.class),
120         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
121     })
122         @Path("/{locationName}")
123         public Response deleteDcaeLocation( 
124                         @PathParam("locationName") String name
125                          ){
126                 locationService.removeDcaeLocation(name);
127                 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
128         }
129
130         @GET
131         @ApiOperation( value = "return dcaeLocation details", notes = "Returns a specific `dcaeLocation` object with specified tag", response = DcaeLocation.class)
132     @ApiResponses( value = {
133         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
134         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
135     })
136         @Path("/{locationName}")
137         public Response getDcaeLocation( 
138                         @PathParam("locationName") String name) {
139
140                 DcaeLocation loc =  locationService.getDcaeLocation( name );
141                 if ( loc == null ) {
142                         ApiError err = new ApiError();
143                                 
144                         err.setCode(NOT_FOUND.getStatusCode());
145                         err.setMessage("dcaeLocation does not exist");
146                         err.setFields("dcaeLocation");
147                         
148                         return responseBuilder.error(err);
149                 }
150
151                 return responseBuilder.success(loc);
152         }
153 }