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 io.swagger.annotations.Api;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
29 import java.util.List;
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;
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;
49 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
50 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
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)
58 public class DcaeLocationResource extends BaseLoggingClass {
59 private DcaeLocationService locationService = new DcaeLocationService();
60 private ResponseBuilder responseBuilder = new ResponseBuilder();
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 )
70 public Response getDcaeLocations() {
71 List<DcaeLocation> locs = locationService.getAllDcaeLocations();
73 GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) {};
74 return responseBuilder.success(list);
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 )
85 public Response addDcaeLocation(DcaeLocation location) {
87 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) != null ) {
88 return responseBuilder.error(new ApiError(Status.CONFLICT.getStatusCode(),
89 "dcaeLocation already exists", "dcaeLocation"));
91 DcaeLocation loc = locationService.addDcaeLocation(location);
92 return responseBuilder.success(Status.CREATED.getStatusCode(), loc);
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 )
103 @Path("/{locationName}")
104 public Response updateDcaeLocation(
105 @PathParam("locationName") String name, DcaeLocation location) {
107 location.setDcaeLocationName(name);
108 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) == null ) {
109 return responseBuilder.notFound();
112 DcaeLocation loc = locationService.updateDcaeLocation(location);
113 return responseBuilder.success(Status.CREATED.getStatusCode(), loc );
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 )
122 @Path("/{locationName}")
123 public Response deleteDcaeLocation(
124 @PathParam("locationName") String name
126 locationService.removeDcaeLocation(name);
127 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
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 )
136 @Path("/{locationName}")
137 public Response getDcaeLocation(
138 @PathParam("locationName") String name) {
140 DcaeLocation loc = locationService.getDcaeLocation( name );
142 ApiError err = new ApiError();
144 err.setCode(NOT_FOUND.getStatusCode());
145 err.setMessage("dcaeLocation does not exist");
146 err.setFields("dcaeLocation");
148 return responseBuilder.error(err);
151 return responseBuilder.success(loc);