2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.resources;
24 import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
25 import static jakarta.ws.rs.core.Response.Status.NO_CONTENT;
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;
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)
55 public class DcaeLocationResource extends BaseLoggingClass {
56 private DcaeLocationService locationService = new DcaeLocationService();
57 private ResponseBuilder responseBuilder = new ResponseBuilder();
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 )
67 public Response getDcaeLocations() {
68 List<DcaeLocation> locs = locationService.getAllDcaeLocations();
70 GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) {};
71 return responseBuilder.success(list);
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 )
82 public Response addDcaeLocation(DcaeLocation location) {
84 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) != null ) {
85 return responseBuilder.error(new ApiError(Status.CONFLICT.getStatusCode(),
86 "dcaeLocation already exists", "dcaeLocation"));
88 DcaeLocation loc = locationService.addDcaeLocation(location);
89 return responseBuilder.success(Status.CREATED.getStatusCode(), loc);
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 )
100 @Path("/{locationName}")
101 public Response updateDcaeLocation(
102 @PathParam("locationName") String name, DcaeLocation location) {
104 location.setDcaeLocationName(name);
105 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) == null ) {
106 return responseBuilder.notFound();
109 DcaeLocation loc = locationService.updateDcaeLocation(location);
110 return responseBuilder.success(Status.CREATED.getStatusCode(), loc );
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 )
119 @Path("/{locationName}")
120 public Response deleteDcaeLocation(
121 @PathParam("locationName") String name
123 locationService.removeDcaeLocation(name);
124 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
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 )
133 @Path("/{locationName}")
134 public Response getDcaeLocation(
135 @PathParam("locationName") String name) {
137 DcaeLocation loc = locationService.getDcaeLocation( name );
139 ApiError err = new ApiError();
141 err.setCode(NOT_FOUND.getStatusCode());
142 err.setMessage("dcaeLocation does not exist");
143 err.setFields("dcaeLocation");
145 return responseBuilder.error(err);
148 return responseBuilder.success(loc);