9663033c20c9dd276b44f7a5431150bcebc454fb
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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.so.adapters.catalogdb.rest;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.QueryParam;
30 import javax.ws.rs.core.MediaType;
31 import org.onap.so.db.catalog.data.repository.ServiceRepository;
32 import org.onap.so.rest.catalog.beans.Service;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35 import org.springframework.transaction.annotation.Transactional;
36 import com.google.common.base.Strings;
37 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
38 import io.swagger.v3.oas.annotations.Operation;
39 import io.swagger.v3.oas.annotations.Parameter;
40 import io.swagger.v3.oas.annotations.info.Info;
41 import io.swagger.v3.oas.annotations.media.ArraySchema;
42 import io.swagger.v3.oas.annotations.media.Content;
43 import io.swagger.v3.oas.annotations.media.Schema;
44 import io.swagger.v3.oas.annotations.responses.ApiResponse;
45
46
47 @OpenAPIDefinition(info = @Info(title = "/v1", description = "model"))
48 @Path("/v1/services")
49 @Component
50 public class ServiceRestImpl {
51
52     @Autowired
53     private ServiceRepository serviceRepo;
54
55     @Autowired
56     private ServiceMapper serviceMapper;
57
58
59     @GET
60     @Path("/{modelUUID}")
61     @Produces({MediaType.APPLICATION_JSON})
62     @Transactional(readOnly = true)
63     public Service findService(@PathParam("modelUUID") String modelUUID, @QueryParam("depth") int depth) {
64         org.onap.so.db.catalog.beans.Service service = serviceRepo.findOneByModelUUID(modelUUID);
65         if (service == null) {
66             throw new CatalogEntityNotFoundException("Unable to find Service " + modelUUID);
67         }
68         return serviceMapper.mapService(service, depth);
69     }
70
71     @GET
72     @Operation(description = "Find Service Models", responses = @ApiResponse(
73             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))))
74     @Produces({MediaType.APPLICATION_JSON})
75     @Transactional(readOnly = true)
76     public List<Service> queryServices(
77             @Parameter(description = "modelName", required = false) @QueryParam("modelName") String modelName,
78             @Parameter(description = "distributionStatus",
79                     required = false) @QueryParam("distributionStatus") String distributionStatus,
80             @Parameter(description = "depth", required = false) @QueryParam("depth") int depth) {
81         List<Service> services = new ArrayList<>();
82         List<org.onap.so.db.catalog.beans.Service> serviceFromDB;
83         if (!Strings.isNullOrEmpty(modelName) && !Strings.isNullOrEmpty(distributionStatus)) {
84             serviceFromDB = serviceRepo.findByModelNameAndDistrobutionStatus(modelName, distributionStatus);
85         } else if (!Strings.isNullOrEmpty(modelName)) {
86             serviceFromDB = serviceRepo.findByModelName(modelName);
87         } else {
88             serviceFromDB = serviceRepo.findAll();
89         }
90         serviceFromDB.stream().forEach(serviceDB -> services.add(serviceMapper.mapService(serviceDB, depth)));
91         return services;
92     }
93 }