1ca899839660fb7368a24e5fd587213545bb3cd2
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / onap / so / adapters / catalogdb / rest / ServiceRestImpl.java
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 javax.ws.rs.core.Response;
32 import org.onap.so.db.catalog.data.repository.ServiceRepository;
33 import org.onap.so.rest.catalog.beans.Service;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36 import org.springframework.transaction.annotation.Transactional;
37 import com.google.common.base.Strings;
38 import io.swagger.annotations.ApiOperation;
39
40 @Path("/v1/")
41 @Component
42 public class ServiceRestImpl {
43
44     @Autowired
45     private ServiceRepository serviceRepo;
46
47     @Autowired
48     private ServiceMapper serviceMapper;
49
50     @GET
51     @Path("/services/{modelUUID}")
52     @Produces({MediaType.APPLICATION_JSON})
53     @Transactional(readOnly = true)
54     public Service findService(@PathParam("modelUUID") String modelUUID, @QueryParam("depth") int depth) {
55         org.onap.so.db.catalog.beans.Service service = serviceRepo.findOneByModelUUID(modelUUID);
56         return serviceMapper.mapService(service, depth);
57     }
58
59     @GET
60     @Path("/services")
61     @ApiOperation(value = "Find Service Models", response = Service.class, responseContainer = "List")
62     @Produces({MediaType.APPLICATION_JSON})
63     @Transactional(readOnly = true)
64     public List<Service> queryServices(@QueryParam("modelName") String modelName,
65             @QueryParam("distributionStatus") String distributionStatus, @QueryParam("depth") int depth) {
66         List<Service> services = new ArrayList<>();
67         List<org.onap.so.db.catalog.beans.Service> serviceFromDB = new ArrayList<>();
68         if (!Strings.isNullOrEmpty(modelName) && !Strings.isNullOrEmpty(distributionStatus)) {
69             serviceFromDB = serviceRepo.findByModelNameAndDistrobutionStatus(modelName, distributionStatus);
70         } else if (!Strings.isNullOrEmpty(modelName)) {
71             serviceFromDB = serviceRepo.findByModelName(modelName);
72         }
73         serviceFromDB.stream().forEach(serviceDB -> services.add(serviceMapper.mapService(serviceDB, depth)));
74         return services;
75     }
76 }