Fix object references to show fields
[dmaap/dbcapi.git] / 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.apache.log4j.Logger;
45 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
46 import org.onap.dmaap.dbcapi.model.ApiError;
47 import org.onap.dmaap.dbcapi.model.DcaeLocation;
48 import org.onap.dmaap.dbcapi.service.ApiService;
49 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
50
51
52 @Path("/dcaeLocations")
53 @Api( value= "dcaeLocations", description = "an OpenStack tenant purposed for OpenDCAE (i.e. where OpenDCAE components might be deployed)" )
54 @Consumes(MediaType.APPLICATION_JSON)
55 @Produces(MediaType.APPLICATION_JSON)
56 @Authorization
57 public class DcaeLocationResource extends BaseLoggingClass {
58         static final Logger logger = Logger.getLogger(DcaeLocationResource.class);      
59         DcaeLocationService locationService = new DcaeLocationService();
60         
61         @GET
62         @ApiOperation( value = "return dcaeLocation details", 
63                 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).", 
64                 response = DcaeLocation.class)
65     @ApiResponses( value = {
66         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
67         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
68     })
69         public Response getDcaeLocations() {
70                 ApiService check = new ApiService();
71
72                 List<DcaeLocation> locs = locationService.getAllDcaeLocations();
73
74                 GenericEntity<List<DcaeLocation>> list = new GenericEntity<List<DcaeLocation>>(locs) {
75         };
76         return check.success(list);
77         }
78         
79         @POST
80         @ApiOperation( value = "return dcaeLocation details", 
81                 notes = "Create some `dcaeLocation` which is a unique identifier for an *OpenStack* tenant purposed for a *dcaeLayer*  (ecomp or edge).", 
82                 response = DcaeLocation.class)
83     @ApiResponses( value = {
84         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
85         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
86     })
87         public Response addDcaeLocation( 
88                         DcaeLocation location 
89                         ) {
90                 ApiService check = new ApiService();
91
92                 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) != null ) {
93                                 
94                         check.setCode(Status.CONFLICT.getStatusCode());
95                         check.setMessage("dcaeLocation already exists");
96                         check.setFields("dcaeLocation");
97                         
98                         return check.error();
99
100                 }
101                 DcaeLocation loc = locationService.addDcaeLocation(location);
102                 return check.success(Status.CREATED.getStatusCode(), loc);
103         }
104         
105         @PUT
106         @ApiOperation( value = "return dcaeLocation details", 
107                 notes = "update the openStackAvailabilityZone of a dcaeLocation", 
108                 response = DcaeLocation.class)
109     @ApiResponses( value = {
110         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
111         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
112     })
113         @Path("/{locationName}")
114         public Response updateDcaeLocation( 
115                         @PathParam("locationName") String name, 
116                         DcaeLocation location
117                          ) {
118                 ApiService check = new ApiService();
119
120                 location.setDcaeLocationName(name);
121                 if ( locationService.getDcaeLocation(location.getDcaeLocationName()) == null ) {
122                         ApiError err = new ApiError();
123                                 
124                         err.setCode(Status.NOT_FOUND.getStatusCode());
125                         err.setMessage("dcaeLocation does not exist");
126                         err.setFields("dcaeLocation");
127                         
128                         return check.notFound();
129
130
131                 }
132                 DcaeLocation loc = locationService.updateDcaeLocation(location);
133                 return check.success(Status.CREATED.getStatusCode(), loc );
134         }
135         
136         @DELETE
137         @ApiOperation( value = "return dcaeLocation details", notes = "delete a dcaeLocation", response = DcaeLocation.class)
138     @ApiResponses( value = {
139         @ApiResponse( code = 204, message = "Success", response = DcaeLocation.class),
140         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
141     })
142         @Path("/{locationName}")
143         public Response deleteDcaeLocation( 
144                         @PathParam("locationName") String name
145                          ){
146                 ApiService check = new ApiService();
147
148                 locationService.removeDcaeLocation(name);
149                 return check.success(Status.NO_CONTENT.getStatusCode(), null);
150         }
151
152         @GET
153         @ApiOperation( value = "return dcaeLocation details", notes = "Returns a specific `dcaeLocation` object with specified tag", response = DcaeLocation.class)
154     @ApiResponses( value = {
155         @ApiResponse( code = 200, message = "Success", response = DcaeLocation.class),
156         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
157     })
158         @Path("/{locationName}")
159         public Response getDcaeLocation( 
160                         @PathParam("locationName") String name
161                          ) {
162                 ApiService check = new ApiService();
163
164                 DcaeLocation loc =  locationService.getDcaeLocation( name );
165                 if ( loc == null ) {
166                         ApiError err = new ApiError();
167                                 
168                         err.setCode(Status.NOT_FOUND.getStatusCode());
169                         err.setMessage("dcaeLocation does not exist");
170                         err.setFields("dcaeLocation");
171                         
172                         return check.error();
173
174
175                 }
176
177                 return check.success(loc);
178         }
179 }