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