update link to upper-constraints.txt
[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 static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
25 import static jakarta.ws.rs.core.Response.Status.NO_CONTENT;
26
27 import io.swagger.annotations.Api;
28 import io.swagger.annotations.ApiOperation;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31 import jakarta.ws.rs.Consumes;
32 import jakarta.ws.rs.DELETE;
33 import jakarta.ws.rs.GET;
34 import jakarta.ws.rs.POST;
35 import jakarta.ws.rs.PUT;
36 import jakarta.ws.rs.Path;
37 import jakarta.ws.rs.PathParam;
38 import jakarta.ws.rs.Produces;
39 import jakarta.ws.rs.core.GenericEntity;
40 import jakarta.ws.rs.core.MediaType;
41 import jakarta.ws.rs.core.Response;
42 import jakarta.ws.rs.core.Response.Status;
43 import java.util.List;
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
50 @Path("/dcaeLocations")
51 @Api( value= "dcaeLocations", description = "an OpenStack tenant purposed for OpenDCAE (i.e. where OpenDCAE components might be deployed)" )
52 @Consumes(MediaType.APPLICATION_JSON)
53 @Produces(MediaType.APPLICATION_JSON)
54 @Authorization
55 public class DcaeLocationResource extends BaseLoggingClass {
56         private DcaeLocationService locationService = new DcaeLocationService();
57         private ResponseBuilder responseBuilder = new ResponseBuilder();
58         
59         @GET
60         @ApiOperation( value = "return dcaeLocation details", 
61                 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).", 
62                 response = DcaeLocation.class)
63     @ApiResponses( value = {
64         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
65         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
66     })
67         public Response getDcaeLocations() {
68                 List<DcaeLocation> locs = locationService.getAllDcaeLocations();
69
70                 GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) {};
71         return responseBuilder.success(list);
72         }
73         
74         @POST
75         @ApiOperation( value = "return dcaeLocation details", 
76                 notes = "Create some `dcaeLocation` which is a unique identifier for an *OpenStack* tenant purposed for a *dcaeLayer*  (ecomp or edge).", 
77                 response = DcaeLocation.class)
78     @ApiResponses( value = {
79         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
80         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
81     })
82         public Response addDcaeLocation(DcaeLocation location) {
83
84                 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) != null ) {
85                         return responseBuilder.error(new ApiError(Status.CONFLICT.getStatusCode(),
86                                         "dcaeLocation already exists", "dcaeLocation"));
87                 }
88                 DcaeLocation loc = locationService.addDcaeLocation(location);
89                 return responseBuilder.success(Status.CREATED.getStatusCode(), loc);
90         }
91         
92         @PUT
93         @ApiOperation( value = "return dcaeLocation details", 
94                 notes = "update the openStackAvailabilityZone of a dcaeLocation", 
95                 response = DcaeLocation.class)
96     @ApiResponses( value = {
97         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
98         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
99     })
100         @Path("/{locationName}")
101         public Response updateDcaeLocation( 
102                         @PathParam("locationName") String name, DcaeLocation location) {
103
104                 location.setDcaeLocationName(name);
105                 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) == null ) {
106                         return responseBuilder.notFound();
107
108                 }
109                 DcaeLocation loc = locationService.updateDcaeLocation(location);
110                 return responseBuilder.success(Status.CREATED.getStatusCode(), loc );
111         }
112         
113         @DELETE
114         @ApiOperation( value = "return dcaeLocation details", notes = "delete a dcaeLocation", response = DcaeLocation.class)
115     @ApiResponses( value = {
116         @ApiResponse( code = 204, message = "Success", response = DcaeLocation.class),
117         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
118     })
119         @Path("/{locationName}")
120         public Response deleteDcaeLocation( 
121                         @PathParam("locationName") String name
122                          ){
123                 locationService.removeDcaeLocation(name);
124                 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
125         }
126
127         @GET
128         @ApiOperation( value = "return dcaeLocation details", notes = "Returns a specific `dcaeLocation` object with specified tag", response = DcaeLocation.class)
129     @ApiResponses( value = {
130         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
131         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
132     })
133         @Path("/{locationName}")
134         public Response getDcaeLocation( 
135                         @PathParam("locationName") String name) {
136
137                 DcaeLocation loc =  locationService.getDcaeLocation( name );
138                 if ( loc == null ) {
139                         ApiError err = new ApiError();
140                                 
141                         err.setCode(NOT_FOUND.getStatusCode());
142                         err.setMessage("dcaeLocation does not exist");
143                         err.setFields("dcaeLocation");
144                         
145                         return responseBuilder.error(err);
146                 }
147
148                 return responseBuilder.success(loc);
149         }
150 }